pb_vRunExp.m 5.16 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
22
   handles	= pb_gethandles(handles);
   handles 	= pb_getblock(handles);
   handles 	= pb_createdir(handles);
   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('vestibular_signal',[],'PL',[],'OT',[],'LSL',[]);
Jesse Heckman's avatar
Jesse Heckman committed
28
   
Jesse Heckman's avatar
Jesse Heckman committed
29
30
31
32
   %  initialize recordings
   rc             = pb_runPupil; 
   [ses,streams]  = pb_runLSL('ot',false);
   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
45
46
47
48
      %  store signal data
      [sig,profile,dur]             = pb_vSignalVC(handles);               % reads, checks, creates & plots vestibular signals
      Dat(iBlck).vestibular_signals = sig;
      
      %  start recording
Jesse Heckman's avatar
Jesse Heckman committed
49
50
      pb_startLSL(ses);
      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
         pb_sendServo(vs, profile);
Jesse Heckman's avatar
Jesse Heckman committed
55
         pb_startServo(vs);
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
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
75
76
         
         %handles        = pb_vStoreData(handles, bDat);
         handles        = updateCount(handles,'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
         Dat = pb_stopServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
89
      end
Jesse Heckman's avatar
Jesse Heckman committed
90
91

      %  stop recording
Jesse Heckman's avatar
Jesse Heckman committed
92
      pb_stopPupil(rc);
Jesse Heckman's avatar
Jesse Heckman committed
93
      pb_stopLSL(ses); 
Jesse Heckman's avatar
Jesse Heckman committed
94
      
Jesse Heckman's avatar
Jesse Heckman committed
95
      %  store data
Jesse Heckman's avatar
Jesse Heckman committed
96
97
98
99
      if ~exist('LSL_Dat','var')
         LSL_Data  = {};
      end
      
Jesse Heckman's avatar
Jesse Heckman committed
100
101
      LSL_Dat.ev_dat = streams(1).read;
      LSL_Dat.pl_dat = streams(2).read;
Jesse Heckman's avatar
Jesse Heckman committed
102
      % LSL_Dat.ot_dat = str(3).read;
Jesse Heckman's avatar
Jesse Heckman committed
103
      
Jesse Heckman's avatar
Jesse Heckman committed
104
      % TODO: SAVE LSL DATA
Jesse Heckman's avatar
Jesse Heckman committed
105
      % save(lsl_file, 'LSL_Dat');
Jesse Heckman's avatar
Jesse Heckman committed
106
107
108
      
      %  update block information
      handles = updateCount(handles,'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(handles.cfg);
116
   pb_vInitialize(handles,false);
Jesse Heckman's avatar
check    
Jesse Heckman committed
117
   toc(experimentTime)
Jesse Heckman's avatar
Jesse Heckman committed
118
119
end

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

   str = num2str(tn(1),'%03d');                                                                       % blocktrial
   set(handles.Tn,'string',str)
end

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

Jesse Heckman's avatar
Jesse Heckman committed
156
%-- Run VC functions --%
Jesse Heckman's avatar
Jesse Heckman committed
157
158
159
160
161
162
% function send_profile(profile)
%    % writing profile to servo
%    
%    vs    = vs_servo;
%    vs.write_profile(profile.v,profile.h);
% end
Jesse Heckman's avatar
Jesse Heckman committed
163

Jesse Heckman's avatar
Jesse Heckman committed
164
165
166
167
168
169
170
171
172
173
174
% function Dat = read_profile(vs)
%    % read profile
%    
%    [sv.vertical,sv.horizontal] = vs.read_profile_sv;
%    [pv.vertical,pv.horizontal] = vs.read_profile_pv;
%    
%    delete(vs);
%    
%    Dat.sv   = sv;
%    Dat.pv   = pv;
% end
Jesse Heckman's avatar
Jesse Heckman committed
175

Jesse Heckman's avatar
Jesse Heckman committed
176

Jesse Heckman's avatar
Jesse Heckman committed
177
178
179
180
181
182
183
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%                                                           %
%       Part of Programmeer Beer Toolbox (PBToolbox)        %
%       Written by: Jesse J. Heckman (2018)                 %
%                                                           %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %