2nd. Actors and blueprints
Actors not only include vehicles and walkers, but also sensors, traffic signs, traffic lights, and the spectator. It is crucial to have fully understanding on how to operate on them.
This section will cover spawning, destruction, types, and how to manage them. However, the possibilities are almost endless. Experiment, take a look at the tutorials in this documentation and share doubts and ideas in the CARLA forum.
- Managing the blueprint library
- Actor life cycle
- Types of actors
- Traffic signs and traffic lights
These layouts allow the user to smoothly incorporate new actors into the simulation. They are already-made models with animations and a series of attributes. Some of these are modifiable and others are not. These attributes include, among others, vehicle color, amount of channels in a lidar sensor, a walker's speed, and much more.
Available blueprints are listed in the blueprint library, along with their attributes.
Managing the blueprint library
blueprint_library = world.get_blueprint_library()
Blueprints have an ID to identify them and the actors spawned with it. The library can be read to find a certain ID, choose a blueprint at random, or filter results using a wildcard pattern.
# Find a specific blueprint. collision_sensor_bp = blueprint_library.find('sensor.other.collision') # Choose a vehicle blueprint at random. vehicle_bp = random.choice(blueprint_library.filter('vehicle.*.*'))
is_bike = [vehicle.get_attribute('number_of_wheels') == 2] if(is_bike) vehicle.set_attribute('color', '255,0,0')
Some of the attributes cannot be modified. Check it out in the blueprint library.
Attributes have an carla.ActorAttributeType variable. It states its type from a list of enums. Also, modifiable attributes come with a list of recommended values.
for attr in blueprint: if attr.is_modifiable: blueprint.set_attribute(attr.id, random.choice(attr.recommended_values))
Users can create their own vehicles. Check the Tutorials (assets) to learn on that. Contributors can add their new content to CARLA.
Actor life cycle
This section mentions different methods regarding actors. The Python API provides for commands to apply batches of the most common ones, in just one frame.
The world object is responsible of spawning actors and keeping track of these. Spawning only requires a blueprint, and a carla.Transform stating a location and rotation for the actor.
The world has two different methods to spawn actors.
Noneif the spawning failed.
try_spawn_actor()raises an exception if the spawning failed.
transform = Transform(Location(x=230, y=195, z=40), Rotation(yaw=180)) actor = world.spawn_actor(blueprint, transform)
The actor will not be spawned in case of collision at the specified location. No matter if this happens with a static object or another actor. It is possible to try avoiding these undesired spawning collisions.
map.get_spawn_points()for vehicles. Returns a list of recommended spawning points.
spawn_points = world.get_map().get_spawn_points()
world.get_random_location()for walkers. Returns a random point on a sidewalk. This same method is used to set a goal location for walkers.
spawn_point = carla.Transform() spawn_point.location = world.get_random_location_from_navigation()
An actor can be attached to another one when spawned. Actors follow the parent they are attached to. This is specially useful for sensors. The attachment can be rigid or eased. It is defined by the helper class carla.AttachmentType.
The next example attaches a camera rigidly to a vehicle, so their relative position remains fixed.
camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle, carla.AttachmentType.Rigid)
When spawning attached actors, the transform provided must be relative to the parent actor.
Once spawned, the world object adds the actors to a list. This can be easily searched or iterated on.
actor_list = world.get_actors() # Find an actor by id. actor = actor_list.find(id) # Print the location of all the speed limit signs in the world. for speed_sign in actor_list.filter('traffic.speed_limit.*'): print(speed_sign.get_location())
carla.Actor mostly consists of get() and set() methods to manage the actors around the map.
print(actor.get_acceleration()) print(actor.get_velocity()) location = actor.get_location() location.z += 10.0 actor.set_location(location)
The actor's physics can be disabled to freeze it in place.
Besides that, actors also have tags provided by their blueprints. These are mostly useful for semantic segmentation sensors.
Most of the methods send requests to the simulator asynchronously. The simulator has a limited amount of time each update to parse them. Flooding the simulator with set() methods will accumulate a significant lag.
Actors are not destroyed when a Python script finishes. They have to explicitly destroy themselves.
destroyed_sucessfully = actor.destroy() # Returns True if successful
Destroying an actor blocks the simulator until the process finishes.
Types of actors
Sensors are actors that produce a stream of data. They have their own section, 4th. Sensors and data. For now, let's just take a look at a common sensor spawning cycle.
This example spawns a camera sensor, attaches it to a vehicle, and tells the camera to save the images generated to disk.
camera_bp = blueprint_library.find('sensor.camera.rgb') camera = world.spawn_actor(camera_bp, relative_transform, attach_to=my_vehicle) camera.listen(lambda image: image.save_to_disk('output/%06d.png' % image.frame))
- Sensors have blueprints too. Setting attributes is crucial.
- Most of the sensors will be attached to a vehicle to gather information on its surroundings.
- Sensors listen to data. When data is received, they call a function described with a Lambda expression (6.13 in the link provided).
Placed by Unreal Engine to provide an in-game point of view. It can be used to move the view of the simulator window. The following example would move the spectator actor, to point the view towards a desired vehicle.
spectator = world.get_spectator() transform = vehicle.get_transform() spectator.set_transform(carla.Transform(transform.location + carla.Location(z=50), carla.Rotation(pitch=-90)))
Traffic signs and traffic lights
So far, CARLA only is aware of some signs: stop, yield and speed limit. Traffic lights are considered an inherited class from the more general traffic sign. None of these can be found in the blueprint library and thus, cannot be spawned. They set traffic conditions, so they are mindfully placed by developers.
#Get the traffic light affecting a vehicle if vehicle_actor.is_at_traffic_light(): traffic_light = vehicle_actor.get_traffic_light()
Vehicles will only notice a traffic light if the light is red.
Traffic lights are found in junctions. They have their unique ID, as any actor, but also a
group ID for the junction. To identify the traffic lights in the same group, a
pole ID is used.
The traffic lights in the same group follow a cycle. The first one is set to green while the rest remain frozen in red. The active one spends a few seconds in green, yellow and red, so there is a period of time where all the lights are red. Then, the next traffic light starts its cycle, and the previous one is frozen with the rest.
The state of a traffic light can be set using the API. So does the seconds spent on each state. Possible states are described with carla.TrafficLightState as a series of enum values.
#Change a red traffic light to green if traffic_light.get_state() == carla.TrafficLightState.Red: traffic_light.set_state(carla.TrafficLightState.Green) traffic_light.set_set_green_time(4.0)
carla.Vehicle are a special type of actor. They are remarkable for having better physics. This is achieved applying four types of different controls.
- carla.VehicleControl provides input for driving commands such as throttle, steering, brake, etc.
- carla.VehiclePhysicsControl defines physical attributes of the vehicle. Besides many different attribute, this controller contains two more controllers. carla.GearPhysicsControl for the gears. The other is a list of carla.WheelPhysicsControl, that provide specific control over the different wheels.
vehicle.apply_physics_control(carla.VehiclePhysicsControl(max_rpm = 5000.0, center_of_mass = carla.Vector3D(0.0, 0.0, 0.0), torque_curve=[[0,400],[5000,400]]))
In order to apply physics and detect collisions, vehicles have a carla.BoundingBox encapsulating them.
box = vehicle.bounding_box print(box.location) # Location relative to the vehicle. print(box.extent) # XYZ half-box extents in meters.
Vehicles include other functionalities unique to them.
- The autopilot mode will subscribe them to the Traffic manager, and simulate real urban conditions. This module is hard-coded, not based on machine learning.
- Vehicle lights created specifically for each vehicle model. They can be accessed from the API. In order to turn them on/off, the vehicle uses flag values. These are defined in carla.VehicleLightState and binary operations are used to combine them.
# Turn on position lights current_lights = carla.VehicleLightState.NONE current_lights |= carla.VehicleLightState.Position vehicle.set_light_state(current_lights)
So far, vehicle lights have been implemented only for a specific set of vehicles, listed in the release post.
carla.Walker work in a similar way as vehicles do. Control over them is provided by controllers.
- carla.WalkerControl moves the pedestrian around with a certain direction and speed. It also allows them to jump.
- carla.WalkerBoneControl provides control over the 3D skeleton. This tutorial explains how to control it.
Walkers can be AI controlled. They do not have an autopilot mode. The carla.WalkerAIController actor moves around the actor it is attached to.
walker_controller_bp = world.get_blueprint_library().find('controller.ai.walker') world.SpawnActor(walker_controller_bp, carla.Transform(), parent_walker)
The AI controller is bodiless and has no physics. It will not appear on scene. Also, location
(0,0,0) relative to its parent will not cause a collision.
Each AI controller needs initialization, a goal and, optionally, a speed. Stopping the controller works in the same manner.
ai_controller.start() ai_controller.go_to_location(world.get_random_location_from_navigation()) ai_controller.set_max_speed(1 + random.random()) # Between 1 and 2 m/s (default is 1.4 m/s). ... ai_controller.stop()
When a walker reaches the target location, they will automatically walk to another random point. If the target point is not reachable, walkers will go to the closest point from their current location.
This recipe uses batches to spawn a lot of walkers and make them wander around.
To destroy AI pedestrians, stop the AI controller and destroy both, the actor, and the controller.
That is a wrap as regarding actors in CARLA. The next step takes a closer look into the map, roads and traffic in CARLA.
Keep reading to learn more or visit the forum to post any doubts or suggestions that have come to mind during this reading.