-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckSaveAndMeasureAngularDisplacements_2Piezos.m
executable file
·396 lines (308 loc) · 14.3 KB
/
checkSaveAndMeasureAngularDisplacements_2Piezos.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
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
function [armL, armR, dispFrames] = checkSaveAndMeasureAngularDisplacements_2Piezos(cam, experimentHandle, piezosTilt_deg, allFr, varargin)
%make it more flexible
%save also the corresponding displacements and camera information
%(self-contained)
%
% input the lines
% extract the angles
% estimate arm length and output a calibration function for each of the
% piezos
%
%
dispFrames = [];
displacements = [0 36 0 -36 0]'; % positive is push - do not change this structure. Code is not robust.
assert(isequal(unique(displacements(1:2:end)), 0), 'code is not robust to changes in structure of variable displacements')
commands = displacements/9; % * [1, -1]; % first column is anterior, second is posterior piezo command
stimOnDur = 0.1; %seconds
rampLength = 0.9;
%% set things up
HWsettings = sensor_settings_PP; % to set AO channels consistently
daqreset;
s = daq.createSession('ni');
s.Rate = 1e4;
% ai = s.addAnalogInputChannel(HWsettings.DAQdevName, ...
% [HWsettings.AIch.PIEZO_SENSOR_LEFT, HWsettings.AIch.PIEZO_SENSOR_RIGHT] , 'Voltage');
% for ch = 1:2
% ai(ch).Range = [-10 10];
% ai(ch).TerminalConfig = 'SingleEnded';
% end
ao = s.addAnalogOutputChannel(HWsettings.DAQdevName, ...
[HWsettings.AOch.PIEZO_OUTPUT_LEFT, HWsettings.AOch.PIEZO_OUTPUT_RIGHT] , 'Voltage');
for ch = 1:2
ao(ch).Range = [-10 10];
ao(ch).TerminalConfig = 'SingleEnded';
end
% here and generally cameras are ordered: 1 post, 2 down, 3 ant
if nargin == 5
currentFrames = varargin{1};
for c = 1:3
allFr(c).frames(:,:,1) = currentFrames(c).baseline;
allFr(c).frames(:,:,2) = currentFrames(c).piezoON;
end
end
vidnames(1).name = 'cam.post.vid';
vidnames(2).name = 'cam.down.vid';
vidnames(3).name = 'cam.ant.vid';
%% play stimuli and record frame -- %OLD
% commands = displacements/9 * [1, -1]; % first column is anterior, second is posterior piezo command
% stimOnDur = 0.6; %seconds
% disp('stimulating and collecting frames: be patient')
%
% % %% play stimuli and record frame -- OLD sharp stimulus
% for i = 1 : size(commands,1)
% disp(displacements(i))
% voltageCommand = commands(i,:);
% outputSingleScan(s, voltageCommand )
% % capture frames at steady state
% pause(stimOnDur)
% for c = 1:3
% dispFrames(c).frames(:,:,i) = getsnapshot(eval(vidnames(c).name));
% end
% pause(0.1)
% end
%% play stimuli and record frame - ramping
disp('stimulating and collecting frames: be patient')
% first zero
for c = 1:3
dispFrames(c).frames(:,:,1) = getsnapshot(eval(vidnames(c).name));
end
for i = 2 : 2 : size(commands,1)
disp(displacements(i))
voltageCommand = commands(i);
stim = linspace(0, voltageCommand, s.Rate*rampLength);
stim = stim(:);
outData = stim * [1, -1]; % first column is anterior, second is posterior piezo command
queueOutputData(s, outData);
s.startForeground;
% capture frames at steady state
pause(stimOnDur)
for c = 1:3
dispFrames(c).frames(:,:,i) = getsnapshot(eval(vidnames(c).name));
end
pause(0.2)
%rampDown
queueOutputData(s, flipud(outData));
s.startForeground;
pause(stimOnDur)
for c = 1:3
dispFrames(c).frames(:,:,i+1) = getsnapshot(eval(vidnames(c).name));
end
pause(0.2)
end
%% play back frames and put lines on
% from the second frame on, they should take the 1st frame lines as default
numFrames = length(displacements);
posR = nan(2,2,length(displacements));
posL = nan(2,2,length(displacements));
linesR = [];
linesL = [];
colors = brewermap(length(displacements), 'Set3');
f = figure('WindowStyle', 'normal');
% f.WindowState = 'fullscreen';
f.WindowState = 'maximized';
% set(f, 'waitstatus', 'waiting')
hbox = uix.HBoxFlex( 'Parent', f ,'Spacing', 3);
vbox1 = uix.VBox('Parent', hbox, 'Spacing', 1);
handles.imSingle.ax = axes(uicontainer('Parent', vbox1),'ActivePositionProperty', 'Position');
axis image, axis off, hold on
controlsObj = uix.VButtonBox('Parent', vbox1, 'Padding',1);
controlsObj.ButtonSize = [controlsObj.Position(3)*0.98 30];
handles.sl = uicontrol('Parent',controlsObj,'Style','slider', ...
'value',1, 'min',1, 'max',numFrames, 'SliderStep', [1/(numFrames-1) 1/(numFrames-1)]);
handles.sl.Units = 'normalized';
handles.sl.Position(1) = 0.02;
handles.sl.Position(3) = 0.96;
handles.slText = uicontrol('Parent',controlsObj, 'Style','text','String',1);
handles.doneButton = uicontrol('Parent', controlsObj, 'Style', 'pushbutton', 'String', 'DONE');
vbox2 = uix.VBox('Parent', hbox, 'Spacing', 3);
handles.imAll.ax = axes(uicontainer('Parent', vbox2),'ActivePositionProperty', 'OuterPosition');
axis image, axis off, hold on
hPanels = uix.HBox('Parent', vbox2, 'Spacing', 3);
pR = uix.Panel( 'Parent', hPanels, 'Title', 'R', 'Padding', 2 );
tR = uitable(pR,'ColumnName',{'displ';'dAng_deg';'arm_um'});
angR_rad = nan(numFrames,1);
armR_um = nan(numFrames,1);
tR.Data(:, 1) = displacements;
tR.Data(:, 2) = nan(numFrames,1);
tR.Data(:, 3) = armR_um;
tR.ColumnWidth = {35 67 67};
pL = uix.Panel( 'Parent', hPanels, 'Title', 'L', 'Padding', 2 );
tL = uitable(pL,'ColumnName',{'displ';'dAng_deg';'arm_um'});
angL_rad = nan(numFrames,1);
armL_um = nan(numFrames,1);
tL.Data = nan(numFrames,2);
tL.Data(:, 1) = displacements;
tL.Data(:, 2) = nan(numFrames,1);
tL.Data(:, 3) = armL_um;
tL.ColumnWidth = {35 67 67};
hPanels2 = uix.HBox('Parent', vbox2, 'Spacing', 3);
handles.plotR = axes(uicontainer('Parent', hPanels2),'ActivePositionProperty', 'Position'); axis manual; hold on
handles.plotL = axes(uicontainer('Parent', hPanels2),'ActivePositionProperty', 'Position'); axis manual; hold on
hbox.Widths = [-1.3, -1];
vbox1.Heights = [-3, -1];
vbox2.Heights = [-2.5, -1.2, -1];
linkaxes([handles.plotR, handles.plotL], 'y')
handles.plotR.YLim = [60 220];
handles.plotR.XLim = [min(displacements), max(displacements)];
handles.plotL.XLim = [min(displacements), max(displacements)];
%% set position 1 as default
markedFrames = false(1, numFrames);
posR(:,:,1) = [ 530 500; 225 640]; %change default to something more plausible
posL(:,:,1) = [1060 500; 1370 640];
axes(handles.imSingle.ax)
imshow(dispFrames(2).frames(:,:,1), [])
lR = imline(gca, posR(:,:,1));
lL = imline(gca, posL(:,:,1)); %base x-y; tip x-y
setColor(lR, 'y');
setColor(lL, 'y');
previousFrame = 1;
axes(handles.imAll.ax)
imshow(dispFrames(2).frames(:,:,1), [])
lh = addlistener(handles.sl,'Action',@(src,evnt)surfzlim(src,evnt,handles));
lh1 = addlistener(handles.doneButton, 'Action', @(src,evnt)doneOut(src,evnt,handles));
uiwait(f);
%%
function surfzlim(source, event, handles)
val = floor(source.Value);
handles.slText.String = num2str(val);
% first, update/store previous frame position info
posR(:,:,previousFrame) = lR.getPosition;
posL(:,:,previousFrame) = lL.getPosition;
lR.delete;
lL.delete;
markedFrames(previousFrame) = true;
axes(handles.imAll.ax)
linesR(previousFrame).l = line(posR(:,1,previousFrame), posR(:,2,previousFrame), 'Color', colors(previousFrame,:));
linesL(previousFrame).l = line(posL(:,1,previousFrame), posL(:,2,previousFrame), 'Color', colors(previousFrame,:));
linesR(previousFrame).l.DisplayName = sprintf('%2d',previousFrame);
linesL(previousFrame).l.DisplayName = sprintf('%2d',previousFrame);
grouping = cat(2, find(markedFrames), find(markedFrames));
clickableLegend( cat(1,linesR.l, linesL.l), 'groups', grouping, 'Location', 'northeastoutside');
%%
baselines = displacements == 0;
ibaselines = find(baselines);
ang_rad_zero = [];
for b = 1 : sum(squeeze(~isnan(posR(1,1,baselines))))
b_i = ibaselines(b);
% ang_rad_zero(b) = atan((posR(2,2,b_i)-posR(1,2,b_i)) / (posR(2,1,b_i)-posR(1,1,b_i)));
ang_rad_zero(b) = atan((posR(2,2,b_i)-posR(1,2,b_i)) / (posR(2,1,b_i)-posR(1,1,b_i))) / cosd(piezosTilt_deg.R);
end
angR_rad_zero = circ_mean(ang_rad_zero(:));
ang_rad_zero = [];
for b = 1 : sum(squeeze(~isnan(posL(1,1,baselines))))
b_i = ibaselines(b);
% ang_rad_zero(b) = atan((posL(2,2,b_i)-posL(1,2,b_i)) / (posL(2,1,b_i)-posL(1,1,b_i)));
ang_rad_zero(b) = atan((posL(2,2,b_i)-posL(1,2,b_i)) / (posL(2,1,b_i)-posL(1,1,b_i))) / cosd(piezosTilt_deg.L);
end
angL_rad_zero = - circ_mean(ang_rad_zero(:)); % note sign for L
% for all the frames, resubtract averaged baseline
% for frames w
for b = 1 : length(displacements)
% angR_rad(b,1) = atan((posR(2,2,b)-posR(1,2,b)) / (posR(2,1,b)-posR(1,1,b))) - angR_rad_zero;
% angL_rad(b,1) = - atan((posL(2,2,b)-posL(1,2,b)) / (posL(2,1,b)-posL(1,1,b))) - angL_rad_zero; % note sign for L
angR_rad(b,1) = atan((posR(2,2,b)-posR(1,2,b)) / (posR(2,1,b)-posR(1,1,b))) / cosd(piezosTilt_deg.R) - angR_rad_zero;
angL_rad(b,1) = - atan((posL(2,2,b)-posL(1,2,b)) / (posL(2,1,b)-posL(1,1,b))) / cosd(piezosTilt_deg.L) - angL_rad_zero; % note sign for L
% if not a zero displacement frame
if displacements(b) ~= 0
armR_um(b) = displacements(b)/tan(angR_rad(b)); %fix others too
armL_um(b) = displacements(b)/tan(angL_rad(b));
end
end
% always update the entire table, in case user updates frame 1 later on
angR = rad2deg(angR_rad);
angL = rad2deg(angL_rad);
tR.Data(:, 2) = round(angR*100)/100;
tR.Data(:, 3) = armR_um;
tL.Data(:, 2) = round(angL*100)/100;
tL.Data(:, 3) = armL_um;
%% then update axes and process new frame
axes(handles.imSingle.ax)
handles.imSingle.ax.NextPlot = 'replaceChildren';
imshow(dispFrames(2).frames(:,:,val), [])
handles.imSingle.ax.NextPlot = 'add';
% display defaults
if isnan(posR(1,1,val))
lR = imline(gca, posR(:,:,1));
lL = imline(gca, posL(:,:,1));
setColor(lR, 'y');
setColor(lL, 'y');
elseif ~isnan(posR(1,1,val)) % pre-existing line
lR = imline(gca, posR(:,:,val));
lL = imline(gca, posL(:,:,val));
setColor(lR, 'g');
setColor(lL, 'g');
end
axes(handles.plotR)
handles.plotR.NextPlot = 'replaceChildren';
plot(displacements, armR_um, 'xb', 'MarkerSize',12)
axes(handles.plotL)
handles.plotL.NextPlot = 'replaceChildren';
plot(displacements, armL_um, 'xr', 'MarkerSize',12)
% before you leave this function
previousFrame = val;
end
%%
function doneOut(source, event, handles)
% save last line
posR(:,:,previousFrame) = lR.getPosition;
posL(:,:,previousFrame) = lL.getPosition;
lR.delete;
lL.delete;
markedFrames(previousFrame) = true;
axes(handles.imAll.ax)
linesR(previousFrame).l = line(posR(:,1,previousFrame), posR(:,2,previousFrame), 'Color', colors(previousFrame,:));
linesL(previousFrame).l = line(posL(:,1,previousFrame), posL(:,2,previousFrame), 'Color', colors(previousFrame,:));
linesR(previousFrame).l.DisplayName = sprintf('%2d',previousFrame);
linesL(previousFrame).l.DisplayName = sprintf('%2d',previousFrame);
%% recalculate angles zero from all baselines available, and all angles in Table
baselines = displacements == 0;
ibaselines = find(baselines);
ang_rad_zero = [];
for b = 1 : sum(squeeze(~isnan(posR(1,1,baselines))))
b_i = ibaselines(b);
% ang_rad_zero(b) = atan((posR(2,2,b_i)-posR(1,2,b_i)) / (posR(2,1,b_i)-posR(1,1,b_i)));
ang_rad_zero(b) = atan((posR(2,2,b_i)-posR(1,2,b_i)) / (posR(2,1,b_i)-posR(1,1,b_i))) / cosd(piezosTilt_deg.R);
end
angR_rad_zero = circ_mean(ang_rad_zero(:));
ang_rad_zero = [];
for b = 1 : sum(squeeze(~isnan(posL(1,1,baselines))))
b_i = ibaselines(b);
% ang_rad_zero(b) = atan((posL(2,2,b_i)-posL(1,2,b_i)) / (posL(2,1,b_i)-posL(1,1,b_i)));
ang_rad_zero(b) = atan((posL(2,2,b_i)-posL(1,2,b_i)) / (posL(2,1,b_i)-posL(1,1,b_i))) / cosd(piezosTilt_deg.L);
end
angL_rad_zero = - circ_mean(ang_rad_zero(:)); % note sign for L
% for all the frames, resubtract averaged baseline
% for frames w
for b = 1 : length(displacements)
% angR_rad(b,1) = atan((posR(2,2,b)-posR(1,2,b)) / (posR(2,1,b)-posR(1,1,b))) - angR_rad_zero;
% angL_rad(b,1) = - atan((posL(2,2,b)-posL(1,2,b)) / (posL(2,1,b)-posL(1,1,b))) - angL_rad_zero; % note sign for L
angR_rad(b,1) = atan((posR(2,2,b)-posR(1,2,b)) / (posR(2,1,b)-posR(1,1,b))) / cosd(piezosTilt_deg.R) - angR_rad_zero;
angL_rad(b,1) = - atan((posL(2,2,b)-posL(1,2,b)) / (posL(2,1,b)-posL(1,1,b))) / cosd(piezosTilt_deg.L) - angL_rad_zero; % note sign for L
% if not a zero displacement frame
if displacements(b) ~= 0
armR_um(b) = displacements(b)/tan(angR_rad(b)); %fix others too
armL_um(b) = displacements(b)/tan(angL_rad(b));
end
end
% always update the entire table, in case user updates frame 1 later on
angR = rad2deg(angR_rad);
angL = rad2deg(angL_rad);
tR.Data(:, 2) = round(angR*100)/100;
tR.Data(:, 3) = armR_um;
tL.Data(:, 2) = round(angL*100)/100;
tL.Data(:, 3) = armL_um;
armL = nanmedian(armL_um); % median
armR = nanmedian(armR_um); % median
% save all
disp('saving timestamped frames and data...')
save(fullfile(experimentHandle.flyfolder, sprintf('piezosDispl_fly%d_%s.mat', experimentHandle.flyNum, datestr(now, 30))), 'posR', 'posL', 'armR_um', 'armL_um', 'angR_rad', 'angL_rad', ...
'dispFrames', 'displacements', 'commands', 'stimOnDur', 'allFr' )
if isequal(get(f, 'waitstatus'), 'waiting')
%the GUI is still in UIWAIT, use UIRESUME
uiresume(f);
end
delete(s)
daqreset;
delete(f)
end
end