Exemplo n.º 1
0
  def drive(self):
    # if in first iteration after learning is finished
    if(self.first_time):
      # make robot drive faster
      # self.speed *= 1.5
      # make sound to signal that learning is finished
      sound.make_sound()
      # save neural network
      self.save_model()
      # indicate end of first iteration after learning is finished
      self.first_time = False

    # wait for new image(s)
    my_imgs, my_img = self.shape_images()
    # get new / current state
    self.curr_state, reward = self.get_robot_state(my_img)

    # stop robot
    stop_arr=[self.lost_line]
    if(self.curr_state in stop_arr):
      self.stopRobot()

    # get q-values by feeding images to the DQN
    # without updating its weights
    self.q_values = self.policy_net.use_network(state=my_imgs)
    # choose action by selecting highest q -value
    action = np.argmax(self.q_values)
    print("Action: " + self.action_strings.get(action))

    # execute action
    self.execute_action(action)
    # stop if line is lost (hopefully never, if
    # robot learned properly)
    if (self.curr_state == self.lost_line):
      self.reset_environment()
Exemplo n.º 2
0
    def __init__(self):
        self.turtle_init()

        self.left_rocket = Rocket(
            'left', CONFIGS.ROCKET_SPEED,
            (-CONFIGS.WIDTH / 2 + CONFIGS.ROCKETS_DXPOS, 0),
            CONFIGS.ROCKET1_COLOR)
        self.right_rocket = Rocket(
            'right', CONFIGS.ROCKET_SPEED,
            (CONFIGS.WIDTH / 2 - CONFIGS.ROCKETS_DXPOS, 0),
            CONFIGS.ROCKET2_COLOR)

        self.ball = Ball(CONFIGS.BALL_SPEED, (0, 0),
                         (self.left_rocket, self.right_rocket))
        self.message_turtle = None

        t.update()
        make_sound(4)
        self.activate_keys()
Exemplo n.º 3
0
    def check_boundaries(self):
        # reflection from the obstacles
        for obs in self.obstacles:
            if -CONFIGS.BALL_SPEED / 2 < obs.x + obs.dx0 - self.x < CONFIGS.BALL_SPEED / 2 \
                    and -CONFIGS.ROCKET_HEIGHT * CONFIGS.TKTURTLE_DX / 2 < self.y - obs.y < CONFIGS.ROCKET_HEIGHT * CONFIGS.TKTURTLE_DX / 2:
                self.dx = -self.dx
                make_sound(4)

        # reach from the left or right sides
        if self.x + self.dx < -CONFIGS.WIDTH / 2 or self.x + self.dx > CONFIGS.WIDTH / 2:
            make_sound(3)
            for obs in self.obstacles:
                if self.x * obs.x < 0:
                    obs.score += 1  # give a score ball
            self.dx = -self.dx
            self.printscore()  # update a score
            # return a ball to a start position
            self.x = 0
            self.y = 0
        # reflection from the upper and bottom sides
        if self.y + self.dy < -CONFIGS.HEIGHT / 2 or self.y + self.dy > CONFIGS.HEIGHT / 2:
            make_sound(1)
            self.dy = -self.dy
Exemplo n.º 4
0
    def __init__(self, task_settings, user_settings, debug=False, fmake=True):
        self.DEBUG = debug
        make = True
        self.IBLRIG_FOLDER = 'C:\\iblrig'
        self.IBLRIG_DATA_FOLDER = None  # ..\\iblrig_data if None
        # =====================================================================
        # IMPORT task_settings, user_settings, and SessionPathCreator params
        # =====================================================================
        ts = {
            i: task_settings.__dict__[i]
            for i in [x for x in dir(task_settings) if '__' not in x]
        }
        self.__dict__.update(ts)
        us = {
            i: user_settings.__dict__[i]
            for i in [x for x in dir(user_settings) if '__' not in x]
        }
        self.__dict__.update(us)
        self = iotasks.deserialize_pybpod_user_settings(self)
        spc = SessionPathCreator(self.IBLRIG_FOLDER,
                                 self.IBLRIG_DATA_FOLDER,
                                 self.PYBPOD_SUBJECTS[0],
                                 protocol=self.PYBPOD_PROTOCOL,
                                 board=self.PYBPOD_BOARD,
                                 make=make)
        self.__dict__.update(spc.__dict__)
        # =====================================================================
        # SETTINGS
        # =====================================================================
        self.RECORD_SOUND = True
        self.RECORD_AMBIENT_SENSOR_DATA = True
        self.RECORD_VIDEO = True
        self.OPEN_CAMERA_VIEW = True  # Always True if RECORD_VIDEO is True

        self.NTRIALS = 2000  # Number of trials for the current session
        self.USE_AUTOMATIC_STOPPING_CRITERIONS = True  # Weather to check for the Automatic stopping criterions or not  # noqa
        self.REPEAT_ON_ERROR = False  # not used
        self.INTERACTIVE_DELAY = 0.0
        self.RESPONSE_WINDOW = 60
        self.ITI_CORRECT = 1
        self.ITI_ERROR = 2
        self.CONTRAST_SET = [1., 0.25, 0.125, 0.0625, 0.]  # Full contrast set
        self.CONTRAST_SET_PROBABILITY_TYPE = 'biased'
        self.STIM_FREQ = 0.10  # Probably constant - NOT IN USE
        self.STIM_ANGLE = 0.  # Vertical orientation of Gabor patch
        self.STIM_SIGMA = 7.  # (azimuth_degree) Size of Gabor patch
        self.STIM_GAIN = 4.  # (azimuth_degree/mm) Gain of the RE
        # =====================================================================
        # SUBJECT
        # =====================================================================
        self.SUBJECT_WEIGHT = self.get_subject_weight()
        self.POOP_COUNT = True
        # =====================================================================
        # OSC CLIENT
        # =====================================================================
        self.OSC_CLIENT_PORT = 7110
        self.OSC_CLIENT_IP = '127.0.0.1'
        self.OSC_CLIENT = udp_client.SimpleUDPClient(self.OSC_CLIENT_IP,
                                                     self.OSC_CLIENT_PORT)
        # =====================================================================
        # PREVIOUS DATA FILES
        # =====================================================================
        self.LAST_TRIAL_DATA = iotasks.load_data(self.PREVIOUS_SESSION_PATH)
        self.LAST_SETTINGS_DATA = iotasks.load_settings(
            self.PREVIOUS_SESSION_PATH)
        self.SESSION_ORDER = []
        self.SESSION_IDX = None
        self = iotasks.load_session_order_and_idx(self)
        # Load from file
        self.POSITIONS = None
        self.CONTRASTS = None
        self.QUIESCENT_PERIOD = None
        self.STIM_PHASE = None
        self.LEN_BLOCKS = None
        self = iotasks.load_session_pcqs(self)
        # =====================================================================
        # ADAPTIVE STUFF
        # =====================================================================
        self.AUTOMATIC_CALIBRATION = True
        self.CALIBRATION_VALUE = 0.067
        self.REWARD_AMOUNT = 3.
        self.REWARD_TYPE = 'Water 10% Sucrose'

        self.CALIB_FUNC = adaptive.init_calib_func(self)
        self.CALIB_FUNC_RANGE = adaptive.init_calib_func_range(self)
        self.REWARD_VALVE_TIME = adaptive.init_reward_valve_time(self)

        # =====================================================================
        # ROTARY ENCODER
        # =====================================================================
        self.STIM_POSITIONS = [-35, 35]  # All possible positions (deg)
        self.QUIESCENCE_THRESHOLDS = [-2, 2]  # degree
        self.ALL_THRESHOLDS = (self.STIM_POSITIONS +
                               self.QUIESCENCE_THRESHOLDS)
        self.ROTARY_ENCODER = MyRotaryEncoder(self.ALL_THRESHOLDS,
                                              self.STIM_GAIN,
                                              self.COM['ROTARY_ENCODER'])
        # =====================================================================
        # SOUNDS
        # =====================================================================
        self.SOFT_SOUND = None
        self.SOUND_SAMPLE_FREQ = sound.sound_sample_freq(self.SOFT_SOUND)
        self.SOUND_BOARD_BPOD_PORT = 'Serial3'
        self.WHITE_NOISE_DURATION = float(0.5)
        self.WHITE_NOISE_AMPLITUDE = float(0.05)
        self.GO_TONE_DURATION = float(0.1)
        self.GO_TONE_FREQUENCY = int(5000)
        self.GO_TONE_AMPLITUDE = float(0.1)

        self.SD = sound.configure_sounddevice(
            output=self.SOFT_SOUND, samplerate=self.SOUND_SAMPLE_FREQ)
        # Create sounds and output actions of state machine
        self.GO_TONE = sound.make_sound(rate=self.SOUND_SAMPLE_FREQ,
                                        frequency=self.GO_TONE_FREQUENCY,
                                        duration=self.GO_TONE_DURATION,
                                        amplitude=self.GO_TONE_AMPLITUDE,
                                        fade=0.01,
                                        chans='stereo')
        self.WHITE_NOISE = sound.make_sound(
            rate=self.SOUND_SAMPLE_FREQ,
            frequency=-1,
            duration=self.WHITE_NOISE_DURATION,
            amplitude=self.WHITE_NOISE_AMPLITUDE,
            fade=0.01,
            chans='stereo')
        self.GO_TONE_IDX = 2
        self.WHITE_NOISE_IDX = 3
        sound.configure_sound_card(
            sounds=[self.GO_TONE, self.WHITE_NOISE],
            indexes=[self.GO_TONE_IDX, self.WHITE_NOISE_IDX],
            sample_rate=self.SOUND_SAMPLE_FREQ)
        # =====================================================================
        # VISUAL STIM
        # =====================================================================
        self.SYNC_SQUARE_X = 0.95
        self.SYNC_SQUARE_Y = 0.17
        self.USE_VISUAL_STIMULUS = True  # Run the visual stim in bonsai
        self.BONSAI_EDITOR = False  # Open the Bonsai editor of visual stim
        bonsai.start_visual_stim(self)
        self.get_recording_site_data()
        # =====================================================================
        # SAVE SETTINGS FILE AND TASK CODE
        # =====================================================================
        if not self.DEBUG:
            iotasks.save_session_settings(self)
            iotasks.copy_task_code(self)
            iotasks.save_task_code(self)
            self.bpod_lights(0)

        self.display_logs()