Hi Josh,

I would like to restrict the duration of each behavioral bock to be 1 hour. The tic/toc does not work for me because the behavioral process can be paused in the middle of one trial.

Is there any convenient way to stop the Bpod after 1h automatically?

Thanks,
Xiong

Hi Xiong,

If you want the hour to include time while the protocol is paused, you could also use:

% Just before the main loop
startTime = now*100000; 

...
% Inside the main loop
currentTime = now*100000; 

% At the bottom of the main loop
if (currentTime - startTime >= 3600) 
   % End the protocol
end

MATLAB's now() function returns the current time (multiply by 100000 to get seconds, 3600 seconds per hour).

If the idea is to exclude time while the protocol is paused, you can create a local variable, you can add the trial duration of each trial, and compare the sum to 3600:

% Just before the main loop
totalTrialTime = 0; 
...

% After each call to AddTrialEvents()
totalTrialTime = totalTrialTime + (BpodSystem.Data.TrialEndTimestamp(currentTrial) - BpodSystem.Data.TrialStartTimestamp(currentTrial)); 

% At the bottom of the main loop
if (totalTrialTime >= 3600) 
  % End the protocol
end

This works because pauses occur in the inter-trial interval. Please note that your sum will not include the ~7-10ms of dead-time between trials (assuming that you're not using TrialManager).

I hope this helps!
-Josh

Josh wrote[quote='Josh' pid='1278' dateline='1576603301']

Hi Xiong,

If you want the hour to include time while the protocol is paused, you could also use:

% Just before the main loop
startTime = now*100000; 

...
% Inside the main loop
currentTime = now*100000; 

% At the bottom of the main loop
if (currentTime - startTime >= 3600) 
   % End the protocol
end

MATLAB's now() function returns the current time (multiply by 100000 to get seconds, 3600 seconds per hour).

If the idea is to exclude time while the protocol is paused, you can create a local variable, you can add the trial duration of each trial, and compare the sum to 3600:

% Just before the main loop
totalTrialTime = 0; 
...

% After each call to AddTrialEvents()
totalTrialTime = totalTrialTime + (BpodSystem.Data.TrialEndTimestamp(currentTrial) - BpodSystem.Data.TrialStartTimestamp(currentTrial)); 

% At the bottom of the main loop
if (totalTrialTime >= 3600) 
  % End the protocol
end

This works because pauses occur in the inter-trial interval. Please note that your sum will not include the ~7-10ms of dead-time between trials (assuming that you're not using TrialManager).

I hope this helps!
-Josh

Hi Josh,

This seems does not work for me. The behavioral task that I am using is an operand task that mice will gradually reduce the motivation to finish the whole trial (the task requires more actions in the later phase). The trial was paused usually during the middle steps of a trial, but not the ITI period (the trial can be paused for several minitues). So I cannot use the "now" function in the Matlab to get the current time, because the Matlab process was paused in the "RawEvents = RunStateMatrix;" line.

Thanks,
Josh

4 years later

% At the bottom of the main loop
if (totalTrialTime >= 3600)
% End the protocol
end

How exactly can I end the protocol?

Hello Panagiota,

The return command exits the main loop.

Each example protocol has the following conditional at the end of the main loop, to end the session if the user has pressed the 'Stop' button on the console GUI:
if BpodSystem.Status.BeingUsed == 0
return
end

You could add a condition to the existing stop criterion:

if (BpodSystem.Status.BeingUsed == 0) || (totalTrialTime >= 3600)
return
end

-Josh

Hi Josh,
Thank you for getting back to me so quickly!
I am testing it in the emulator mode. While the task seems to be ending - no more rewards given, etc. - the Bpod GUI seems to still be in use. In other words, I still need to press the STOP button on the GUI to start another protocol. Is there a way around it?

Hi Panagiota,

To stop the protocol and restore the button state, you'll need one additional line:

if (BpodSystem.Status.BeingUsed == 0) || (totalTrialTime >= 10)
RunProtocol('Stop');
return
end

However, this will still not work because there is an issue with totalTrialTime. I found a bug in the emulator - trial end time was not computed correctly. To make this example work, you'll need to patch a file. From the develop branch of Bpod_Gen2, copy /Functions/RunStateMachine.m over the equivalent file in your local repository. After this patch you should have the correct totalTrialTime. The corrected file will be included with the next release.

-Josh