def load_scene(sim: Simulator, scene_name: str) -> Transform: if sim.current_scene == scene_name: sim.reset() else: sim.load(scene_name, 650387) return sim.get_spawn()[0]
def execute(sim: Simulator, config: TestConfig) -> Optional[bool]: import logging from common.scene import load_ego, load_scene, load_pedestrian, detect_collisions from lgsvl.agent import Agent from time import time load_scene(sim, config.test_place.map_name) pedestrian_behavior = TestCase6._generate_initial_pedestrian_behavior( config.test_place, config.pedestrian_direction) pedestrian = load_pedestrian(sim, config.test_place.map_name, pedestrian_behavior.initial_state) pedestrian.follow(pedestrian_behavior.waypoints) ego_state_result = TestCase6._generate_initial_ego_state( pedestrian_behavior, config.ego_distance, config.test_place, config.ego_speed, config.pedestrian_direction) if ego_state_result is None: logging.warning( "Could not generate initial ego state within allowed parameter range" ) return None initial_ego_state, time_to_crash_point = ego_state_result ego = load_ego(sim, config.ego_car_name, initial_ego_state) test_result = _TestResult() def _on_collision(agent1: Agent, agent2: Agent, contact: Vector) -> None: test_result.successful = False detect_collisions(sim, _on_collision) start_time = time() running_simulation_failed_once = False while (time() - start_time) < (time_to_crash_point * 2) and test_result.successful: try: sim.run(1) if running_simulation_failed_once: logging.info( "Continuing the execution failed once but could be restarted" ) running_simulation_failed_once = False except Exception: if running_simulation_failed_once: logging.exception( "Continuing the execution failed again. Skipping test." ) return None else: logging.exception( "Continuing the execution failed. Retrying once.") running_simulation_failed_once = True # FIXME Sometimes the following print is not visible on the console (but being in debug mode) return test_result.successful
class SimConnection: def __init__(self, scene: str = "SingleLaneRoad", load_scene: bool = True): self.scene = scene self.load_scene = load_scene def execute(self, vehicles: Optional[list] = [], timeout: float = 10, debug: bool = False): if debug and len(vehicles) == 0: raise Exception("Debug Mode requires some vehicles to print the log!") time_point = 1 t0 = time.time() if debug: self.debug_vehicles(f'Step {time_point}', vehicles) while True: self.sim.run(1) time_point += 1 if debug: self.debug_vehicles(f'Step {time_point}', vehicles) if time.time() - t0 > timeout: break def debug_vehicles(self, message: str, vehicles: list): print(f'{message}:') for vehicle in vehicles: print(self.extract_position_from_state(vehicle.state)) @staticmethod def extract_position_from_state(state: lgsvl.AgentState) -> Tuple[float, float, float]: return state.position.x, state.position.y, state.position.z @staticmethod def extract_rotation_from_state(state: lgsvl.AgentState) -> Tuple[float, float, float]: return state.rotation.x, state.rotation.y, state.rotation.z @staticmethod def connect_simulation(host: str, port: int) -> Simulator: return Simulator(address=host, port=port) def connect(self): self.sim = Simulator(config("LGSVL__SIMULATOR_HOST", default="127.0.0.1"), int(config("LGSVL__SIMULATOR_PORT", default=8181))) if self.load_scene: load_scene(self.sim, self.scene) return self.sim def __enter__(self): return self.connect() def __exit__(self, exc_type, exc_val, exc_tb): self.sim.close()
def detect_collisions( sim: Simulator, on_collision_detected: Callable[[Agent, Agent, Vector], None] = _default_on_collision ) -> None: """ NOTE This method should only be called after all agents were added to the given simulation. NOTE If the collision callback is not registered for ALL agents of the detected collision the simulation hangs. """ for agent in sim.get_agents(): agent.on_collision(on_collision_detected)
def load_pedestrian(sim: Simulator, map_name: str, initial_state: AgentState) -> Pedestrian: from lgsvl import AgentType import random # The list of available pedestrians can be found in the maps PedestrianManager prefab # NOTE The availability of pedestrian names may depend on the currently used map allowed_names = [ "Bob", "EntrepreneurFemale", "Howard", "Johny", "Pamela", "Presley", "Robin", "Stephen", "Zoe" ] return sim.add_agent(random.choice(allowed_names), AgentType.PEDESTRIAN, initial_state)
def connect(self): self.sim = Simulator(config("LGSVL__SIMULATOR_HOST", default="127.0.0.1"), int(config("LGSVL__SIMULATOR_PORT", default=8181))) if self.load_scene: load_scene(self.sim, self.scene) return self.sim
def connect_simulation(host: str, port: int) -> Simulator: return Simulator(address=host, port=port)
def place_car_on_the_point(state: lgsvl.AgentState, point: lgsvl.Vector, sim: lgsvl.Simulator) -> lgsvl.AgentState: state.transform = sim.map_point_on_lane(point) return state
def add_random_traffic(sim: Simulator) -> None: from lgsvl import AgentType sim.add_random_agents(AgentType.NPC) sim.add_random_agents(AgentType.PEDESTRIAN)
def load_npc(sim: Simulator, NPC_car_name: str, initial_state: AgentState) -> Agent: from lgsvl import AgentType # name: Sedan, SUV, Jeep, Hatchback, SchoolBus, BoxTruck return sim.add_agent(NPC_car_name, AgentType.NPC, initial_state)
def load_ego(sim: Simulator, ego_car_name: str, initial_state: AgentState) -> EgoVehicle: from lgsvl import AgentType return sim.add_agent(ego_car_name, AgentType.EGO, initial_state)