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