def __init__(self, params: Dict[str, Any]): """ carla_config: CarlaConfig, agent_config: AgentConfig, npc_agent_class, num_frames_per_step: int = 1, use_manual_control: bool = False Args: params: """ self.env_params = params carla_config: CarlaConfig = params["carla_config"] agent_config: AgentConfig = params["agent_config"] ego_agent_class = params.get("ego_agent_class", Agent) npc_agent_class = params.get("npc_agent_class", PurePursuitAgent) num_frames_per_step: int = params.get("num_frames_per_step", 1) # use_manual_control: bool = params.get("use_manual_control", False) self.max_collision_allowed: int = params.get("max_collision", 5) self.logger = logging.getLogger("ROAR Gym") self.agent_config = agent_config self.EgoAgentClass = ego_agent_class self.npc_agent_class = npc_agent_class self.carla_config = carla_config self.carla_runner = CarlaRunner(carla_settings=self.carla_config, agent_settings=self.agent_config, npc_agent_class=self.npc_agent_class) self.num_frames_per_step = num_frames_per_step self.agent: Optional[Agent] = None self.clock: Optional[pygame.time.Clock] = None self.action_space = None # overwrite in higher classes self.observation_space = None # overwrite in higher classes
def reset(self) -> Any: self.carla_runner.on_finish() self.carla_runner = CarlaRunner(agent_settings=self.agent_config, carla_settings=self.carla_config, npc_agent_class=self.npc_agent_class) vehicle = self.carla_runner.set_carla_world() self.agent = self.EgoAgentClass(vehicle=vehicle, agent_settings=self.agent_config, kwargs=self.env_params) self.clock: Optional[pygame.time.Clock] = None self._start_game() return self._get_obs()
def run(agent_class, agent_config_file_path: Path, carla_config_file_path: Path, num_laps: int = 10) -> Tuple[float, int, int]: """ Run the agent along the track and produce a score based on certain metrics Args: num_laps: int number of laps that the agent should run agent_class: the participant's agent agent_config_file_path: agent configuration path carla_config_file_path: carla configuration path Returns: float between 0 - 1 representing scores """ agent_config: AgentConfig = AgentConfig.parse_file(agent_config_file_path) carla_config = CarlaConfig.parse_file(carla_config_file_path) # hard code agent config such that it reflect competition requirements agent_config.num_laps = num_laps carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent, competition_mode=True, lap_count=num_laps) try: my_vehicle = carla_runner.set_carla_world() agent = agent_class(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=False) return compute_score(carla_runner) except Exception as e: print(f"something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server") return 0, 0, False
def run(agent_class, agent_config_file_path: Path, carla_config_file_path: Path) -> Tuple[float, int, bool]: """ Run the agent along the track and produce a score based on certain metrics Args: agent_class: the participant's agent agent_config_file_path: agent configuration path carla_config_file_path: carla configuration path Returns: float between 0 - 1 representing scores """ agent_config = AgentConfig.parse_file(agent_config_file_path) carla_config = CarlaConfig.parse_file(carla_config_file_path) carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent, competition_mode=True, max_collision=3) try: my_vehicle = carla_runner.set_carla_world() agent = agent_class(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=True) return compute_score(carla_runner) except Exception as e: print(f"something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server") return 0, 0, False
def main(): agent_config = AgentConfig.parse_file( Path("./ROAR_Sim/configurations/agent_configuration.json")) carla_config = CarlaConfig.parse_file( Path("./ROAR_Sim/configurations/configuration.json")) carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent) ''' Data collection code. Currently unnecessary # make csv file to store some data in # we have current position x, y, z, current velocity x, y, z, next waypoint position x, y, z, # next waypoint direction relative to the current position of the car x, y, z, steering, and throttle csvNotes = "{}\n{}\n".format("we have current car position x, y, z, current car velocity x, y, z, next waypoint position x, y, z,", "next waypoint direction relative to the current position of the car x, y, z, steering, and throttle") csvHeader = "px, py, pz, vx, vy, vz, wpx, wpy, wpz, wvx, wvy, wvz, steering, throttle\n" with open("tmp/pid_data.csv", "w") as f: f.write(csvNotes) f.write(csvHeader) ''' try: my_vehicle = carla_runner.set_carla_world() # agent = PIDAgent(vehicle=my_vehicle, agent_settings=agent_config) agent = LQRAgent(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=False) except Exception as e: logging.error(f"Something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server")
def main(): agent_config = AgentConfig.parse_file(Path("../ROAR/configurations/carla_configuration.json")) carla_config = CarlaConfig.parse_file(Path("../ROAR_Sim/configurations/carla_configuration.json")) carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent) try: my_vehicle = carla_runner.set_carla_world() agent = PointCloudMapRecordingAgent(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=False) except Exception as e: print("Ending abnormally: ", e) carla_runner.on_finish() logging.error(f"Hint: Might be a good idea to restart Server. ")
def main(): agent_config = AgentConfig.parse_file( Path("./ROAR_Sim/configurations/agent_configuration.json")) carla_config = CarlaConfig.parse_file( Path("./ROAR_Sim/configurations/configuration.json")) carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent) try: my_vehicle = carla_runner.set_carla_world() agent = PIDAgent(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=False) except Exception as e: logging.error(f"Something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server")
def main(): """Starts game loop""" agent_config = AgentConfig.parse_file( Path("./ROAR_Sim/configurations/agent_configuration.json")) carla_config = CarlaConfig.parse_file( Path("./ROAR_Sim/configurations/configuration.json")) carla_runner = CarlaRunner(carla_settings=carla_config, agent_settings=agent_config, npc_agent_class=PurePursuitAgent) try: my_vehicle = carla_runner.set_carla_world() #agent = PIDAgent(vehicle=my_vehicle, agent_settings=agent_config) #agent = OccupancyMapAgent(vehicle=my_vehicle, agent_settings=agent_config) #agent = PurePursuitAgent(vehicle=my_vehicle, agent_settings=agent_config) #agent = JAM1Agent_old(vehicle=my_vehicle, agent_settings=agent_config) # *** roll controller agent = PIDRollAgent( vehicle=my_vehicle, agent_settings=agent_config) # *** roll controller #agent = JAM1Agent(vehicle=my_vehicle, agent_settings=agent_config) #agent = JAM2Agent(vehicle=my_vehicle, agent_settings=agent_config) #agent = JAM3Agent_old(vehicle=my_vehicle, agent_settings=agent_config) # *** bstanley #agent = JAM3Agent(vehicle=my_vehicle, agent_settings=agent_config) # *** use to record new waypoints *** # waypointrecord = agent.bstanley_controller.blat_stanley_controller.waypointrecord # np.save("James_waypoints", np.array(waypointrecord)) #agent = RecordingAgent(vehicle=my_vehicle, agent_settings=agent_config) #carla_runner.start_game_loop(agent=agent, use_manual_control=True)#*******True for manual control, False auto carla_runner.start_game_loop( agent=agent, use_manual_control=False ) # *******True for manual control, False auto except Exception as e: logging.error(f"Something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server")
class ROAREnv(gym.Env, ABC): def __init__(self, params: Dict[str, Any]): """ carla_config: CarlaConfig, agent_config: AgentConfig, npc_agent_class, num_frames_per_step: int = 1, use_manual_control: bool = False Args: params: """ self.env_params = params carla_config: CarlaConfig = params["carla_config"] agent_config: AgentConfig = params["agent_config"] ego_agent_class = params.get("ego_agent_class", Agent) npc_agent_class = params.get("npc_agent_class", PurePursuitAgent) num_frames_per_step: int = params.get("num_frames_per_step", 1) # use_manual_control: bool = params.get("use_manual_control", False) self.max_collision_allowed: int = params.get("max_collision", 5) self.logger = logging.getLogger("ROAR Gym") self.agent_config = agent_config self.EgoAgentClass = ego_agent_class self.npc_agent_class = npc_agent_class self.carla_config = carla_config self.carla_runner = CarlaRunner(carla_settings=self.carla_config, agent_settings=self.agent_config, npc_agent_class=self.npc_agent_class) self.num_frames_per_step = num_frames_per_step self.agent: Optional[Agent] = None self.clock: Optional[pygame.time.Clock] = None self.action_space = None # overwrite in higher classes self.observation_space = None # overwrite in higher classes @abstractmethod def step(self, action: Any) -> Tuple[Any, float, bool, dict]: """ This provides an example implementation of step, intended to be overwritten Args: action: Any Returns: Tuple of Observation, reward, is done, other information """ self.clock.tick_busy_loop(60) should_continue, carla_control = self.carla_runner.controller.parse_events( client=self.carla_runner.client, world=self.carla_runner.world, clock=self.clock) self.carla_runner.world.tick(self.clock) self.carla_runner.fetch_data_async() sensor_data, new_vehicle = self.carla_runner.sensor_data.copy( ), self.carla_runner.vehicle_state.copy() if self.carla_runner.agent_settings.enable_autopilot: if self.agent is None: raise Exception("In autopilot mode, but no agent is defined.") agent_control = self.agent.run_step(vehicle=new_vehicle, sensors_data=sensor_data) carla_control = self.carla_runner.carla_bridge.convert_control_from_agent_to_source( agent_control) self.carla_runner.world.player.apply_control(carla_control) result = self._get_obs(), self.get_reward(), self._terminal( ), self._get_info() return result def reset(self) -> Any: if self.agent is not None: self.agent.shutdown_module_threads() self.carla_runner.on_finish() self.carla_runner = CarlaRunner(agent_settings=self.agent_config, carla_settings=self.carla_config, npc_agent_class=self.npc_agent_class) vehicle = self.carla_runner.set_carla_world() self.agent = self.EgoAgentClass(vehicle=vehicle, agent_settings=self.agent_config, kwargs=self.env_params) self.clock: Optional[pygame.time.Clock] = None self._start_game() return self._get_obs() def render(self, mode='ego'): self.carla_runner.world.render(display=self.carla_runner.display) pygame.display.flip() def _start_game(self): try: self.logger.debug("Initiating game") self.agent.start_module_threads() self.clock = pygame.time.Clock() self.start_simulation_time = self.carla_runner.world.hud.simulation_time self.start_vehicle_position = self.agent.vehicle.transform.location.to_array( ) except Exception as e: self.logger.error(e) @abstractmethod def get_reward(self) -> float: """ Intended to be overwritten Returns: """ raise NotImplementedError def _terminal(self) -> bool: if self.carla_runner.get_num_collision() > self.max_collision_allowed: return True return self.agent.is_done # TODO temporary, needs to be changed @abstractmethod def _get_info(self) -> dict: return dict() @abstractmethod def _get_obs(self) -> Any: raise NotImplementedError
def main(): """Starts game loop""" carla_config = CarlaConfig.parse_file(Path("./ROAR_Sim/configurations/configuration.json")) agent_config = AgentConfig.parse_file(Path("./ROAR_Sim/configurations/agent_configuration.json")) """ Pit Stop: Use different kinds of 'set' functions at PitStop to tune/fix your own car! """ pitstop = PitStop(carla_config, agent_config) pitstop.set_carla_version(version = "0.9.9") pitstop.set_carla_sync_mode(False) pitstop.set_autopilot_mode(True) pitstop.set_car_color(CarlaCarColor(r = 255,g = 200,b = 00,a = 255)) pitstop.set_num_laps(num=1) pitstop.set_output_data_folder_path("./data/output") pitstop.set_output_data_file_name(time.strftime("%Y%m%d-%H%M%S-") + "map-waypoints") pitstop.set_max_speed(speed = 200) pitstop.set_target_speed(speed = 30) print(agent_config.target_speed, " target speed") pitstop.set_steering_boundary(boundary = (-1.0, 1.0)) pitstop.set_throttle_boundary(boundary = (0, 0.5)) pitstop.set_waypoints_look_ahead_values(values={ "60": 5, "80": 10, "120": 20, "180": 50}) pid_value = { "longitudinal_controller": { "40": { "Kp": 0.8, "Kd": 0.2, "Ki": 0 }, "60": { "Kp": 0.5, "Kd": 0.2, "Ki": 0 }, "150": { "Kp": 0.2, "Kd": 0.1, "Ki": 0.1 } }, "latitudinal_controller": { "60": { "Kp": 0.8, "Kd": 0.1, "Ki": 0.1 }, "100": { "Kp": 0.6, "Kd": 0.2, "Ki": 0.1 }, "150": { "Kp": 0.5, "Kd": 0.2, "Ki": 0.1 } } } pitstop.set_pid_values(pid_value) """Passing configurations to Carla and Agent""" carla_runner = CarlaRunner(carla_settings=carla_config, # ROAR Academy: fine agent_settings=agent_config, # ROAR Academy: fine npc_agent_class=PurePursuitAgent) try: my_vehicle = carla_runner.set_carla_world() agent = PIDAgent(vehicle=my_vehicle, agent_settings=agent_config) # agent = WaypointGeneratigAgent(vehicle=my_vehicle, agent_settings=agent_config) carla_runner.start_game_loop(agent=agent, use_manual_control=False) # for PIDAgent # carla_runner.start_game_loop(agent=agent, use_manual_control=True) # for WaypointGeneratingAgent except Exception as e: logging.error(f"Something bad happened during initialization: {e}") carla_runner.on_finish() logging.error(f"{e}. Might be a good idea to restart Server")