-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlslTracker.m
179 lines (160 loc) · 6.56 KB
/
lslTracker.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
% In MonkeyLogic a tracker is the object responsible with interfacing with
% the data collection hardware/software. It receives calls to acquire
% samples and holds the tracer pointer to display on screen.
% In this case it holds the pointers to the LSL frame inlet to receive
% frame data, time differences between the clocks and properly timestamps
% everything in monkeylogic time.
classdef lslTracker < mltracker
properties
Frame_Inlet % LSL inlet for Unity Frame data
Trial_Inlet
Lib % lsl_lib
% for tasks with multiple targets we'll send a reward when one is
% hit. We need to store the default value here.
RewardDuration
end
properties (SetAccess = protected)
Counter = 1 % position within the pre-allocated memory arrays
% current frame data has 22 x sample data where each sample is:
% 1: Position X
% 2: Position Y
% 3: Position Z
% 4: Rotation
% 5: Joystick Position X
% 6: Joystick Position Y
% 7: Player State (Collider ID)
% 8: Gaze Position X
% 9: Gaze Position Y
%
% 10-14: Gaze Targets ID
% 15-19: Gaze Targets Number of rays
%
% 20: Trial State
% 21: PhotoDiodeIntensity
% 22: Unity LSL Time
% To this we add:
% 23: Time Correction between the two LSL clocks
% 24: Sample TimeStamp
% 25: Local LSL TimeStamp
% 26: MonkeyLogic Trial Time.
Frame_Data = NaN(26, 120 * 100); % 120 sec @ 100Hz
Trial_Data = struct()
end
methods
function obj = lslTracker(frame_inlet, trial_inlet, MLConfig)
obj = obj@mltracker(MLConfig,[],[],[]);
obj.Frame_Inlet = frame_inlet;
obj.Trial_Inlet = trial_inlet;
obj.Signal = 'LSL';
end
function tracker_init(obj,~)
%Clear data
obj.Counter = 1;
end
function tracker_fini(~,~)
end
function acquire(obj, p)
% Trial data acquisition
if ~isempty(obj.Trial_Inlet)
[sample, timestamp] = obj.Trial_Inlet.pull_sample(0);
if ~isempty(sample)
obj.Trial_Data = obj.ProcessTrial(sample{1}, timestamp, p.trialtime());
end
end
reward = 0;
% Frame Data acquisition for tracker execution
if ~isempty(obj.Frame_Inlet)
% ~ .036 ms to acquire.
% stamps are on the remote (sender) clock time
% To get proper time:
% Local clock + time correction = remote clock;
[sample, timestamp] = obj.Frame_Inlet.pull_sample(0);
if ~isempty(sample)
[temp_array, reward] = obj.ProcessSample(sample, timestamp, lsl_local_clock(obj.Lib), p.trialtime(), reward);
obj.Frame_Data(:, obj.Counter) = temp_array;
obj.Counter = obj.Counter + 1;
% make sure we have all the available samples
has_buffer = true;
while has_buffer
[sample, timestamp] = obj.Frame_Inlet.pull_sample(0);
if isempty(sample)
has_buffer = false;
continue
end
[temp_array, reward] = obj.ProcessSample(sample, timestamp, lsl_local_clock(obj.Lib), p.trialtime(), reward);
obj.Frame_Data(:, obj.Counter) = temp_array;
obj.Counter = obj.Counter + 1;
end
% Check if the latest player state is an intermediate
% reward.
if (reward >0)
obj.DAQ.goodmonkey(obj.RewardDuration * reward);
end
obj.Success = true;
else
obj.Success = false;
end
end
end
function [temp_array, reward] = ProcessSample(obj, sample, timestamp, lsl_clock, trialtime, reward)
Time_Corr = obj.Frame_Inlet.time_correction();
temp_array = [sample'; % is a (1,22) then -> (22,1)
Time_Corr;
timestamp;
lsl_clock;
trialtime];
switch sample(20) % trial state
case 15 % quarter
reward_add = 0.25;
case 16 % half
reward_add = 0.5;
case 17 % 3/4
reward_add = 0.75;
case 18 % full
reward_add = 1;
case 19 % none
reward_add = 0;
otherwise
reward_add = 0;
end
reward = reward + reward_add;
end
function temp_struct = ProcessTrial(obj, sample, timestamp, trialtime)
Time_Corr = obj.Trial_Inlet.time_correction();
temp_struct = jsondecode(sample);
temp_struct.Time_Corr = Time_Corr;
temp_struct.ML_Sample_Time = timestamp;
temp_struct.ML_Local_Time = lsl_local_clock(obj.Lib);
temp_struct.ML_Trial_Time = trialtime;
end
function sample = GetLastSample(obj)
if obj.Counter > 1
sample = obj.Frame_Data(:,obj.Counter-1);
else
sample = [];
end
end
function sample = GetLastState(obj)
if obj.Counter > 1
sample = obj.Frame_Data(7, obj.Counter-1);
else
sample = [];
end
end
function sample = GetOutcome(obj)
if isfield(obj.Trial_Data, 'Outcome')
sample = obj.Trial_Data.Outcome;
else
sample = []; % defaults to aborted when no data.
end
end
function [frame_data, trial_data, xml_data] = GetTrialData(obj, p)
% get remaining frame samples in lsl stream
% obj.acquire(p)
% remove nan columns
frame_data = obj.Frame_Data(:, sum(isnan(obj.Frame_Data),1)==0);
trial_data = obj.Trial_Data;
xml_data = obj.Trial_Inlet.info().as_xml();
end
end
end