def do_bpod_sound(bpod, sound_msg): sma = StateMachine(bpod) sma.add_state( state_name='play_tone', state_timer=0, output_actions=[('Serial3', sound_msg)], state_change_conditions={'BNC2Low': 'exit'}, ) bpod.send_state_machine(sma) bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached return
def do_valve_click(bpod, reward_valve_time): sma = StateMachine(bpod) sma.add_state( state_name='valve_open', state_timer=reward_valve_time, output_actions=[('Valve1', 255), ('BNC1', 255)], # To FPGA state_change_conditions={'Tup': 'exit'}, ) bpod.send_state_machine(sma) bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached return
def do_valve_click(bpod, reward_valve_time): sma = StateMachine(bpod) sma.add_state( state_name="valve_open", state_timer=reward_valve_time, output_actions=[("Valve1", 255), ("BNC1", 255)], # To FPGA state_change_conditions={"Tup": "exit"}, ) bpod.send_state_machine(sma) bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached return
def do_bpod_sound(bpod, sound_msg): if sound_msg == sc_play_tone: t = 0.102 elif sound_msg == sc_play_noise: t = 0.510 sma = StateMachine(bpod) sma.add_state( state_name="play_tone", state_timer=t, output_actions=[("Serial3", sound_msg)], state_change_conditions={"BNC2Low": "exit", "Tup": "exit"}, ) bpod.send_state_machine(sma) bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached return
# !/usr/bin/python3 # -*- coding: utf-8 -*- """ Example adapted from Josh Sanders' original version on Sanworks Bpod repository """ from pybpodapi.protocol import Bpod, StateMachine """ Run this protocol now """ my_bpod = Bpod() sma = StateMachine(my_bpod) sma.add_state(state_name='Port1LightOn', state_timer=1, state_change_conditions={Bpod.Events.BNC1High: 'Port3LightOn'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)]) sma.add_state(state_name='Port3LightOn', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[(Bpod.OutputChannels.PWM3, 255)]) my_bpod.send_state_machine(sma) my_bpod.run_state_machine(sma) print("Current trial info: {0}".format(my_bpod.session.current_trial)) my_bpod.close()
# ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) f, axes = op.make_fig(sph) plt.pause(1) for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f'Starting trial: {i + 1}') # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: # First trial exception start camera sma.add_state( state_name='trial_start', state_timer=0, # ~100µs hardware irreducible delay state_change_conditions={'Tup': 'reset_rotary_encoder'}, output_actions=[('Serial1', re_stop_stim), ('SoftCode', 3)]) # sart camera else: sma.add_state( state_name='trial_start', state_timer=0, # ~100µs hardware irreducible delay state_change_conditions={'Tup': 'reset_rotary_encoder'}, output_actions=[('Serial1', re_stop_stim), ('SoftCode', 0)]) # stop stim
tph = TrialParamHandler(sph) log.debug('TPH CREATED!') log.debug('make fig') f, axes = op.make_fig(sph) log.debug('pause') plt.pause(1) log.debug('start SM definition') for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f'Starting trial: {i + 1}') # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: log.info(f'Waiting for camera pulses...') sma.add_state( state_name='trial_start', state_timer=3600, # ~100µs hardware irreducible delay state_change_conditions={'Port1In': 'reset_rotary_encoder'}, output_actions=[('BNC1', 255)]) # To FPGA else: sma.add_state( state_name='trial_start', state_timer=0, # ~100µs hardware irreducible delay state_change_conditions={'Tup': 'reset_rotary_encoder'}, output_actions=[('BNC1', 255)]) # To FPGA sma.add_state(
# -*- coding: utf-8 -*- """ Light Chasing example Follow light on 2 pokes Connect noseports to ports 1-2. """ from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod() sma = StateMachine(my_bpod) sma.add_state( state_name='Port1Active1', # Add a state state_timer=0, state_change_conditions={Bpod.Events.Port1In: 'Port2Active1'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)]) sma.add_state( state_name='Port2Active1', state_timer=0, state_change_conditions={Bpod.Events.Port2In: 'Port1Active2'}, output_actions=[(Bpod.OutputChannels.PWM2, 255)]) sma.add_state( state_name='Port1Active2',
# !/usr/bin/python3 # -*- coding: utf-8 -*- """ Example adapted from Josh Sanders' original version on Sanworks Bpod repository """ from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod() sma = StateMachine(my_bpod) # Set global timer 1 for 3 seconds sma.set_global_timer_legacy(timer_id=1, timer_duration=3) sma.add_state( state_name='TimerTrig', # Trigger global timer state_timer=0, state_change_conditions={Bpod.Events.Tup: 'Port1Lit'}, output_actions=[(Bpod.OutputChannels.GlobalTimerTrig, 1)]) sma.add_state( state_name='Port1Lit', # Infinite loop (with next state). Only a global timer can save us. state_timer=.25, state_change_conditions={Bpod.Events.Tup: 'Port3Lit', Bpod.Events.GlobalTimer1_End: 'exit'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)]) sma.add_state( state_name='Port3Lit',
import time from pybpodapi.protocol import Bpod, StateMachine from concurrent.futures import ThreadPoolExecutor my_bpod = Bpod(emulator_mode=True) sma = StateMachine(my_bpod) sma.set_condition(condition_number=1, condition_channel='Port2', channel_value=1) sma.add_state(state_name='Port1Light', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'Port2Light'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)]) sma.add_state(state_name='Port2Light', state_timer=1, state_change_conditions={ Bpod.Events.Tup: 'Port1Light', Bpod.Events.Condition1: 'Port3Light' }, output_actions=[(Bpod.OutputChannels.PWM2, 255)]) sma.add_state(state_name='Port3Light', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[(Bpod.OutputChannels.PWM3, 255)]) my_bpod.send_state_machine(sma) def mouse(data): time.sleep(5) my_bpod.manual_override(Bpod.ChannelTypes.INPUT, 'Port', channel_number=2,
# !/usr/bin/python3 # -*- coding: utf-8 -*- """ Example adapted from Josh Sanders' original version on Sanworks Bpod repository """ from pybpodapi.protocol import Bpod, StateMachine """ Run this protocol now """ my_bpod = Bpod() sma = StateMachine(my_bpod) sma.add_state( state_name='WaitForChoice', state_timer=0, state_change_conditions={Bpod.Events.Port1In: 'FlashPort1', Bpod.Events.Port2In: 'FlashPort2'}, output_actions=[] ) sma.add_state( state_name='FlashPort1', state_timer=0.5, state_change_conditions={Bpod.Events.Tup: 'WaitForExit'}, output_actions=[(Bpod.OutputChannels.LED, 1)] )
from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod(emulator_mode=True) sma = StateMachine(my_bpod) n_pwm_outputs = len([attr for attr in dir( Bpod.OutputChannels) if attr.startswith('PWM')]) for i in range(10): sma.add_state( state_name='State{}'.format(i), state_timer=1, state_change_conditions={Bpod.Events.Tup: 'State{}'.format(i + 1)}, output_actions=[(getattr(Bpod.OutputChannels, f'PWM{(i % n_pwm_outputs) + 1}'), 255)]) sma.add_state( state_name='State10', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[]) my_bpod.send_state_machine(sma) my_bpod.run_state_machine(sma)
from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod(emulator_mode=True) sma = StateMachine(my_bpod) # Set global timer 1 for 5 seconds sma.set_global_timer(timer_id=1, timer_duration=5) sma.add_state(state_name='state1', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'state2'}, output_actions=[(Bpod.OutputChannels.GlobalTimerTrig, 1)]) for i in range(2, 10): sma.add_state(state_name=f'state{i}', state_timer=0.2, state_change_conditions={Bpod.Events.Tup: f'state{i+1}'}, output_actions=[]) # The next one shouldn't fire except after 15 seconds sma.add_state(state_name='state10', state_timer=0, state_change_conditions={Bpod.Events.GlobalTimer1_End: 'exit'}, output_actions=[]) my_bpod.send_state_machine(sma) my_bpod.run_state_machine(sma) my_bpod.close()
# For each trial, collect the necessary values from each list per trial trial_iti_time = iti_times[trial] trial_odor_delay = delay_times[trial] trial_odor_sample = odor_sample_times[trial] trial_reward_time = reward_delay_times[trial] #Will need to remake to allow for adjustments of reward based on calibration #and GUI interface, but for now just use standard values #Checking for odor updates since last trial (Need GUI again) #if odorFractions != odorFractionsPrev: #update the current trial, then update OdorFractionsPrev #Now begin state machine sma = StateMachine(my_bpod) #May also need to set a global timer (learn this later) #Now build parameters for each trialtype based on TrialType value for current trial thisTrialType = TrialTypes[trial] if thisTrialType == 0: #Null trial; both sides won't reward mouse leftPokeTimer = 10 leftPoke_SCCs = {'Port1Out': 'completed_state'} leftRewardTimer = 0 leftReward_SCCs = {} leftRewardOutput = {} rightPokeTimer = 10 rightPoke_SCCs = {'Port3Out': 'completed_state'} rightRewardTimer = 0
from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod(emulator_mode=True) sma = StateMachine(my_bpod) for i in range(10): sma.add_state( state_name='State{}'.format(i), state_timer=1, state_change_conditions={Bpod.Events.Tup: 'State{}'.format(i + 1)}, output_actions=[]) sma.add_state(state_name='State10', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[]) my_bpod.send_state_machine(sma) my_bpod.run_state_machine(sma)
from pybpodapi.protocol import Bpod, StateMachine import random my_bpod = Bpod(serial_port='COM14') nTrials = 5 trialTypes = [1, 2] for i in range(nTrials): print('Trial: ', i + 1) thisTrialType = random.choice(trialTypes) # Randomly choose trial type sma = StateMachine(my_bpod) sma.add_state(state_name='WaitForLick', state_timer=0, state_change_conditions={Bpod.Events.Port1In: 'Reward'}, output_actions=[]) sma.add_state(state_name='Reward', state_timer=0.1, state_change_conditions={Bpod.Events.Tup: 'Delay'}, output_actions=[(Bpod.OutputChannels.Valve, 1)]) sma.add_state(state_name='Delay', state_timer=2, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[]) my_bpod.send_state_machine(sma)
for i in range(nTrials): # Main loop print('Trial: ', i + 1) thisTrialType = random.choice(trialTypes) # Randomly choose trial type if thisTrialType == 1: stimulus = Bpod.OutputChannels.PWM1 # set stimulus channel for trial type 1 leftAction = 'Reward' rightAction = 'Punish' rewardValve = 1 elif thisTrialType == 2: stimulus = Bpod.OutputChannels.PWM3 # set stimulus channel for trial type 1 leftAction = 'Punish' rightAction = 'Reward' rewardValve = 3 sma = StateMachine(my_bpod) sma.add_state( state_name='WaitForPort2Poke', state_timer=1, state_change_conditions={Bpod.Events.Port2In: 'FlashStimulus'}, output_actions=[(Bpod.OutputChannels.PWM2, 255)]) sma.add_state( state_name='FlashStimulus', state_timer=0.1, state_change_conditions={Bpod.Events.Tup: 'WaitForResponse'}, output_actions=[(stimulus, 255)]) sma.add_state( state_name='WaitForResponse', state_timer=1, state_change_conditions={Bpod.Events.Port1In: leftAction, Bpod.Events.Port3In: rightAction},
# ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) f, axes = op.make_fig(sph) plt.pause(1) for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f"Starting trial: {i + 1}") # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: # First trial exception start camera log.info(f"Waiting for camera pulses...") sma.add_state( state_name="trial_start", state_timer=0, state_change_conditions={"Port1In": "reset_rotary_encoder"}, output_actions=[("SoftCode", 3)], ) # sart camera else: sma.add_state( state_name="trial_start", state_timer=0, # ~100µs hardware irreducible delay state_change_conditions={"Tup": "reset_rotary_encoder"}, output_actions=[tph.out_stop_sound],
bpod.load_serial_message(sound_card, sc_play_tone, [ord('P'), sph.GO_TONE_IDX]) # ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f'Starting trial: {i + 1}') # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: # First trial exception start camera log.info(f'Waiting for camera pulses...') sma.add_state(state_name='trial_start', state_timer=3600, state_change_conditions={'Port1In': 'stim_on'}, output_actions=[('Serial1', re_stop_stim), ('SoftCode', 3)]) # sart camera else: sma.add_state( state_name='trial_start', state_timer=1, # Stim off for 1 sec state_change_conditions={'Tup': 'stim_on'}, output_actions=[('Serial1', re_stop_stim)])
# ============================================================================= # CONNECT TO BPOD # ============================================================================= bpod = Bpod() # Soft code handler function can run arbitrary code from within state machine bpod.softcode_handler_function = softcode_handler NITER = 500 log.info(f"Starting {NITER} iterations of 1000 sync square pulses @60Hz") sys.stdout.flush() # ============================================================================= # Start state machine definition # ============================================================================= for i in range(NITER): log.info(f"Starting iteration {i+1} of {NITER}") sma = StateMachine(bpod) sma.add_state( state_name="start", state_timer=2, output_actions=[("SoftCode", 1)], state_change_conditions={"Tup": "listen"}, ) sma.add_state( state_name="listen", state_timer=25, output_actions=[], state_change_conditions={"Tup": "exit"}, ) # Send state machine description to Bpod device bpod.send_state_machine(sma) # Run state machine
from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod(emulator_mode=True) sma = StateMachine(my_bpod) # Set global timer 1 for 3 seconds, following a 1.5 second onset delay after # trigger. Link to LED of port 2. sma.set_global_timer(timer_id=1, timer_duration=3, on_set_delay=1.5, channel=Bpod.OutputChannels.PWM2, on_message=255) sma.add_state( state_name='TimerTrig', # Trigger global timer state_timer=0, state_change_conditions={Bpod.Events.Tup: 'Port1Lit_Pre'}, output_actions=[('GlobalTimerTrig', 1)]) sma.add_state( state_name='Port1Lit_Pre', state_timer=.25, state_change_conditions={Bpod.Events.Tup: 'Port3Lit_Pre', Bpod.Events.GlobalTimer1_Start: 'Port1Lit_Post'}, output_actions=[(Bpod.OutputChannels.PWM1, 16)]) sma.add_state( state_name='Port3Lit_Pre', state_timer=.25, state_change_conditions={Bpod.Events.Tup: 'Port1Lit_Pre', Bpod.Events.GlobalTimer1_Start: 'Port3Lit_Post'}, output_actions=[(Bpod.OutputChannels.PWM3, 16)]) sma.add_state( state_name='Port1Lit_Post', state_timer=.25, state_change_conditions={ Bpod.Events.Tup: 'Port3Lit_Post', Bpod.Events.GlobalTimer1_End: 'exit'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)])
log = logging.getLogger('iblrig') log.setLevel(logging.INFO) global sph sph = SessionParamHandler(task_settings, user_settings) # ============================================================================= # CONNECT TO BPOD # ============================================================================= bpod = Bpod() log.info('Listening...') # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) sma.add_state(state_name='trial_start', state_timer=0, state_change_conditions={'Tup': 'listen'}, output_actions=[]) sma.add_state(state_name='listen', state_change_conditions={}, output_actions=[]) # Send state machine description to Bpod device bpod.send_state_machine(sma) # Run state machine bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached
# ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) bad_stim_count = 0 bad_tone_count = 0 for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f'Starting trial: {i + 1}') # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: sma.add_state(state_name='stim_on', state_timer=10, state_change_conditions={ 'Tup': 'bad_stim', 'BNC1High': 'stim_off', 'BNC1Low': 'stim_off' }, output_actions=[('Serial1', re_show_stim)]) else: sma.add_state(state_name='stim_on', state_timer=1, state_change_conditions={ 'Tup': 'bad_stim',
bpod.load_serial_message(sound_card, sc_play_tone, [ord("P"), sph.GO_TONE_IDX]) # ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f"Starting trial: {i + 1}") # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: # First trial exception start camera log.info(f"Waiting for camera pulses...") sma.add_state( state_name="trial_start", state_timer=3600, state_change_conditions={"Port1In": "stim_on"}, output_actions=[("Serial1", re_stop_stim), ("SoftCode", 3)], ) # sart camera else: sma.add_state( state_name="trial_start", state_timer=1, # Stim off for 1 sec state_change_conditions={"Tup": "stim_on"}, output_actions=[("Serial1", re_stop_stim)],
# ============================================================================= # CONNECT TO BPOD # ============================================================================= bpod = Bpod() # Soft code handler function can run arbitrary code from within state machine bpod.softcode_handler_function = softcode_handler log.info(f'Starting 1000 iterations of 1000 sync square pulses @60Hz') sys.stdout.flush() NITER = 500 # ============================================================================= # Start state machine definition # ============================================================================= for i in range(NITER): log.info(f"Starting iteration {i+1} of {NITER}") sma = StateMachine(bpod) sma.add_state(state_name='start', state_timer=2, output_actions=[('SoftCode', 1)], state_change_conditions={'Tup': 'listen'}) sma.add_state(state_name='listen', state_timer=25, output_actions=[], state_change_conditions={'Tup': 'exit'}) # Send state machine description to Bpod device bpod.send_state_machine(sma) # Run state machine bpod.run_state_machine(sma) # Locks until state machine 'exit' is reached data = bpod.session.current_trial.export()
# ============================================================================= # TRIAL PARAMETERS AND STATE MACHINE # ============================================================================= global tph tph = TrialParamHandler(sph) bad_stim_count = 0 bad_tone_count = 0 for i in range(sph.NTRIALS): # Main loop tph.next_trial() log.info(f"Starting trial: {i + 1}") # ============================================================================= # Start state machine definition # ============================================================================= sma = StateMachine(bpod) if i == 0: sma.add_state( state_name="stim_on", state_timer=10, state_change_conditions={ "Tup": "bad_stim", "BNC1High": "stim_off", "BNC1Low": "stim_off", }, output_actions=[("Serial1", bonsai_show_stim)], ) else: sma.add_state( state_name="stim_on",
# !/usr/bin/python3 # -*- coding: utf-8 -*- """ Example adapted from Josh Sanders' original version on Sanworks Bpod repository """ from pybpodapi.protocol import Bpod, StateMachine """ Run this protocol now """ my_bpod = Bpod() sma = StateMachine(my_bpod) sma.add_state(state_name='myState', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'exit'}, output_actions=[]) my_bpod.send_state_machine(sma) my_bpod.run_state_machine(sma) print("Current trial info: {0}".format(my_bpod.session.current_trial)) my_bpod.close()
for i in range(nTrials): # Main loop print('Trial: ', i + 1) thisTrialType = random.choice(trialTypes) # Randomly choose trial type if thisTrialType == 1: stimulus = Bpod.OutputChannels.PWM1 # set stimulus channel for trial type 1 leftAction = 'Reward' rightAction = 'Punish' rewardValve = 1 elif thisTrialType == 2: stimulus = Bpod.OutputChannels.PWM3 # set stimulus channel for trial type 1 leftAction = 'Punish' rightAction = 'Reward' rewardValve = 3 sma = StateMachine(my_bpod) sma.set_global_timer_legacy(timer_id=1, timer_duration=graceTime) # Set timeout sma.add_state( state_name='WaitForPort2Poke', state_timer=1, state_change_conditions={Bpod.Events.Port2In: 'FlashStimulus'}, output_actions=[('PWM2', 255)]) sma.add_state(state_name='FlashStimulus', state_timer=0.1, state_change_conditions={Bpod.Events.Tup: 'WaitForResponse'}, output_actions=[(stimulus, 255, Bpod.OutputChannels.GlobalTimerTrig, 1)])
# !/usr/bin/python3 # -*- coding: utf-8 -*- """ Light Chasing Loop example Follow light on 3 pokes and repeat states until a timeout occurs. Connect noseports to ports 1-3. """ from pybpodapi.protocol import Bpod, StateMachine my_bpod = Bpod() sma = StateMachine(my_bpod) # Set global timer 1 for 3 seconds sma.set_global_timer_legacy(timer_id=1, timer_duration=10) sma.add_state( state_name='TimerTrig', # Trigger global timer state_timer=0, state_change_conditions={Bpod.Events.Tup: 'Port1Active1'}, output_actions=[(Bpod.OutputChannels.GlobalTimerTrig, 1)]) # Infinite loop (with next state). Only a global timer can save us. sma.add_state( state_name='Port1Active1', state_timer=0, state_change_conditions={Bpod.Events.Port1In: 'Port2Active1', Bpod.Events.GlobalTimer1_End: 'exit'},
# ------------------------------------------------------------------ START_APP = timeit.default_timer() my_bpod = Bpod() my_bpod.softcode_handler_function = my_softcode_handler nTrials = 2 for i in range(nTrials): # Main loop print('Trial: ', i + 1) sma = StateMachine(my_bpod) soundStream.load(s1) # load the sound <----------------- sma.add_state(state_name='WaitForPort2Poke', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'PlaySound'}, output_actions=[(Bpod.OutputChannels.PWM1, 255)]) sma.add_state(state_name='PlaySound', state_timer=0.5, state_change_conditions={Bpod.Events.Tup: 'StopSound'}, output_actions=[(Bpod.OutputChannels.SoftCode, 55), (Bpod.OutputChannels.Wire1, 5)]) sma.add_state(state_name='StopSound', state_timer=1, state_change_conditions={Bpod.Events.Tup: 'Exit'}, output_actions=[(Bpod.OutputChannels.SoftCode, 66)])