pb_vRunExp.m 4.83 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
29
   block       = handles.block;  
   nblocks     = handles.cfg.Blocks;
   Dat         = pb_dataobj(nblocks);
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
      %  Runs blocks of trials with a vestibular condition
      
      %  set block information
43
44
45
      nTrials              = length(block(iBlck).trial);
      handles              = updateCount(handles,'trial','reset');
      [sig,profile,dur]    = pb_vSignalVC(handles);
Jesse Heckman's avatar
Jesse Heckman committed
46
47
      
      %  start recording
Jesse Heckman's avatar
Jesse Heckman committed
48
      pb_startLSL(ses);
49
      pb_startPupil(rc);
50
      
Jesse Heckman's avatar
Jesse Heckman committed
51
      %  start vestibular chair
52
53
      if ~ismac && ~debug     
         pb_vCheckServo;
Jesse Heckman's avatar
Jesse Heckman committed
54
55
         vs          = pb_sendServo(profile);
         blockTime   = tic; 
56
         pb_startServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
57
         pause(6);                                                         % allow vestibular chair to get in sync with input signal
Jesse Heckman's avatar
Jesse Heckman committed
58
59
      end

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

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

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

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

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


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