pb_vRunExp.m 4.68 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
   debug = true;
Jesse Heckman's avatar
check    
Jesse Heckman committed
15
   experimentTime = tic;
Jesse Heckman's avatar
Jesse Heckman committed
16
   
17
   pb_setupShow(handles);
18
19
20
21
   handles	= pb_gethandles(handles);
   handles 	= pb_getblock(handles);
   handles 	= pb_createdir(handles);
   handles	= pb_vInitialize(handles,true);
Jesse Heckman's avatar
Jesse Heckman committed
22
   
23
24
25
   block          = handles.block;  
   nblocks        = handles.cfg.Blocks;
   bDat(nblocks)  = struct('v',[],'h',[]);
Jesse Heckman's avatar
Jesse Heckman committed
26
   
Jesse Heckman's avatar
Jesse Heckman committed
27
28
   rc          = pb_runPupil; 
   [ses,str]   = pb_runLSL('ot',false);
Jesse Heckman's avatar
Jesse Heckman committed
29
   
30
   %% CORE BODY 
Jesse Heckman's avatar
Jesse Heckman committed
31
   %  iterate experiment 
32
33
   
   for iBlck = 1:nblocks
Jesse Heckman's avatar
Jesse Heckman committed
34
35
      % Runs blocks of trials with a vestibular condition
      
36
      nTrials                       = length(block(iBlck).trial);
37
38
      handles                       = updateCount(handles,'trial','reset');
      [bDat(iBlck),profile,dur]     = pb_vSignalVC(handles);               % reads, checks, creates & plots vestibular signals
Jesse Heckman's avatar
Jesse Heckman committed
39
40
41
            
      pb_startLSL(ses);
      pb_startPupil(rc);
42
      
Jesse Heckman's avatar
Jesse Heckman committed
43
44
      if ~ismac && ~debug      
         %  START CHAIR
Jesse Heckman's avatar
Jesse Heckman committed
45
         send_profile(profile); 
Jesse Heckman's avatar
Jesse Heckman committed
46
47
         pb_sendServo(vs,profile);
         pb_startServo(vs);
Jesse Heckman's avatar
Jesse Heckman committed
48
      end
Jesse Heckman's avatar
Jesse Heckman committed
49
      blockTime = tic;
Jesse Heckman's avatar
Jesse Heckman committed
50

Jesse Heckman's avatar
Jesse Heckman committed
51
52
53
      %% RUN TRIALS
      %  iterate trials
      
54
      for iTrl = 1:nTrials
Jesse Heckman's avatar
Jesse Heckman committed
55
         % Runs all trials within one block
Jesse Heckman's avatar
Jesse Heckman committed
56
         
Jesse Heckman's avatar
check    
Jesse Heckman committed
57
         trialTime = tic;
58
         
59
         updateTrial(handles);
60
61
         stim				= handles.block(iBlck).trial(iTrl).stim;
         handles.cfg    = pb_vClearTrial(stim,handles.cfg); 
62
         
Jesse Heckman's avatar
dbg    
Jesse Heckman committed
63
64
65
         [stim, cfg]    = pb_vSetupTrial(stim, handles.cfg);
         
         pb_vRunTrial(handles.cfg, stim);
66
67
68
69
70

         % pb_vFeedbackGUI();          %% <-- MAYBE NOT NECESSAIRY?
         pb_vTraces(handles);       
         handles        = pb_vStoreData(handles, bDat);
         handles        = updateCount(handles,'trial','count');            % update trial
Jesse Heckman's avatar
check    
Jesse Heckman committed
71
         toc(trialTime)
Jesse Heckman's avatar
Jesse Heckman committed
72
73
      end
      
74
      %  STOP CHAIR
Jesse Heckman's avatar
Jesse Heckman committed
75
      if ~ismac && ~debug  
Jesse Heckman's avatar
check    
Jesse Heckman committed
76
         elapsedTime = toc(blockTime);                
Jesse Heckman's avatar
Jesse Heckman committed
77
78
         if elapsedTime < dur; pause(dur-floor(elapsedTime)); end          % wait untill chair is finished running before disabling.

Jesse Heckman's avatar
Jesse Heckman committed
79
         pb_stopServo(vs)
Jesse Heckman's avatar
Jesse Heckman committed
80
         
Jesse Heckman's avatar
Jesse Heckman committed
81
82
         Dat  = pb_readServo(vs);

Jesse Heckman's avatar
Jesse Heckman committed
83
      end
84
      handles = updateCount(handles,'block','count');
Jesse Heckman's avatar
Jesse Heckman committed
85
      
Jesse Heckman's avatar
Jesse Heckman committed
86
      pb_stopPupil(rc);
Jesse Heckman's avatar
Jesse Heckman committed
87
      pb_stopLSL(ses); 
Jesse Heckman's avatar
Jesse Heckman committed
88
89
90
91
92
93
94
      
      if ~exist('LSL_Dat','var')
         LSL_Data  = {};
      end
      
      LSL_Dat.ev_dat = str(1).read;
      LSL_Dat.pl_dat = str(2).read;
Jesse Heckman's avatar
Jesse Heckman committed
95
      % LSL_Dat.ot_dat = str(3).read;
Jesse Heckman's avatar
Jesse Heckman committed
96
      
Jesse Heckman's avatar
Jesse Heckman committed
97
      % TODO: SAVE LSL DATA
Jesse Heckman's avatar
Jesse Heckman committed
98
      % save(lsl_file, 'LSL_Dat');
Jesse Heckman's avatar
Jesse Heckman committed
99
   end 
Jesse Heckman's avatar
Jesse Heckman committed
100
   
Jesse Heckman's avatar
Jesse Heckman committed
101
102
   %% CHECK OUT
   %  finalizes experiment, and resets handles.
Jesse Heckman's avatar
Jesse Heckman committed
103
   
Jesse Heckman's avatar
Jesse Heckman committed
104
   pb_vEndExp(handles.cfg);
105
   pb_vInitialize(handles,false);
Jesse Heckman's avatar
check    
Jesse Heckman committed
106
   toc(experimentTime)
Jesse Heckman's avatar
Jesse Heckman committed
107
108
end

109
110
%-- Feedback functions --%
function updateTrial(handles)
Jesse Heckman's avatar
Jesse Heckman committed
111
   % Updates the trial information to the GUI
112
113
114
115
116
117
118
119
120
121
122
   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
123
   
124
125
126
127
128
129
130
131
132
133
134
   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
135
   
136
137
138
139
140
141
142
   if ~isempty(block)
      switch block
         case 'count'
            cfg.blocknumber      = cfg.blocknumber+1;
      end
   end
   handles.cfg = cfg;
Jesse Heckman's avatar
Jesse Heckman committed
143
144
end

Jesse Heckman's avatar
Jesse Heckman committed
145
146
%-- Run VC functions --%
function send_profile(profile)
147
   % writing profile to servo
Jesse Heckman's avatar
Jesse Heckman committed
148
   
149
   vs    = vs_servo;
Jesse Heckman's avatar
Jesse Heckman committed
150
151
152
   vs.write_profile(profile.v,profile.h);
end

Jesse Heckman's avatar
Jesse Heckman committed
153
function Dat = read_profile(vs)
154
155
   % read profile
   
Jesse Heckman's avatar
Jesse Heckman committed
156
157
   [sv.vertical,sv.horizontal] = vs.read_profile_sv;
   [pv.vertical,pv.horizontal] = vs.read_profile_pv;
158
   
Jesse Heckman's avatar
Jesse Heckman committed
159
   delete(vs);
Jesse Heckman's avatar
Jesse Heckman committed
160
161
162
   
   Dat.sv   = sv;
   Dat.pv   = pv;
Jesse Heckman's avatar
Jesse Heckman committed
163
164
end

Jesse Heckman's avatar
Jesse Heckman committed
165

Jesse Heckman's avatar
Jesse Heckman committed
166
167
168
169
170
171
172
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%                                                           %
%       Part of Programmeer Beer Toolbox (PBToolbox)        %
%       Written by: Jesse J. Heckman (2018)                 %
%                                                           %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %