pb_vRunExp.m 5.02 KB
Newer Older
1
function pb_vRunExp(handles)
2
% PB_VRUNEXP
Jesse Heckman's avatar
Jesse Heckman committed
3
%
4
% PB_VRUNEXP(handles) forms the core body of experimental paradigms run in 
5
% the VC, and will loop over the blocks and trials provided by the exp-file.
6
7
% Note that PB_VRUNEXP is called from the vPrime GUI, from which it
% receives its experimental handles.
Jesse Heckman's avatar
Jesse Heckman committed
8
%
9
% See also PB_VPRIME, PB_VPRIMEGUI.
Jesse Heckman's avatar
Jesse Heckman committed
10
11
12
13
14
15

% PBToolbox (2018): JJH: j.heckman@donders.ru.nl

   %% INITIALIZE
   %  load & read experiment
   
Jesse Heckman's avatar
Jesse Heckman committed
16
   %  set debug mode
Jesse Heckman's avatar
Jesse Heckman committed
17
   debug = true;
Jesse Heckman's avatar
Jesse Heckman committed
18
   
Jesse Heckman's avatar
Jesse Heckman committed
19
   %  set handles
20
   pb_setupShow(handles);
21
22
23
   handles	= pb_gethandles(handles);
   handles 	= pb_getblock(handles);
   handles 	= pb_createdir(handles);
Jesse Heckman's avatar
Jesse Heckman committed
24
   handles	= pb_vInitialize(handles, true);
Jesse Heckman's avatar
Jesse Heckman committed
25
   
Jesse Heckman's avatar
Jesse Heckman committed
26
   %  set block information
27
28
   block          = handles.block;  
   nblocks        = handles.cfg.Blocks;
Jesse Heckman's avatar
Jesse Heckman committed
29
   Dat(nblocks)   = struct('VS',[],'EV',[],'PL_Gaze',[],'PL_Python',[],'PL_Primitive',[],'OT_Rigid',[],'BlockInfo',[]);
Jesse Heckman's avatar
Jesse Heckman committed
30
   
Jesse Heckman's avatar
Jesse Heckman committed
31
   %  initialize recordings
32
   rc             = pb_runPupil; 
33
   [ses,streams]  = pb_runLSL;
Jesse Heckman's avatar
Jesse Heckman committed
34
   experimentTime = tic;
Jesse Heckman's avatar
Jesse Heckman committed
35
   
Jesse Heckman's avatar
Jesse Heckman committed
36
37
   %% START BLOCK 
   %  iterate experimental blocks 
38
39
   
   for iBlck = 1:nblocks
Jesse Heckman's avatar
Jesse Heckman committed
40
41
42
43
44
      %  Runs blocks of trials with a vestibular condition
      
      %  set block information
      nTrials  	= length(block(iBlck).trial);
      handles  	= updateCount(handles,'trial','reset');
Jesse Heckman's avatar
Jesse Heckman committed
45
      
Jesse Heckman's avatar
Jesse Heckman committed
46
      %  store signal data
Jesse Heckman's avatar
Jesse Heckman committed
47
      [sig,profile,dur]       = pb_vSignalVC(handles);                     % reads, checks, creates & plots vestibular signals
Jesse Heckman's avatar
Jesse Heckman committed
48
      Dat(iBlck).VS           = sig;
Jesse Heckman's avatar
Jesse Heckman committed
49
50
      
      %  start recording
Jesse Heckman's avatar
Jesse Heckman committed
51
      pb_startLSL(ses);
52
      pb_startPupil(rc);
53
      
Jesse Heckman's avatar
Jesse Heckman committed
54
      %  start vestibular chair
Jesse Heckman's avatar
Jesse Heckman committed
55
      if ~ismac && ~debug      
56
57
         vs = pb_sendServo(profile);
         pb_startServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
58
         pause(6);   blockTime   = tic;                                    % allow vestibular chair to get in sync with input signal
Jesse Heckman's avatar
Jesse Heckman committed
59
60
      end

Jesse Heckman's avatar
Jesse Heckman committed
61
      %% RUN TRIALS
Jesse Heckman's avatar
Jesse Heckman committed
62
      %  iterate over trials per block
Jesse Heckman's avatar
Jesse Heckman committed
63
      
64
      for iTrl = 1:nTrials
Jesse Heckman's avatar
Jesse Heckman committed
65
         % Runs all trials within one block
Jesse Heckman's avatar
Jesse Heckman committed
66
         
Jesse Heckman's avatar
Jesse Heckman committed
67
         % setup trial
68
         updateTrial(handles);
Jesse Heckman's avatar
Jesse Heckman committed
69
         stim                 = handles.block(iBlck).trial(iTrl).stim;
Jesse Heckman's avatar
Jesse Heckman committed
70
         handles.cfg          = pb_vClearTrial(stim, handles.cfg); 
Jesse Heckman's avatar
Jesse Heckman committed
71
72
         [stim, handles.cfg]  = pb_vSetupTrial(stim, handles.cfg);
         trialTime            = tic;
Jesse Heckman's avatar
dbg    
Jesse Heckman committed
73
         
Jesse Heckman's avatar
Jesse Heckman committed
74
         pb_vRunTrial(handles.cfg.zBus, handles.cfg.trialdur);
75
         pb_vTraces(handles);       
Jesse Heckman's avatar
Jesse Heckman committed
76
         
Jesse Heckman's avatar
Jesse Heckman committed
77
         handles        = pb_vStoreData(handles, sig);
Jesse Heckman's avatar
Jesse Heckman committed
78
         handles.cfg    = updateCount(handles.cfg,'trial','count');
Jesse Heckman's avatar
check    
Jesse Heckman committed
79
         toc(trialTime)
Jesse Heckman's avatar
Jesse Heckman committed
80
81
      end
      
Jesse Heckman's avatar
Jesse Heckman committed
82
83
84
85
      %% END BLOCK
      %  stop chair, pupil labs, optitrack and LSL, save data
      
      %  stop vestibular chair
Jesse Heckman's avatar
Jesse Heckman committed
86
      if ~ismac && ~debug  
Jesse Heckman's avatar
check    
Jesse Heckman committed
87
         elapsedTime = toc(blockTime);                
Jesse Heckman's avatar
Jesse Heckman committed
88
         if elapsedTime < dur; pause(dur-floor(elapsedTime)); end          % wait untill chair is finished running before disabling.
Jesse Heckman's avatar
Jesse Heckman committed
89
         pb_stopServo(vs);
90
         Dat = pb_readServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
91
      end
Jesse Heckman's avatar
Jesse Heckman committed
92
93

      %  stop recording
94
      pb_stopPupil(rc);
Jesse Heckman's avatar
Jesse Heckman committed
95
      pb_stopLSL(ses); 
Jesse Heckman's avatar
Jesse Heckman committed
96
      
Jesse Heckman's avatar
Jesse Heckman committed
97
      %  store data
Jesse Heckman's avatar
Jesse Heckman committed
98
99
100
101
      Dat(iBlck).EV           = streams(1).read;
      Dat(iBlck).PL_Python    = streams(2).read;
      Dat(iBlck).PL_Gaze      = streams(3).read;
      Dat(iBlck).PL_Primitive = streams(4).read;
Jesse Heckman's avatar
Jesse Heckman committed
102
      Dat(iBlck).OT_Rigid     = streams(5).read;
Jesse Heckman's avatar
Jesse Heckman committed
103
      Dat(iBlck).BlockInfo    = handles.block(iBlck);
Jesse Heckman's avatar
Jesse Heckman committed
104

Jesse Heckman's avatar
Jesse Heckman committed
105
      %  update block information
Jesse Heckman's avatar
Jesse Heckman committed
106
      handles.cfg = updateCount(handles.cfg,'block','count');
Jesse Heckman's avatar
Jesse Heckman committed
107
   end 
Jesse Heckman's avatar
Jesse Heckman committed
108
   
Jesse Heckman's avatar
Jesse Heckman committed
109
110
   %% CHECK OUT
   %  finalizes experiment, and resets handles.
Jesse Heckman's avatar
Jesse Heckman committed
111
   
Jesse Heckman's avatar
Jesse Heckman committed
112
   %  check out experiment
Jesse Heckman's avatar
Jesse Heckman committed
113
   pb_vEndExp;
Jesse Heckman's avatar
Jesse Heckman committed
114
115
   pb_vStoreBlockDat(handles.cfg, Dat);
   pb_vInitialize(handles, false);
Jesse Heckman's avatar
check    
Jesse Heckman committed
116
   toc(experimentTime)
Jesse Heckman's avatar
Jesse Heckman committed
117
118
end

119
120
%-- Feedback functions --%
function updateTrial(handles)
Jesse Heckman's avatar
Jesse Heckman committed
121
122
   %  Updates the trial information to the GUI
   
123
   tn = handles.cfg.trialnumber;
Jesse Heckman's avatar
Jesse Heckman committed
124
   handles.figure1.Name = ['vPrime - ' num2str(tn(2)) '/' num2str(handles.cfg.Trials) ' Trials'];        % counting title
125

Jesse Heckman's avatar
Jesse Heckman committed
126
   str = num2str(tn(1),'%03d');                                                                          % blocktrial
Jesse Heckman's avatar
Jesse Heckman committed
127
   set(handles.Tn,'string',str);
128
129
end

Jesse Heckman's avatar
Jesse Heckman committed
130
131
function cfg = updateCount(cfg,varargin)
   %  Updates the count of trialnumber and block number during experiment
Jesse Heckman's avatar
Jesse Heckman committed
132
   
Jesse Heckman's avatar
Jesse Heckman committed
133
   %  initializes update information
134
135
136
   trial = pb_keyval('trial',varargin);
   block = pb_keyval('block',varargin);
   
Jesse Heckman's avatar
Jesse Heckman committed
137
   %  sets trials
138
139
140
141
142
143
144
145
   if ~isempty(trial)
      switch trial
         case 'count'
            cfg.trialnumber      = cfg.trialnumber+1;
         case 'reset'
            cfg.trialnumber(1)   = 1;
      end
   end
Jesse Heckman's avatar
Jesse Heckman committed
146
   
Jesse Heckman's avatar
Jesse Heckman committed
147
   %  sets block
148
149
150
151
152
153
   if ~isempty(block)
      switch block
         case 'count'
            cfg.blocknumber      = cfg.blocknumber+1;
      end
   end
Jesse Heckman's avatar
Jesse Heckman committed
154
155
156
end


Jesse Heckman's avatar
Jesse Heckman committed
157
158
159
160
161
162
163
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%                                                           %
%       Part of Programmeer Beer Toolbox (PBToolbox)        %
%       Written by: Jesse J. Heckman (2018)                 %
%                                                           %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %