pb_vRunExp.m 4.84 KB
Newer Older
1
function pb_vRunExp(handles)
2
% PB_VRUNEXP(HANDLES)
Jesse Heckman's avatar
Jesse Heckman committed
3
%
4
5
% PB_VRUNEXP(HANDLES) forms the core body of experimental paradigms run in 
% the VC, and will loop over the blocks and trials provided by the exp-file.
Jesse Heckman's avatar
Jesse Heckman committed
6
%
7
% See also PB_VPRIME, PB_VPRIMEGUI.
Jesse Heckman's avatar
Jesse Heckman committed
8
9
10
11
12
13

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

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

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

      %  stop recording
93
      pb_stopPupil(rc);
Jesse Heckman's avatar
Jesse Heckman committed
94
      pb_stopLSL(ses); 
Jesse Heckman's avatar
Jesse Heckman committed
95
      
Jesse Heckman's avatar
Jesse Heckman committed
96
      %  store data
Jesse Heckman's avatar
Jesse Heckman committed
97
98
99
100
      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
101
      Dat(iBlck).OT_Rigid     = streams(5).read;
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(handles.cfg);
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
118
%-- Feedback functions --%
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)                 %
%                                                           %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %