-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample01a.m
237 lines (211 loc) · 7.45 KB
/
sample01a.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
%Screen('Preference','SkipSyncTests',1);
AssertOpenGL;
ipAddress = input('SimpleGazeTracker address: ','s');
settings = {
{'RECORDED_EYE', 'L'},...
{'SCREEN_ORIGIN', 'TopLeft'},...
{'TRACKER_ORIGIN', 'TopLeft'},...
{'SCREEN_WIDTH', 1024},...
{'SCREEN_HEIGHT', 768},...
{'VIEWING_DISTANCE', 57.3},...
{'DOTS_PER_CENTIMETER_H', 24.26},...
{'DOTS_PER_CENTIMETER_V', 24.26},...
{'SACCADE_VELOCITY_THRESHOLD', 20.0},...
{'SACCADE_ACCELERATION_THRESHOLD', 3800.0},...
{'SACCADE_MINIMUM_DURATION', 12},...
{'SACCADE_MINIMUM_AMPLITUDE', 0.2},...
{'FIXATION_MINIMUM_DURATION', 12},...
{'BLINK_MINIMUM_DURATION', 50},...
{'RESAMPLING', 0},...
{'FILTER_TYPE', 'identity'},...
{'FILTER_WN', 0.2},...
{'FILTER_SIZE', 5},...
{'FILTER_ORDER', 3}
};
try
%-----------------------------------------------------------------
% Open PsychToolbox Window.
% wptr and wrect are necessary to initialize SimpleGazeTracker
% toolbox later.
%-----------------------------------------------------------------
%[wptr, wrect] = Screen('OpenWindow',0,[0,0,0],[0,0,1024,768]);
[wptr, wrect] = Screen('OpenWindow',0);
cx = wrect(3)/2;
cy = wrect(4)/2;
%-----------------------------------------------------------------
% Initialize SimpleGazeTracker.
% Return value of SimpleGazeTracker is necessary to customize
% parameters later.
%-----------------------------------------------------------------
param = SimpleGazeTracker('Initialize',wptr,wrect);
%-----------------------------------------------------------------
% Update SimpleGazeTracker Toolbox parameters.
%-----------------------------------------------------------------
%'localhost' means that SimpleGazeTracker is running on the same PC.
param.IPAddress = ipAddress;
param.calArea = wrect;
param.calTargetPos = [0,0;-400,-300; 0,-300; 400,-300;...
-400, 0; 0, 0; 400, 0;...
-400, 300; 0, 300; 400, 300];
for i=1:length(param.calTargetPos)
param.calTargetPos(i,:) = param.calTargetPos(i,:)+[cx,cy];
end
result = SimpleGazeTracker('UpdateParameters',param);
if result{1} < 0 %failed
disp('Could not update parameter. Abort.');
Screen('CloseAll');
return;
end
%-----------------------------------------------------------------
% Connect to SimpleGazeTracker and open data file.
%-----------------------------------------------------------------
res = SimpleGazeTracker('Connect');
if res==-1 %connection failed
Screen('CloseAll');
return;
end
SimpleGazeTracker('OpenDataFile','data.csv',0); %datafile is not overwritten.
%
% Update Camera Image Buffer
%
imgsize = SimpleGazeTracker('GetCameraImageSize');
param.imageWidth = imgsize(1);
param.imageHeight = imgsize(2);
result = SimpleGazeTracker('UpdateParameters',param);
if result{1} < 0 %failed
disp('Could not update parameter. Abort.');
Screen('CloseAll');
return;
end
%
% Send settings
%
res = SimpleGazeTracker('SendSettings', settings);
%-----------------------------------------------------------------
% Perform calibration.
%-----------------------------------------------------------------
while 1
res = SimpleGazeTracker('CalibrationLoop');
if res{1}=='q'
%Quit if calibrationloop is finished by 'q' key.
SimpleGazeTracker('CloseConnection');
Screen('CloseAll');
return;
end
if strcmp(res{1},'ESCAPE') && res{2}==1
%Leave from loop if calibration has been performed (res{2}==1).
break;
end
end
%-----------------------------------------------------------------
% Recording.
% If space key is pressed, a message 'Space' is inserted to the
% data file and latest 6 samples of gaze position is transferred
% from SimpleGazeTracker.
% Current gaze position is transferred every frame and a white
% square is drawn at the current gaze position.
%-----------------------------------------------------------------
gazeposlist = {};
geteyeposdelaylist = [];
previousKeyPressTime = GetSecs();
targetColor = 255;
%Start recording.
SimpleGazeTracker('StartRecording','Test trial',0.1);
for q = 1:360 %360 frames
[keyIsDown, secs, keyCode, deltaSecs] = KbCheck();
if keyCode(KbName('Space'))==1
% prevent chattering...
if GetSecs()-previousKeyPressTime > 0.2
SimpleGazeTracker('SendMessage','Space');
%get the latest 6 samples.
tmp = SimpleGazeTracker('GetEyePositionList',6,0,0.02);
if ~isempty(tmp)
gazeposlist(length(gazeposlist)+1) = {tmp};
end
%update previousKeyPressTime
previousKeyPressTime = GetSecs();
%change target color
if targetColor==255
targetColor=0;
else
targetColor=255;
end
end
end
if mod(q,60)==0
%Send message every 60 frames.
SimpleGazeTracker('SendMessage',num2str(q));
end
st = GetSecs();
%get current gaze position (moving average of 3 samples).
pos = SimpleGazeTracker('GetEyePosition',3,0.02);
geteyeposdelaylist = [geteyeposdelaylist, 1000*(GetSecs()-st)];
stimx = 200*cos(q/180*pi)+cx;
stimy = 200*sin(q/180*pi)+cy;
%horizontal component of current gaze position
markerx = pos{1}(1);
%vertical component of current gaze position
markery = pos{1}(2);
Screen('FillRect',wptr,127);
Screen('FillRect',wptr,0,[stimx-5,stimy-5,stimx+5,stimy+5]);
%draw marker at the current gaze position.
Screen('FillRect',wptr,targetColor,[markerx-5,markery-5,markerx+5,markery+5]);
Screen('Flip',wptr);
end
%Stop recording.
SimpleGazeTracker('StopRecording','',0.1);
%-----------------------------------------------------------------
% Clear Screen
%-----------------------------------------------------------------
Screen('FillRect',wptr,127);
Screen('Flip',wptr);
%-----------------------------------------------------------------
% Transfer data from SimpleGazeTracker.
%-----------------------------------------------------------------
fid = fopen('log.txt','wt');
%Get all messages.
msglist = SimpleGazeTracker('GetWholeMessageList',3.0);
fprintf(fid,'GetWholeMessageList test\n');
for i=1:length(msglist)
fprintf(fid,'%f,%s\n',msglist{i,1},msglist{i,2});
end
fprintf(fid,'\n');
%Get all gaze position data.
wholegazeposlist = SimpleGazeTracker('GetWholeEyePositionList',1,3.0);
fprintf(fid,'GetWholeEyePositionList test\n');
for i=1:length(wholegazeposlist)
fprintf(fid,'%f,%.1f,%.1f\n',...
wholegazeposlist(i,1),wholegazeposlist(i,2),wholegazeposlist(i,3));
end
fprintf(fid,'\n');
%Output result of GetEyePositionList
fprintf(fid,'GetEyePositionList test\n');
fprintf(fid,'Number of space-key press:%d\n',length(gazeposlist));
for i=1:length(gazeposlist)
fprintf(fid,'Keypress %d\n',i);
for j=1:length(gazeposlist{i})
fprintf(fid,'%f,%.1f,%.1f\n',...
gazeposlist{i}(j,1),gazeposlist{i}(j,2),gazeposlist{i}(j,3));
end
end
fprintf(fid,'\n');
%Output delay of SimpleGazeTracker('GetEyePosition')
fprintf(fid,'Delay of SimpleGazeTracker(''GetEyePosition'')\n');
for i=1:length(geteyeposdelaylist)
fprintf(fid,'%f\n',geteyeposdelaylist(i));
end
fclose(fid);
%-----------------------------------------------------------------
% Close remote data file and network connection.
%-----------------------------------------------------------------
SimpleGazeTracker('CloseDataFile');
SimpleGazeTracker('CloseConnection');
%-----------------------------------------------------------------
% Close Psychtoolbox screen.
%-----------------------------------------------------------------
Screen('CloseAll');
catch
SimpleGazeTracker('CloseConnection');
Screen('CloseAll');
psychrethrow(psychlasterror);
end