Beispiel #1
0
def detect_scene_change():
    # scene change requested; start counting
    # Opening and closing valve could depend on scene name and could use its own counter
    # Note that scene name changes between opening and closing the valve
    if GameLogic.Object['scene_changed']>0:
        sys.stdout.write("BLENDER: Scene change requested\n")
        GameLogic.Object['scene_changed'] += 1
        # open valve

    # reset the animal and the rewards 10 frames later
    # to make sure the scene has really changed
    # Better alternative: check that scene name has changed
    # over the previous state
    scene = GameLogic.getCurrentScene()
    if scene.name != GameLogic.Object['scene_name']:
        # scene has changed; do all the scene change stuff here
        sys.stdout.write("BLENDER: Took %d frames to actually change the scene\n" % GameLogic.Object['scene_changed'])
        zeroPos()
        gc.zeroPump()
        GameLogic.Object['scene_changed'] = 0
    
    GameLogic.Object['scene_name'] = scene.name
Beispiel #2
0
def init():
    if settings.looming:
        scene = GameLogic.getCurrentScene()
        scene.replace("Looming")
    
    GameLogic.Object = {}
    print("BLENDER: GameLogic object created")
    GameLogic.Object['closed'] = False
    GameLogic.Object['lapCounter'] = 0
    GameLogic.Object['frameCounter'] = 0
    GameLogic.setLogicTicRate(100)
    GameLogic.setMaxLogicFrame(100)
    GameLogic.setMaxPhysicsFrame(100)

    sys.stdout.write("BLENDER: Maximum number of logic frames per render frame: %d\n" % GameLogic.getMaxLogicFrame() )
    sys.stdout.write("BLENDER: Logic tic rate: %d\n" % GameLogic.getLogicTicRate() )
    sys.stdout.write("BLENDER: Maximum number of physics frames per render frame: %d\n" % GameLogic.getMaxPhysicsFrame() )
    sys.stdout.write("BLENDER: Physics update frequency: %d Hz\n" % GameLogic.getPhysicsTicRate() )

    # open arduino and pump

    try:
        arduino = arduino_serial.SerialPort(settings.arduino_port, 19200)
        sys.stdout.write("BLENDER: Successfully opened arduino on {0}\n".format(settings.arduino_port))
    except OSError:
        sys.stdout.write("BLENDER: Failed to open arduino\n")
        arduino = None
    GameLogic.Object['arduino'] = arduino
    if arduino is not None:
        arduino.write(b'd')
        # Set all valves to low
        arduino.write(b'6')
        arduino.write(b'8')
        arduino.write(b'0')
        arduino.write(b'f')

    try:
        pumppy = serial.Serial(settings.pump_port, 19200, timeout=1)
        sys.stdout.write("BLENDER: Successfully opened pump\n")
        gc.setPumpVolume(pumppy, settings.reward_volume)
    except:
        sys.stdout.write("BLENDER: Failed to open pump\n")
        pumppy = None

    if settings.replay_track is not None:
        fn_pos = settings.replay_track + '.position'
        if not os.path.exists(fn_pos):
            print("BLENDER: Could not find " + fn_pos)
            settings.replay_track = None
        else:
            sys.path.append(os.path.expanduser("~/../cs/py2p/tools"))
            import training
            print("BLENDER: Reading replay track from " + fn_pos)
            GameLogic.Object['replay_pos'] = training.read_pos(
                fn_pos, 1e9, False, meters=False)
            posy = GameLogic.Object['replay_pos'][1]
            evlist, timeev = training.read_events(
                settings.replay_track + ".events", teleport_times=None)
            GameLogic.Object['replay_rewards'] = np.array([
                ev.time for ev in evlist if ev.evcode == b'RE'])
            GameLogic.Object['replay_rewards'] = np.sort(GameLogic.Object['replay_rewards'])
            if settings.replay_rewards_shuffle:
                intervals = np.diff([0] + GameLogic.Object['replay_rewards'].tolist())
                intervals = np.random.permutation(intervals)
                print(intervals)
                GameLogic.Object['replay_rewards'] = np.cumsum(intervals)
            print("Replay reward times: ", GameLogic.Object['replay_rewards'])
            GameLogic.Object['nreplay'] = 0

    if settings.gratings:
        if not "grating1" in GameLogic.Object.keys():
            GameLogic.Object["grating1"] = chooseWalls.grating(
                ori=settings.verticalGratingAngle, sf=settings.verticalGratingScale)
            GameLogic.Object["grating2"] = chooseWalls.grating(
                ori=settings.obliqueGratingAngle, sf=settings.obliqueGratingScale)
            chooseWalls.initWalls()
#            for wallkey in ["LW1", "RW1"]:
#                chooseWalls.set_texture_buffer(
#                    GameLogic.Object["grating1"], wallkey)
#            for wallkey in ["LW2", "RW2"]:
#                chooseWalls.set_texture_buffer(
#                    GameLogic.Object["grating2"], wallkey)
#                

    if ncl.has_comedi:
        print("BLENDER: Found comedi library")
        gOuttrigsubdev = 2
        gOuttrigchan = 0
        gOutfrchan = 1
        gOutleftchan = 2
        gOutrightchan = 3
        gOutexpchan = 4
    
        gIntrigsubdev = 7
        gIntrigchan = 0
    
        gDevrange = 0
    
        # open ni trigger channels
        devintrigch, fdintrigch, nameintrigch = ncl.open_dev("/dev/comedi0_subd2")
        devouttrigch, fdouttrigch, nameouttrigch = ncl.open_dev("/dev/comedi0_subd11")
    
        intrigch = ncl.nichannel(devintrigch, gIntrigchan, gIntrigsubdev, fdintrigch, gDevrange) 
        outtrigch = ncl.nichannel(devouttrigch, gOuttrigchan, gOuttrigsubdev, fdouttrigch, gDevrange) 
        outfrch = ncl.nichannel(devouttrigch, gOutfrchan, gOuttrigsubdev, fdouttrigch, gDevrange) 
        outleftch = ncl.nichannel(devouttrigch, gOutleftchan, gOuttrigsubdev, fdouttrigch, gDevrange) 
        outrightch = ncl.nichannel(devouttrigch, gOutrightchan, gOuttrigsubdev, fdouttrigch, gDevrange) 
        outexpch = ncl.nichannel(devouttrigch, gOutexpchan, gOuttrigsubdev, fdouttrigch, gDevrange) #MC2015
    
        ncl.init_comedi_dig(intrigch, outtrigch, outfrch, outleftch, outrightch, 
            outexpch #MC2015
            )
    else:
        intrigch = None
        outtrigch = None
        outfrch = None
        outleftch = None
        outrightch = None
        outexpch = None #MC2015
    
    GameLogic.Object['intrigch'] = intrigch
    GameLogic.Object['outtrigch'] = outtrigch
    GameLogic.Object['outfrch'] = outfrch
    GameLogic.Object['outleftch'] = outleftch
    GameLogic.Object['outrightch'] = outrightch
    GameLogic.Object['outexpch'] = outexpch #MC2015

    GameLogic.Object['pumppy'] = pumppy
    GameLogic.Object['left_on'] = False
    GameLogic.Object['right_on'] = False

    GameLogic.Object['file_open'] = False
    GameLogic.Object['train_open'] = False
    GameLogic.Object['bcstatus'] = False

    gio.create_data_dir()
    GameLogic.Object['time0'] = time.time()
    GameLogic.Object['prevtime'] = time.time()
    GameLogic.Object['nframes'] = 0

    GameLogic.Object['rewcount'] = 0
    GameLogic.Object['rewfail'] = 0
    GameLogic.Object['puffcount'] = 0
    GameLogic.Object['puffthistrial'] = 0
    GameLogic.Object['isloom'] = 0
    GameLogic.Object['loomcounter']=0
    GameLogic.Object['totalLooms'] = 0
    GameLogic.Object['loom_first_trial']=0
    GameLogic.Object['rewpos'] = [0.98] # 1.0 # np.zeros((16))
    GameLogic.Object['boundx'] =   8.0
    GameLogic.Object['boundy'] = 158.0
    GameLogic.Object['hysteresis'] = 0.5
    GameLogic.Object['speed_tracker'] = np.zeros((100))
    GameLogic.Object['lapCounter'] = 0

    blenderpath = GameLogic.expandPath('//')
    
    if not settings.cpp:
        s1, conn1, addr1, p1 = gc.spawn_process("\0mouse0socket", ['python3', '%s/py/usbclient.py' % blenderpath, '0'])
        s2, conn2, addr2, p2 = gc.spawn_process("\0mouse1socket", ['python3', '%s/py/usbclient.py' % blenderpath, '1'])
    else:
        if settings.readlib=="xinput":
            mice = xinput.find_mice(model=settings.mouse)
            for mouse in mice:
                # xinput.set_owner(mouse) # Don't need this if using correct udev rule
                xinput.switch_mode(mouse)
            if settings.usezmq:
                procname = 'readout_zmq'
            else:
                procname = 'readout'
            if len(mice)>=1:
                s1, conn1, addr1, p1 = \
                    gc.spawn_process(
                        "\0mouse0socket", 
                        [('%s/cpp/generic-ev/' % blenderpath) + procname, '%d' % mice[0].evno, '0'],
                        usezmq=settings.usezmq)
            else:
                s1, conn1, addr1, p1 = None, None, None, None
            if len(mice)>=3:
                s2, conn2, addr2, p2 = \
                    gc.spawn_process(
                        "\0mouse1socket", 
                        [('%s/cpp/generic-ev/readout' % blenderpath) + procname, '%d' % mice[2].evno, '1'],
                        usezmq=settings.usezmq)
            else:
                s2, conn2, addr2, p2 = None, None, None, None
        elif settings.readlib=="libusb":
            s1, conn1, addr1, p1 = \
                gc.spawn_process("\0mouse1socket", 
                              ['%s/cpp/g500-usb/readout' % blenderpath, '1'])
            s2, conn2, addr2, p2 = \
                gc.spawn_process("\0mouse0socket", 
                              ['%s/cpp/g500-usb/readout' % blenderpath, '0'])
        else:    
            s1, conn1, addr1, p1, s2, conn2, add2, p2 = \
                None, None, None, None, None, None, None, None

    if settings.has_fw:
        if not settings.fw_local:
            GameLogic.Object['fwip'] = '' #"128.40.202.203"
            sfw, connfw, addrfw = gc.spawn_process_net(GameLogic.Object['fwip'])
            if connfw is None:
                settings.has_fw = False
        else:
            sys.stdout.write("BLENDER: Starting fw... ")
            sys.stdout.flush()
            sfw, connfw, addrfw, pfw = \
                gc.spawn_process("\0fwsocket", ['%s/cpp/dc1394/dc1394' % blenderpath,], #MC2015
                              system=False, addenv={"SDL_VIDEO_WINDOW_POS":"\"1280,480\""})
            print("done")

        connfw.send(GameLogic.Object['fw_trunk'].encode('latin-1'))
        gc.recv_ready(connfw)
        connfw.setblocking(0)
        GameLogic.Object['fwconn'] = connfw

    GameLogic.Object['has_fw'] = settings.has_fw

    if settings.has_usb3:
        sys.stdout.write("BLENDER: Starting usb3... ")
        sys.stdout.flush()
        susb3, connusb3, addrusb3, pusb3 = \
            gc.spawn_process(
                "\0" + settings.usb3_pupil,
                ['{0}/cpp/usb3/arv-camera-test'.format(blenderpath),
                 '-n', settings.usb3_pupil], #MC2015
                system=False, addenv={"SDL_VIDEO_WINDOW_POS":"\"1280,480\""})
        print("done")
        print("Sending usb3 file name " + GameLogic.Object['fw_trunk'])
        connusb3.send(GameLogic.Object['fw_trunk'].encode('latin-1'))
        gc.recv_ready(connusb3)
        connusb3.setblocking(0)
        GameLogic.Object['usb3conn'] = connusb3
        if settings.usb3_body is not None:
            s2usb3, conn2usb3, addr2usb3, p2usb3 = \
                gc.spawn_process(
                    "\0" + settings.usb3_body,
                    ['{0}/cpp/usb3/arv-camera-test'.format(blenderpath),
                     '-n', settings.usb3_body], #MC2015
                    system=False, addenv={"SDL_VIDEO_WINDOW_POS":"\"1280,480\""})
            print("done")
            print("Sending usb3 file name " + GameLogic.Object['fw_trunk'] + 'body')
            conn2usb3.send((GameLogic.Object['fw_trunk'] + 'body').encode('latin-1'))
            gc.recv_ready(conn2usb3)
            conn2usb3.setblocking(0)
            GameLogic.Object['usb3conn2'] = conn2usb3

    GameLogic.Object['has_usb3'] = settings.has_usb3
    
    if settings.has_comedi and ncl.has_comedi:
        scomedi, conncomedi, addrcomedi, pcomedi = \
            gc.spawn_process("\0comedisocket", ['python3', '%s/py/nicomedi.py' % blenderpath,])

        conncomedi.send(blenderpath.encode('latin-1'))
        gc.recv_ready(conncomedi)
        conncomedi.setblocking(0)
        GameLogic.Object['comediconn'] = conncomedi

    if settings.has_licksensor:
        slick, connlick, addrlick, plick = \
            gc.spawn_process("\0licksocket", ['python3', '%s/py/licksensor.py' % blenderpath,])

        connlick.send(blenderpath.encode('latin-1'))
        gc.recv_ready(connlick)
        connlick.setblocking(0)
        GameLogic.Object['lickconn'] = connlick

    if settings.has_licksensor_piezo:
        slickpiezo, connlickpiezo, addrlickpiezo, plickpiezo = \
            gc.spawn_process("\0lickpiezosocket", ['python3', '%s/py/licksensorpiezo.py' % blenderpath,])

        connlickpiezo.send(blenderpath.encode('latin-1'))
        gc.recv_ready(connlickpiezo)
        connlickpiezo.setblocking(0)
        GameLogic.Object['lickpiezoconn'] = connlickpiezo

    if settings.cpp:
        for mconn in [conn1, conn2]:
            if mconn is not None:
                mconn.send(b'start')
                gc.recv_ready(mconn, usezmq=settings.usezmq)
                if not settings.usezmq:
                    mconn.setblocking(0)
 
    if len(mice):
        GameLogic.Object['m1conn'] = conn1
        GameLogic.Object['m2conn'] = conn2
    else:
        GameLogic.Object['m1conn'] = None
        GameLogic.Object['m2conn'] = None

    GameLogic.Object['tmprec'] = False
    GameLogic.Object['trainrec'] = False
    GameLogic.Object['RewardTicksCounter'] = None
    GameLogic.Object['RewardChange'] = False
    GameLogic.Object['WallTouchTicksCounter'] = None
    GameLogic.Object['OdorTicksCounter'] = None  
    
    GameLogic.Object['piezolicks'] = 0
    GameLogic.Object['piezoframes'] = 0
    GameLogic.Object['piezoframepause'] = 0
 
    GameLogic.Object['lapCounter'] = 0
    
    scene = GameLogic.getCurrentScene()
    if scene.name == "Scene":
        playerName = 'MovingCube'
        legName = 'LeftLeg'
    elif scene.name == "Looming":
        playerName = 'MovingCube.002'
        legName = 'LeftLeg.002'
    else:
        playerName = 'MovingCube.001'
        legName = 'LeftLeg.001'
        
    rew_sensor = scene.objects[playerName]
    touch_sensor = scene.objects[legName]
    
    rew_sensor.sensors['SReward'].usePosPulseMode = True
    touch_sensor.sensors['SLeftTouch'].usePosPulseMode = True

    GameLogic.Object['scene_changed'] = 0
    GameLogic.Object['scene_name'] = scene.name

    GameLogic.Object['reset_pulse'] = False
    
    #current injection variables
    #variables for current injection - MC2015
    GameLogic.Object['start_pulse_y'] = 50
    GameLogic.Object['inj_amp'] = 50
    GameLogic.Object['do_tbs1'] = False
    GameLogic.Object['do_tbs2'] = False
    
    gu.zeroPos()
    gc.zeroPump()
Beispiel #3
0
def reward_linear(pumppy, controller):
    
    if not 'SReward' in controller.sensors:
        return
    radar = controller.sensors['SReward']
    reward = radar.hitObject
    if GameLogic.Object['RewardTicksCounter'] is not None:
        if GameLogic.Object['RewardTicksCounter'] == settings.reward_delay_ticks_pre and not GameLogic.Object['RewardChange']:
            # Give reward
            if reward is not None:
                nrew = len(GameLogic.Object['rewpos'])
                newy = GameLogic.Object['rewpos'][np.mod(GameLogic.Object['rewcount'],nrew)] * settings.reward_pos_linear # newCoords()
                if GameLogic.Object['WallTouchTicksCounter'] is None:
                    rand = np.random.uniform(0,1)
                    if rand <= settings.reward_probability:
                        if settings.gratings:
                            give_reward = GameLogic.Object["current_walls"] == settings.rewarded_env
                        else:
                            give_reward = True
                        if settings.replay_track is None:
                            gc.runPump(pumppy, reward=give_reward, buzz=settings.reward_buzz)
                        GameLogic.Object['rewcount'] += 1
                        reward_success = True
                        #Matthias 2016/02/15
                        if settings.reward_delay_ticks_pre <= settings.reward_change_max:
                            settings.reward_delay_ticks_pre += settings.reward_change_step
                            print('\n{}'.format(settings.reward_delay_ticks_pre))
                            GameLogic.Object['RewardChange'] = True
                    else:
                        reward_success = False
                        GameLogic.Object['rewfail'] += 1

                    if settings.replay_track is None:
                        gio.write_reward(newy, reward_success)
                else:
                    sys.stdout.write('WallTouchTicksCounter is not None\n')
            GameLogic.Object['RewardTicksCounter'] += 1

        elif GameLogic.Object['RewardTicksCounter'] == settings.reward_delay_ticks_pre + settings.reward_delay_ticks_post:
            # Return animal with delay after reward
            if reward is not None:
                newx = 0
                # Move to fantasy land to avoid multiple rewards:
                reward.localPosition = [newx,1000.0,reward.position[2]]
            GameLogic.Object['RewardChange'] = False
            GameLogic.Object['RewardTicksCounter'] += 1
            zeroPos()

        elif GameLogic.Object['RewardTicksCounter'] >= settings.reward_delay_ticks_pre + settings.reward_delay_ticks_post + 1:
            # Reward was just given, animal is back at beginning of track,
            # so it's now safe to return the pump
            gc.zeroPump()
            GameLogic.Object['RewardChange'] = False
            GameLogic.Object['RewardTicksCounter'] = None
            GameLogic.Object['WallTouchTicksCounter'] = None
        else:
            # Still touching pump but not long enough yet
            GameLogic.Object['RewardTicksCounter'] += 1
    else:
        # First reward touch, buzz and start counter
        GameLogic.Object['RewardTicksCounter'] = 0
        # CSH 2013-11-19 Muted pump
        gc.runPump(pumppy, reward=False, buzz=False)
Beispiel #4
0
def init():
    if settings.looming:
        scene = GameLogic.getCurrentScene()
        scene.replace("Looming")

    GameLogic.Object = {}
    print("BLENDER: GameLogic object created")
    GameLogic.Object['closed'] = False
    GameLogic.setLogicTicRate(100)

    sys.stdout.write(
        "BLENDER: Maximum number of logic frames per render frame: %d\n" %
        GameLogic.getMaxLogicFrame())
    sys.stdout.write(
        "BLENDER: Maximum number of physics frames per render frame: %d\n" %
        GameLogic.getMaxPhysicsFrame())
    sys.stdout.write("BLENDER: Physics update frequency: %d Hz\n" %
                     GameLogic.getPhysicsTicRate())

    # open arduino and pump

    try:
        arduino = arduino_serial.SerialPort(settings.arduino_port, 19200)
        sys.stdout.write("BLENDER: Successfully opened arduino\n")
    except OSError:
        sys.stdout.write("BLENDER: Failed to open arduino\n")
        arduino = None
    GameLogic.Object['arduino'] = arduino
    if arduino is not None:
        arduino.write(b'd')
        # Set all valves to low
        arduino.write(b'6')
        arduino.write(b'8')
        arduino.write(b'0')
        arduino.write(b'f')

    try:
        pumppy = serial.Serial(settings.pump_port, 19200, timeout=1)
        sys.stdout.write("BLENDER: Successfully opened pump\n")
        gc.setPumpVolume(pumppy, settings.reward_volume)
    except:
        sys.stdout.write("BLENDER: Failed to open pump\n")
        pumppy = None

    if ncl.has_comedi:
        print("BLENDER: Found comedi library")
        gOuttrigsubdev = 2
        gOuttrigchan = 0
        gOutfrchan = 1
        gOutleftchan = 2
        gOutrightchan = 3
        gOutexpchan = 4

        gIntrigsubdev = 7
        gIntrigchan = 0

        gDevrange = 0

        # open ni trigger channels
        devintrigch, fdintrigch, nameintrigch = ncl.open_dev(
            "/dev/comedi0_subd2")
        devouttrigch, fdouttrigch, nameouttrigch = ncl.open_dev(
            "/dev/comedi0_subd11")

        intrigch = ncl.nichannel(devintrigch, gIntrigchan, gIntrigsubdev,
                                 fdintrigch, gDevrange)
        outtrigch = ncl.nichannel(devouttrigch, gOuttrigchan, gOuttrigsubdev,
                                  fdouttrigch, gDevrange)
        outfrch = ncl.nichannel(devouttrigch, gOutfrchan, gOuttrigsubdev,
                                fdouttrigch, gDevrange)
        outleftch = ncl.nichannel(devouttrigch, gOutleftchan, gOuttrigsubdev,
                                  fdouttrigch, gDevrange)
        outrightch = ncl.nichannel(devouttrigch, gOutrightchan, gOuttrigsubdev,
                                   fdouttrigch, gDevrange)
        outexpch = ncl.nichannel(devouttrigch, gOutexpchan, gOuttrigsubdev,
                                 fdouttrigch, gDevrange)  #MC2015

        ncl.init_comedi_dig(
            intrigch,
            outtrigch,
            outfrch,
            outleftch,
            outrightch,
            outexpch  #MC2015
        )
    else:
        intrigch = None
        outtrigch = None
        outfrch = None
        outleftch = None
        outrightch = None
        outexpch = None  #MC2015

    GameLogic.Object['intrigch'] = intrigch
    GameLogic.Object['outtrigch'] = outtrigch
    GameLogic.Object['outfrch'] = outfrch
    GameLogic.Object['outleftch'] = outleftch
    GameLogic.Object['outrightch'] = outrightch
    GameLogic.Object['outexpch'] = outexpch  #MC2015

    GameLogic.Object['pumppy'] = pumppy
    GameLogic.Object['left_on'] = False
    GameLogic.Object['right_on'] = False

    GameLogic.Object['file_open'] = False
    GameLogic.Object['train_open'] = False
    GameLogic.Object['bcstatus'] = False

    gio.create_data_dir()
    GameLogic.Object['time0'] = time.time()
    GameLogic.Object['prevtime'] = time.time()
    GameLogic.Object['nframes'] = 0

    GameLogic.Object['rewcount'] = 0
    GameLogic.Object['rewfail'] = 0
    GameLogic.Object['puffcount'] = 0
    GameLogic.Object['puffthistrial'] = 0
    GameLogic.Object['isloom'] = 0
    GameLogic.Object['loomcounter'] = 0
    GameLogic.Object['loom_first_trial'] = 0
    if settings.linear:
        GameLogic.Object['rewpos'] = [0.98]  # 1.0 # np.zeros((16))
    else:
        GameLogic.Object['rewpos'] = [
            -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1.0, 0.75, 0.5, 0.25, 0.0,
            -0.25, -0.5, -0.75, -1.0
        ]  # 1.0 # np.zeros((16))
    GameLogic.Object['boundx'] = 8.0
    GameLogic.Object['boundy'] = 158.0
    GameLogic.Object['hysteresis'] = 0.5
    GameLogic.Object['speed_tracker'] = np.zeros((100))

    blenderpath = GameLogic.expandPath('//')

    if not settings.cpp:
        s1, conn1, addr1, p1 = gc.spawn_process(
            "\0mouse0socket",
            ['python3', '%s/py/usbclient.py' % blenderpath, '0'])
        s2, conn2, addr2, p2 = gc.spawn_process(
            "\0mouse1socket",
            ['python3', '%s/py/usbclient.py' % blenderpath, '1'])
    else:
        if settings.readlib == "xinput":
            mice = xinput.find_mice(model=settings.mouse)
            for mouse in mice:
                # xinput.set_owner(mouse) # Don't need this if using correct udev rule
                xinput.switch_mode(mouse)
            if settings.usezmq:
                procname = 'readout_zmq'
            else:
                procname = 'readout'
            if len(mice) >= 1:
                s1, conn1, addr1, p1 = \
                    gc.spawn_process(
                        "\0mouse0socket",
                        [('%s/cpp/generic-ev/' % blenderpath) + procname, '%d' % mice[0].evno, '0'],
                        usezmq=settings.usezmq)
            else:
                s1, conn1, addr1, p1 = None, None, None, None
            if len(mice) >= 3:
                s2, conn2, addr2, p2 = \
                    gc.spawn_process(
                        "\0mouse1socket",
                        [('%s/cpp/generic-ev/readout' % blenderpath) + procname, '%d' % mice[2].evno, '1'],
                        usezmq=settings.usezmq)
            else:
                s2, conn2, addr2, p2 = None, None, None, None
        elif settings.readlib == "libusb":
            s1, conn1, addr1, p1 = \
                gc.spawn_process("\0mouse1socket",
                              ['%s/cpp/g500-usb/readout' % blenderpath, '1'])
            s2, conn2, addr2, p2 = \
                gc.spawn_process("\0mouse0socket",
                              ['%s/cpp/g500-usb/readout' % blenderpath, '0'])
        else:
            s1, conn1, addr1, p1, s2, conn2, add2, p2 = \
                None, None, None, None, None, None, None, None

    if settings.has_webcam:
        sys.stdout.write("BLENDER: Starting webcam... ")
        sys.stdout.flush()
        if not settings.cpp:
            svid, connvid, addrvid, pvid = gc.spawn_process("\0vidsocket", [
                'python3 %s/py/webcam.py' % blenderpath,
            ],
                                                            shell=True)
        else:
            svid, connvid, addrvid, pvid = gc.spawn_process("\0vidsocket", [
                '%s/cpp/webcam/webcam' % blenderpath,
            ],
                                                            system=False)
        print("done")
        connvid.send(GameLogic.Object['day_dir'].encode('latin-1'))
        gc.recv_ready(connvid)
        connvid.setblocking(0)
        GameLogic.Object['vidconn'] = connvid

    if settings.has_fw:
        if not settings.fw_local:
            GameLogic.Object['fwip'] = ''  #"128.40.202.203"
            sfw, connfw, addrfw = gc.spawn_process_net(
                GameLogic.Object['fwip'])
            if connfw is None:
                settings.has_fw = False
        else:
            sys.stdout.write("BLENDER: Starting fw... ")
            sys.stdout.flush()
            sfw, connfw, addrfw, pfw = \
                gc.spawn_process("\0fwsocket", ['%s/cpp/dc1394/dc1394' % blenderpath,], #MC2015
                              system=False, addenv={"SDL_VIDEO_WINDOW_POS":"\"1280,480\""})
            print("done")

        connfw.send(GameLogic.Object['fw_trunk'].encode('latin-1'))
        gc.recv_ready(connfw)
        connfw.setblocking(0)
        GameLogic.Object['fwconn'] = connfw

    GameLogic.Object['has_fw'] = settings.has_fw

    if settings.has_comedi and ncl.has_comedi:
        scomedi, conncomedi, addrcomedi, pcomedi = \
            gc.spawn_process("\0comedisocket", ['python3', '%s/py/nicomedi.py' % blenderpath,])

        conncomedi.send(blenderpath.encode('latin-1'))
        gc.recv_ready(conncomedi)
        conncomedi.setblocking(0)
        GameLogic.Object['comediconn'] = conncomedi

    if settings.has_licksensor:
        slick, connlick, addrlick, plick = \
            gc.spawn_process("\0licksocket", ['python3', '%s/py/licksensor.py' % blenderpath,])

        connlick.send(blenderpath.encode('latin-1'))
        gc.recv_ready(connlick)
        connlick.setblocking(0)
        GameLogic.Object['lickconn'] = connlick

    if settings.has_licksensor_piezo:
        slickpiezo, connlickpiezo, addrlickpiezo, plickpiezo = \
            gc.spawn_process("\0lickpiezosocket", ['python3', '%s/py/licksensorpiezo.py' % blenderpath,])

        connlickpiezo.send(blenderpath.encode('latin-1'))
        gc.recv_ready(connlickpiezo)
        connlickpiezo.setblocking(0)
        GameLogic.Object['lickpiezoconn'] = connlickpiezo

    if settings.cpp:
        for mconn in [conn1, conn2]:
            if mconn is not None:
                mconn.send(b'start')
                gc.recv_ready(mconn, usezmq=settings.usezmq)
                if not settings.usezmq:
                    mconn.setblocking(0)

    if len(mice):
        GameLogic.Object['m1conn'] = conn1
        GameLogic.Object['m2conn'] = conn2
    else:
        GameLogic.Object['m1conn'] = None
        GameLogic.Object['m2conn'] = None

    GameLogic.Object['tmprec'] = False
    GameLogic.Object['trainrec'] = False
    GameLogic.Object['RewardTicksCounter'] = None
    GameLogic.Object['RewardChange'] = False
    GameLogic.Object['WallTouchTicksCounter'] = None
    GameLogic.Object['OdorTicksCounter'] = None

    GameLogic.Object['piezolicks'] = 0
    GameLogic.Object['piezoframes'] = 0
    GameLogic.Object['piezoframepause'] = 0

    scene = GameLogic.getCurrentScene()
    if scene.name == "Scene":
        playerName = 'MovingCube'
        legName = 'LeftLeg'
    elif scene.name == "Looming":
        playerName = 'MovingCube.002'
        legName = 'LeftLeg.002'
    else:
        playerName = 'MovingCube.001'
        legName = 'LeftLeg.001'

    rew_sensor = scene.objects[playerName]
    touch_sensor = scene.objects[legName]

    if settings.linear:
        rew_sensor.sensors['SReward'].usePosPulseMode = True
        touch_sensor.sensors['SLeftTouch'].usePosPulseMode = True
    else:
        rew_sensor.sensors['SReward'].usePosPulseMode = False
        touch_sensor.sensors['SLeftTouch'].usePosPulseMode = False
    GameLogic.Object['scene_changed'] = 0
    GameLogic.Object['scene_name'] = scene.name

    GameLogic.Object['reset_pulse'] = False

    #current injection variables
    #variables for current injection - MC2015
    GameLogic.Object['start_pulse_y'] = 50
    GameLogic.Object['inj_amp'] = 50
    GameLogic.Object['do_tbs1'] = False
    GameLogic.Object['do_tbs2'] = False

    zeroPos()
    gc.zeroPump()
Beispiel #5
0
def zeroPos():
    # controller = GameLogic.getCurrentController()
    # own = controller.owner
    scene = GameLogic.getCurrentScene()

    # Romain
    # avoid multiple resettings
    # this is important here because the gratings are changed
    # when calling zeroPos and we don't want to have two events
    # associated with one resetting

    # finally I generalize it to all cases
    try:  # if initialisation, "last_zero" still doesn't exist
        GameLogic.Object["last_zero"]
        init = 0
    except:
        init = 1
    if not init and (settings.gratings or settings.cues):
        if GameLogic.Object["last_zero"] <= 3:
            return

    if init and settings.gratings:
        # Romain
        # make some objects disappear
        objectsToRemove = [
        ]  # put the object name into quotes exple : ["name1","name2"]
        for i in objectsToRemove:
            scene.objects[i].visible = False
        # set normal walls to invisible
        for i in scene.objects:
            if i.name[:8] == "LeftWall" or i.name[:9] == "RightWall":
                i.visible = False
    if init and not settings.gratings:
        try:
            for i in ["LW1", "RW1", "CeilingGrating"]:
                scene.objects[i].visible = False
        except KeyError:
            sys.stderr.write("BLENDER: Warning: Grating walls are missing")
    if init and not settings.cues:
        try:
            for i in settings.objects_cues:
                scene.objects[i].visible = False
        except AttributeError:
            sys.stderr.write("BLENDER: Warning: Cues are missing")
    GameLogic.Object["last_zero"] = 0

    if scene.name == "Scene":
        playerName = 'MovingCube'
        rew1Name = 'Cylinder.001'
        rew2Name = 'Cylinder.002'
        rew3Name = 'Cylinder.003'
    elif scene.name == "Looming":
        playerName = 'MovingCube.002'
    else:
        playerName = 'MovingCube.001'
        rew1Name = 'Cone.001'
        rew2Name = 'Cone.002'
        rew3Name = 'Cylinder.000'
    own = scene.objects[playerName]
    if sys.version_info >= (3, ):
        euler = mu.Euler((0, 0, 0))
        euler = euler.to_matrix()
    else:
        euler = mu.Euler(0, 0, 0)
    own.localOrientation = euler  # [[0,0,0],[0,0,0],[0,0,0]]
    if settings.linear and not settings.looming:
        startOdorCounter()
        own.localPosition = [0, settings.startPos, 1.5]  # y was +80
        if settings.reward_double:

            # Randomly select one of the 2 rewards:
            GameLogic.Object['select_rew'] = "%s" % (np.random.randint(2) + 1)

            # Distribute rewards with at least 20 units distance between the 2:
            rew1 = scene.objects[rew1Name]
            rew2 = scene.objects[rew2Name]
            new_rew_y = np.random.uniform(20.0, 120.0, 2)
            while np.diff(new_rew_y)[0] < 20.0:
                new_rew_y = np.random.uniform(20.0, 120.0, 2)
            rew1.localPosition = [0, new_rew_y[0], rew1.position[2]]
            rew2.localPosition = [0, new_rew_y[1], rew2.position[2]]

        elif settings.reward_cpp:
            rew1 = scene.objects[rew1Name]
            rew2 = scene.objects[rew2Name]

            rew1.localPosition = [0, 60, rew1.position[2]]
            rew2.localPosition = [200, 0, rew2.position[2]]
        else:
            rew2 = scene.objects[rew2Name]
            rew3 = scene.objects[rew3Name]

            rew2.localPosition = [0, 1000, rew2.position[2]]
            rew3.localPosition = [0, 1000, rew3.position[2]]

    elif not settings.looming:
        own.localPosition = [0, -150, 1.5]
    else:
        own.localPosition = [0, 0, 0]

    # Romain : randomly change wall gratings
    if settings.gratings:
        chooseWalls.randomWalls(settings.proba_env1)

    # Romain : select the pair of cues either 'randomly without replacement' settings.groups_trials = True
    # or completely randomly if settings.groups_trials = False
    if settings.cues:
        if settings.groups_trials:
            if GameLogic.Object['run_number'] in [None, settings.n_runs - 1]:
                if GameLogic.Object['run_number'] == settings.n_runs - 1:
                    GameLogic.Object["stopped_counter_sessions"] = 0
                GameLogic.Object['run_number'] = 0
                GameLogic.Object['current_order'] = chooseCues.generate_order(
                    settings.content_trials)
                cues.write_new_session()
                print(GameLogic.Object['current_order'])
            else:
                GameLogic.Object['run_number'] += 1
                GameLogic.Object["stopped_counter_trials"] = 0
            chooseCues.chooseCues(GameLogic.Object['current_order'][
                GameLogic.Object['run_number']])
        else:
            chooseCues.randomCues(settings.proba_mismatch)

    if settings.cues or settings.gratings:
        gc.zeroPump()
        GameLogic.Object['WallTouchTicksCounter'] = None
        GameLogic.Object['RewardTicksCounter'] = None