Example #1
0
def world_loop(opts_dict):
    params = {
            'spawn': 15,
            'weather': 'clear_noon',
            'n_vehicles': 0
            }

    with cu.CarlaWrapper(TOWN, cu.VEHICLE_NAME, PORT) as env:
        env.init(**params)
        agent = RoamingAgent(env._player, False, opts_dict)

        # Hack: fill up controller experience.
        for _ in range(30):
            env.tick()
            env.apply_control(agent.run_step()[0])

        for _ in tqdm.tqdm(range(125)):
            env.tick()

            observations = env.get_observations()
            inputs = cu.get_inputs(observations)

            debug = dict()
            control, command = agent.run_step(inputs, debug_info=debug)
            env.apply_control(control)

            observations.update({'control': control, 'command': command})

            processed = cu.process(observations)

            yield debug

            bzu.show_image('rgb', processed['rgb'])
            bzu.show_image('birdview', cu.visualize_birdview(processed['birdview']))
Example #2
0
def game_loop(args):
    pygame.init()
    pygame.font.init()
    world = None

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)
        #world=client.load_world('Town02')
        #print(client.get_available_maps())

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args.filter)
        controller = KeyboardControl(world, False)
        rgb_manager = CameraManager(world.player, hud)
        frame_manager = CameraManager(world.player, hud)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        else:
            agent = BasicAgent(world.player)
            spawn_point = world.map.get_spawn_points()[0]
            agent.set_destination(
                (spawn_point.location.x, spawn_point.location.y,
                 spawn_point.location.z))
        rgb_manager.set_sensor(0, True)
        frame_manager.set_sensor_frame(0, True)
        rgb_manager.toggle_camera()
        frame_manager.toggle_camera_frame()
        rgb_manager.toggle_recording()
        frame_manager.toggle_recording_frame()

        clock = pygame.time.Clock()
        while True:
            if controller.parse_events(client, world, clock):
                return

            # as soon as the server is ready continue!
            if not world.world.wait_for_tick(10.0):
                continue

            world.tick(clock)
            world.render(display)
            pygame.display.flip()
            control = agent.run_step()
            control.manual_gear_shift = False
            world.player.apply_control(control)

    finally:
        world.destroy_sensors()
        if world is not None:
            world.destroy()

        pygame.quit()
def game_loop(args):
    pygame.init()
    pygame.font.init()
    world = None

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode(
            (args.width, args.height),
            pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args.filter)
        controller = KeyboardControl(world, False)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        else:
            agent = BasicAgent(world.player)
            list = world.map.get_spawn_points()
            if list == []:
                spawn_point = carla.Transform()
            else:
                spawn_point = list[0]
            agent.set_destination((spawn_point.location.x,
                                   spawn_point.location.y,
                                   spawn_point.location.z))

        clock = pygame.time.Clock()
        while True:
            if controller.parse_events(client, world, clock):
                return

            # as soon as the server is ready continue!
            if not world.world.wait_for_tick(10.0):
                continue

            world.tick(clock)
            world.render(display)
            pygame.display.flip()
            control = agent.run_step()
            control.manual_gear_shift = False
            world.player.apply_control(control)

    finally:
        if world is not None:
            world.destroy()

        pygame.quit()
def game_loop(args):
    """ Main loop for agent"""

    pygame.init()
    pygame.font.init()
    world = None
    tot_target_reached = 0
    num_min_waypoints = 21
    rand_int = np.random.randint(100)

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        client.load_world('Town0%d' % args.town)
        client.reload_world()

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args)
        controller = KeyboardControl(world)

        # map_dir = "/home/yash/Downloads/CARLA_0.9.9/PythonAPI/examples/Recordings_%d/" % args.town
        map_dir = "D:\\CARLA\\WindowsNoEditor\\PythonAPI\\examples\\Recordings_%d\\" % args.town
        map_obj = ParseCarlaMap(map_dir)
        points = [46, 81, 132, 10, 40, 14, 110, 71, 58, 4]

        edges = list(permutations(points, 2))
        edge_table = np.zeros(shape=(len(edges), 3), dtype=np.float32)
        edge_table[:, 0:2] = edges

        for index in range(edge_table.shape[0]):
            spawn_index, dest_index = edge_table[index, 0:2].astype(np.int)
            spawn_loc = carla.Location(
                x=map_obj.node_dict["location"]["x"][spawn_index],
                y=map_obj.node_dict["location"]["y"][spawn_index],
                z=map_obj.node_dict["location"]["z"][spawn_index])
            spawn_rot = carla.Rotation(
                roll=map_obj.node_dict["rotation"]["roll"][spawn_index],
                pitch=map_obj.node_dict["rotation"]["pitch"][spawn_index],
                yaw=map_obj.node_dict["rotation"]["yaw"][spawn_index])
            spawn_pt = carla.Transform(location=spawn_loc, rotation=spawn_rot)

            dest_loc = carla.Location(
                x=map_obj.node_dict["location"]["x"][dest_index],
                y=map_obj.node_dict["location"]["y"][dest_index],
                z=map_obj.node_dict["location"]["z"][dest_index])
            # dest_rot = carla.Rotation(roll=map_obj.node_dict["rotation"]["roll"][dest_index],
            #                            pitch=map_obj.node_dict["rotation"]["pitch"][dest_index],
            #                            yaw=map_obj.node_dict["rotation"]["yaw"][dest_index])
            # dest_pt = carla.Transform(location=dest_loc, rotation=dest_rot)

            world.restart(args, spawn_pt=spawn_pt)

            if args.agent == "Roaming":
                agent = RoamingAgent(world.player)
            elif args.agent == "Basic":
                agent = BasicAgent(world.player)
                spawn_point = world.map.get_spawn_points()[0]
                agent.set_destination(
                    (spawn_point.location.x, spawn_point.location.y,
                     spawn_point.location.z))
            else:
                agent = BehaviorAgent(
                    world.player,
                    ignore_traffic_light=args.ignore_traffic_light,
                    behavior=args.behavior)
                agent.set_destination(agent.vehicle.get_location(),
                                      dest_loc,
                                      clean=True)

            clock = pygame.time.Clock()
            start_time = time.time()
            while True:
                clock.tick_busy_loop(60)
                if controller.parse_events():
                    return

                # As soon as the server is ready continue!
                if not world.world.wait_for_tick(10.0):
                    continue

                if args.agent == "Roaming" or args.agent == "Basic":
                    if controller.parse_events():
                        return

                    # as soon as the server is ready continue!
                    world.world.wait_for_tick(10.0)

                    world.tick(clock)
                    world.render(display)
                    pygame.display.flip()
                    control = agent.run_step()
                    control.manual_gear_shift = False
                    world.player.apply_control(control)
                else:
                    agent.update_information(world)

                    world.tick(clock)
                    world.render(display)
                    pygame.display.flip()

                    # Set new destination when target has been reached
                    if len(agent.get_local_planner().waypoints_queue
                           ) < num_min_waypoints and args.loop:
                        agent.reroute(spawn_points)
                        tot_target_reached += 1
                        world.hud.notification("The target has been reached " +
                                               str(tot_target_reached) +
                                               " times.",
                                               seconds=4.0)

                    elif len(agent.get_local_planner().waypoints_queue
                             ) == 0 and not args.loop:
                        print("Target reached, mission accomplished...")
                        break

                    speed_limit = world.player.get_speed_limit()
                    agent.get_local_planner().set_speed(speed_limit)

                    control = agent.run_step()
                    world.player.apply_control(control)
                if time.time() - start_time >= 280:
                    break

            edge_table[index, 2] = time.time() - start_time
            print("edge %d was completed in %3.3f seconds" %
                  (index, edge_table[index, 2]))
            np.savetxt('Carla_0%d_%d.csv' % (args.town, rand_int),
                       edge_table,
                       fmt="%3d,%3d,%3.3f")

    finally:
        if world is not None:
            world.destroy()

        pygame.quit()
Example #5
0
def game_loop(args):
    """ Main loop for agent"""

    pygame.init()
    pygame.font.init()
    world = None
    original_settings = None
    tot_target_reached = 0
    num_min_waypoints = 21

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args)
        # make synchronous and disable keyboard controller BEGIN
        logging.info("make synchronous and disable keyboard controller")
        original_settings = world.world.get_settings()
        settings = world.world.get_settings()
        traffic_manager = client.get_trafficmanager(8000)
        traffic_manager.set_synchronous_mode(True)

        delta = 0.06

        settings.fixed_delta_seconds = delta
        settings.synchronous_mode = True
        world.world.apply_settings(settings)
        # END
        controller = KeyboardControl(world)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        elif args.agent == "Basic":
            agent = BasicAgent(world.player)
            spawn_point = world.map.get_spawn_points()[0]
            agent.set_destination(
                (spawn_point.location.x, spawn_point.location.y,
                 spawn_point.location.z))
        else:
            agent = BehaviorAgent(world.player, behavior=args.behavior)

            spawn_points = world.map.get_spawn_points()
            random.shuffle(spawn_points)

            if spawn_points[0].location != agent.vehicle.get_location():
                destination = spawn_points[0].location
            else:
                destination = spawn_points[1].location

            agent.set_destination(agent.vehicle.get_location(),
                                  destination,
                                  clean=True)

        clock = pygame.time.Clock()

        while True:
            clock.tick_busy_loop(60)
            world.world.tick()  # synchronous
            hud.on_world_tick(world.world.get_snapshot().timestamp)
            if controller.parse_events():
                return

            # As soon as the server is ready continue!
            # if not world.world.wait_for_tick(10.0):
            #     continue

            if args.agent == "Roaming" or args.agent == "Basic":
                if controller.parse_events():
                    return

                # as soon as the server is ready continue!
                # world.world.wait_for_tick(10.0)

                world.tick(clock)
                world.render(display)
                pygame.display.flip()
                control = agent.run_step()
                control.manual_gear_shift = False
                world.player.apply_control(control)
            else:
                agent.update_information()

                world.tick(clock)
                world.render(display)
                pygame.display.flip()

                # Set new destination when target has been reached
                if len(agent.get_local_planner().waypoints_queue
                       ) < num_min_waypoints and args.loop:
                    agent.reroute(spawn_points)
                    tot_target_reached += 1
                    world.hud.notification("The target has been reached " +
                                           str(tot_target_reached) + " times.",
                                           seconds=4.0)

                elif len(agent.get_local_planner().waypoints_queue
                         ) == 0 and not args.loop:
                    print("Target reached, mission accomplished...")
                    break

                speed_limit = world.player.get_speed_limit()
                agent.get_local_planner().set_speed(speed_limit)

                control = agent.run_step()
                world.player.apply_control(control)

    finally:
        if world is not None:
            if original_settings is not None:
                world.world.apply_settings(original_settings)
            world.destroy()

        pygame.quit()
Example #6
0
def game_loop(args):
    """ Main loop for agent"""

    pygame.init()
    pygame.font.init()
    world = None
    tot_target_reached = 0
    num_min_waypoints = 21

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args)
        controller = KeyboardControl(world)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        elif args.agent == "Basic":
            agent = BasicAgent(world.player)
            spawn_point = world.map.get_spawn_points()[0]
            agent.set_destination(
                (spawn_point.location.x, spawn_point.location.y,
                 spawn_point.location.z))
        else:
            agent = BehaviorAgent(world.player, behavior=args.behavior)

            spawn_points = world.map.get_spawn_points()
            random.shuffle(spawn_points)

            if spawn_points[0].location != agent.vehicle.get_location():
                destination = spawn_points[0].location
            else:
                destination = spawn_points[1].location

            agent.set_destination(agent.vehicle.get_location(),
                                  destination,
                                  clean=True)

        clock = pygame.time.Clock()

        while True:
            clock.tick_busy_loop(60)
            if controller.parse_events():
                return

            # As soon as the server is ready continue!
            if not world.world.wait_for_tick(10.0):
                continue

            if args.agent == "Roaming" or args.agent == "Basic":
                if controller.parse_events():
                    return

                # as soon as the server is ready continue!
                world.world.wait_for_tick(10.0)

                world.tick(clock)
                world.render(display)
                pygame.display.flip()
                control = agent.run_step()
                control.manual_gear_shift = False
                world.player.apply_control(control)
            else:
                agent.update_information()

                world.tick(clock)
                world.render(display)
                pygame.display.flip()
                # if world.save_count<5:
                #     world.camera_manager.image.save_to_disk('_out/%08d' % image.frame)
                #     world.save_count+=1

                # Set new destination when target has been reached
                if len(agent.get_local_planner().waypoints_queue
                       ) < num_min_waypoints and args.loop:
                    agent.reroute(spawn_points)
                    tot_target_reached += 1
                    world.hud.notification("The target has been reached " +
                                           str(tot_target_reached) + " times.",
                                           seconds=4.0)

                elif len(agent.get_local_planner().waypoints_queue
                         ) == 0 and not args.loop:
                    print("Target reached, mission accomplished...")
                    break

                speed_limit = world.player.get_speed_limit()
                agent.get_local_planner().set_speed(speed_limit)

                control = agent.run_step()
                world.player.apply_control(control)

    finally:
        if world is not None:
            world.destroy1()

        pygame.quit()
Example #7
0
def game_loop(args):
    pygame.init()
    pygame.font.init()
    world = None
    #f = open('location_list.txt', 'r')
    i = 0
    location_list = [
        [500, 5.25, 31.0, 0.0, 0.0],
        [
            475.4852470983795, 5.678740381744522, 30.0, 0.011942474433034074,
            9.481388061770087
        ],
        [
            472.1113589818001, 5.715332086040473, 29.0, 0.012105306942113502,
            9.012409139522038
        ],
        [
            454.0871193356091, 5.8597591238060405, 28.0, 0.012903765907572399,
            8.997941150502005
        ],
        [
            436.09148605811964, 5.765087408368869, 27.0, 0.013219133555865806,
            7.907664017308204
        ],
        [
            420.27927717255363, 5.450999838173674, 26.0, 0.012969219540954719,
            2.6903561539015364
        ],
        [
            414.90291419050664, 5.234699064670276, 25.0, 0.012616014874265873,
            5.500995187472245
        ],
        [
            403.90286894692605, 5.027825070288866, 24.0, 0.012447461468861111,
            9.710390691465745
        ],
        [
            384.48235362442995, 4.926168135884138, 23.0, 0.012811767710883288,
            9.985278899462706
        ],
        [
            364.51211033566875, 4.814088753707787, 22.0, 0.013206169974161696,
            7.8825923991263895
        ],
        [
            348.7487507701559, 5.053978168677228, 21.0, 0.014490731237345338,
            4.500000000000008
        ], [339.749, 4.987, 20.0, 0.014677429171226393, 10.078971599325023],
        [319.592, 5.182, 19.0, 0.016213002649283742, 11.495219267156243],
        [296.602, 5.324, 18.0, 0.017948052642080513, 13.122339625615528],
        [270.36, 4.949, 17.0, 0.018303178498518525, 11.423603229740003],
        [
            247.51286058561587, 5.0043496323322945, 16.0, 0.02021578951147572,
            4.500602163768276
        ],
        [
            238.5129217492751, 5.037530080270346, 15.0, 0.021117435478132073,
            12.529842286551135
        ],
        [
            213.45347683222624, 4.927933762933469, 14.0, 0.02308258733630485,
            4.5
        ],
        [
            204.45529975615295, 5.109067160265785, 13.0, 0.024983476288845008,
            4.499999999999997
        ], [195.457, 5.284, 12.0, 0.027027496118305478, 8.689527849659033],
        [178.078, 5.24, 11.0, 0.02941681979829673, 11.581500388550705],
        [154.915, 5.246, 10.0, 0.03385079618528805, 13.327193937584907],
        [128.262, 4.974, 9.0, 0.038760574456180405, 6.338593889815],
        [115.585, 5.043, 8.0, 0.04360257562440571, 9.88808981805889],
        [95.81, 4.827, 7.0, 0.050338400773856, 7.777515445179138],
        [80.255, 4.858, 6.0, 0.060458283762458445, 7.375550860783211],
        [65.506, 5.107, 5.0, 0.07780494272413684, 6.951037493288717],
        [
            51.60396193342049, 5.0749605275278284, 4.0, 0.09802917448443067,
            4.500000000000002
        ],
        [42.60499983592313, 4.938281458881817, 3.0, 0.11539357217913827, 4.5],
        [33.605, 4.94, 2.0, 0.14595657222297126, 8.715192998436693],
        [16.175, 5.056, 1.0, 0.30295879121347374, 8.088081679236431],
        [0, 5.25, 0.0, 0.0, 0.0]
    ]

    location_list.reverse()

    for location in location_list:
        location[0] += -315.8
        location[1] += 25

    print(location_list)

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args.filter)
        controller = KeyboardControl(world, False)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        else:
            agent = BasicAgent(world.player)
            spawn_point = carla.Location(x=-315.791, y=29.9778, z=4.17103)
            print(spawn_point)

            waypoint = world.map.get_waypoint(world.player.get_location())
            target = world.map.get_waypoint(
                carla.Location(location_list[i][0], location_list[i][1], 0))
            if waypoint.transform.location.distance(
                    carla.Location(location_list[i][0], location_list[i][1],
                                   0)) < 0.1:
                i += 1
                agent.set_destination(
                    (location_list[i][0], location_list[i][1], 0))
                print(1)

        clock = pygame.time.Clock()

        while True:
            if controller.parse_events(client, world, clock):
                return

            # as soon as the server is ready continue!
            if not world.world.wait_for_tick(10.0):
                continue

            world.tick(clock)
            world.render(display)
            pygame.display.flip()
            control = agent.run_step()
            control.manual_gear_shift = False
            world.player.apply_control(control)

    finally:
        if world is not None:
            world.destroy()

        pygame.quit()
Example #8
0
def main():
    # The actor list is necessary, because we need to keep track of all created actors.
    # Thatway, we can delete all actors at the end.
    actor_list = []

    # In this tutorial script, we are going to add a vehicle to the simulation
    # and let it drive in autopilot. We will also create a camera attached to
    # that vehicle, and save all the images generated by the camera to disk.

    try:
        # First of all, we need to create the client that will send the requests
        # to the simulator. Here we'll assume the simulator is accepting
        # requests in the localhost at port 2000# print(current_vehicle_waypoint.transform.location).
        client = carla.Client('localhost', 2000)
        client.set_timeout(2.0)

        # Once we have a client we can retrieve the world that is currently
        # running.
        world = client.get_world()

        # Get the map of the World
        map = world.get_map()

        # The world contains the list blueprints that we can use for adding new
        # actors into the simulation.
        blueprint_library = world.get_blueprint_library()

        # Now let's filter all the blueprints of type 'vehicle' and choose one
        # at random.
        bp = random.choice(blueprint_library.filter('vehicle'))

        # choose first possible spawn point
        transform = world.get_map().get_spawn_points()[0]
        print(transform)

        # So let's tell the world to spawn the vehicle.
        vehicle = world.spawn_actor(bp, transform)

        # It is important to note that the actors we create won't be destroyed
        # unless we call their "destroy" function. If we fail to call "destroy"
        # they will stay in the simulation even after we quit the Python script.
        # For that reason, we are storing all the actors we create so we can
        # destroy them afterwards.
        actor_list.append(vehicle)
        print('created %s' % vehicle.type_id)

        # create an RoamingAgent object
        roaming_agent = RoamingAgent(vehicle)

        start_time = time.time()
        bRunSim = True
        # run the simulation
        while bRunSim:

            # run step and apply control
            control = roaming_agent.run_step()

            # apply the control object
            vehicle.apply_control(control)

            # get spectator,
            spectator = world.get_spectator()

            # get ego vehicle transform for spectator transform
            spectator_transform = vehicle.get_transform()

            # go backwards
            forward_vector = spectator_transform.get_forward_vector()
            print(forward_vector)
            view_distance = 8
            spectator_transform.location.x -= forward_vector.x * view_distance
            spectator_transform.location.y -= forward_vector.y * view_distance

            # spectator upwards
            spectator_transform.location.z += 3

            # look downwards
            spectator_transform.rotation.pitch -= 20

            # set spectator position
            spectator.set_transform(spectator_transform)
            # print(spectator.get_transform())

            time.sleep(1.0 / 60)

            if (time.time() - start_time) > actor_duration:
                bRunSim = False

    finally:

        print('destroying actors')
        for actor in actor_list:
            actor.destroy()
        print('done.')
Example #9
0
def game_loop(args):
    """ Main loop for agent"""

    pygame.init()
    pygame.font.init()
    world = None
    tot_target_reached = 0
    num_min_waypoints = 21

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args)
        controller = KeyboardControl(world)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        elif args.agent == "Basic":
            agent = BasicAgent(world.player)
            spawn_point = world.map.get_spawn_points()[0]
            agent.set_destination(
                (spawn_point.location.x, spawn_point.location.y,
                 spawn_point.location.z))
        else:
            # start and destination
            agent = BehaviorAgent(world.player, behavior=args.behavior)
            spawn_points = world.map.get_spawn_points()  # 7 spawn points
            # --- !!! modify: start and destination --- #
            # start --> destination way-points
            start = agent.vehicle.get_location()
            # end 1 and end 2
            destination = spawn_points[1].location

            agent.set_destination(start, destination, clean=True)
            # --- draw the waypoints --- #
            waypoints = agent._local_planner.waypoints_queue
            for w in waypoints:
                world.world.debug.draw_string(w[0].transform.location,
                                              'O',
                                              draw_shadow=False,
                                              color=carla.Color(r=255,
                                                                g=0,
                                                                b=0),
                                              life_time=120.0,
                                              persistent_lines=True)
            # -------------------------------------------------------------------------- #
            # random.shuffle(spawn_points)
            # if spawn_points[0].location != agent.vehicle.get_location():
            #     destination = spawn_points[0].location
            # else:
            #     destination = spawn_points[1].location
            # agent.set_destination(agent.vehicle.get_location(), destination, clean=True)

        clock = pygame.time.Clock()
        time_step = 0

        with open('driving_log.csv', 'a') as csv_file:
            writer = csv.writer(csv_file,
                                delimiter=',',
                                quotechar=' ',
                                quoting=csv.QUOTE_MINIMAL,
                                lineterminator='\n')
            while True:
                time_step += 1
                clock.tick_busy_loop(10.0)
                if controller.parse_events():
                    return

                # As soon as the server is ready continue!
                if not world.world.wait_for_tick(10.0):
                    continue

                if args.agent == "Roaming" or args.agent == "Basic":
                    if controller.parse_events():
                        return

                    # as soon as the server is ready continue!
                    world.world.wait_for_tick(10.0)

                    world.tick(clock)
                    world.render(display)
                    pygame.display.flip()
                    control = agent.run_step()
                    control.manual_gear_shift = False
                    world.player.apply_control(control)
                else:
                    agent.update_information(world)
                    world.tick(clock)
                    world.render(display)
                    pygame.display.flip()

                    # Set new destination when target has been reached
                    if len(agent.get_local_planner().waypoints_queue
                           ) < num_min_waypoints and args.loop:
                        agent.reroute(spawn_points)
                        tot_target_reached += 1
                        world.hud.notification("The target has been reached " +
                                               str(tot_target_reached) +
                                               " times.",
                                               seconds=4.0)

                    elif len(agent.get_local_planner().waypoints_queue
                             ) == 0 and not args.loop:
                        print("Target reached, mission accomplished...")
                        break

                    # --- modify limit speed --- #
                    speed_limit = world.player.get_speed_limit()
                    agent.get_local_planner().set_speed(speed_limit)
                    control = agent.run_step()
                    world.player.apply_control(control)

                    # --- save the image and vehicle information --- #
                    # save the camera-rgb image
                    image_name = str(world.camera_manager.image_name) + '.png'
                    pos = agent.vehicle.get_location()  # position
                    vel = agent.vehicle.get_velocity()
                    velocity = 3.6 * math.sqrt(
                        vel.x**2 + vel.y**2 + vel.z**2)  # velocity
                    steer = round(control.steer, 5)  # steer
                    throttle = round(control.throttle, 5)
                    writer.writerow([image_name, steer, throttle])

    finally:
        if world is not None:
            world.destroy()

        pygame.quit()
Example #10
0
def game_loop(args):
    pygame.init()
    pygame.font.init()
    world = None

    try:
        client = carla.Client(args.host, args.port)
        client.set_timeout(4.0)

        display = pygame.display.set_mode((args.width, args.height),
                                          pygame.HWSURFACE | pygame.DOUBLEBUF)

        hud = HUD(args.width, args.height)
        world = World(client.get_world(), hud, args.filter)
        controller = KeyboardControl(world, False)

        map = world.world.get_map()
        distance = 2
        waypoints = map.generate_waypoints(distance)
        for w in waypoints:
            world.world.debug.draw_string(w.transform.location,
                                          'O',
                                          draw_shadow=False,
                                          color=carla.Color(r=255, g=0, b=0),
                                          life_time=120.0,
                                          persistent_lines=True)

        if args.agent == "Roaming":
            agent = RoamingAgent(world.player)
        else:
            agent = BasicAgent(world.player)
            spawn_point = world.map.get_spawn_points()[0]
            agent.set_destination(
                (spawn_point.location.x, spawn_point.location.y,
                 spawn_point.location.z))

        clock = pygame.time.Clock()
        while True:
            if controller.parse_events(client, world, clock):
                return

            map = world.world.get_map()
            distance = 2
            waypoints = map.generate_waypoints(distance)
            # for w in waypoints:
            #     print("waypoint", w)
            #     world.world.debug.draw_string(w.transform.location, 'O', draw_shadow=False,
            #                                        color=carla.Color(r=255, g=0, b=0), life_time=120.0,
            #                                        persistent_lines=True)
            # # as soon as the server is ready continue!
            world.world.wait_for_tick(10.0)

            world.tick(clock)
            world.render(display)
            pygame.display.flip()
            control = agent.run_step()
            control.manual_gear_shift = False
            world.player.apply_control(control)

    finally:
        if world is not None:
            world.destroy()

        pygame.quit()