def accelerate(self): control = VehicleControl() control.throttle = 1.0 if (self.too_close_to('vehicle', self.vehicle_distance_threshold)): control.throttle = self.measurements.player_measurements.autopilot_control.throttle control.reverse = self._is_on_reverse self.client.send_control(control)
def _get_joystick_control(self): control = VehicleControl() control.steer = self._joystick.get_axis(0) control.throttle = max(self._joystick.get_axis(1) * -1, 0) control.brake = max(self._joystick.get_axis(1), 0) return control
def _restart_environment_episode(self, force_environment_reset=False): # select start and end positions if self.experiment_suite: # if an expeirent suite is available, follow its given poses if self.current_pose >= len(self.current_experiment.poses): # load a new experiment self.current_experiment_idx = (self.current_experiment_idx + 1) % len(self.experiment_suite.get_experiments()) self._load_experiment(self.current_experiment_idx) self.current_start_position_idx = self.current_experiment.poses[self.current_pose][0] self.current_goal = self.positions[self.current_experiment.poses[self.current_pose][1]] self.current_pose += 1 else: # go over all the possible positions in a cyclic manner self.current_start_position_idx = (self.current_start_position_idx + 1) % self.num_positions # choose a random goal destination self.current_goal = random.choice(self.positions) try: self.game.start_episode(self.current_start_position_idx) except: self.game.connect() self.game.start_episode(self.current_start_position_idx) # start the game with some initial speed for i in range(self.num_speedup_steps): self.control = VehicleControl(throttle=1.0, brake=0, steer=0, hand_brake=False, reverse=False) self.game.send_control(VehicleControl())
def steer_right(self): control = VehicleControl() control.steer = min(control.steer + 0.01, 0.05) print(control.steer) control.reverse = self._is_on_reverse control.throttle = self.measurements.player_measurements.autopilot_control.throttle self.client.send_control(control)
def run_step(self, measurements, sensor_data, directions, target, frame): actions = [-1.0, -0.5, 0, 0.5, 1, 0, 0.5, 1.0] control = VehicleControl() if frame < 30: control.throttle = 0 else: control.throttle = 0.6 return control
def _on_loop(self, frame): self._timer.tick() skip_frames = 40 measurements, sensor_data = self.client.read_data() current_position = vec3tovec2(measurements.player_measurements.transform.location) self._velocities.append(measurements.player_measurements.forward_speed * 3.6) # convert to km/h steer = 0.0 acceleration = 0.0 for name, measurement in sensor_data.items(): model_input = preprocess(measurement.data) model_input = np.array(model_input / 127.5 - 1, dtype=np.float32) model_input = np.expand_dims(model_input, axis=0) ret = self._model.predict(model_input)[0] steer = ret[0] acceleration = ret[1] if USE_SPEED_CONSTRAINTS: if measurements.player_measurements.forward_speed * 3.6 < MINIMAL_SPEED: acceleration = 0.7 elif measurements.player_measurements.forward_speed * 3.6 > MAXIMAL_SPEED: acceleration = 0 control = VehicleControl() control.steer = steer control.throttle = acceleration self.client.send_control(control) if frame < skip_frames: logging.info("Skipping first {} frames...".format(skip_frames)) return True self.line_points.append(current_position) self.points_x.append(current_position[0]) self.points_y.append(current_position[1]) if len(self.line_points) > 1: dist_from_start = distance(self.line_points[0], current_position) else: dist_from_start = 10000 if dist_from_start < 1 and frame > skip_frames + 100: logging.info("Position: {} is already logged".format(current_position)) return False if self._timer.elapsed_seconds_since_lap() > 0.5: self._print_player_measurements(control) logging.info("Add point: [{:.4f},{:.4f}], points count: {:0>4d}, distance from start: {:.4f}".format( current_position[0], current_position[1], len(self.line_points), dist_from_start)) self._timer.lap() return True
def _get_joystick_control(self, joystick): control = VehicleControl() tmp1 = 0.6 * joystick.get_axis(1) if (tmp1 <= 0): control.throttle = -tmp1 control.brake = 0 else: control.throttle = 0 control.brake = tmp1 control.steer = joystick.get_axis(2) control.steer = 0.5 * control.steer * control.steer * control.steer # print('steer....',control.steer) #provide a stable autopilot autopilot = joystick.get_button(0) if autopilot == 1: self._enable_autopilot = not self._enable_autopilot # provide a stable reverse reverse = joystick.get_button(2) if reverse == 1: self._is_on_reverse = not self._is_on_reverse control.reverse = self._is_on_reverse return control
def _get_keyboard_control(self, keys, args): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ global flg_warning if keys[K_r]: return None control = VehicleControl() if keys[K_LEFT] or keys[K_a]: control.steer = -1.0 if keys[K_RIGHT] or keys[K_d]: control.steer = 1.0 if keys[K_UP] or keys[K_w]: control.throttle = 1.0 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 if keys[K_SPACE]: control.hand_brake = True if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_p]: self._enable_autopilot = not self._enable_autopilot if args.app == 'Control': if flg_warning == -1: control.throttle = 0.0 control.reverse = self._is_on_reverse return control
def action_joystick(self): # joystick steering_axis = self.joystick.get_axis(0) acc_axis = self.joystick.get_axis(2) brake_axis = self.joystick.get_axis(5) # print("axis 0 %f, axis 2 %f, axis 3 %f" % (steering_axis, acc_axis, brake_axis)) if (self.joystick.get_button(3)): self._rear = True if (self.joystick.get_button(2)): self._rear = False control = VehicleControl() control.steer = steering_axis control.throttle = (acc_axis + 1) / 2.0 control.brake = (brake_axis + 1) / 2.0 if control.brake < 0.001: control.brake = 0.0 control.hand_brake = 0 control.reverse = self._rear control.steer -= 0.0822 #print("steer %f, throttle %f, brake %f" % (control.steer, control.throttle, control.brake)) pygame.event.pump() return control
def _get_keyboard_control(self, keys): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ if keys[K_r]: return None control = VehicleControl() if keys[K_LEFT] or keys[K_a]: control.steer = -0.5 if keys[K_RIGHT] or keys[K_d]: control.steer = 0.5 if keys[K_UP] or keys[K_w]: control.throttle = 0.7 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 if keys[K_SPACE]: control.hand_brake = True if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_p]: self._enable_autopilot = not self._enable_autopilot if keys[K_2]: self._command = 2 if keys[K_3]: self._command = 3 if keys[K_4]: self._command = 4 if keys[K_5]: self._command = 5 control.reverse = self._is_on_reverse return control
def _control_using_agent(self, action): ''' Here we pass the action output by our agent and control the car outputs : As of now consider the agent also outputs the keys W, A, S, D, space, reverse so according to the output vector "action" For example : action = [1, 1, 0, 0, 0, 0] => it uses throttle and steer left action = [0, 0, 0, 0, 0, 1] => car will be on reverse gear ''' '''To be completed''' # if action[6]: # # if r is pressed # return None control = VehicleControl() if action[1]: control.steer = -1.0 if action[3]: control.steer = 1.0 if action[0]: control.throttle = 1.0 if action[2]: control.brake = 1.0 if action[4]: control.hand_brake = True if action[5]: self._is_on_reverse = not self._is_on_reverse control.reverse = self._is_on_reverse return control
def _get_joystick_control(self,joystick): control = VehicleControl() tmp1 = 0.6 * joystick.get_axis(1) if (tmp1 <= 0): control.throttle = -tmp1 control.brake = 0 else: control.throttle = 0 control.brake = tmp1 control.steer = joystick.get_axis(2) control.steer = 0.5 * control.steer * control.steer * control.steer # print('steer....',control.steer) #provide a stable autopilot autopilot = joystick.get_button(0) if autopilot == 1: self._enable_autopilot = not self._enable_autopilot # provide a stable reverse reverse = joystick.get_button(2) if reverse == 1: self._is_on_reverse = not self._is_on_reverse control.reverse = self._is_on_reverse return control
def _get_keyboard_control(self, keys, measurements): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ if keys[K_p]: control = measurements.player_measurements.autopilot_control control.steer += random.uniform(-0.1, 0.1) return control if keys[K_r]: return None control = VehicleControl() if keys[K_LEFT] or keys[K_a]: control.steer = -1.0 if keys[K_RIGHT] or keys[K_d]: control.steer = 1.0 if keys[K_UP] or keys[K_w]: control.throttle = 1.0 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 if keys[K_SPACE]: control.hand_brake = True if keys[K_q]: self._is_on_reverse = not self._is_on_reverse control.reverse = self._is_on_reverse return control
def _get_keyboard_control(self, keys): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ if keys[K_r]: return None, None if keys[K_t]: self.should_display = not self.should_display return 'done', None if keys[K_m]: self.manual = True return 'done', None if keys[K_n]: self.manual = False return 'done', None if keys[K_v]: self.endnow = True return 'done', None control = VehicleControl() if keys[K_LEFT] or keys[K_a]: control.steer = -1.0 if keys[K_RIGHT] or keys[K_d]: control.steer = 1.0 if keys[K_UP] or keys[K_w]: control.throttle = 1.0 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 if keys[K_SPACE]: control.hand_brake = True if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_c]: self.manual_control = not self.manual_control if keys[K_p]: self._enable_autopilot = not self._enable_autopilot control.reverse = self._is_on_reverse reward = None if keys[K_1]: reward = -1 if keys[K_2]: reward = -0.5 if keys[K_3]: reward = -0.25 if keys[K_4]: reward = -0.1 if keys[K_5]: reward = 0 if keys[K_6]: reward = 0.1 if keys[K_7]: reward = 0.25 if keys[K_8]: reward = 0.5 if keys[K_9]: reward = 1 return control, reward
def _get_keyboard_control(self, keys, measurements, sensor_data): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ self._at_intersection = False if keys[K_r]: return None if self._NNagent_drives: if keys[K_LEFT] or keys[K_a]: control = self.NNagent.run_step(measurements, sensor_data, 3, None) elif keys[K_RIGHT] or keys[K_d]: control = self.NNagent.run_step(measurements, sensor_data, 4, None) elif keys[K_UP] or keys[K_w]: control = self.NNagent.run_step(measurements, sensor_data, 2, None) else: control = self.NNagent.run_step(measurements, sensor_data, -1, None) if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_p]: self._enable_autopilot = True self._NNagent_drives = not self._NNagent_drives if keys[K_n]: print("Turning off") self._NNagent_drives = not self._NNagent_drives if keys[K_i]: self._at_intersection = True control.reverse = self._is_on_reverse else: control = VehicleControl() if keys[K_LEFT] or keys[K_a]: control.steer = -1.0 if keys[K_RIGHT] or keys[K_d]: control.steer = 1.0 if keys[K_UP] or keys[K_w]: control.throttle = 1.0 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 if keys[K_SPACE]: control.hand_brake = True if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_p]: self._enable_autopilot = not self._enable_autopilot if keys[K_n]: print("Turning on") self._NNagent_drives = not self._NNagent_drives self._enable_autopilot = False if keys[K_i]: self._at_intersection = True control.reverse = self._is_on_reverse return control
def _take_action(self, action): self.control = VehicleControl() if self.separate_actions_for_throttle_and_brake: self.control.steer = np.clip(action[0], -1, 1) self.control.throttle = np.clip(action[1], 0, 1) self.control.brake = np.clip(action[2], 0, 1) else: # transform the 2 value action (steer, throttle - brake) into a 3 value action (steer, throttle, brake) self.control.steer = np.clip(action[0], -1, 1) self.control.throttle = np.clip(action[1], 0, 1) self.control.brake = np.abs(np.clip(action[1], -1, 0)) # prevent braking if not self.allow_braking or self.control.brake < 0.1 or self.control.throttle > self.control.brake: self.control.brake = 0 # prevent over speeding if hasattr(self, 'measurements') and self.measurements[ 0] * 3.6 > self.max_speed and self.control.brake == 0: self.control.throttle = 0.0 self.control.hand_brake = False self.control.reverse = False self.game.send_control(self.control)
def __init__(self, carla_client, args): self.client = carla_client self._carla_settings = make_carla_settings(args) self._timer = None self._display = None self._main_image = None self._mini_view_image1 = None self._mini_view_image2 = None self._enable_autopilot = args.autopilot self._lidar_measurement = None self._map_view = None self._is_on_reverse = False self._display_map = args.map self._city_name = None self._map = None self._map_shape = None self._map_view = None self._position = None self._agent_positions = None self.get_control = VehicleControl() self.steer = 0 # variables to enable saving to disk self.out_filename_format = args.out_filename_format self.save_images_to_disk = args.save_images_to_disk self.episode_count = 0 self.frame_count = 0 self.saver = None self.out_dir = "/media/akshata/DATA/carla/CARLA_0.8.4/Data"
def __init__(self, carla_client, args): self.client = carla_client self._carla_settings = make_carla_settings(args) self._control = VehicleControl() self._control.throttle = 0.58 self._startID = [0,2,4,6,11,13,17,19,21,24,30,39,55,57,66,70] self.state = State()
def __init__(self): pygame.init() pygame.joystick.init() self.js = pygame.joystick.Joystick(0) self.js.init() self._is_on_reverse = False self.control = VehicleControl() self.parser = SafeConfigParser() self.parser.read('wheel_config.ini') self.steer_idx = int( self.parser.get('G29 Racing Wheel', 'steering_wheel')) self.throttle_idx = int(self.parser.get('G29 Racing Wheel', 'throttle')) self.brake_idx = int(self.parser.get('G29 Racing Wheel', 'brake')) self.reverse_idx = int(self.parser.get('G29 Racing Wheel', 'reverse')) self.handbrake_idx = int( self.parser.get('G29 Racing Wheel', 'handbrake')) # ADDITION: Added high level command inputs to be taken from joystick self.cmd_left_idx = int(self.parser.get('G29 Racing Wheel', 'cmd_left')) self.cmd_right_idx = int( self.parser.get('G29 Racing Wheel', 'cmd_right')) self.cmd_straight_idx = int( self.parser.get('G29 Racing Wheel', 'cmd_straight')) self.cmd = 2 self.savetoggle = int( self.parser.get('G29 Racing Wheel', 'save_toggle')) self._savemode = False
def _take_action(self, action_idx): if not self.is_game_setup: print("Reset the environment before calling step") sys.exit(1) if type(action_idx) == int: action = self.actions[action_idx] else: action = action_idx self.control = VehicleControl() if self.car_speed>35.0 and action[0]>0: action[0] -= 0.20*(self.car_speed/35.0) self.control.throttle = np.clip(action[0], 0, 1) self.control.steer = np.clip(action[1], -1, 1) self.control.brake = np.abs(np.clip(action[0], -1, 0)) if not self.allow_braking: self.control.brake = 0 self.control.hand_brake = False self.control.reverse = False controls_sent = False while not controls_sent: try: self.game.send_control(self.control) controls_sent = True except: if self.kill_when_connection_lost: raise print("Connection to server lost while sending controls. Reconnecting...........") self.close_client_and_server() self.setup_client_and_server(reconnect_client_only=False) self.done = True return
def __init__(self, carla_client, args): self.client = carla_client self._carla_settings = make_carla_settings(args) self._display = None self._enable_autopilot = args.autopilot self._is_on_reverse = False self._city_name = args.map_name self._map = CarlaMap(self._city_name, 16.43, 50.0) if self._city_name is not None else None self._map_shape = self._map.map_image.shape if self._city_name is not None else None #self._map_view = self._map.get_map(WINDOW_HEIGHT) if self._city_name is not None else None self._map_view = None self._position = None self._control = VehicleControl() self._control.throttle = 0.58 self._location = [0, 0, 0] self._started = False self._startID = [ 0, 2, 4, 6, 11, 13, 17, 19, 21, 24, 30, 39, 55, 57, 66, 70 ] self._speed = 0 self._offroad = 0 self._terminal = False self._error_dis = 0 self._error_yaw = 0 self._road_left, self._road, self._road_right = self._load_road( 'waypoints', 15)
def run_step(self, measurements, sensor_data, directions, target): # We basically ignore all the parameters. for event in pygame.event.get(): if event.type == pygame.QUIT: return VehicleControl() return self._get_keyboard_control(pygame.key.get_pressed())
def reset(self, vehicle_num): #if not self.is_process_alive(self.server_pid): #self.setup_client_and_server() self.nospeed_times = 0 pose_type = random.choice(self.poses) #pose_type = self.poses[0] self.current_position = random.choice( pose_type) #start and end index #self.current_position = (53,67) # self.number_of_vehicles = random.randint( self.vehicle_pair[0],self.vehicle_pair[1]) # self.number_of_pedestrians = random.randint( self.vehicle_pair[0],self.vehicle_pair[1]) self.number_of_vehicles = vehicle_num self.number_of_pedestrians = 0 self.weather = random.choice(self.weather_set) self.timestep = 0 self.collision = 0 self.collision_vehicles = 0 self.collision_other = 0 self.stuck_cnt = 0 self.collision_cnt = 0 self.offroad_cnt = 0 self.ignite = False settings = carla_config.make_carla_settings() settings.set(NumberOfVehicles=self.number_of_vehicles, NumberOfPedestrians=self.number_of_pedestrians, WeatherId=self.weather) self.carla_setting = settings self.scene = self.game.load_settings(settings) self.game.start_episode( self.current_position[0]) #set the start position #print(self.current_position) self.target_transform = self.scene.player_start_spots[ self.current_position[1]] self.planner = Planner(self.scene.map_name) #skip the car fall to sence frame for i in range(self.speed_up_steps): self.control = VehicleControl() self.control.steer = 0 self.control.throttle = 0.025 * i self.control.brake = 0 self.control.hand_brake = False self.control.reverse = False time.sleep(0.05) send_success = self.send_control(self.control) if not send_success: return None self.game.send_control(self.control) #measurements, sensor_data = self.game.read_data() #measurements,sensor #direction =self.get_directions(measurements,self.target_transform,self.planner) #self.get_state(measurements,sensor_data,direction) measurements, sensor_data = self.game.read_data() #measurements,sensor directions = self.get_directions(measurements, self.target_transform, self.planner) if directions is None or measurements is None: return None state, _, _ = self.get_state(measurements, sensor_data, directions) return state
def send_control_command(client, throttle, steer, brake, hand_brake=False, reverse=False): """Send control command to CARLA client. Send control command to CARLA client. Args: client: The CARLA client object throttle: Throttle command for the sim car [0, 1] steer: Steer command for the sim car [-1, 1] brake: Brake command for the sim car [0, 1] hand_brake: Whether the hand brake is engaged reverse: Whether the sim car is in the reverse gear """ control = VehicleControl() # Clamp all values within their limits steer = np.fmax(np.fmin(steer, 1.0), -1.0) throttle = np.fmax(np.fmin(throttle, 1.0), 0) brake = np.fmax(np.fmin(brake, 1.0), 0) control.steer = steer control.throttle = throttle control.brake = brake control.hand_brake = hand_brake control.reverse = reverse client.send_control(control)
def _get_keyboard_control(self, keys): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ if keys[K_r]: return None control = VehicleControl() if keys[K_LEFT] or keys[K_a]: self._control.steer += -0.02 self._control.steer = max(self._control.steer, -1.0) control.steer = -1.0 if keys[K_RIGHT] or keys[K_d]: self._control.steer += 0.02 self._control.steer = min(self._control.steer, 1.0) control.steer = 1.0 if keys[K_l] or keys[K_SPACE]: self._control.steer = 0 if keys[K_UP] or keys[K_w]: control.throttle = 1.0 self._control.throttle = 0.6 self._control.throttle = min(self._control.throttle, 1) self._control.brake = 0.0 if keys[K_DOWN] or keys[K_s]: control.brake = 1.0 self._control.brake = 1.0 self._control.throttle = 0.0 if keys[K_q]: self._is_on_reverse = not self._is_on_reverse if keys[K_p]: self._enable_autopilot = not self._enable_autopilot control.reverse = self._is_on_reverse self._control.reverse = self._is_on_reverse return control
def _compute_action(self, rgb_image, speed, directions=None): #shit for demo img = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) cv2.imwrite(self.img_dir + "/image_" + str(self.nr_img) + ".jpg", img) #shit for demo rgb_image = rgb_image[self._image_cut[0]:self._image_cut[1], :] steer, acc, brake = self._control_function(rgb_image, speed, directions) # This a bit biased, but is to avoid fake breaking if brake < 0.1: brake = 0.0 if acc > brake: brake = 0.0 # We limit speed to 35 km/h to avoid if speed > 10.0 and brake == 0: acc = 0.0 control = VehicleControl() control.steer = steer control.throttle = acc control.brake = brake control.hand_brake = 0 control.reverse = 0 return control
def _get_autopilot_control(self): speed = int(self._measurements.player_measurements.forward_speed * 3.6) autopilot = self._measurements.player_measurements.autopilot_control control = VehicleControl() steer_noise = self._settings["autopilot_steer_noise"] if steer_noise != 0: steer_noise = np.random.uniform(-steer_noise, steer_noise) control.steer = autopilot.steer + steer_noise throttle_noise = ( self._settings["autopilot_throttle_noise"] if speed > 10 else 0 ) if throttle_noise != 0: throttle_noise = np.random.uniform(-throttle_noise, throttle_noise) control.throttle = autopilot.throttle + throttle_noise control.brake = autopilot.brake return control
def _get_XBOX_control(self, joystick): control = VehicleControl() control.throttle = 0.4 * (joystick.get_axis(5) + 1) if control.throttle > 0.6: control.throttle = 0.6 # if control.throttle <0.3: # control.throttle = 0 # elif control.throttle<0.7: # control.throttle = 0.5 # else: # control.throttle =1.0 control.brake = 0.5 * (joystick.get_axis(2) + 1) control.brake = max(0, control.brake - 0.1) control.steer = joystick.get_axis(0) if (abs(control.steer) < 0.05): control.steer = 0 if control.steer <= -0.05: control.steer += 0.05 if control.steer >= 0.05: control.steer -= 0.05 control.steer = 0.8 * control.steer control.reverse = self._is_on_reverse # command = joystick.get_hat(0) # if command[0] == -1: # self._command = 3 # elif command[0] == 1: # self._command = 4 # if command[1] == -1: # self._command = 5 # elif command[1] == 1: # self._command = 2 # return control if joystick.get_axis(3) > 0.5: self._command = 4 elif joystick.get_axis(3) < -0.5: self._command = 3 elif joystick.get_axis(4) > 0.5: self._command = 5 elif joystick.get_axis(4) < -0.5: self._command = 2 return control
def get_control_from_a(a): control = VehicleControl() control.steer = a[0] control.throttle = a[1] control.brake = a[2] control.hand_brake = bool(a[3]) control.reverse = bool(a[4]) return control
def _take_action(self, action_idx): if not self.is_game_setup: print("Reset the environment duh by reset() before calling step()") sys.exit(1) if type(action_idx) == np.int64 or type(action_idx) == np.int: action = self.actions[action_idx] else: action = action_idx self.control = VehicleControl() if len(action) == 3: self.control.throttle = np.clip(action[1], 0, 1) self.control.steer = np.clip(action[0], -1, 1) self.control.brake = np.clip(action[2], 0, 1) else: self.control.throttle = np.clip(action[0], 0, 1) self.control.steer = np.clip(action[1], -1, 1) self.control.brake = np.abs(np.clip(action[0], -1, 0)) if not self.allow_braking: self.control.brake = 0 self.control.hand_brake = False self.control.reverse = False controls_sent = False while not controls_sent: try: self.game.send_control(self.control) controls_sent = True # print(self.control) # # # rand = random.randint(0, 4) # if rand == 0 and self.first_debug: # self.first_debug = False # raise Exception except: print( "Connection to server lost while sending controls. Reconnecting..........." ) import psutil info = psutil.virtual_memory() print("memory used", str(info.percent)) self.close_client_and_server() self.setup_client_and_server(reconnect_client_only=False) scene = self.game.load_settings(self.cur_experiment.conditions) self.positions = scene.player_start_spots self.start_point = self.positions[self.pose[0]] self.end_point = self.positions[self.pose[1]] self.game.start_episode(self.pose[0]) self.done = True controls_sent = False return
def _get_keyboard_control(self, keys): """ Return a VehicleControl message based on the pressed keys. Return None if a new episode was requested. """ if keys[K_r]: return None control = VehicleControl() self.resetVal() ################################################### YOU CAN EDIT IT ACCORDING YOU.... if keys[K_LEFT] or keys[K_a]: self._val1 = -1 print("Left") if (pid.prev > 0): pid.prev = 0 elif keys[K_RIGHT] or keys[K_d]: self._val1 = 1 print("Right") if (pid.prev < 0): pid.prev = 0 pid.prev += self._val1 output = pid.kp * self._val1 + pid.ki * pid.prev print(output) control.steer = output #Imp Line if keys[K_UP] or keys[K_w]: self._val2 = 1 pid.prev = 0 elif keys[K_DOWN] or keys[K_s]: control.brake = 1 pid.prev = 0 ### if (self._velocity < 77): control.throttle = self._val2 #Imp Line else: control.throttle = self._val2 * 0.8 if keys[K_SPACE]: control.hand_brake = True pid.prev = 0 if keys[K_f]: self._val3 = 1 - self._val3 if keys[K_q]: self._is_on_reverse = not self._is_on_reverse pid.prev = 0 if keys[K_p]: self._enable_autopilot = not self._enable_autopilot control.reverse = self._is_on_reverse ################################################################################ #print(control) return control
def _get_XBOX_control(self, joystick): control = VehicleControl() control.throttle = 0.4 * (joystick.get_axis(5) + 1) if control.throttle > 0.6: control.throttle = 0.6 # if control.throttle <0.3: # control.throttle = 0 # elif control.throttle<0.7: # control.throttle = 0.5 # else: # control.throttle =1.0 control.brake = 0.5 * (joystick.get_axis(2) + 1) control.brake = max(0, control.brake - 0.1) control.steer = joystick.get_axis(0) if(abs(control.steer)<0.05): control.steer = 0 if control.steer <=-0.05: control.steer += 0.05 if control.steer >=0.05: control.steer -= 0.05 control.steer = 0.8 * control.steer control.reverse = self._is_on_reverse # command = joystick.get_hat(0) # if command[0] == -1: # self._command = 3 # elif command[0] == 1: # self._command = 4 # if command[1] == -1: # self._command = 5 # elif command[1] == 1: # self._command = 2 # return control if joystick.get_axis(3) > 0.5: self._command = 4 elif joystick.get_axis(3) < -0.5: self._command = 3 elif joystick.get_axis(4) > 0.5: self._command = 5 elif joystick.get_axis(4) < -0.5: self._command = 2 return control
def run_step(self, measurements, sensor_data, directions, target): control = VehicleControl() control.throttle = 0.9 return control