Esempio n. 1
0
def bpod_modules_ok() -> bool:
    # List bpod modules
    # figure out if RE is in Module 1, Ambient sensore in port 2 and
    # if ephys in board name if SoundCard in port 3
    ephys_rig = "ephys" in _grep_param_dict("NAME")
    if ephys_rig:
        expected_modules = [
            "RotaryEncoder1",
            "AmbientModule1",
            "SoundCard1",
        ]
    else:
        expected_modules = [
            "RotaryEncoder1",
            "AmbientModule1",
        ]
    out = False
    try:
        comport = _grep_param_dict("COM_BPOD")
        bpod = Bpod(serial_port=comport)
        mods = [x.name for x in bpod.modules]
        bpod.close()
        oks = [x in mods for x in expected_modules]
        if all(oks):
            out = True
        else:
            missing = set(expected_modules) - set(mods)
            log.warning(f"Missing modules: {missing}")
    except BaseException as e:
        log.warning(f"{e} \nCan't check modules from Bpod.")

    return out
Esempio n. 2
0
# !/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'},
    global sph
    if data == 0:
        sph.stop_sound()
    elif data == 1:
        sph.play_tone()
    elif data == 2:
        sph.play_noise()
    elif data == 3:
        sph.start_camera_recording()
    # sph.OSC_CLIENT.send_message("/e", data)


# =============================================================================
# CONNECT TO BPOD
# =============================================================================
bpod = Bpod()

# Loop handler function is used to flush events for the online plotting
bpod.loop_handler = bpod_loop_handler
# Soft code handler function can run arbitrary code from within state machine
bpod.softcode_handler_function = softcode_handler
# Rotary Encoder State Machine handler
rotary_encoder = [x for x in bpod.modules if x.name == 'RotaryEncoder1'][0]
# ROTARY ENCODER SEVENTS
# Set RE position to zero 'Z' + eneable all RE thresholds 'E'
# re_reset = rotary_encoder.create_resetpositions_trigger()
re_reset = 1
bpod.load_serial_message(
    rotary_encoder,
    re_reset,
    [
Esempio n. 4
0
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)])
Esempio n. 5
0
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)
Esempio n. 6
0
    }

    if save_to is not None:
        data = {k: v.tolist() for k, v in Measures.items()}
        with open(
                os.path.join(save_to,
                             "_iblrig_ambientSensorData.raw.jsonable"),
                "a") as f:
            f.write(json.dumps(data))
            f.write("\n")
            f.flush()

    return {k: v.tolist()[0] for k, v in Measures.items()}


if __name__ == "__main__":
    from pybpodgui_api.models import project

    root = "/home/nico/Projects/IBL/github/iblrig/scratch"
    path = root + "/test_iblrig_params/IBL"
    p = project.Project()
    try:
        p.load(path)
    except TypeError as blabla:
        print("PyBpod says:", blabla)
        pass
    my_bpod = Bpod(serial_port=p.boards[0].serial_port)
    data = get_reading(my_bpod)
    print(data)
    my_bpod.close()
Esempio n. 7
0
 def _create_bpod(self):
     self.__bpod = Bpod(emulator_mode=True)
Esempio n. 8
0
# First draft outline of Python BPOD-mix2afc module

#Call Bpod and StateMachine from pybpodapi.protocol
from pybpodapi.protocol import Bpod, StateMachine
from random import choices, randint, triangular, gauss

# set the serial port from which the Bpod runs,
# then set Bpod to variable my_bpod
serial_port = ''  #add serial port location when ready
my_bpod = Bpod(serial_port)

###########################################################################
# Set Default Parameters for use in states and main function #
###########################################################################

# Trial Parameters #
itiMean = 1
itiStdDev = 0.1
errorPenalty = 2
inEarlyPenalty = 0.5
outEarlyPenalty = 2
#trialLength = 45    #Not currently in use
totalNumtrials = 1000

# Odor Parameters #
minOdorDelay = 0.2
maxOdorDelay = 0.4
odor_delay = 1
odor_sample_time = 0.55
odor_sample_time_range = 0.055
Esempio n. 9
0
PARAMS = params.load_params_file()
# start sph
sph = SessionParamHandler(task_settings, user_settings)
# frame2TTL seg thresholds
if frame2TTL.get_and_set_thresholds() == 0:
    sph.F2TTL_GET_AND_SET_THRESHOLDS = True
# Rotary encoder
re = MyRotaryEncoder(
    sph.ALL_THRESHOLDS, sph.STIM_GAIN, sph.PARAMS["COM_ROTARY_ENCODER"]
)
sph.ROTARY_ENCODER = re

# Turn bpod lights off
bpod_lights(PARAMS["COM_BPOD"], 0)
# get bpod
bpod = Bpod(serial_port=PARAMS["COM_BPOD"])
# Build messages
msg = BpodMessageCreator(bpod)
sc_play_tone = msg.sound_card_play_idx(sph.GO_TONE_IDX)
sc_play_noise = msg.sound_card_play_idx(sph.WHITE_NOISE_IDX)
bpod = msg.return_bpod()

# get soundcard
card = usb.core.find(idVendor=0x04D8, idProduct=0xEE6A)
card_play_tone = bytes(np.array([2, 6, 32, 255, 2, 2, 0, 43], dtype=np.int8))
card_play_noise = bytes(np.array([2, 6, 32, 255, 2, 3, 0, 44], dtype=np.int8))


def do_gabor(osc_client, pcs_idx, pos, cont, phase):
    # send pcs to Bonsai
    bonsai.send_stim_info(
log = logging.getLogger('iblrig')
log.setLevel(logging.INFO)

PARAMS = params.load_params_file()
# start sph
sph = SessionParamHandler(task_settings, user_settings)
# frame2TTL seg thresholds
if frame2TTL.get_and_set_thresholds() == 0:
    sph.F2TTL_GET_AND_SET_THRESHOLDS = True
# Rotary encoder
re = MyRotaryEncoder(sph.ALL_THRESHOLDS, sph.STIM_GAIN,
                     sph.PARAMS['COM_ROTARY_ENCODER'])
sph.ROTARY_ENCODER = re

# get bpod
bpod = Bpod(serial_port=PARAMS['COM_BPOD'])
# Build messages
msg = BpodMessageCreator(bpod)
sc_play_tone = msg.sound_card_play_idx(sph.GO_TONE_IDX)
sc_play_noise = msg.sound_card_play_idx(sph.WHITE_NOISE_IDX)
bpod = msg.return_bpod()

# get soundcard
card = usb.core.find(idVendor=0x04d8, idProduct=0xee6a)
card_play_tone = bytes(np.array([2, 6, 32, 255, 2, 2, 0, 43], dtype=np.int8))
card_play_noise = bytes(np.array([2, 6, 32, 255, 2, 3, 0, 44], dtype=np.int8))


def do_gabor(osc_client, pcs_idx, pos, cont, phase):
    # send pcs to Bonsai
    bonsai.send_stim_info(sph.OSC_CLIENT,