Beispiel #1
0
def game_loop(args):
    """
    Main loop of the simulation. It handles updating all the HUD information,
    ticking the agent and, if needed, the world.
    """

    pygame.init()
    pygame.font.init()
    world = None

    try:
        if args.seed:
            random.seed(args.seed)

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

        traffic_manager = client.get_trafficmanager()
        sim_world = client.get_world()

        if args.sync:
            settings = sim_world.get_settings()
            settings.synchronous_mode = True
            settings.fixed_delta_seconds = 0.05
            sim_world.apply_settings(settings)

            traffic_manager.set_synchronous_mode(True)

        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 == "Basic":
            agent = BasicAgent(world.player)
        else:
            agent = BehaviorAgent(world.player, behavior=args.behavior)

        # Set the agent destination
        spawn_points = world.map.get_spawn_points()
        destination = random.choice(spawn_points).location
        agent.set_destination(destination)

        clock = pygame.time.Clock()

        while True:
            clock.tick()
            if args.sync:
                world.world.tick()
            else:
                world.world.wait_for_tick()
            if controller.parse_events():
                return

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

            if agent.done():
                if args.loop:
                    agent.set_destination(random.choice(spawn_points).location)
                    world.hud.notification(
                        "The target has been reached, searching for another target",
                        seconds=4.0)
                    print(
                        "The target has been reached, searching for another target"
                    )
                else:
                    print(
                        "The target has been reached, stopping the simulation")
                    break

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

    finally:

        if world is not None:
            settings = world.world.get_settings()
            settings.synchronous_mode = False
            settings.fixed_delta_seconds = None
            world.world.apply_settings(settings)
            traffic_manager.set_synchronous_mode(True)

            world.destroy()

        pygame.quit()
Beispiel #2
0
class ConvoyStrategy(EgoStrategy):
    def __init__(self,
                 id: int,
                 waypoint_provider: WaypointProvider = None,
                 config: int = 0,
                 wait_for_egos: int = 0,
                 **kwargs):
        super().__init__()

        self.id: int = id
        self.config: int = config
        self.ready: bool = False
        self.point_start: carla.Transform = None
        self.point_end: carla.Transform = None
        self.agent: BasicAgent = None
        self.wait_for: int = wait_for_egos
        self.wpp: WaypointProvider = waypoint_provider

        self.kwargs = kwargs

        self._player: carla.Vehicle = None
        self._step_count: int = 0
        self._start_time: float = 0

    def init(self, ego):
        super().init(ego)

        if not self.wpp:
            self._init_missing_waypoint_provider(ego)

        n_egos_present: int = simulation.count_present_vehicles(
            SCENE2_EGO_PREFIX, self.ego.world)
        self.point_start = self.wpp.get_by_index(n_egos_present)
        self.point_end = self.wpp.get_by_index(-1)
        self._player = self._create_player()  # Create player

        self.agent = BasicAgent(self.player,
                                target_speed=EGO_TARGET_SPEED,
                                ignore_traffic_lights=True)
        self.agent.set_location_destination(self.point_end.location)

    def step(self, clock=None) -> bool:
        if self.ego is None or not self.ready and simulation.count_present_vehicles(
                SCENE2_EGO_PREFIX, self.ego.world) < self.wait_for:
            return False

        if self._start_time == 0:
            self._start_time = time.monotonic()

        self.ready = True

        # Wait some time before starting
        if time.monotonic() - self._start_time < 3.0:
            return False

        control: carla.VehicleControl = self.agent.run_step(debug=False)
        self._step_count += 1

        if self._step_count % 10 == 0 and self.agent.done():
            logging.info(f'{self.ego.name} has reached its destination.')
            return True

        return self.player.apply_control(control)

    @property
    def player(self) -> carla.Vehicle:
        return self._player

    def _create_player(self) -> carla.Vehicle:
        blueprint = random.choice(
            self.ego.world.get_blueprint_library().filter(
                'vehicle.tesla.model3'))
        blueprint.set_attribute('role_name', self.ego.name)
        if blueprint.has_attribute('color'):
            colors = blueprint.get_attribute('color').recommended_values
            blueprint.set_attribute('color',
                                    colors[self.id % (len(colors) - 1)])

        return self.ego.world.spawn_actor(blueprint, self.point_start)

    def _init_missing_waypoint_provider(self, ego: 'ego.Ego'):
        seed: int = self.kwargs['seed'] if 'seed' in self.kwargs else 0
        logging.info(f'Using {seed} as a random seed.')

        self.wpp = WaypointProvider(ego.world, seed=seed)

        if self.config == 0:
            self.wpp.set_waypoints([
                carla.Transform(location=carla.Location(296, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(302, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(308, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(314, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(320, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(326, 55, .1),
                                rotation=carla.Rotation(0, 180, 0)),
                carla.Transform(location=carla.Location(92, 36, .1),
                                rotation=carla.Rotation(0, -90, 0))
            ])