import logging
import matplotlib.pyplot as plt
from pybpodapi.protocol import Bpod, StateMachine
from iblrig.bpod_helper import BpodMessageCreator
from iblrig.rotary_encoder import MyRotaryEncoder
log = logging.getLogger("iblrig")
# =============================================================================
# CONNECT TO BPOD
# =============================================================================
bpod = Bpod()
# Bpod message creator
msg = BpodMessageCreator(bpod)
re_reset = msg.rotary_encoder_reset()
th_reset = msg.reset_thresholds()
bpod = msg.return_bpod()
WHEEL_THRESHOLD = [-30, 30]
STIM_GAIN = 1
COM_ROTARY_ENCODER = "COM6"
ROTARY_ENCODER = MyRotaryEncoder(WHEEL_THRESHOLD, STIM_GAIN, COM_ROTARY_ENCODER)
threshold_events_dict = ROTARY_ENCODER.THRESHOLD_EVENTS # dictionary with key being angle and value being RE event name
move_left = threshold_events_dict[WHEEL_THRESHOLD[0]]
move_right = threshold_events_dict[WHEEL_THRESHOLD[1]]
RESPONSE_PERIOD = 5
ITI = 0
REWARD_VALVE_TIME = 0.1
for i in range(99999): # Main loop
log.info(f"Starting trial: {i + 1}")
sma = StateMachine(bpod)
sma.set_global_timer_legacy(timer_id=1, timer_duration=RESPONSE_PERIOD)
sma.set_global_timer_legacy(timer_id=2, timer_duration=ITI)
sma.add_state(
state_name="trial_start",
state_timer=0,
state_change_conditions={"Tup": "reset_rotary_encoder"},
output_actions=[(Bpod.OutputChannels.GlobalTimerTrig,1)],
)
sma.add_state(
state_name="reset_rotary_encoder",
state_timer=0,
state_change_conditions={"Tup": "response_period",
"GlobalTimer1_End": "ITI"
},
output_actions=[("Serial1", re_reset)],
)
sma.add_state(
state_name="response_period",
state_timer=0,
state_change_conditions={
move_left: "reward",
move_right: "reward",
"GlobalTimer1_End": "ITI"
},
output_actions=[],
)
sma.add_state(
state_name="reward",
state_timer = REWARD_VALVE_TIME,
state_change_conditions={
"Tup": "ITI",
"GlobalTimer1_End": "ITI"
},
output_actions=[("Valve1", 255),("Serial1", re_reset)],
)
sma.add_state(
state_name="ITI",
state_timer = ITI,
state_change_conditions={
"Tup": "exit_state",
},
output_actions=[("Serial1", re_reset)],
)
sma.add_state(
state_name="exit_state",
state_timer=0,
state_change_conditions={"Tup": "exit"},
output_actions=[("Serial1", re_reset)],
)
# Send state machine description to Bpod device
bpod.send_state_machine(sma)
# Run state machine
if not bpod.run_state_machine(sma): # Locks until state machine 'exit' is reached
break
Josh wrote[quote='Josh' pid='4583' dateline='1677039496']
Hello RK,
The analog output channel reports the wheel position.
If your state machine code resets the position (command 'Z' in a state's output actions), the analog signal will also reset to reflect the new position.
Is this what's happening? If not, please share the section of your code with addState()
The second analog channel currently toggles its logic each time the position is checked (at 10kHz). I'm open to suggestions regarding additional sync schemes to add. The line could be toggled only if the position changes, every 100ms, etc.
Thanks,
Josh
Josh wrote[quote='Josh' pid='4583' dateline='1677039496']
Hello RK,
The analog output channel reports the wheel position.
If your state machine code resets the position (command 'Z' in a state's output actions), the analog signal will also reset to reflect the new position.
Is this what's happening? If not, please share the section of your code with addState()
The second analog channel currently toggles its logic each time the position is checked (at 10kHz). I'm open to suggestions regarding additional sync schemes to add. The line could be toggled only if the position changes, every 100ms, etc.
Thanks,
Josh
I have videos showing what I am seeing. However, I can't figure out how to post them on the forum.