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()
Example #4
0
    def __init__(self, args):
        pygame.init()
        pygame.font.init()

        self._controller = DriveController("/dev/ttyUSB0", 1, False)

        ##### Register Callbacks #####
        self._controller.logCallback = self._logCallback
        self._controller.errorCallback = self._errorCallback
        self._controller.readingCallback = self._readingCallback
        self._controller.connectedCallback = self._connectedCallback

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

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

            self._hud = HUD(args.width, args.height)
            self._world = World(client.get_world(), self._hud, args.filter)
            self._keyboardController = KeyboardControl(self._world)

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

            self._clock = pygame.time.Clock()

            # Steering Wheel is connected
            self._setupSteeringWheel(args)

            # run loop logic
            while not self._keyboardController.parse_events():
                self._runLoop()
        finally:
            self.__del__()
Example #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()
Example #6
0
    def reset(self):

        actors = self.world.get_actors()
        self.client.apply_batch([
            carla.command.DestroyActor(x) for x in actors
            if "vehicle" in x.type_id
        ])
        self.client.apply_batch([
            carla.command.DestroyActor(x) for x in actors
            if "sensor" in x.type_id
        ])
        for a in actors.filter("vehicle*"):
            if a.is_alive:
                a.destroy()
        for a in actors.filter("sensor*"):
            if a.is_alive:
                a.destroy()

        print("Scene init done!")

        self.actor_list = []

        self.transform = self.world.get_map().get_spawn_points()[0]
        self.transform.location.z += 1
        self.vehicle = self.world.spawn_actor(self.model_3, self.transform)
        # self.vehicle.set_autopilot()
        ##### Setup Agent #####
        self.agent = BehaviorAgent(self.vehicle, behavior="normal")
        destination_location = self.world.get_map().get_spawn_points(
        )[50].location
        self.agent.set_destination(self.vehicle.get_location(),
                                   destination_location,
                                   clean=True)
        self.agent.update_information(self.world)

        ########################

        self.actor_list.append(self.vehicle)

        self.other_vehicles = []
        self.other_agents = []
        spawn_points = self.world.get_map().get_spawn_points()
        for _ in range(50):
            print("Agent", _, "spawned!")
            tmp_transform = random.choice(spawn_points)
            tmp_transform.location.z += 1
            tmp_vehicle = self.world.spawn_actor(self.model_3, tmp_transform)
            tmp_agent = RoamingAgent(tmp_vehicle)
            # tmp_destination_location = random.choice(spawn_points).location
            # tmp_agent.set_destination(tmp_vehicle.get_location(), tmp_destination_location, clean=True)
            # tmp_agent.update_information(self.world)
            self.other_vehicles.append(tmp_vehicle)
            self.other_agents.append(tmp_agent)
            self.actor_list.append(tmp_vehicle)

        self.rgb_cam = self.world.get_blueprint_library().find(
            "sensor.camera.rgb")

        self.rgb_cam.set_attribute("image_size_x", f"{self.im_width}")
        self.rgb_cam.set_attribute("image_size_y", f"{self.im_height}")
        self.rgb_cam.set_attribute("fov", "110")
        fx = self.im_width / (2 * np.tan(self.fov * np.pi / 360))
        fy = self.im_height / (2 * np.tan(self.fov * np.pi / 360))
        self.front_camera_intrinsics = np.array([[fx, 0, self.im_width / 2],
                                                 [0, fy, self.im_height / 2],
                                                 [0, 0, 1]])

        self.depth_cam = self.world.get_blueprint_library().find(
            "sensor.camera.depth")
        self.depth_cam.set_attribute("image_size_x", f"{self.im_width}")
        self.depth_cam.set_attribute("image_size_y", f"{self.im_height}")
        self.depth_cam.set_attribute("fov", "110")

        self.semantic_cam = self.world.get_blueprint_library().find(
            "sensor.camera.depth")
        self.semantic_cam.set_attribute("image_size_x", f"{self.im_width}")
        self.semantic_cam.set_attribute("image_size_y", f"{self.im_height}")
        self.semantic_cam.set_attribute("fov", "110")

        transform = carla.Transform(carla.Location(x=2.5, z=1))

        self.sensor_rgb = self.world.spawn_actor(self.rgb_cam,
                                                 transform,
                                                 attach_to=self.vehicle)
        self.actor_list.append(self.sensor_rgb)
        self.sensor_rgb.listen(lambda data: self.process_img(data))

        self.sensor_depth = self.world.spawn_actor(self.depth_cam,
                                                   transform,
                                                   attach_to=self.vehicle)
        self.actor_list.append(self.sensor_depth)
        self.sensor_depth.listen(lambda data: self.process_img_depth(data))

        self.sensor_semantic = self.world.spawn_actor(self.semantic_cam,
                                                      transform,
                                                      attach_to=self.vehicle)
        self.actor_list.append(self.sensor_semantic)
        self.sensor_semantic.listen(
            lambda data: self.process_img_semantic(data))

        self.imu_sensor = IMUSensor(self.vehicle)

        while self.front_camera is None:
            time.sleep(0.01)

        self.episode_start = time.time()

        return self.front_camera
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 game_loop(self):
        pygame.init()
        pygame.font.init()
        # world = None
        try:

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

            display = pygame.display.set_mode(
                (self.args.width, self.args.height),
                pygame.HWSURFACE | pygame.DOUBLEBUF)
            connect_request_msg = connect_request()
            self.lc.publish(connect_request_keyword,
                            connect_request_msg.encode())

            print(
                "connect request message publish done, waiting for connecting response..."
            )
            # 不断监听初始化回应的消息
            while True:
                try:
                    [keyword, msg] = self.msg_queue.get(timeout=0.01)
                    if keyword == connect_response_keyword:
                        self.connect_response_dealer(msg)
                        break
                except queue.Empty:
                    continue

            hud = HUD(self.args.width, self.args.height)
            self.world = World(client.get_world(), hud, self.args.filter)

            controller = KeyboardControl(self.world, False)
            spawn_point = self.transform_waypoint(self.init_waypoint)
            print("spawn_point: ", spawn_point)
            self.world.player.set_transform(spawn_point)
            # world.vehicle.set_location(spawn_point.location)
            clock = pygame.time.Clock()

            # time.sleep(10)
            # print("location: ", world.vehicle.get_location())
            if self.args.agent == "Roaming":
                # print("Roaming!")
                self.agent = RoamingAgent(self.world.player)
            else:
                self.agent = BasicAgent(self.world.player,
                                        target_speed=self.args.speed)
                spawn_point = self.world.map.get_spawn_points()[0]
                print(spawn_point)
                self.agent.set_destination(
                    (spawn_point.location.x, spawn_point.location.y,
                     spawn_point.location.z))
            self.agent.drop_waypoint_buffer()
            # 在这里发送车辆初始位置给服务器
            # print("location: ", world.vehicle.get_transform())

            # init_lcm_waypoint = self.transform_to_lcm_waypoint(world.vehicle.get_transform())

            # connect_request_msg.init_pos = init_lcm_waypoint

            # clock = pygame.time.Clock()
            # print(len(client.get_world().get_map().get_spawn_points()))
            # pre_loc = [0.0, 0.0, 0.0]
            # 在这里进行后续的循环接收消息
            '''
            main loop of the client end.
            
            '''
            i_var = 0
            settings = self.world.world.get_settings()
            settings.fixed_delta_seconds = None
            self.world.world.apply_settings(settings)
            while True:
                if controller.parse_events(client, self.world, clock):
                    return
                # as soon as the server is ready continue!
                if not self.world.world.wait_for_tick(10.0):
                    continue
                self.world.tick(clock)
                self.world.render(display)
                pygame.display.flip()
                # 是否需要向SUMO服务器发送action result消息
                should_publish_result_msg = False
                try:
                    [keyword, msg] = self.msg_queue.get(timeout=0.01)
                    # print("keyword of message is ", keyword)
                    # Receive an action package
                    if keyword == action_package_keyword:
                        print("Receive an action package!")
                        self.agent.drop_waypoint_buffer()
                        # 在收到新的路点消息后丢弃当前缓冲中剩余的路点
                        self.action_package_dealer(msg)
                        # print("waypoint length: ", len(self.waypoints_buffer))
                        for i in range(self.message_waypoints):
                            temp_waypoint = self.waypoints_buffer.popleft()

                            # print("waypoint in main loop is ", temp_waypoint)

                            self.agent.add_waypoint(temp_waypoint)
                        self.waypoints_buffer.clear()
                    elif keyword == connect_response_keyword:
                        self.connect_response_dealer(msg)
                    elif keyword == end_connection_keyword:
                        if msg.vehicle_id != self.veh_id:
                            print(
                                "invalid vehicle id from end connection package"
                            )
                        else:
                            print("connection to SUMO ended.")
                            # self.agent.set_target_speed(20)
                            self.agent.set_sumo_drive(False)
                            # self.agent.compute_next_waypoints(3)
                            # print("simulation of this vehicle ended. Destroying ego.")
                            # self.should_publish = False
                            # if self.world is not None:
                            #     self.world.destroy()
                            # return
                    else:
                        pass
                except queue.Empty:
                    pass
                control = self.agent.run_step()
                if control:
                    self.world.player.apply_control(control)

                if self.agent.get_finished_waypoints(
                ) >= self.message_waypoints:
                    should_publish_result_msg = True
                # 获取当前位置和速度信息并发送到SUMO服务器
                if should_publish_result_msg:
                    current_speed = self.world.player.get_velocity()
                    current_transform = self.world.player.get_transform()
                    action_res_pack = action_result()
                    action_res_pack.current_pos = self.transform_to_lcm_waypoint(
                        current_transform)
                    action_res_pack.vehicle_id = self.veh_id
                    # print("current speed: ", current_speed)
                    action_res_pack.current_speed = [
                        current_speed.x, current_speed.y, current_speed.z
                    ]

                    self.lc.publish(action_result_keyword,
                                    action_res_pack.encode())
                    self.action_result_count += 1
                    should_publish_result_msg = False

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

            pygame.quit()
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()
    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)
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 = []
Example #12
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)
Example #13
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 = []
Example #14
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()
Example #15
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()
Example #16
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()
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 #18
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.')