pb_vRunExp.m 5.08 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      
Jesse Heckman's avatar
Jesse Heckman committed
56
57
         vs          = pb_sendServo(profile);
         blockTime   = tic; 
58
         pb_startServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
59
         pause(6);                                                         % allow vestibular chair to get in sync with input signal
Jesse Heckman's avatar
Jesse Heckman committed
60
61
      end

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

      %  stop recording
96
      pb_stopPupil(rc);
Jesse Heckman's avatar
Jesse Heckman committed
97
      pb_stopLSL(ses); 
Jesse Heckman's avatar
Jesse Heckman committed
98
      
Jesse Heckman's avatar
Jesse Heckman committed
99
      %  store data
Jesse Heckman's avatar
Jesse Heckman committed
100
101
102
103
      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
104
      Dat(iBlck).OT_Rigid     = streams(5).read;
Jesse Heckman's avatar
Jesse Heckman committed
105
      Dat(iBlck).BlockInfo    = handles.block(iBlck);
Jesse Heckman's avatar
Jesse Heckman committed
106

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

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

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

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


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