Example #1
0
def spawn_people(client, world, num_people: int, logger):
    """Spawns people at random locations inside the world.

    Args:
        num_people: The number of people to spawn.
    """
    from carla import command, Transform
    p_blueprints = world.get_blueprint_library().filter('walker.pedestrian.*')
    unique_locs = set([])
    spawn_points = []
    # Get unique spawn points.
    for i in range(num_people):
        attempt = 0
        while attempt < 10:
            spawn_point = Transform()
            loc = world.get_random_location_from_navigation()
            if loc is not None:
                # Transform to tuple so that location is comparable.
                p_loc = (loc.x, loc.y, loc.z)
                if p_loc not in unique_locs:
                    spawn_point.location = loc
                    spawn_points.append(spawn_point)
                    unique_locs.add(p_loc)
                    break
            attempt += 1
        if attempt == 10:
            logger.error('Could not find unique person spawn point')
    # Spawn the people.
    batch = []
    for spawn_point in spawn_points:
        p_blueprint = random.choice(p_blueprints)
        if p_blueprint.has_attribute('is_invincible'):
            p_blueprint.set_attribute('is_invincible', 'false')
        batch.append(command.SpawnActor(p_blueprint, spawn_point))
    # Apply the batch and retrieve the identifiers.
    ped_ids = []
    for response in client.apply_batch_sync(batch, True):
        if response.error:
            logger.info('Received an error while spawning a person: {}'.format(
                response.error))
        else:
            ped_ids.append(response.actor_id)
    # Spawn the person controllers
    ped_controller_bp = world.get_blueprint_library().find(
        'controller.ai.walker')
    batch = []
    for ped_id in ped_ids:
        batch.append(command.SpawnActor(ped_controller_bp, Transform(),
                                        ped_id))
    ped_control_ids = []
    for response in client.apply_batch_sync(batch, True):
        if response.error:
            logger.info('Error while spawning a person controller: {}'.format(
                response.error))
        else:
            ped_control_ids.append(response.actor_id)

    return (ped_ids, ped_control_ids)
Example #2
0
def spawn_driving_vehicle(client, world):
    """ This function spawns the driving vehicle and puts it into
    an autopilot mode.

    Args:
        client: The client instance representing the simulation to
          connect to.
        world: The world inside the current simulation.

    Returns:
        An Actor instance representing the vehicle that was just spawned.
    """
    # Get the blueprint of the vehicle and set it to AutoPilot.
    vehicle_bp = random.choice(
        world.get_blueprint_library().filter('vehicle.*'))
    while not vehicle_bp.has_attribute('number_of_wheels') or not int(
            vehicle_bp.get_attribute('number_of_wheels')) == 4:
        vehicle_bp = random.choice(
            world.get_blueprint_library().filter('vehicle.*'))
    vehicle_bp.set_attribute('role_name', 'autopilot')

    # Get the spawn point of the vehicle.
    start_pose = random.choice(world.get_map().get_spawn_points())

    # Spawn the vehicle.
    batch = [
        command.SpawnActor(vehicle_bp, start_pose).then(
            command.SetAutopilot(command.FutureActor, True))
    ]
    vehicle_id = client.apply_batch_sync(batch)[0].actor_id

    # Find the vehicle and return the Actor instance.
    time.sleep(
        0.5)  # This is so that the vehicle gets registered in the actors.
    return world.get_actors().find(vehicle_id)
Example #3
0
def spawn_vehicles(client, world, num_vehicles: int, logger):
    """ Spawns vehicles at random locations inside the world.

    Args:
        num_vehicles: The number of vehicles to spawn.
    """
    from carla import command
    logger.debug('Trying to spawn {} vehicles.'.format(num_vehicles))
    # Get the spawn points and ensure that the number of vehicles
    # requested are less than the number of spawn points.
    spawn_points = world.get_map().get_spawn_points()
    if num_vehicles >= len(spawn_points):
        logger.warning(
            'Requested {} vehicles but only found {} spawn points'.format(
                num_vehicles, len(spawn_points)))
        num_vehicles = len(spawn_points)
    else:
        random.shuffle(spawn_points)

    # Get all the possible vehicle blueprints inside the world.
    v_blueprints = world.get_blueprint_library().filter('vehicle.*')
    #v_blueprints = world.get_blueprint_library().filter('vehicle.diamondback.*')

    # Construct a batch message that spawns the vehicles.
    batch = []
    for transform in spawn_points[:num_vehicles]:
        blueprint = random.choice(v_blueprints)

        # Change the color of the vehicle.
        if blueprint.has_attribute('color'):
            color = random.choice(
                blueprint.get_attribute('color').recommended_values)
            blueprint.set_attribute('color', color)

        # Let the vehicle drive itself.
        blueprint.set_attribute('role_name', 'autopilot')

        batch.append(
            command.SpawnActor(blueprint, transform).then(
                command.SetAutopilot(command.FutureActor, True)))

    # Apply the batch and retrieve the identifiers.
    vehicle_ids = []
    for response in client.apply_batch_sync(batch, True):
        if response.error:
            logger.info(
                'Received an error while spawning a vehicle: {}'.format(
                    response.error))
        else:
            vehicle_ids.append(response.actor_id)
    return vehicle_ids