Implementation:CARLA simulator Carla RPC Command
| Knowledge Sources | |
|---|---|
| Domains | Client-Server Communication, RPC, Actor Control |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Command defines a variant-based RPC command type that encapsulates all batch-executable actor operations (spawn, destroy, control, physics, transforms) for efficient client-to-server communication in CARLA.
Description
The Command class (in carla::rpc, defined in Command.h, 314 lines) implements a type-safe discriminated union (std::variant) of all operations that can be sent as batch commands from the CARLA client to the server. This enables the client to bundle multiple actor operations into a single RPC call, reducing network round trips.
Architecture: The class uses the Curiously Recurring Template Pattern (CRTP) via the private CommandBase<T> struct. Each command struct inherits from CommandBase<T>, which provides an implicit conversion operator Command() that wraps the specific command into the Command variant. All command structs are serializable via MSGPACK_DEFINE_ARRAY macros.
Command Types:
Actor Lifecycle:
- SpawnActor - Creates a new actor with an ActorDescription, Transform, optional parent actor ID, and a do_after vector of chained commands to execute after spawning
- DestroyActor - Removes an actor by its ActorId
Vehicle Control:
- ApplyVehicleControl - Applies throttle, steer, brake, hand brake, reverse, and gear via VehicleControl
- ApplyVehicleAckermannControl - Applies Ackermann-model steering via VehicleAckermannControl
- ApplyVehiclePhysicsControl - Sets detailed physics parameters via VehiclePhysicsControl
Walker Control:
- ApplyWalkerControl - Applies direction and speed via WalkerControl
- ApplyWalkerState - Sets walker transform and speed simultaneously
Physics:
- ApplyTransform - Teleports an actor to a new Transform
- ApplyLocation - Teleports an actor to a new Location (position only)
- ApplyTargetVelocity - Sets target linear velocity as Vector3D
- ApplyTargetAngularVelocity - Sets target angular velocity as Vector3D
- ApplyImpulse - Applies an instantaneous linear impulse
- ApplyForce - Applies a continuous force
- ApplyAngularImpulse - Applies an instantaneous angular impulse
- ApplyTorque - Applies a continuous torque
Settings:
- SetSimulatePhysics - Enables or disables physics simulation for an actor
- SetEnableGravity - Enables or disables gravity for an actor
- SetAutopilot - Toggles autopilot with a traffic manager port
- ShowDebugTelemetry - Toggles debug telemetry display
- SetVehicleLightState - Sets vehicle light flags
- SetTrafficLightState - Sets traffic light state
Utility:
- ConsoleCommand - Executes a console command string on the server
The CommandType variant aggregates all 21 command structs into a single serializable type, stored in the command member field.
Usage
Use Command when you need to execute multiple actor operations in a single simulation tick. The CARLA Python and C++ APIs provide apply_batch() and apply_batch_sync() methods that accept vectors of Command objects. This is especially useful for spawning many actors, applying controls to large fleets, or performing bulk physics operations.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/rpc/Command.h - Lines: 1-314
Signature
namespace carla {
namespace rpc {
class Command {
private:
template <typename T>
struct CommandBase {
operator Command() const;
};
public:
struct SpawnActor : CommandBase<SpawnActor> {
ActorDescription description;
geom::Transform transform;
std::optional<ActorId> parent;
std::vector<Command> do_after;
MSGPACK_DEFINE_ARRAY(description, transform, parent, do_after);
};
struct DestroyActor : CommandBase<DestroyActor> { ActorId actor; };
struct ApplyVehicleControl : CommandBase<ApplyVehicleControl> { ActorId actor; VehicleControl control; };
struct ApplyVehicleAckermannControl : CommandBase<ApplyVehicleAckermannControl> { ActorId actor; VehicleAckermannControl control; };
struct ApplyWalkerControl : CommandBase<ApplyWalkerControl> { ActorId actor; WalkerControl control; };
struct ApplyVehiclePhysicsControl : CommandBase<ApplyVehiclePhysicsControl> { ActorId actor; VehiclePhysicsControl physics_control; };
struct ApplyTransform : CommandBase<ApplyTransform> { ActorId actor; geom::Transform transform; };
struct ApplyLocation : CommandBase<ApplyLocation> { ActorId actor; geom::Location location; };
struct ApplyWalkerState : CommandBase<ApplyWalkerState> { ActorId actor; geom::Transform transform; float speed; };
struct ApplyTargetVelocity : CommandBase<ApplyTargetVelocity> { ActorId actor; geom::Vector3D velocity; };
struct ApplyTargetAngularVelocity : CommandBase<ApplyTargetAngularVelocity> { ActorId actor; geom::Vector3D angular_velocity; };
struct ApplyImpulse : CommandBase<ApplyImpulse> { ActorId actor; geom::Vector3D impulse; };
struct ApplyForce : CommandBase<ApplyForce> { ActorId actor; geom::Vector3D force; };
struct ApplyAngularImpulse : CommandBase<ApplyAngularImpulse> { ActorId actor; geom::Vector3D impulse; };
struct ApplyTorque : CommandBase<ApplyTorque> { ActorId actor; geom::Vector3D torque; };
struct SetSimulatePhysics : CommandBase<SetSimulatePhysics> { ActorId actor; bool enabled; };
struct SetEnableGravity : CommandBase<SetEnableGravity> { ActorId actor; bool enabled; };
struct SetAutopilot : CommandBase<SetAutopilot> { ActorId actor; bool enabled; uint16_t tm_port; };
struct ShowDebugTelemetry : CommandBase<ShowDebugTelemetry> { ActorId actor; bool enabled; };
struct SetVehicleLightState : CommandBase<SetVehicleLightState> { ActorId actor; VehicleLightState::flag_type light_state; };
struct ConsoleCommand : CommandBase<ConsoleCommand> { std::string cmd; };
struct SetTrafficLightState : CommandBase<SetTrafficLightState> { ActorId actor; rpc::TrafficLightState traffic_light_state; };
using CommandType = std::variant<
SpawnActor, DestroyActor, ApplyVehicleControl, ApplyVehicleAckermannControl,
ApplyWalkerControl, ApplyVehiclePhysicsControl, ApplyTransform,
ApplyWalkerState, ApplyTargetVelocity, ApplyTargetAngularVelocity,
ApplyImpulse, ApplyForce, ApplyAngularImpulse, ApplyTorque,
SetSimulatePhysics, SetEnableGravity, SetAutopilot, ShowDebugTelemetry,
SetVehicleLightState, ApplyLocation, ConsoleCommand, SetTrafficLightState>;
CommandType command;
MSGPACK_DEFINE_ARRAY(command);
};
} // namespace rpc
} // namespace carla
Import
#include "carla/rpc/Command.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| actor | ActorId |
Yes | Target actor identifier for the command (not needed for SpawnActor/ConsoleCommand) |
| description | ActorDescription |
Yes (SpawnActor) | Blueprint description for the actor to spawn |
| transform | geom::Transform |
Yes (SpawnActor, ApplyTransform) | 3D position and rotation for placement |
| control | VehicleControl / WalkerControl |
Yes (Apply*Control) | Control inputs for vehicle or walker |
| parent | std::optional<ActorId> |
No | Optional parent actor for attached spawning |
| do_after | std::vector<Command> |
No | Chained commands to execute after SpawnActor completes |
Outputs
| Name | Type | Description |
|---|---|---|
| Command | Command |
Serializable command object ready for batch RPC transmission |
| CommandType | std::variant<...> |
The underlying variant holding the specific command data |
Usage Examples
Batch Spawning Actors
#include "carla/rpc/Command.h"
// Create spawn commands
std::vector<carla::rpc::Command> batch;
for (const auto &spawn_point : spawn_points) {
batch.push_back(carla::rpc::Command::SpawnActor{
vehicle_blueprint,
spawn_point
});
}
// Send as batch to server
client.ApplyBatch(std::move(batch));
Applying Controls to Multiple Vehicles
#include "carla/rpc/Command.h"
std::vector<carla::rpc::Command> batch;
// Apply brake to all vehicles
for (auto actor_id : vehicle_ids) {
carla::rpc::VehicleControl ctrl;
ctrl.brake = 1.0f;
batch.push_back(carla::rpc::Command::ApplyVehicleControl{actor_id, ctrl});
}
client.ApplyBatchSync(std::move(batch));
Chaining Commands with SpawnActor
#include "carla/rpc/Command.h"
// Spawn a vehicle and immediately set autopilot
carla::rpc::Command::SpawnActor spawn_cmd{
vehicle_blueprint,
spawn_transform
};
// Chain an autopilot command to run after spawning
spawn_cmd.do_after.push_back(
carla::rpc::Command::SetAutopilot{0, true, 8000}
);