Exemple #1
0
def spawn_vehicles(amount: int, blueprints: list, client: carla.Client,
                   spawn_points: List[carla.Transform]) -> list:
    """Spawns vehicles, based on spawn_npc.py"""
    assert amount >= 0

    traffic_manager = client.get_trafficmanager()
    batch = []
    vehicles = []
    random.shuffle(spawn_points)

    for i, transform in enumerate(spawn_points):
        if i >= amount:
            break

        blueprint = random.choice(blueprints)

        if blueprint.has_attribute('color'):
            color = random.choice(
                blueprint.get_attribute('color').recommended_values)
            blueprint.set_attribute('color', color)

        if blueprint.has_attribute('driver_id'):
            driver_id = random.choice(
                blueprint.get_attribute('driver_id').recommended_values)
            blueprint.set_attribute('driver_id', driver_id)

        blueprint.set_attribute('role_name', 'autopilot')

        # spawn the cars and set their autopilot
        batch.append(
            carla.command.SpawnActor(blueprint, transform).then(
                carla.command.SetAutopilot(carla.command.FutureActor, True,
                                           traffic_manager.get_port())))

    for response in client.apply_batch_sync(batch, True):
        if response.error:
            print(response.error)
        else:
            vehicles.append(response.actor_id)

    return vehicles
Exemple #2
0
def spawn_pedestrians(amount: int,
                      blueprints: list,
                      client: carla.Client,
                      running=0.0,
                      crossing=0.0) -> list:
    """Spawns pedestrians, based on spawn_npc.py"""
    assert amount >= 0
    assert 0.0 <= running <= 1.0
    assert 0.0 <= crossing <= 1.0

    percentage_pedestrians_running = running  # how many pedestrians will run
    percentage_pedestrians_crossing = crossing  # how many pedestrians will walk through the road

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

    # 1. Spawn point with random location (for navigation purpose)
    spawn_points = []

    for i in range(amount):
        spawn_point = carla.Transform()
        loc = world.get_random_location_from_navigation()

        if loc is not None:
            spawn_point.location = loc
            spawn_points.append(spawn_point)

    # 2. spawn the walker object
    batch = []
    walker_speed = []

    for spawn_point in spawn_points:
        walker_bp = random.choice(blueprints)

        # set as not invincible
        if walker_bp.has_attribute('is_invincible'):
            walker_bp.set_attribute('is_invincible', 'false')

        # set the max speed
        if walker_bp.has_attribute('speed'):
            if random.random() > percentage_pedestrians_running:
                # walking
                walker_speed.append(
                    walker_bp.get_attribute('speed').recommended_values[1])
            else:
                # running
                walker_speed.append(
                    walker_bp.get_attribute('speed').recommended_values[2])
        else:
            print("Walker has no speed")
            walker_speed.append(0.0)

        batch.append(carla.command.SpawnActor(walker_bp, spawn_point))

    results = client.apply_batch_sync(batch, True)
    walker_speed2 = []
    walkers = []

    for i in range(len(results)):
        if results[i].error:
            print(results[i].error)
        else:
            walkers.append(dict(id=results[i].actor_id, controller=None))
            walker_speed2.append(walker_speed[i])

    walker_speed = walker_speed2

    # 3. Spawn the walker controller
    batch = []
    walker_controller_bp = world.get_blueprint_library().find(
        'controller.ai.walker')

    for i in range(len(walkers)):
        batch.append(
            carla.command.SpawnActor(walker_controller_bp, carla.Transform(),
                                     walkers[i]['id']))

    results = client.apply_batch_sync(batch, True)

    for i in range(len(results)):
        if results[i].error:
            print(results[i].error)
        else:
            walkers[i]['controller'] = results[i].actor_id

    # 4. Put altogether the walkers and controllers id to get the objects from their id
    all_id = []

    for i in range(len(walkers)):
        all_id.append(walkers[i]['controller'])
        all_id.append(walkers[i]['id'])

    all_actors = world.get_actors(all_id)

    # wait for a tick to ensure client receives the last transform of the walkers we have just created
    if not world.get_settings().synchronous_mode:
        world.wait_for_tick()
    else:
        world.tick()

    # 5. initialize each controller and set target to walk to (list is [controller, actor, controller, actor ...])
    # set how many pedestrians can cross the road
    world.set_pedestrians_cross_factor(percentage_pedestrians_crossing)

    for i in range(0, len(all_id), 2):
        # start walker
        all_actors[i].start()
        # set walk to random point
        all_actors[i].go_to_location(
            world.get_random_location_from_navigation())
        # max speed
        all_actors[i].set_max_speed(float(walker_speed[int(i / 2)]))

    return all_id