Пример #1
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()
Пример #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)

        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()
Пример #4
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()
Пример #5
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()
Пример #6
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()
Пример #7
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()
class World(object):
    """
    Creates a object which has all the necessary attributes of carla world and ego vehicle. For example
    --> Carla World (a world Object)
    --> Weather Presets
    --> List of sensors for ego vehicle
    --> List of other vwhicle in the world
    
    """
    def __init__(self, carla_world, hud, spawn_location, actor_filter,
                 agent_str, scene):
        # Crala World
        self.world = carla_world
        self.map = self.world.get_map()
        self.hud = hud
        self.world.on_tick(hud.on_world_tick)

        # Weather presets
        self._weather_index = 0
        self._weather_presets = None
        self.find_weather_presets()

        # Autonomous vehicle and sensors variables

        self._spawn_loc = spawn_location  # spawn location
        self._actor_filter = actor_filter  # vehicle type
        self.player = None  # ego vehicle
        self.collision_sensor = None  # sensors
        self.gnss_sensor = None
        self._ego_list = []
        # Other actors
        self._scene = scene
        self._actor_list = []
        # camera manager
        self.main_rgb_camera = None
        self.depth_camera = None

        # radar set
        self.front_radar = None
        self.back_radar = None
        self.left_front_radar = None
        self.right_front_radar = None

        # Agent
        self.agent_name = agent_str
        self.agent = None
        self.autopilot_mode = True
        self.restart()

    def restart(self):
        # Destroy existing ego and actors
        if self._ego_list or self._actor_list:
            self.destroy()

        # Spawn the player
        # get a random blueprint from the filter
        blueprint = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        blueprint.set_attribute('role_name', 'ego')
        blueprint.set_attribute('color', '255, 255, 255')
        spawn_point = carla.Transform(
            carla.Location(x=self._spawn_loc[0],
                           y=self._spawn_loc[1],
                           z=self._spawn_loc[2]))
        self.player = self.world.try_spawn_actor(blueprint, spawn_point)
        if self.player is None:
            print("Ego vehicle spawn location occupied.\n Spawning failed!")
            return

        # Set up the sensors.
        self.main_rgb_camera = CameraSet(self.player, self.hud)

        self.collision_sensor = CollisionSensor(self.player, self.hud)
        self.gnss_sensor = GnssSensor(self.player)

        self.front_radar = FakeRadarSensor(self.player,
                                           self.hud,
                                           x=0.5,
                                           y=0.0,
                                           z=1.0,
                                           yaw=0.0,
                                           fov=5)
        self.left_front_radar = FakeRadarSensor(self.player,
                                                self.hud,
                                                x=1.0,
                                                y=-0.5,
                                                z=1.0,
                                                yaw=-25.0)
        self.left_back_radar = FakeRadarSensor(self.player,
                                               self.hud,
                                               x=-1.0,
                                               y=-0.5,
                                               z=1.0,
                                               yaw=-155.0)

        self._ego_list = [
            self.main_rgb_camera, self.collision_sensor.sensor,
            self.gnss_sensor.sensor, self.front_radar, self.left_front_radar,
            self.left_back_radar, self.player
        ]

        # Spawn other actors
        self._actor_list = spawn_surrounding_vehicles(self.world, self._scene)

        # Reset agent
        if self.agent_name == "Autonomous":
            self.agent = AutonomousAgent(self)
            # destination Setting
            self.agent.set_destination((100, -4.5, 0))
        elif self.agent_name == "Basic":
            self.agent = BasicAgent(self.player)
            # destination Setting
            self.agent.set_destination((300, 45, 0))
        else:
            self.agent = RoamingAgent(self.player)

        # Display
        actor_type = get_actor_display_name(self.player)
        self.hud.notification(actor_type)

    def find_weather_presets(self):
        rgx = re.compile(
            '.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)')
        name = lambda x: ' '.join(m.group(0) for m in rgx.finditer(x))
        presets = [
            x for x in dir(carla.WeatherParameters) if re.match('[A-Z].+', x)
        ]
        self._weather_presets = [(getattr(carla.WeatherParameters, x), name(x))
                                 for x in presets]

    def next_weather(self, reverse=False):
        self._weather_index += -1 if reverse else 1
        self._weather_index %= len(self._weather_presets)
        preset = self._weather_presets[self._weather_index]
        self.hud.notification('Weather: %s' % preset[1])
        self.player.get_world().set_weather(preset[0])

    def tick(self, clock):
        self.hud.tick(self, clock)

    def render(self, display):
        self.main_rgb_camera.render(display)
        self.hud.render(display)

    def enable_agent(self, enabled):
        self.autopilot_mode = enabled

    def destroy(self):
        for actor in self._ego_list:
            if actor is not None:
                actor.destroy()
        self._ego_list = []

        for actor in self._actor_list:
            if actor is not None:
                # disconnect from client
                actor.set_autopilot(False)
                actor.destroy()
        self._actor_list = []
Пример #9
0
class World(object):
    def __init__(self, carla_world, hud, spawn_location, actor_filter,
                 agent_str, scene):
        # World
        self.world = carla_world
        self.map = self.world.get_map()
        self.hud = hud
        self.world.on_tick(hud.on_world_tick)
        # Weather
        self._weather_index = 0
        self._weather_presets = None
        self.find_weather_presets()
        # Ego and sensors
        self._spawn_loc = spawn_location  # spawn location
        self._actor_filter = actor_filter  # vehicle type
        self.player = None  # ego vehicle
        self.collision_sensor = None  # sensors
        self.gnss_sensor = None
        self._ego_list = []
        # Other actors
        self._scene = scene
        self._actor_list = []
        # camera manager
        self.main_rgb_camera = None
        self.depth_camera = None
        self.segmentation_camera = None
        self.lidar = None
        # radar set
        self.front_radar = None
        self.back_radar = None
        self.left_front_radar = None
        self.right_front_radar = None
        self.left_back_radar = None
        self.right_back_radar = None
        # Agent
        self.agent_name = agent_str
        self.agent = None
        self.autopilot_mode = True  # driving with agent
        self.is_learning = False  # learning mode
        self.restart()
        # Record
        self.recording_enabled = False
        self.recording_start = 0

    def restart(self):
        # Destroy existing ego and actors
        if self._ego_list or self._actor_list:
            self.destroy()

        # Spawn the player
        # get a random blueprint from the filter
        blueprint = random.choice(self.world.get_blueprint_library().filter(
            self._actor_filter))
        blueprint.set_attribute('role_name', 'ego')
        blueprint.set_attribute('color', '0, 0, 0')
        spawn_point = carla.Transform(
            carla.Location(x=self._spawn_loc[0],
                           y=self._spawn_loc[1],
                           z=self._spawn_loc[2]))
        self.player = self.world.try_spawn_actor(blueprint, spawn_point)
        if self.player is None:
            print("Ego vehicle spawn location occupied.\n Spawning failed!")
            return

        # Set up the sensors.
        self.main_rgb_camera = CameraSet(self.player, self.hud)
        '''
        self.depth_camera = CameraManager(self.player, self.hud)
        self.depth_camera.set_sensor(3, notify=False)
        self.segmentation_camera = CameraManager(self.player, self.hud)
        self.segmentation_camera.set_sensor(5, notify=False)
        self.lidar = CameraManager(self.player, self.hud)
        self.lidar.transform_index = 1
        self.lidar.set_sensor(6, notify=False)
        '''

        self.collision_sensor = CollisionSensor(self.player, self.hud)
        self.gnss_sensor = GnssSensor(self.player)

        self.front_radar = FakeRadarSensor(self.player,
                                           self.hud,
                                           x=0.5,
                                           y=0.0,
                                           z=1.0,
                                           yaw=0.0,
                                           fov=5)
        self.left_front_radar = FakeRadarSensor(self.player,
                                                self.hud,
                                                x=1.0,
                                                y=-0.5,
                                                z=1.0,
                                                yaw=-25.0)
        self.left_back_radar = FakeRadarSensor(self.player,
                                               self.hud,
                                               x=-1.0,
                                               y=-0.5,
                                               z=1.0,
                                               yaw=-155.0)
        '''
        # Radars are implemented but not used for high computing resources required
        self.front_radar = RadarSensor(self.player, self.hud, rr='20',
                                       x=2.5, y=0.0, z=1.0, yaw=0.0)
        self.left_front_radar = RadarSensor(self.player, self.hud, hf='40', pps='1000',
                                            x=2.5, y=-0.8, z=1.0, yaw=-25.0)
        self.left_back_radar = RadarSensor(self.player, self.hud, hf='40', pps='1000', 
                                           x=-2.5, y=-0.8, z=1.0, yaw=-155.0)
        self.back_radar = RadarSensor(self.player, self.hud, rr='20',
                                      x=-2.5, y=0.0, z=1.0, yaw=180.0)
        self.right_front_radar = RadarSensor(self.player, self.hud, hf='40', pps='800', 
                                           x=2.5, y=0.5, z=1.0, yaw=20.0)
        self.right_back_radar = RadarSensor(self.player, self.hud, hf='40', pps='800', 
                                           x=-2.5, y=0.5, z=1.0, yaw=140.0)
        '''

        self._ego_list = [
            self.main_rgb_camera,
            #self.depth_camera.sensor,
            #self.segmentation_camera.sensor,
            #self.lidar.sensor,
            self.collision_sensor.sensor,
            self.gnss_sensor.sensor,
            self.front_radar,
            #self.back_radar.sensor,
            self.left_front_radar,
            self.left_back_radar,
            #self.right_front_radar.sensor,
            #self.right_back_radar.sensor,
            self.player
        ]

        # Spawn other actors
        self._actor_list = spawn_surrounding_vehicles(self.world, self._scene)

        # Reset agent
        if self.agent_name == "Learning":
            self.agent = LearningAgent(self)
            # destination Setting
            self.agent.set_destination((235, 45, 0))
        elif self.agent_name == "Basic":
            self.agent = BasicAgent(self.player)
            # destination Setting
            self.agent.set_destination((235, 45, 0))
        else:
            self.agent = RoamingAgent(self.player)

        # Display
        actor_type = get_actor_display_name(self.player)
        self.hud.notification(actor_type)

    def find_weather_presets(self):
        rgx = re.compile(
            '.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)')
        name = lambda x: ' '.join(m.group(0) for m in rgx.finditer(x))
        presets = [
            x for x in dir(carla.WeatherParameters) if re.match('[A-Z].+', x)
        ]
        self._weather_presets = [(getattr(carla.WeatherParameters, x), name(x))
                                 for x in presets]

    def next_weather(self, reverse=False):
        self._weather_index += -1 if reverse else 1
        self._weather_index %= len(self._weather_presets)
        preset = self._weather_presets[self._weather_index]
        self.hud.notification('Weather: %s' % preset[1])
        self.player.get_world().set_weather(preset[0])

    def tick(self, clock):
        self.hud.tick(self, clock)

    def render(self, display):
        self.main_rgb_camera.render(display)
        self.hud.render(display)

    def enable_agent(self, enabled):
        self.autopilot_mode = enabled

    def enable_learning(self, enabled):
        self.is_learning = enabled

    def destroy(self):
        for actor in self._ego_list:
            if actor is not None:
                actor.destroy()
        self._ego_list = []

        for actor in self._actor_list:
            if actor is not None:
                # disconnect from client
                actor.set_autopilot(False)
                actor.destroy()
        self._actor_list = []
Пример #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)

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

        clock = pygame.time.Clock()

        count = 0
        while True:
            count = count + 1
            print(count)
            # 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 = carla.VehicleControl(
                                throttle = 1,
                                steer = 0.0,
                                brake = 0.0,
                                hand_brake = False,
                                reverse = False,
                                manual_gear_shift = False,
                                gear = 0)
            if count > 200:
                control = carla.VehicleControl(
                                throttle = 0.5,
                                steer = 0.6,
                                brake = 0.0,
                                hand_brake = True,
                                reverse = False,
                                manual_gear_shift = False,
                                gear = 0)
            if count > 260:
                control = carla.VehicleControl(
                                throttle = 1,
                                steer = 0,
                                brake = 0.0,
                                hand_brake = False,
                                reverse = False,
                                manual_gear_shift = False,
                                gear = 0)
            control.manual_gear_shift = False
            world.player.apply_control(control)

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

        pygame.quit()
Пример #11
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()