def setup_cars(sim_connection: SimConnection, ego_speed: float):
    lgsvl_sim = sim_connection.connect()
    # Placing the sedan
    sedan_state = spawn_state(lgsvl_sim)
    sedan_state = CarControl.place_car_from_the_point(dimension="vertical",
                                                      distance=15,
                                                      state=sedan_state)
    sedan_state = CarControl.place_car_from_the_point(dimension="horizontal",
                                                      distance=-8,
                                                      state=sedan_state)
    sedan_state = CarControl.rotate_car_by_degree(state=sedan_state,
                                                  degree=-45)
    sedan = load_npc(lgsvl_sim, "Sedan", sedan_state)

    # Placing the ego on the starting point
    ego_state = spawn_state(lgsvl_sim)
    ego_state = CarControl.drive_ego_car(ego_state, [("vertical", ego_speed)])
    ego = load_ego(lgsvl_sim, "Lincoln2017MKZ (Apollo 5.0)", ego_state)
    return {
        "sedan": sedan,
        "ego": ego,
    }
Example #2
0
    def test_EGO_exit_park_with_incoming_NPC(self):
        NPC_START = lgsvl.geometry.Vector(120, -0.0120140314102173, -2)
        NPC_TARGET = lgsvl.geometry.Vector(60, -0.0121138095855713, -2)
        NPC_SPEED = 4
        VEHICLE_SET = [
            {
                "name": "Sedan",
                "load_vehicle": load_npc,
                "distance": 0,
                "type": "Sedan"
            },
            {
                "name": "Ego",
                "load_vehicle": load_ego,
                "distance": 10,
                "type": "Lincoln2017MKZ (Apollo 5.0)"
            },
            {
                "name": "SUV",
                "load_vehicle": load_npc,
                "distance": 5,
                "type": "SUV"
            },
            {
                "name": "Jeep",
                "load_vehicle": load_npc,
                "distance": 20,
                "type": "Jeep"
            },
        ]
        # Setup environment
        sim_connection = SimConnection()
        lgsvl_sim = sim_connection.connect()
        scenario = Scenario(sim_connection)
        # Setup vehicles position
        sedan, ego, suv, jeep = scenario.generate_vehicles(
            lgsvl_sim, VEHICLE_SET)
        # Setup a new NPC
        NPC_state = spawn_state(lgsvl_sim)
        NPC_state = CarControl.place_car_on_the_point(sim=lgsvl_sim,
                                                      point=NPC_START,
                                                      state=NPC_state)
        NPC_state = CarControl.rotate_car_by_degree(state=NPC_state,
                                                    degree=-90)
        NPC = load_npc(lgsvl_sim, "Sedan", NPC_state)
        NPC.on_collision(scenario.on_collision)
        waypointsCommand = [
            lgsvl.DriveWaypoint(NPC_START, NPC_SPEED,
                                NPC_state.transform.rotation),
            lgsvl.DriveWaypoint(NPC_TARGET, NPC_SPEED,
                                NPC_state.transform.rotation)
        ]

        # Delay the scenario for 2s
        sim_connection.execute(timeout=2)
        try:
            NPC.follow(waypointsCommand)
            scenario.drive_ego(sim_connection, ego)
        except Exception:
            sim_connection.sim.close()
            self.fail("Failed!")

        # Passed!
        sim_connection.sim.close()
        self.assertTrue(True, True)
        'Localization', 'Transform', 'Routing', 'Prediction', 'Planning',
        'Control'
    ]
    destination = spawns[0].destinations[0]
    dv.setup_apollo(destination.position.x, destination.position.z, modules)
    endOfRoad = destination

    POVState = lgsvl.AgentState()
    sedan_state = POVState
    sedan_state = CarControl.place_car_from_the_point(dimension="vertical",
                                                      distance=25,
                                                      state=sedan_state)
    sedan_state = CarControl.place_car_from_the_point(dimension="horizontal",
                                                      distance=-3,
                                                      state=sedan_state)
    sedan_state = CarControl.rotate_car_by_degree(state=sedan_state,
                                                  degree=180)
    POV = sim.add_agent("Sedan", lgsvl.AgentType.NPC, sedan_state)

    def on_collision(agent1, agent2, contact):
        raise lgsvl.evaluator.TestException(
            "Ego collided with {}".format(agent2))

    ego.on_collision(on_collision)
    POV.on_collision(on_collision)

    try:
        t0 = time.time()
        sim.run(5)  # The EGO should start moving first
        POV.follow_closest_lane(True, MAX_POV_SPEED, False)
        while True:
            sim.run(0.5)
    def test_EGO_encroach_NPC_speed_20_with_apollo(self):
        LGSVL__APOLLO_HOST = config("LGSVL__APOLLO_HOST")
        LGSVL__APOLLO_PORT = int(config("LGSVL__APOLLO_PORT"))
        LGSVL__DREAMVIEW_HOST = config("LGSVL__DREAMVIEW_HOST")
        MODULES = [
            'Recorder', 'Localization', 'Perception', 'Transform', 'Routing',
            'Prediction', 'Planning', 'Traffic Light', 'Control'
        ]
        TARGET = Vector(4.85003185272217, -0.0120296478271484,
                        22.7699680328369)
        COLLISIONS = []
        SEDAN_WPS = [
            Vector(-3.15000438690186, 0, 37.7700042724609),
            Vector(4.85003137588501, -0.0120288729667664, 22.7699680328369)
        ]
        NPC_SPEED = 10

        sim_connection = SimConnection(scene="CubeTown")
        lgsvl_sim = sim_connection.connect()
        # Placing the sedan
        sedan_state = spawn_state(lgsvl_sim)
        sedan_state = CarControl.place_car_from_the_point(dimension="vertical",
                                                          distance=15,
                                                          state=sedan_state)
        sedan_state = CarControl.place_car_from_the_point(
            dimension="horizontal", distance=-8, state=sedan_state)
        sedan_state = CarControl.rotate_car_by_degree(state=sedan_state,
                                                      degree=-45)
        sedan = load_npc(lgsvl_sim, "Sedan", sedan_state)

        # Placing the ego on the starting point
        ego_state = spawn_state(lgsvl_sim)
        # ego_state = CarControl.drive_ego_car(ego_state, [("vertical", 3)])
        ego = load_ego(lgsvl_sim, "Lincoln2017MKZ (Apollo 5.0)", ego_state)

        def on_collision(agent1, agent2, contact):
            COLLISIONS.append([agent1, agent2, contact])
            sim_connection.sim.close()
            print("Exception: {} collided with {}".format(agent1, agent2))

        ego.on_collision(on_collision)
        sedan.on_collision(on_collision)

        try:
            ego.connect_bridge(LGSVL__APOLLO_HOST, LGSVL__APOLLO_PORT)
            dv = lgsvl.dreamview.Connection(lgsvl_sim, ego,
                                            LGSVL__DREAMVIEW_HOST)
            dv.set_hd_map("CubeTown")
            dv.set_vehicle("Lincoln2017MKZ (Apollo 5.0)")
            dv.setup_apollo(TARGET.x, TARGET.z, MODULES)
            # Start the scenario
            waypoints = []
            for point in SEDAN_WPS:
                waypoints.append(
                    lgsvl.DriveWaypoint(point, NPC_SPEED,
                                        sedan.state.transform.rotation))

            sedan.follow(waypoints)
            sim_connection.execute(timeout=15)
        except Exception:
            sim_connection.sim.close()
            self.fail("Failed!")

        sim_connection.sim.close()
        self.assertTrue(True, True)