Hi Josh,
I am trying to add a feature for manually delivering water reward in a protocol for habituation (spontaneous licking triggers reward). I am using TrialManager and my main loop looks like this:
%% Main trial loop
for currentTrial = 1:MaxTrials
if BpodSystem.Status.BeingUsed == 0; return; end % If user hit console "stop" button, end session
[sma, S] = PrepareStateMachine(S); % Prepare next state machine.
% Since PrepareStateMachine is a function with a separate workspace, pass any local variables needed to make
% the state machine as fields of settings struct S e.g. S.learningRate = 0.2.
SendStateMachine(sma, 'RunASAP'); % With TrialManager, you can send the next trial's state machine while the current trial is ongoing
RawEvents = TrialManager.getTrialData; % Hangs here until trial is over, then retrieves full trial's raw data
if BpodSystem.Status.BeingUsed == 0; return; end % If user hit console "stop" button, end session
HandlePauseCondition; % Checks to see if the protocol is paused. If so, waits until user resumes.
TrialManager.startTrial(); % Start processing the next trial's events (call with no argument since SM was already sent)
%If trial data was returned from last trial, update plots and save data
if ~isempty(fieldnames(RawEvents)) % If trial data was returned from last trial, update plots and save data
BpodSystem.Data = AddTrialEvents(BpodSystem.Data,RawEvents); % Computes trial events from raw data
BpodSystem.Data.TrialSettings(currentTrial) = S; % Adds the settings used for the current trial to the Data struct (to be saved after the trial ends)
% define outcome types
if ~isnan(BpodSystem.Data.RawEvents.Trial{currentTrial}.States.reward(1))
Outcomes(currentTrial) = 1; % Hit
else
Outcomes(currentTrial) = 2; % Miss
end
BpodSystem.Data.Outcomes = Outcomes;
% Update online plots
trial_info_plot(currentTrial,time_temp)
lick_responses_plot(BpodSystem.Data, currentTrial, MaxTrials)
trial_outcome_plot(Outcomes, currentTrial,f)
% Saves the field BpodSystem.Data to the current data file
SaveBpodSessionData;
end
end
and my state machine looks like this:
function [sma, S] = PrepareStateMachine(S)
S = BpodParameterGUI('sync', S); % Sync parameters with BpodParameterGUI plugin
sma = NewStateMachine(); % Initialize new state machine description
sma = SetGlobalTimer(sma, 'TimerID', 1, 'Duration', 1); % set response window
sma = AddState(sma, 'Name', 'trial_start', ...
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer1_End', 'exit','BNC1High', 'reward','SoftCode1','reward'},...
'OutputActions', {'GlobalTimerTrig',1});
sma = AddState(sma, 'Name', 'reward', ...
'Timer', S.GUI.Rewad_Amount,...
'StateChangeConditions', {'Tup', 'continue_trial'},...
'OutputActions', {'Valve1',1 });
sma = AddState(sma, 'Name', 'continue_trial', ...
'Timer', 0,...
'StateChangeConditions', {'GlobalTimer1_End', 'exit','SoftCode1','reward'},...
'OutputActions', {});
However, there is no response when I input SendBpodSoftCode(1) in the MATLAB command window during the trial. When I terminate the session, I got an error message saying "Error sending soft code:Bpod must be in a trial". I am quite confused because I was sending the soft code during a trial.
Do you have any idea how I could solve this problem?
Thank you in advance.
Best,
Tin