|
| 1 | +import secrets |
| 2 | +from pybpodapi.protocol import Bpod, StateMachine |
| 3 | +my_bpod = Bpod(emulator_mode=True) |
| 4 | +nTrials = 5 |
| 5 | +graceTime = 5 |
| 6 | +trialTypes = [1, 2] # 1 (rewarded left) or 2 (rewarded right) |
| 7 | +for i in range(nTrials): # Main loop |
| 8 | + print('Trial: ', i + 1) |
| 9 | + thisTrialType = secrets.choice(trialTypes) # Randomly choose trial type |
| 10 | + if thisTrialType == 1: |
| 11 | + # set stimulus channel for trial type 1 |
| 12 | + stimulus = Bpod.OutputChannels.PWM1 |
| 13 | + leftAction = 'Reward' |
| 14 | + rightAction = 'Punish' |
| 15 | + rewardValve = 1 |
| 16 | + elif thisTrialType == 2: |
| 17 | + # set stimulus channel for trial type 1 |
| 18 | + stimulus = Bpod.OutputChannels.PWM3 |
| 19 | + leftAction = 'Punish' |
| 20 | + rightAction = 'Reward' |
| 21 | + rewardValve = 3 |
| 22 | + sma = StateMachine(my_bpod) |
| 23 | + sma.set_global_timer_legacy( |
| 24 | + timer_id=1, timer_duration=graceTime) # Set timeout |
| 25 | + sma.add_state( |
| 26 | + state_name='WaitForPort2Poke', |
| 27 | + state_timer=1, |
| 28 | + state_change_conditions={Bpod.Events.Port2In: 'FlashStimulus'}, |
| 29 | + output_actions=[('PWM2', 255)]) |
| 30 | + sma.add_state( |
| 31 | + state_name='FlashStimulus', |
| 32 | + state_timer=0.1, |
| 33 | + state_change_conditions={Bpod.Events.Tup: 'WaitForResponse'}, |
| 34 | + output_actions=[(stimulus, 255), |
| 35 | + (Bpod.OutputChannels.GlobalTimerTrig, 1)]) |
| 36 | + sma.add_state( |
| 37 | + state_name='WaitForResponse', |
| 38 | + state_timer=1, |
| 39 | + state_change_conditions={Bpod.Events.Port1In: leftAction, |
| 40 | + Bpod.Events.Port3In: rightAction, |
| 41 | + Bpod.Events.Port2In: 'Warning', |
| 42 | + Bpod.Events.GlobalTimer1_End: 'MiniPunish'}, |
| 43 | + output_actions=[]) |
| 44 | + sma.add_state( |
| 45 | + state_name='Warning', |
| 46 | + state_timer=0.1, |
| 47 | + state_change_conditions={Bpod.Events.Tup: 'WaitForResponse', |
| 48 | + Bpod.Events.GlobalTimer1_End: 'MiniPunish'}, |
| 49 | + output_actions=[(Bpod.OutputChannels.LED, 1), |
| 50 | + (Bpod.OutputChannels.LED, 2), |
| 51 | + (Bpod.OutputChannels.LED, 3)]) # Reward correct choice |
| 52 | + sma.add_state( |
| 53 | + state_name='Reward', |
| 54 | + state_timer=0.1, |
| 55 | + state_change_conditions={Bpod.Events.Tup: 'exit'}, |
| 56 | + # Reward correct choice |
| 57 | + output_actions=[(Bpod.OutputChannels.Valve, rewardValve)]) |
| 58 | + sma.add_state( |
| 59 | + state_name='Punish', |
| 60 | + state_timer=3, |
| 61 | + state_change_conditions={Bpod.Events.Tup: 'exit'}, |
| 62 | + # Signal incorrect choice |
| 63 | + output_actions=[(Bpod.OutputChannels.LED, 1), |
| 64 | + (Bpod.OutputChannels.LED, 2), |
| 65 | + (Bpod.OutputChannels.LED, 3)]) |
| 66 | + sma.add_state( |
| 67 | + state_name='MiniPunish', |
| 68 | + state_timer=1, |
| 69 | + state_change_conditions={Bpod.Events.Tup: 'exit'}, |
| 70 | + # Signal incorrect choice |
| 71 | + output_actions=[(Bpod.OutputChannels.LED, 1), |
| 72 | + (Bpod.OutputChannels.LED, 2), |
| 73 | + (Bpod.OutputChannels.LED, 3)]) |
| 74 | + # Send state machine description to Bpod device |
| 75 | + my_bpod.send_state_machine(sma) |
| 76 | + print("Waiting for poke. Reward: ", |
| 77 | + 'left' if thisTrialType == 1 else 'right') |
| 78 | + # Run state machine |
| 79 | + my_bpod.run_state_machine(sma) |
| 80 | + print("Current trial info: {0}".format(my_bpod.session.current_trial)) |
| 81 | +my_bpod.close() # Disconnect Bpod |
0 commit comments