pb_vRunExp.m 4.29 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
check    
Jesse Heckman committed
14
   debug = false;
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
   
27
28
   %% CORE BODY 
   %  iterate experiment
29
30
   
   for iBlck = 1:nblocks
Jesse Heckman's avatar
Jesse Heckman committed
31
32
      % Runs blocks of trials with a vestibular condition
      
33
      nTrials                       = length(block(iBlck).trial);
34
35
      handles                       = updateCount(handles,'trial','reset');
      [bDat(iBlck),profile,dur]     = pb_vSignalVC(handles);               % reads, checks, creates & plots vestibular signals
36
      
37
      %  START CHAIR
Jesse Heckman's avatar
Jesse Heckman committed
38
      if ~ismac && ~debug                                        
Jesse Heckman's avatar
Jesse Heckman committed
39
         send_profile(profile); 
40
         
Jesse Heckman's avatar
Jesse Heckman committed
41
         vs = vs_servo;
42
         vs.enable;        pause(1); 
Jesse Heckman's avatar
check    
Jesse Heckman committed
43
         vs.start;         blockTime = tic;
Jesse Heckman's avatar
Jesse Heckman committed
44
45
      end

46
47
      %%  RUN TRIALS
      for iTrl = 1:nTrials
Jesse Heckman's avatar
Jesse Heckman committed
48
         % Runs all trials within one block
Jesse Heckman's avatar
check    
Jesse Heckman committed
49
         trialTime = tic;
50
         
51
         updateTrial(handles);
52
53
         stim				= handles.block(iBlck).trial(iTrl).stim;
         handles.cfg    = pb_vClearTrial(stim,handles.cfg); 
54
         
Jesse Heckman's avatar
dbg    
Jesse Heckman committed
55
56
57
         [stim, cfg]    = pb_vSetupTrial(stim, handles.cfg);
         
         pb_vRunTrial(handles.cfg, stim);
Jesse Heckman's avatar
dbg    
Jesse Heckman committed
58
         % pb_trialclean(stim, cfg);
59
60
61
62
63

         % 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
64
         toc(trialTime)
Jesse Heckman's avatar
Jesse Heckman committed
65
66
      end
      
67
      %  STOP CHAIR
Jesse Heckman's avatar
Jesse Heckman committed
68
      if ~ismac && ~debug  
Jesse Heckman's avatar
check    
Jesse Heckman committed
69
         elapsedTime = toc(blockTime);                
Jesse Heckman's avatar
Jesse Heckman committed
70
71
72
73
74
         if elapsedTime < dur; pause(dur-floor(elapsedTime)); end          % wait untill chair is finished running before disabling.

         vs.stop;
         vs.disable;
         delete(vs);
Jesse Heckman's avatar
Jesse Heckman committed
75
         
Jesse Heckman's avatar
Jesse Heckman committed
76
77
78
         [sv,pv]  = read_profile;
         Dat.sv   = sv;
         Dat.pv   = pv;
Jesse Heckman's avatar
Jesse Heckman committed
79
      end
80
      handles = updateCount(handles,'block','count');
Jesse Heckman's avatar
Jesse Heckman committed
81
   end 
Jesse Heckman's avatar
Jesse Heckman committed
82
   %% CHECK OUT
83
   pb_vInitialize(handles,false);
Jesse Heckman's avatar
check    
Jesse Heckman committed
84
   toc(experimentTime)
Jesse Heckman's avatar
Jesse Heckman committed
85
86
end

87
88
%-- Feedback functions --%
function updateTrial(handles)
Jesse Heckman's avatar
Jesse Heckman committed
89
   % Updates the trial information to the GUI
90
91
92
93
94
95
96
97
98
99
100
   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
101
   
102
103
104
105
106
107
108
109
110
111
112
   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
113
   
114
115
116
117
118
119
120
   if ~isempty(block)
      switch block
         case 'count'
            cfg.blocknumber      = cfg.blocknumber+1;
      end
   end
   handles.cfg = cfg;
Jesse Heckman's avatar
Jesse Heckman committed
121
122
end

Jesse Heckman's avatar
Jesse Heckman committed
123
124
%-- Run VC functions --%
function send_profile(profile)
125
   % writing profile to servo
Jesse Heckman's avatar
Jesse Heckman committed
126
   
127
   vs    = vs_servo;
Jesse Heckman's avatar
Jesse Heckman committed
128
   vs.write_profile(profile.v,profile.h);
129
   
Jesse Heckman's avatar
Jesse Heckman committed
130
131
132
133
   delete(vs);
end

function [sv,pv] = read_profile
134
135
136
   % read profile
   
   vs    = vs_servo;
Jesse Heckman's avatar
Jesse Heckman committed
137
138
139
   
   [sv.vertical,sv.horizontal] = vs.read_profile_sv;
   [pv.vertical,pv.horizontal] = vs.read_profile_pv;
140
   
Jesse Heckman's avatar
Jesse Heckman committed
141
142
143
   delete(vs);
end

Jesse Heckman's avatar
Jesse Heckman committed
144

Jesse Heckman's avatar
Jesse Heckman committed
145
146
147
148
149
150
151
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%                                                           %
%       Part of Programmeer Beer Toolbox (PBToolbox)        %
%       Written by: Jesse J. Heckman (2018)                 %
%                                                           %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %