Forums
Simple TTL out help - Printable Version

+- Forums (https://sanworks.io/forum)
+-- Forum: Bpod (https://sanworks.io/forum/forumdisplay.php?fid=1)
+--- Forum: Software (https://sanworks.io/forum/forumdisplay.php?fid=6)
+--- Thread: Simple TTL out help (/showthread.php?tid=760)



Simple TTL out help - k8flr - 03-06-2020

Hello,

I am new to Bpod programming and running into some basic roadblocks as I learn to develop my own protocol. 

For now, I just want to generate a TTL pulse with a specific delay from a specific BNC port on my Bpod r2 state machine. I have managed to launch the code, however I keep getting error messages that do not indicate (to me) what I am doing incorrectly.


Code:
function TTLpulse
global BpodSystem

MaxTrials = 10;

S = BpodSystem.ProtocolSettings;
if isempty(fieldnames(S))
% no field names needed
end

BpodParameterGUI('init',S);

for currentTrial = 1:MaxTrials
  S = BpodSystem.ProtocolSettings;

  sma = NewStateMachine()
  sma = AddState(sma, 'Name', 'Trigger1', 'Timer', 0, 'StateChangeConditions', {'BNC1', 'High'}, 'OutputActions', {});
  sma = AddState(sma, 'Name', 'Delay', 'Timer', 10, 'StateChangeConditions', {'Tup', 'exit'}, 'OutputActions', {});
  SendStateMatrix(sma);  
  RawEvents = RunStateMatrix;

end

end

Thank you for any help, advice, or resources for programming BNC TTL pulses from a Bpod r2.


RE: Simple TTL out help - Josh - 03-09-2020

Hi K8flr,

Can you please share the error messages you're seeing?

One thing I'd point out - if you don't need a settings struct, you can remove all code referring to it. I'm actually not sure how that plugin handles the case where you pass an empty struct. Also, in your output actions, the state of 'BNC1' must be either 0 or 1 (for low and high). With those changes, your code becomes:

Code:
function TTLpulse
global BpodSystem
MaxTrials = 10;
for currentTrial = 1:MaxTrials
 sma = NewStateMachine()
 sma = AddState(sma, 'Name', 'Trigger1', 'Timer', 0, 'StateChangeConditions', {'BNC1', 1}, 'OutputActions', {});
 sma = AddState(sma, 'Name', 'Delay', 'Timer', 10, 'StateChangeConditions', {'Tup', 'exit'}, 'OutputActions', {});
 SendStateMatrix(sma);  
 RawEvents = RunStateMatrix;
end
end


-Josh


RE: Simple TTL out help - k8flr - 03-09-2020

(03-09-2020, 01:34 AM)Josh Wrote: Hi Josh,

thank you so much for this! Unfortunately, when I run the modified code I continue to get the same error messages. which are as follows: 
Code:
Error using AddState>EventSpellingErrorMessage (line 267)
Check spelling of your state transition events for state: . Valid events (% is an index): Port%In Port%Out BNC%High
BNC%Low Wire%High Wire%Low SoftCode% GlobalTimer%End Tup

Error in AddState (line 164)
       EventSpellingErrorMessage(ThisStateName);

Error in TTLpulse (line 72)
sma = AddState(sma, 'Name', 'Trigger1', 'Timer', 0, 'StateChangeConditions', {'BNC1', 1}, 'OutputActions', {});

Error in run (line 91)
evalin('caller', strcat(script, ';'));

Error in NewLaunchManager>LaunchProtocol (line 707)
run(ProtocolPath);

Error while evaluating DestroyedObject Callback.
I have double checked that I am using the correct protocol and data path, so its not that. Also, this EventSpellingErrorMessage has me quite puzzled since I have tried changing numerous things and it is always the same message (this error has been consistent throughout my protocol development.

thanks again for the help.

Cheers,

K8

-=-=-=-=-=-=-=-=-=-----
END MESSAGE
---=-=-=-=-=-=-=-=-=-=--------

Hi K8flr,

Can you please share the error messages you're seeing?

One thing I'd point out - if you don't need a settings struct, you can remove all code referring to it. I'm actually not sure how that plugin handles the case where you pass an empty struct. Also, in your output actions, the state of 'BNC1' must be either 0 or 1 (for low and high). With those changes, your code becomes:

Code:
function TTLpulse
global BpodSystem
MaxTrials = 10;
for currentTrial = 1:MaxTrials
 sma = NewStateMachine()
 sma = AddState(sma, 'Name', 'Trigger1', 'Timer', 0, 'StateChangeConditions', {'BNC1', 1}, 'OutputActions', {});
 sma = AddState(sma, 'Name', 'Delay', 'Timer', 10, 'StateChangeConditions', {'Tup', 'exit'}, 'OutputActions', {});
 SendStateMatrix(sma);  
 RawEvents = RunStateMatrix;
end
end


-Josh



RE: Simple TTL out help - Josh - 03-10-2020

Hi K8flr,


My error - in the 'Trigger1' state, {'BNC1', 1} should be in 'OutputActions', not 'StateChangeConditions'.
StateChangeConditions should be {'Tup', 'Delay'}
So your code would then be:

Code:
function TTLpulse
global BpodSystem
MaxTrials = 10;
for currentTrial = 1:MaxTrials
sma = NewStateMachine()
sma = AddState(sma, 'Name', 'Trigger1', 'Timer', 0, 'StateChangeConditions', {'Tup', Delay}, 'OutputActions', {'BNC1', 1});
sma = AddState(sma, 'Name', 'Delay', 'Timer', 10, 'StateChangeConditions', {'Tup', 'exit'}, 'OutputActions', {});
SendStateMatrix(sma);  
RawEvents = RunStateMatrix;
end
end

If you leave the 'Timer' of the 'Trigger1' state set to 0, the BNC output will go high for only 100 microseconds. I'd consider setting a longer delay to create a longer pulse, depending on what is reading it out (e.g. if it's a channel that's sampled at 1kHz, you'd want at least 2ms to ensure the pulse is read).

-Josh


RE: Simple TTL out help - k8flr - 03-12-2020

Brilliant! This worked! Thank you, Josh!
Cheers,

Kate