-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageAndMove.m
More file actions
193 lines (170 loc) · 4.78 KB
/
ImageAndMove.m
File metadata and controls
193 lines (170 loc) · 4.78 KB
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
classdef ImageAndMove < SubSystem
properties % OBLIGATORY
experimentSyncObj
trialSyncObj
frameSyncObj
end
properties
clockPulseObj
rawVelocity
time
maxFrameRate = 25;
end
properties (Hidden)
lastFrameAcquiredTime
end
events
ExperimentStart
ExperimentStop
FrameAcquired
end
methods
function obj = ImageAndMove(varargin)
if nargin > 1
for k = 1:2:length(varargin)
obj.(varargin{k}) = varargin{k+1};
end
end
obj.defineDefaults()
obj.checkProperties()
obj.updateExperimentName()
end
function defineDefaults(obj)
obj.defineDefaults@SubSystem;
end
function checkProperties(obj)
obj.savedDataFiles = DataFile.empty(1,0);
obj.currentDataFileSet = DataFile.empty(1,0);
obj.checkProperties@SubSystem;
end
end
methods % Required by SubSystem
function createSystemComponents(obj,varargin)
if isempty(obj.frameSyncObj)
obj.frameSyncObj = obj;
end
if isempty(obj.experimentSyncObj)
obj.experimentSyncObj = obj;
end
if nargin < 2
obj.clockPulseObj = NiPulseOutput(...
'pulseTime',.005,...
'activeHigh',true,...
'portNumber',1,...
'lineNumber',0);
obj.clockPulseObj.setup();
else
obj.clockPulseObj = NiPulseOutput(varargin{:});
obj.clockPulseObj.setup();
end
end
function start(obj)
obj.updateExperimentName()
fprintf('STARTING VRSYSTEM:\n\tSession-Path: %s\n',...
obj.sessionPath);
if ~isdir(obj.sessionPath)
mkdir(obj.sessionPath)
end
if isempty(obj.frameSyncListener)
warning('VrSystem:start:NoFrameSyncListener',...
'The Behavior-Control sysem is not connected to a camera, and will not record data every frame');
else
obj.frameSyncListener.Enabled = true;
end
obj.experimentStateListener.Enabled = true;
obj.ready = true;
end
function stop(obj)
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.experimentStateListener.Enabled = false;
if obj.experimentRunning
obj.experimentRunning = false;
if ~isempty(obj.currentDataFile) ...
&& isopen(obj.currentDataFile) ...
&& ~issaved(obj.currentDataFile)
obj.saveDataFile;
obj.currentDataFile = DataFile.empty(1,0);
end
obj.saveDataSet();
obj.clearDataSet();
fprintf('Experiment Stopped\n');
end
end
function experimentStateChangeFcn(obj,~,evnt)
fprintf('VrSystem: Received ExperimentStateChange event\n')
switch evnt.EventName
case 'ExperimentStart'
if ~obj.experimentRunning
obj.updateExperimentName();
obj.start()
end
case 'ExperimentStop'
obj.stop();
end
end
function frameAcquiredFcn(obj,~,evnt)
if ~isempty(obj.lastFrameAcquiredTime)
timeSinceLastFrameStart = toc(obj.lastFrameAcquiredTime);
maxFramePeriod = 1/obj.maxFrameRate;
while timeSinceLastFrameStart < maxFramePeriod
timeSinceLastFrameStart = toc(obj.lastFrameAcquiredTime);
pause(maxFramePeriod - timeSinceLastFrameStart)
end
end
try
framePeriod = toc(obj.lastFrameAcquiredTime);
catch
framePeriod = inf;
end
obj.lastFrameAcquiredTime = tic;
obj.framesAcquired = obj.framesAcquired + 1;
if ~isempty(obj.clockPulseObj)
obj.clockPulseObj.sendPulse()
end
if isempty(obj.currentDataFile)
% called on first frame
obj.currentDataFile = DataFile(...
'rootPath',obj.currentDataSetPath,...
'experimentName',obj.currentExperimentName);%changed rootPath from sessionPath
end
info.Time = evnt.Time;
data = evnt.RawVelocity;
addFrame2File(obj.currentDataFile,data,info);
end
function trialStateChangeFcn(obj)
end
function set.experimentSyncObj(obj,bhv)
if ~isempty(obj.experimentStateListener)
obj.experimentStateListener.Enabled = false;
end
obj.experimentSyncObj = bhv;
obj.experimentStateListener = addlistener(obj.experimentSyncObj,...
'ExperimentStart',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
addlistener(obj.experimentSyncObj,...
'ExperimentStop',@(src,evnt)experimentStateChangeFcn(obj,src,evnt));
obj.experimentStateListener.Enabled = true;
end
function set.frameSyncObj(obj,cam)
if ~isempty(obj.frameSyncListener)
obj.frameSyncListener.Enabled = false;
end
obj.frameSyncObj = cam;
% Define Listener
obj.frameSyncListener = addlistener(obj.frameSyncObj,...
'FrameAcquired',@(src,evnt)frameAcquiredFcn(obj,src,evnt));
obj.frameSyncListener.Enabled = false;
end
end
methods
function delete(obj)
try
obj.saveDataSet();
delete(obj.clockPulseObj);
catch me
disp(me.message)
end
end
end
end