-
Notifications
You must be signed in to change notification settings - Fork 3
/
Eyelink.cpp
482 lines (393 loc) · 17.3 KB
/
Eyelink.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Eyelink.cpp
* Eyelink
*
* Created by Philipp Schwedhelm on 13.12.10.
* Copyright 2010 German Primate Center. All rights reserved.
*
*/
#include "Eyelink.h"
#include <boost/bind.hpp>
// Allocate the lock on the heap so it's never destructed
boost::mutex& Eyelink::EyelinkDriverLock = *(new boost::mutex);
bool Eyelink::Eyelink_Initialized = false;
const std::string Eyelink::TAG("tag");
const std::string Eyelink::RX("eye_rx");
const std::string Eyelink::RY("eye_ry");
const std::string Eyelink::LX("eye_lx");
const std::string Eyelink::LY("eye_ly");
const std::string Eyelink::EX("eye_x");
const std::string Eyelink::EY("eye_y");
const std::string Eyelink::EZ("eye_z");
const std::string Eyelink::H_RX("href_rx");
const std::string Eyelink::H_RY("href_ry");
const std::string Eyelink::H_LX("href_lx");
const std::string Eyelink::H_LY("href_ly");
const std::string Eyelink::P_RX("pupil_rx");
const std::string Eyelink::P_RY("pupil_ry");
const std::string Eyelink::P_LX("pupil_lx");
const std::string Eyelink::P_LY("pupil_ly");
const std::string Eyelink::P_R("pupil_size_r");
const std::string Eyelink::P_L("pupil_size_l");
const std::string Eyelink::E_DIST("tracking_dist");
const std::string Eyelink::EYE_TIME("eye_time");
const std::string Eyelink::UPDATE_PERIOD("data_interval");
const std::string Eyelink::IP("tracker_ip");
// External function for scheduling
void *update_(const shared_ptr<Eyelink> &gp){
gp->update();
return NULL;
}
void Eyelink::describeComponent(ComponentInfo &info) {
IODevice::describeComponent(info);
info.setSignature("iodevice/eyelink");
info.setDisplayName("Eyelink 1k, Socket Link");
info.setDescription(
"Eyelink 1000 Plugin. INSTRUCTIONS BELOW\n"
"REQUIRED PARAMETERS:\n"
"tracking_dist = The tracking range set in Eyelink configuration (Eyelink default is 1024 (pixels))\n"
"data_interval should not be too small (i.e. not shorter than 0.5 ms)\n"
"tracker_ip contains the trackers IP, check for working connection using SR-Research's 'simpleexample'\n"
"OUTPUT:\n"
"eye_x/_y/_z is the midpoint of the shortest connecting line that is orthogonal to both gaze vectors "
"assuming the tracker runs in binocular mode. Otherwise these values will be empty.\n"
"all other output parameters are specified and described in the Eyelink 1000 manual.\n"
);
info.addParameter(RX, false);
info.addParameter(RY, false);
info.addParameter(LX, false);
info.addParameter(LY, false);
info.addParameter(EX, false);
info.addParameter(EY, false);
info.addParameter(EZ, false);
info.addParameter(H_RX, false);
info.addParameter(H_RY, false);
info.addParameter(H_LX, false);
info.addParameter(H_LY, false);
info.addParameter(P_RX, false);
info.addParameter(P_RY, false);
info.addParameter(P_LX, false);
info.addParameter(P_LY, false);
info.addParameter(P_R, false);
info.addParameter(P_L, false);
info.addParameter(E_DIST, true, "1024");
info.addParameter(EYE_TIME, false);
info.addParameter(UPDATE_PERIOD, true, "1ms");
info.addParameter(IP, true, "10.1.1.2");
}
Eyelink::Eyelink(const ParameterValueMap ¶meters) :
IODevice(parameters),
e_dist(parameters[E_DIST]),
update_period(parameters[UPDATE_PERIOD]),
tracker_ip(parameters[IP].str()),
clock(Clock::instance()),
errors(0),
ack_msg_counter(0) {
if (!(parameters[RX].empty())) { e_rx = VariablePtr(parameters[RX]); }
if (!(parameters[RY].empty())) { e_ry = VariablePtr(parameters[RY]); }
if (!(parameters[LX].empty())) { e_lx = VariablePtr(parameters[LX]); }
if (!(parameters[LY].empty())) { e_ly = VariablePtr(parameters[LY]); }
if (!(parameters[EX].empty())) { e_x = VariablePtr(parameters[EX]); }
if (!(parameters[EY].empty())) { e_y = VariablePtr(parameters[EY]); }
if (!(parameters[EZ].empty())) { e_z = VariablePtr(parameters[EZ]); }
if (!(parameters[H_RX].empty())) { h_rx = VariablePtr(parameters[H_RX]); }
if (!(parameters[H_RY].empty())) { h_ry = VariablePtr(parameters[H_RY]); }
if (!(parameters[H_LX].empty())) { h_lx = VariablePtr(parameters[H_LX]); }
if (!(parameters[H_LY].empty())) { h_ly = VariablePtr(parameters[H_LY]); }
if (!(parameters[P_RX].empty())) { p_rx = VariablePtr(parameters[P_RX]); }
if (!(parameters[P_RY].empty())) { p_ry = VariablePtr(parameters[P_RY]); }
if (!(parameters[P_LX].empty())) { p_lx = VariablePtr(parameters[P_LX]); }
if (!(parameters[P_LY].empty())) { p_ly = VariablePtr(parameters[P_LY]); }
if (!(parameters[P_R].empty())) { p_r = VariablePtr(parameters[P_R]); }
if (!(parameters[P_L].empty())) { p_l = VariablePtr(parameters[P_L]); }
if (!(parameters[EYE_TIME].empty())) { e_time = VariablePtr(parameters[EYE_TIME]); }
}
bool Eyelink::initialize(){
boost::mutex::scoped_lock lock(EyelinkDriverLock);
if (Eyelink_Initialized) {
merror(M_IODEVICE_MESSAGE_DOMAIN,"Eyelink was previously initialized! Trying again, but this is dangerous!!");
}
Eyelink_Initialized = false;
// Initializes the link
//mprintf(M_IODEVICE_MESSAGE_DOMAIN, "Trying to find Eyelink System at %s",tracker_ip);
if (set_eyelink_address( &tracker_ip[0] ) )
merror(M_IODEVICE_MESSAGE_DOMAIN,"Failed to set Tracker to address %s", tracker_ip.c_str());
if(open_eyelink_connection(0))
merror(M_IODEVICE_MESSAGE_DOMAIN,"Failed to connect to Tracker at %s", tracker_ip.c_str());
else {
ELINKNODE node;
// generate data file name
time_t now = time(NULL);
struct tm* t = gmtime(&now);
sprintf(data_file_name, "%02d%06d.edf",(t->tm_year-100),t->tm_yday*1440 + t->tm_hour*60 + t->tm_min);
//YYMMMMMM : YY=Years since 2k, MMMMMM=Minutes in current year
if ( open_data_file( data_file_name ) ) {
mwarning(M_IODEVICE_MESSAGE_DOMAIN,"Eyelink datafile setting failed (%s)",data_file_name);
}
else {
mprintf(M_IODEVICE_MESSAGE_DOMAIN,"Eyelink logs to local file %s",data_file_name);
}
// Tell the tracker what data to include in samples
if (OK_RESULT != eyecmd_printf("link_sample_data = LEFT,RIGHT,GAZE,HREF,PUPIL,AREA")) {
mwarning(M_IODEVICE_MESSAGE_DOMAIN,
"Eyelink did not respond to link_sample_data command; sample data may be incomplete");
}
// tell the tracker who we are
eyelink_set_name((char*)("MWorks_over_Socket"));
// verify the name
if( eyelink_get_node(0,&node) != OK_RESULT )
merror(M_IODEVICE_MESSAGE_DOMAIN,"Error, EyeLink doesn't respond");
else eyemsg_printf((char*)("%s connected"), node.name);
// Enable link data reception
eyelink_reset_data(1);
}
// Eyelink should now be in "TCP-Link Open" mode
if(eyelink_is_connected()) {
tracker_version = eyelink_get_tracker_version(version_info);
mprintf(M_IODEVICE_MESSAGE_DOMAIN, "Eyelink %d System Version %s connected via Socket",tracker_version,version_info);
Eyelink_Initialized = true;
stopped = true;
}
else {
merror(M_IODEVICE_MESSAGE_DOMAIN,"Error, Eyelink Connection could not be established");
}
return Eyelink_Initialized;
}
Eyelink::~Eyelink(){
boost::mutex::scoped_lock lock(EyelinkDriverLock);
if (Eyelink_Initialized) {
if (!stopped) {
mwarning(M_IODEVICE_MESSAGE_DOMAIN,"Eyelink is still running !");
//eyelink stop recording
if(eyelink_is_connected()) { stop_recording(); }
}
if (schedule_node) {
schedule_node->cancel();
schedule_node.reset();
}
if(eyelink_is_connected()) {
// Places EyeLink tracker in off-line (idle) mode
set_offline_mode();
// close any open data files
if ( close_data_file() == 0 ) {
mprintf(M_IODEVICE_MESSAGE_DOMAIN,"Eyelink closed data file %s.",data_file_name);
}
if (eyelink_close(1)) // disconnect from tracker
{
merror(M_IODEVICE_MESSAGE_DOMAIN, "Could not close Eyelink connection");
}
//close_eyelink_system();
mprintf(M_IODEVICE_MESSAGE_DOMAIN, "Eyelink %d System Version %s disconnected.",tracker_version,version_info);
}
else {
mwarning(M_IODEVICE_MESSAGE_DOMAIN,"Error, Eyelink Shutdown failed");
}
Eyelink_Initialized = false;
}
}
bool Eyelink::update() {
MWTime inputtime;
boost::mutex::scoped_lock lock(EyelinkDriverLock);
if(eyelink_is_connected()) {
while(eyelink_get_next_data(NULL)) {
if(eyelink_in_data_block(1,0)) { //only if data contains samples
eyelink_get_float_data(&evt);
inputtime = this->clock->getCurrentTimeUS();
// occasionally, send the current time together with the sample time back to the tracker (and log it there)
if ( ack_msg_counter++ % 512 == 0 )
eyemsg_printf((char*)"SAMPLE %ld received %lld",(long)evt.time,inputtime);
// now update all the variables
if (e_time != NULL) e_time -> setValue( (long)evt.time ,inputtime);
if( evt.gx[RIGHT_EYE] != MISSING_DATA &&
evt.gy[RIGHT_EYE] != MISSING_DATA &&
evt.gx[LEFT_EYE] != MISSING_DATA &&
evt.gy[LEFT_EYE] != MISSING_DATA &&
(e_x != NULL || e_y != NULL || e_z != NULL) ) {
//p4321z = 1;
p43x = evt.gx[LEFT_EYE]/e_dist + 1;
p43y = evt.gy[LEFT_EYE]/e_dist;
p21x = evt.gx[RIGHT_EYE]/e_dist - 2;
p21y = evt.gy[RIGHT_EYE]/e_dist;
d4321 = p43x * p21x + p43y * p21y + 1;
d4343 = p43x * p43x + p43y * p43y + 1;
d2121 = p21x * p21x + p21y * p21y + 1;
denom = d2121 * d4343 - d4321 * d4321;
if (abs(denom) > 1e-6) { // should always be true when e_dist is really tracking range
numer = p43x * d4321 - p21x * d4343;
mua = numer / denom;
mub = (p43x + d4321 * mua) / d4343;
pax = 1 + mua * p21x;
pay = mua * p21y;
paz = -1 + mua; //-p4321z + mua * p4321z;
pbx = mub * p43x;
pby = mub * p43y;
pbz = -1 + mub; //-p4321z + mub * p4321z;
if (e_x != NULL) e_x -> setValue(pax + 0.5*(pbx-pax),inputtime);
if (e_y != NULL) e_y -> setValue(pay + 0.5*(pby-pay),inputtime);
if (e_z != NULL) e_z -> setValue(paz + 0.5*(pbz-paz),inputtime);
}
}
else {
if (e_x != NULL && e_x->getValue().getFloat() != MISSING_DATA)
e_x -> setValue((float)MISSING_DATA,inputtime);
if (e_y != NULL && e_y->getValue().getFloat() != MISSING_DATA)
e_y -> setValue((float)MISSING_DATA,inputtime);
if (e_z != NULL && e_z->getValue().getFloat() != MISSING_DATA)
e_z -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.gx[RIGHT_EYE] != MISSING_DATA &&
evt.gy[RIGHT_EYE] != MISSING_DATA ){
if (e_rx != NULL) e_rx -> setValue( evt.gx[RIGHT_EYE] ,inputtime);
if (e_ry != NULL) e_ry -> setValue( evt.gy[RIGHT_EYE] ,inputtime);
}
else {
if (e_rx != NULL && e_rx->getValue().getFloat() != MISSING_DATA)
e_rx -> setValue((float)MISSING_DATA,inputtime);
if (e_ry != NULL && e_ry->getValue().getFloat() != MISSING_DATA)
e_ry -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.gx[LEFT_EYE] != MISSING_DATA &&
evt.gy[LEFT_EYE] != MISSING_DATA ){
if (e_lx != NULL) e_lx -> setValue( evt.gx[LEFT_EYE] ,inputtime);
if (e_ly != NULL) e_ly -> setValue( evt.gy[LEFT_EYE] ,inputtime);
}
else {
if (e_lx != NULL && e_lx->getValue().getFloat() != MISSING_DATA)
e_lx -> setValue((float)MISSING_DATA,inputtime);
if (e_ly != NULL && e_ly->getValue().getFloat() != MISSING_DATA)
e_ly -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.hx[RIGHT_EYE] != -7936.0f &&
evt.hy[RIGHT_EYE] != -7936.0f ){
if (h_rx != NULL) h_rx -> setValue( evt.hx[RIGHT_EYE] ,inputtime);
if (h_ry != NULL) h_ry -> setValue( evt.hy[RIGHT_EYE] ,inputtime);
}
else {
if (h_rx != NULL && h_rx->getValue().getFloat() != MISSING_DATA)
h_rx -> setValue((float)MISSING_DATA,inputtime);
if (h_ry != NULL && h_ry->getValue().getFloat() != MISSING_DATA)
h_ry -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.hx[LEFT_EYE] != -7936.0f &&
evt.hy[LEFT_EYE] != -7936.0f ){
if (h_lx != NULL) h_lx -> setValue( evt.hx[LEFT_EYE] ,inputtime);
if (h_ly != NULL) h_ly -> setValue( evt.hy[LEFT_EYE] ,inputtime);
}
else {
if (h_lx != NULL && h_lx->getValue().getFloat() != MISSING_DATA)
h_lx -> setValue((float)MISSING_DATA,inputtime);
if (h_ly != NULL && h_ly->getValue().getFloat() != MISSING_DATA)
h_ly -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.px[RIGHT_EYE] != MISSING_DATA &&
evt.py[RIGHT_EYE] != MISSING_DATA ){
if (p_rx != NULL) p_rx -> setValue( evt.px[RIGHT_EYE] ,inputtime);
if (p_ry != NULL) p_ry -> setValue( evt.py[RIGHT_EYE] ,inputtime);
}
else {
if (p_rx != NULL && p_rx->getValue().getFloat() != MISSING_DATA)
p_rx -> setValue((float)MISSING_DATA,inputtime);
if (p_ry != NULL && p_ry->getValue().getFloat() != MISSING_DATA)
p_ry -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.px[LEFT_EYE] != MISSING_DATA &&
evt.py[LEFT_EYE] != MISSING_DATA ){
if (p_lx != NULL) p_lx -> setValue( evt.px[LEFT_EYE] ,inputtime);
if (p_ly != NULL) p_ly -> setValue( evt.py[LEFT_EYE] ,inputtime);
}
else {
if (p_lx != NULL && p_lx->getValue().getFloat() != MISSING_DATA)
p_lx -> setValue((float)MISSING_DATA,inputtime);
if (p_ly != NULL && p_ly->getValue().getFloat() != MISSING_DATA)
p_ly -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.pa[RIGHT_EYE] != 0 ){
if (p_r != NULL) p_r -> setValue( evt.pa[RIGHT_EYE] ,inputtime);
}
else {
if (p_r != NULL && p_r->getValue().getFloat() != 0)
p_r -> setValue((float)MISSING_DATA,inputtime);
}
if( evt.pa[LEFT_EYE] != 0 ){
if (p_l != NULL) p_l -> setValue( evt.pa[LEFT_EYE] ,inputtime);
}
else {
if (p_l != NULL && p_l->getValue().getFloat() != 0)
p_l -> setValue((float)MISSING_DATA,inputtime);
}
}
}
}
else {
if(++errors * update_period > (MWorksTime)1000000) { //just a quick hack but impossible to ignore by the user
merror(M_IODEVICE_MESSAGE_DOMAIN, "Fatal Error! EyeLink Connection Lost!!");
errors = 0;
}
}
return true;
}
bool Eyelink::startDeviceIO(){
boost::mutex::scoped_lock lock(EyelinkDriverLock);
if(eyelink_is_connected() && stopped) {
//Eyelink to offline mode
set_offline_mode();
// Eyelink to record mode
if( start_recording(0,1,1,0) ) {
merror(M_IODEVICE_MESSAGE_DOMAIN, "Eyelink does not start!");
}
shared_ptr<Scheduler> scheduler = Scheduler::instance();
shared_ptr<Eyelink> this_one = component_shared_from_this<Eyelink>();
schedule_node = scheduler->scheduleUS(std::string(FILELINE ": ") + getTag(),
update_period, //defer first start one period
update_period, //repeat interval
M_REPEAT_INDEFINITELY,
boost::bind(update_, this_one),
M_DEFAULT_IODEVICE_PRIORITY,
M_DEFAULT_IODEVICE_WARN_SLOP_US,
M_DEFAULT_IODEVICE_FAIL_SLOP_US,
M_MISSED_EXECUTION_DROP);
stopped = false;
mprintf(M_IODEVICE_MESSAGE_DOMAIN, "Eyelink successfully started.");
}
else {
mwarning(M_IODEVICE_MESSAGE_DOMAIN, "Warning! Could not start EyeLink! (StartIO)");
}
return !stopped;
}
bool Eyelink::stopDeviceIO() {
boost::mutex::scoped_lock lock(EyelinkDriverLock);
if(!stopped) {
if (schedule_node) {
schedule_node->cancel();
schedule_node.reset();
}
if(eyelink_is_connected()) {
//eyelink stop recording
stop_recording();
//go to eyelink offline mode
set_offline_mode();
}
else mwarning(M_IODEVICE_MESSAGE_DOMAIN, "Warning! Could not stop EyeLink! Connection Lost!! (StopIO)");
if (e_time != NULL) e_time -> setValue( (float)MISSING_DATA );
if (e_rx != NULL) e_rx -> setValue( (float)MISSING_DATA );
if (e_ry != NULL) e_ry -> setValue( (float)MISSING_DATA );
if (e_lx != NULL) e_lx -> setValue( (float)MISSING_DATA );
if (e_ly != NULL) e_ly -> setValue( (float)MISSING_DATA );
if (e_x != NULL) e_x -> setValue( (float)MISSING_DATA );
if (e_y != NULL) e_y -> setValue( (float)MISSING_DATA );
if (e_z != NULL) e_z -> setValue( (float)MISSING_DATA );
if (h_rx != NULL) h_rx -> setValue( (float)MISSING_DATA );
if (h_ry != NULL) h_ry -> setValue( (float)MISSING_DATA );
if (h_lx != NULL) h_lx -> setValue( (float)MISSING_DATA );
if (h_ly != NULL) h_ly -> setValue( (float)MISSING_DATA );
if (p_rx != NULL) p_rx -> setValue( (float)MISSING_DATA );
if (p_ry != NULL) p_ry -> setValue( (float)MISSING_DATA );
if (p_lx != NULL) p_lx -> setValue( (float)MISSING_DATA );
if (p_ly != NULL) p_ly -> setValue( (float)MISSING_DATA );
if (p_r != NULL) p_r -> setValue( (float)MISSING_DATA );
if (p_l != NULL) p_l -> setValue( (float)MISSING_DATA );
stopped = true;
mprintf(M_IODEVICE_MESSAGE_DOMAIN, "Eyelink successfully stopped.");
}
return stopped;
}