Optitrak + Dikablis

From REALab Wiki
Jump to navigation Jump to search

Demo Experiment

This very simple script will display 2 photos in sequence, each for 20 seconds. During this time, both the Dikablis and the optitrack will be recording data samples, and storing this data into an array.

Click below to download the zip file:


rand('state',sum(100*clock));

%%%%%%%%%%%VARIABLES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
duration=20.000;
optistate='off';  %on/off
dikabstate='off';  %on/off
filewrite='off';

%defaults, changed through dialogue below
subject='999999';
sex='na';
age='na';
handed='na';

%%%%%%%%%%%%DIALOGUE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%can comment this out and use defaults above for programming
prompt={'Participant ID:', 'Sex(m/f):', 'Age:', 'Handed(l/r/b):'};
def = {'', '', '', '', ''};
title = 'Gallery Study';
lineNo=1;
details= inputdlg(prompt, title, lineNo, def);

subject=details{1};
sex=details{2};
age=details{3};
handed=details{4};
if strcmp(filewrite,'on')
	filename=['.\subjects\', subject, '.txt'];
end

%%%%%%%%%%%%%%VARIABLES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filelist=textread('photos\imagelist.txt', '%s');
randimages=filelist(randperm(length(filelist)));
trials=length(filelist);

%%%%%%%%OPTITRACK%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(optistate,'on')
    %start listening to head tracking from Optitrack
    %Craig worked this out
    %In tracking tools, "Streaming properties" panel, need to tick
    %broadcast data and make sure is set to Unicast
    %needs to know where this dll is
    Opti = NET.addAssembly('C:\Users\UBC\Documents\MATLAB\NatNetSDK\lib\NatNetML.dll');
    client = NatNetML.NatNetClientML(1);

    %needs to have the right IP
    % can run system ipconfig to help find out
    client.Initialize('142.103.0.138','142.103.0.138');
    data = client.GetLastFrameOfData;

    %create a place to store the data from each frame
    optiData=[];%not possible to just save the .NET object?
    %store x,y,z,qx,qy,qz,qw
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%DIKABLIS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(dikabstate,'on')
	%start communicating with the Dikablis laptop
	c = tcpip('150.100.100.20',2001);%set up the IP/port
	% c = tcpip('142.103.0.138',2001);%set up the IP/port
	c.Terminator = 'LF/CR';  %tell it about line endings
	fopen(c)    %open the channel
	fprintf(c,'Luser,pass');pause(1); %tell Dik we're going to be communicating
	WaitSecs(2);


	%open project, and create new subject
	fprintf(c,'Opeter_picture_gallery_1');pause(1);
	fprintf(c,['CE', subject]);pause(1)
	WaitSecs(1.5);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%SET UP SCREEN%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
screens=Screen('Screens');
screenNumber=max(screens);

HideCursor;

gray=GrayIndex(screenNumber); 
white=WhiteIndex(screenNumber);

[w, wRect]=Screen('OpenWindow',screenNumber, [0 0 0]);

Screen('TextSize', w, 32);

priorityLevel=MaxPriority(w);
Priority(priorityLevel);

message = 'Press any key to begin ...';
DrawFormattedText(w, message, 'center', 'center', white);
Screen('Flip', w);
KbWait;  
Screen('FillRect', w, [0 0 0]);
Screen('Flip', w);



    
if strcmp(dikabstate,'on')
    %start Dikablis recording
	fprintf(c, 'Rstart');pause(1);
	WaitSecs(1.5);
end



img=imread(['.\photos\', randimages{1}]);
tximg=Screen('MakeTexture', w, img);
DrawFormattedText(w, 'Ready...', 'center', 'center', white);
Screen('Flip', w);
KbWait;
Screen('DrawTexture', w, tximg, [], wRect + [wRect(3)*20/100 0 -wRect(3)*20/100 0]);
[VBLTimestamp startrt]=Screen('Flip', w)
if strcmp(dikabstate,'on')
	fprintf(c, 'Eimage1on');pause(1);
end

while (GetSecs - startrt)<=duration
    if strcmp(optistate,'on') %store the trackable's position
        data = client.GetLastFrameOfData;%current data
        optiData=[optiData;data.RigidBodies(1).x,...
            data.RigidBodies(1).y,...
            data.RigidBodies(1).z,...
            data.RigidBodies(1).qx,...
            data.RigidBodies(1).qy,...
            data.RigidBodies(1).qz,...
			data.RigidBodies(1).qw
			data.RigidBodies(2).x,...
            data.RigidBodies(2).y,...
            data.RigidBodies(2).z,...
            data.RigidBodies(2).qx,...
            data.RigidBodies(2).qy,...
            data.RigidBodies(2).qz,...
			data.RigidBodies(2).qw];
    end
end

Screen('FillRect', w, [0 0 0]);
Screen('Flip', w);
if strcmp(dikabstate, 'on')
	fprintf(c, 'Eimage1off');pause(1);
end
WaitSecs(1.5);



img=imread(['.\photos\', randimages{2}]);
tximg=Screen('MakeTexture', w, img);
DrawFormattedText(w, 'Ready...', 'center', 'center', white);
Screen('Flip', w);
KbWait;
Screen('DrawTexture', w, tximg, [], wRect + [wRect(3)*26/100 0 -wRect(3)*26/100 0]);
[VBLTimestamp startrt]=Screen('Flip', w)
if strcmp(dikabstate, 'on')
	fprintf(c, 'Eimage2on');pause(1);
end
while (GetSecs - startrt)<=duration
    %store the trackable's position
    if strcmp(optistate,'on')
        data = client.GetLastFrameOfData;%current data
        optiData=[optiData;data.RigidBodies(1).x,...
            data.RigidBodies(1).y,...
            data.RigidBodies(1).z,...
            data.RigidBodies(1).qx,...
            data.RigidBodies(1).qy,...
            data.RigidBodies(1).qz,...
			data.RigidBodies(1).qw
			data.RigidBodies(2).x,...
            data.RigidBodies(2).y,...
            data.RigidBodies(2).z,...
            data.RigidBodies(2).qx,...
            data.RigidBodies(2).qy,...
            data.RigidBodies(2).qz,...
			data.RigidBodies(2).qw];
    end
end

Screen('FillRect', w, [0 0 0]);
Screen('Flip', w);
if strcmp(dikabstate, 'on')
	fprintf(c, 'Eimage2off');pause(1);
end
WaitSecs(1.5);

DrawFormattedText(w, 'thanks for participating!', 'center', 'center', white);
Screen('Flip', w);
KbWait;


if strcmp(dikabstate, 'on') %stop Dikablis recording
	fprintf(c, 'Rstop');pause(1);
end
if strcmp(optistate,'on')
    client.Uninitialize; %CLOSE OPTITRACK CLIENT 
end
Screen('CloseAll');
ShowCursor;
% fclose(c);
fclose('all');


Gallery Empathy script

This experiment involves showing people pictures of people, and asking them to make empathy judgments about one of them. We varied the clarity of a target person's image to be either sharp or blurry, and hypothesized that this would influence the participant's empathy towards them.

Gallery_empathy.zip