def run(self): # Setup environment lgsvl_sim = self.simConnection.connect() control = lgsvl.NPCControl() ego_control = lgsvl.VehicleControl() # Placing the school_bus school_bus_state = spawn_state(lgsvl_sim) school_bus_state = CarControl.place_car_on_the_point( state=school_bus_state, sim=lgsvl_sim, point=self.npc_source) school_bus = load_npc(lgsvl_sim, "SchoolBus", school_bus_state) # Placing the ego on the starting point ego_state = spawn_state(lgsvl_sim) ego_state = CarControl.place_car_from_the_point(dimension="horizontal", distance=-6, state=ego_state) ego_state = CarControl.drive_ego_car(ego_state, [("vertical", self.ego_speed)]) ego = load_ego(lgsvl_sim, "Lincoln2017MKZ (Apollo 5.0)", ego_state) # Callback collision function ego.on_collision(self.on_collision) school_bus.on_collision(self.on_collision) # Set waypoints for School Bus waypoints = [] for point in [self.npc_source, self.npc_target]: waypoints.append( lgsvl.DriveWaypoint(point, self.npc_speed, school_bus.state.transform.rotation)) try: # Start the scenario # The School Bus is parked on the street control.headlights = 2 control.e_stop = True school_bus.apply_control(control) # Let the ego running for 2 seconds self.simConnection.execute(timeout=2) # The school bus turns on signal to prepare for the turn control.headlights = 0 # turn off headlight control.turn_signal_left = True school_bus.apply_control(control) self.simConnection.execute(timeout=2) # Brake the ego CarControl.brake_ego(ego=ego, control=ego_control, brake_value=self.ego_brake, sticky=True) # The school bus starts to turn right school_bus.follow(waypoints) self.simConnection.execute(timeout=10) except Exception: print("Failed!")
def test_set_lights_exceptions(self): with SimConnection() as sim: npc = self.create_NPC(sim, "Sedan") control = lgsvl.NPCControl() control.headlights = 2 npc.apply_control(control) with self.assertRaises(ValueError): control.headlights = 15 npc.apply_control(control)
def test_set_hazards_exceptions(self): with SimConnection() as sim: npc = self.create_NPC(sim, "HatchBack") control = lgsvl.NPCControl() control.hazards = 5 with self.assertRaises(TypeError) as e: npc.apply_control(control) self.assertIn("input a bool: True (on), False (off)", repr(e.exception))
def test_e_stop(self): with SimConnection() as sim: sim.add_agent("XE_Rigged-apollo_3_5", lgsvl.AgentType.EGO, spawnState(sim, 1)) npc = self.create_NPC(sim, "Jeep") npc.follow_closest_lane(True, 30) sim.run(2) self.assertGreater(npc.state.speed, 0) control = lgsvl.NPCControl() control.e_stop = True npc.apply_control(control) sim.run(2) self.assertAlmostEqual(npc.state.speed, 0, delta=0.01) control.e_stop = False npc.apply_control(control) sim.run(2) self.assertGreater(npc.state.speed, 0)
def test_set_lights_exceptions(self): with SimConnection() as sim: npc = self.create_NPC(sim, "Sedan") #npc.set_lights(2) control = lgsvl.NPCControl() control.headlights = 2 npc.apply_control(control) with self.assertRaises(TypeError) as e: control.headlights = 5.0 npc.apply_control(control) self.assertIn("input an integer: 0 (off), 1 (low), or 2 (high)", repr(e.exception)) with self.assertRaises(ValueError) as e: control.headlights = 15 npc.apply_control(control) self.assertIn("unsupported intensity value", repr(e.exception))
def test_e_stop(self): with SimConnection() as sim: npc = self.create_NPC(sim, "Jeep") npc.follow_closest_lane(True, 30) sim.run(2) self.assertGreater(npc.state.speed, 0) control = lgsvl.NPCControl() control.e_stop = True npc.apply_control(control) sim.run(2) self.assertAlmostEqual(npc.state.speed, 0, delta=4) control.e_stop = False npc.apply_control(control) sim.run(2) self.assertGreater(npc.state.speed, 0) with self.assertRaises(TypeError) as e: control.e_stop = 5 npc.apply_control(control) self.assertIn("input a bool: True (stop), False (continue)", repr(e.exception))
def test_e_stop(self): with SimConnection(60) as sim: state = lgsvl.AgentState() state.transform = sim.map_point_on_lane( lgsvl.Vector(78.962, -3.363, -40.292)) sim.add_agent(lgsvl.wise.DefaultAssets.ego_jaguar2015xe_apollo5, lgsvl.AgentType.EGO, state) forward = lgsvl.utils.transform_to_forward(state.transform) state.transform.position = state.position + 10 * forward npc = sim.add_agent("Jeep", lgsvl.AgentType.NPC, state) npc.follow_closest_lane(True, 10) sim.run(2) self.assertGreater(npc.state.speed, 0) control = lgsvl.NPCControl() control.e_stop = True npc.apply_control(control) sim.run(2) self.assertAlmostEqual(npc.state.speed, 0, delta=0.01) control.e_stop = False npc.apply_control(control) sim.run(2) self.assertGreater(npc.state.speed, 0)
# Run Simulator for at most 30 seconds for the AD stack to to initialize sim.run(30) # If a Control message was not received, then the AD stack is not ready and the scenario should not continue if not controlReceived: raise Exception("AD stack is not ready") sys.exit() # NPC will follow the HD map at a max speed of 11.176 m/s (25 mph) npc.follow_closest_lane(follow=True, max_speed=11.176) # Run Simulation for 10 seconds sim.run(10) # Force the NPC to come to a stop control = lgsvl.NPCControl() control.e_stop = True npc.apply_control(control) # Simulation will run for 10 seconds # NPC will be stopped for this time sim.run(10) # NPC told to resume following the HD map npc.follow_closest_lane(True, 11.176) # Simulation runs for another 10 seconds sim.run(10)