-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepareNextTrial.m
111 lines (92 loc) · 3.31 KB
/
prepareNextTrial.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
%=========================================================================%
% individual states are
%sessionStart
%prepareNextTrial
%run
%timeOut
%trialEnd
%enOfExperiment
%=========================================================================%
% prepareNextTrial
% initializes trial specific information such us initializing base
% information
function fhandle = prepareNextTrial
global TRIAL; % save trial specific info here
global TRIAL_COUNT; % necessary to keep track of TRIAL count and set it for next trial;
global EXP;
global OFFLINE
global ScanImageUDP;
global EyeCameraUDP;
global TimelineUDP;
global OptiStimUDP;
global EXPREF;
% move servo to neutral position
[stagePos, Vout] = moveStage(Inf, Inf);
dL = Inf;
dR = Inf;
TRIAL_COUNT = TRIAL_COUNT +1;
info = [];
info.no = TRIAL_COUNT;
info.epoch = 0; %number of steps within a run loop
info.abort = 0;
info.start = -1; % it will be set to 1 at the beginning of run.m
if EXP.optiStim
nOptions = length(EXP.optiStimList);
optInd = ceil(rand*nOptions);
info.optiStim = EXP.optiStimList(optInd);
end
TRIAL.info = info;
% fprintf('PrepareNextTrial\n'); % debug
fprintf('*** trial %4d of %4d ***\n', TRIAL.info.no, EXP.maxNTrials); % debug
if isequal(EXP.stimType, 'REPLAY_SCRAMBLED')
nCounts = length(TRIAL.syncState);
TRIAL.pospars = {'X','Y','Z','theta','speed','inRoom'};
TRIAL.time = nan(nCounts,1,'double');
TRIAL.balldata = nan(nCounts,5,'double');
TRIAL.balldataParams = {'ballTime', 'dax', 'dbx', 'day', 'dby'};
else
TRIAL.posdata = zeros(3000,6,'double'); % x,y,z,theta,speed,inRoom
TRIAL.pospars = {'X','Y','Z','theta','speed','inRoom'};
TRIAL.glassWallsData = zeros(3000,4,'double'); % dL, dR, stagePos, Vout
TRIAL.glassWallsPars = {'distLeftWall', 'distRightWall', 'stagePosition', 'stageVoltageSignal'};
TRIAL.time = zeros(3000,1,'double');
TRIAL.balldata = []; %zeros(3000,5,'double');
TRIAL.balldataParams = {'ballTime', 'dax', 'dbx', 'day', 'dby'};
TRIAL.syncState=[0;0];
TRIAL.trialActive=[false; false];
TRIAL.freezeOver=[false; false];
end
%% now send UDPs to all the data hosts
if ~OFFLINE
[animalID, iseries, iexp] = dat.expRefToMpep(EXPREF);
istim=TRIAL.info.no;
irepeat=1;
StimDurSeconds=EXP.grayScreenDur+EXP.maxTrialDuration;
StimDur=ceil(StimDurSeconds*10); % to be consistent with mpep units
msgString = sprintf('StimStart %s %d %d %d %d %d', animalID, iseries, iexp, irepeat, istim, StimDur);
pnet(ScanImageUDP, 'write', msgString);
pnet(ScanImageUDP, 'writePacket');
pnet(EyeCameraUDP, 'write', msgString);
pnet(EyeCameraUDP, 'writePacket');
pnet(TimelineUDP, 'write', msgString);
pnet(TimelineUDP, 'writePacket');
if EXP.optiStim
msgStruct = struct('instruction', 'ZapPrepare',...
'ExpRef', EXPREF,...
'iTrial', TRIAL.info.no,...
'maxDuration', EXP.maxTrialDuration,...
'ML', info.optiStim.ML,...
'AP', info.optiStim.AP,...
'power', info.optiStim.laserPower);
msgJson = savejson('msg', msgStruct);
pnet(OptiStimUDP, 'write', msgJson);
pnet(OptiStimUDP, 'writePacket');
end
end
if isequal(EXP.stimType, 'REPLAY_SCRAMBLED')
fhandle = @runReplayScrambled;
else
fhandle = @run;
end
return
end