Ejemplo n.º 1
0
    def add_agent(self,
                  agent: ip.Agent,
                  rolename: str = None,
                  blueprint: carla.ActorBlueprint = None):
        """ Add a vehicle to the simulation. Defaults to an Audi A2 for blueprints if not explicitly given.

        Args:
            agent: Agent to add.
            rolename: Unique name for the actor to spawn.
            blueprint: Optional blueprint defining the properties of the actor.

        Returns:
            The newly added actor.
        """
        if blueprint is None:
            blueprint_library = self.__world.get_blueprint_library()
            blueprint = blueprint_library.find('vehicle.audi.a2')

        if rolename is not None:
            blueprint.set_attribute('role_name', rolename)

        state = agent.state
        yaw = np.rad2deg(-state.heading)
        transform = Transform(Location(x=state.position[0], y=-state.position[1], z=0.1), Rotation(yaw=yaw))
        actor = self.__world.spawn_actor(blueprint, transform)
        actor.set_target_velocity(Vector3D(state.velocity[0], -state.velocity[1], 0.))

        carla_agent = ip.carla.CarlaAgentWrapper(agent, actor)
        self.agents[carla_agent.agent_id] = carla_agent
Ejemplo n.º 2
0
def randomize_attributes(bp: carla.ActorBlueprint) -> None:
    """Works in-place.

    Attributes for all blueprints: https://github.com/carla-simulator/carla/blob/7f91986f6957260b33b939e1e066a9094e272087/Docs/bp_library.md
    There are more attributes, but most of them is read-only... (number_of_wheels, age, gender, size)
    """

    # Applies to vehicles only
    if bp.has_attribute("color"):
        rgb = np.random.randint(0, 255, size=3, dtype=int)
        value = ",".join(map(str, rgb))
        bp.set_attribute("color", value)
Ejemplo n.º 3
0
def spawn_lidar(
    hero: carla.ActorBlueprint,  # pylint: disable=no-member
    config: Mapping[str, Any],
) -> carla.ServerSideSensor:  # pylint: disable=no-member
    """Spawns LIDAR sensor on `hero`.

  Args:
    hero: The agent to attach the LIDAR sensor on.
    config: The attribute-value pairs for the configuration
      of the sensor.

  Returns:
    The spawned LIDAR sensor.
  """
    # Get hero's world.
    world = hero.get_world()
    # Blueprints library.
    bl = world.get_blueprint_library()
    # Configure blueprint.
    lidar_bp = bl.find("sensor.lidar.ray_cast")
    for attribute, value in config["attributes"].items():
        lidar_bp.set_attribute(attribute, value)
    logging.debug("Spawns a LIDAR sensor")
    return world.spawn_actor(
        lidar_bp,
        carla.Transform(  # pylint: disable=no-member
            carla.Location(**config["actor"]["location"]),  # pylint: disable=no-member
            carla.Rotation(),  # pylint: disable=no-member
        ),
        attach_to=hero,
    )
Ejemplo n.º 4
0
def spawn_camera(
    hero: carla.ActorBlueprint,  # pylint: disable=no-member
    config: Mapping[str, Any],
    camera_type: str,
) -> carla.ServerSideSensor:  # pylint: disable=no-member
    """Spawns a camera on `hero`.

  Args:
    hero: The agent to attach the camera on.
    config: The attribute-value pairs for the configuration
      of the sensor.
    camera_type: Camera type, one of ("rgb", "semantic_segmentation", "depth").

  Returns:
    The spawned  camera sensor.
  """
    assert camera_type in ("rgb", "semantic_segmentation", "depth")

    # Get hero's world.
    world = hero.get_world()
    # Blueprints library.
    bl = world.get_blueprint_library()
    # Configure blueprint.
    camera_bp = bl.find("sensor.camera.{}".format(camera_type))
    for attribute, value in config["attributes"].items():
        camera_bp.set_attribute(attribute, value)
    logging.debug("Spawns a {} camera".format(camera_type))
    return world.spawn_actor(
        camera_bp,
        carla.Transform(  # pylint: disable=no-member
            carla.Location(**config["actor"]["location"]),  # pylint: disable=no-member
            carla.Rotation(**config["actor"]["rotation"]),  # pylint: disable=no-member
        ),
        attach_to=hero,
    )
Ejemplo n.º 5
0
def spawn_lane_invasion(
    hero: carla.ActorBlueprint,  # pylint: disable=no-member
) -> carla.ServerSideSensor:  # pylint: disable=no-member
    """Spawns lane invasion sensor on `hero`.

  Args:
    hero: The agent to attach the collision sensor on.

  Returns:
    The spawned lane invasion sensor.
  """
    # Get hero's world.
    world = hero.get_world()
    # Blueprints library.
    bl = world.get_blueprint_library()
    # Configure blueprint.
    collision_bp = bl.find("sensor.other.lane_invasion")
    logging.debug("Spawns a lane invasion sensor")
    return world.spawn_actor(
        collision_bp,
        carla.Transform(),  # pylint: disable=no-member
        attach_to=hero,
    )
Ejemplo n.º 6
0
 def _add_attr_to_blueprint(self, bp: carla.ActorBlueprint):
     bp.set_attribute('image_size_x', str(self.img_width))
     bp.set_attribute('image_size_y', str(self.img_height))
     bp.set_attribute('fov', str(self.fov_horizontal))