Implementation:CARLA simulator Carla Vehicle Class
| Knowledge Sources | |
|---|---|
| Domains | Client Library, Vehicle Control, Autonomous Driving |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Vehicle is the client-side class representing a vehicle actor in CARLA, providing comprehensive control over driving, physics, lighting, doors, autopilot, and integration with external physics engines.
Description
The Vehicle class in the carla::client namespace extends Actor and provides vehicle-specific functionality. It defines several type aliases for convenience:
- Control =
rpc::VehicleControl - AckermannControl =
rpc::VehicleAckermannControl - PhysicsControl =
rpc::VehiclePhysicsControl - LightState =
rpc::VehicleLightState::LightState - VehicleDoor =
rpc::VehicleDoor - WheelLocation =
carla::rpc::VehicleWheelLocation
Construction
The constructor checks the actor's attributes for a sticky_control attribute (defaulting to true). When sticky control is enabled, ApplyControl only sends commands to the simulator when the control values actually change, optimizing network traffic.
Driving Control
- ApplyControl(control) -- Applies throttle, steering, brake, etc. Only sends to simulator if control changed (when sticky).
- ApplyAckermannControl(control) -- Applies Ackermann steering control model.
- GetAckermannControllerSettings() / ApplyAckermannControllerSettings(settings) -- Get/set Ackermann controller parameters.
- SetAutopilot(enabled, tm_port) -- Registers/unregisters the vehicle with the TrafficManager on the specified port (default: TM_DEFAULT_PORT).
- ShowDebugTelemetry(enabled) -- Toggle debug telemetry display.
Physics Control
- ApplyPhysicsControl(physics_control) -- Sets detailed physics parameters (mass, drag, engine, wheels, etc.).
- GetPhysicsControl() -- Retrieves current physics control (calls simulator).
- GetControl() -- Returns the last applied control from the last tick snapshot.
Vehicle Features
- OpenDoor(door_idx) / CloseDoor(door_idx) -- Open/close vehicle doors by index.
- SetLightState(light_state) / GetLightState() -- Control vehicle lights (headlights, brake lights, etc.).
- SetWheelSteerDirection(wheel_location, angle_in_deg) -- Set wheel steer direction (visual only, affects bone).
- GetWheelSteerAngle(wheel_location) -- Get physics-based wheel steer angle.
- GetVehicleBoneWorldTransforms() -- Returns world transforms of all vehicle bones.
Traffic Awareness
- GetSpeedLimit() -- Returns the current speed limit affecting the vehicle.
- GetTrafficLightState() -- Returns the state of the current traffic light (Green if none).
- IsAtTrafficLight() -- Returns whether a traffic light is affecting this vehicle.
- GetTrafficLight() -- Returns the traffic light actor affecting this vehicle.
External Physics Engines
- EnableCarSim(simfile_path) -- Enables CarSim simulation.
- UseCarSimRoad(enabled) -- Uses CarSim internal road definition instead of Unreal's.
- EnableChronoPhysics(MaxSubsteps, MaxSubstepDeltaTime, VehicleJSON, PowertrainJSON, TireJSON, BaseJSONPath) -- Enables Project Chrono physics simulation.
Failure State
- GetFailureState() -- Returns the current rpc::VehicleFailureState.
Usage
Vehicles are the primary actors in most CARLA scenarios. Spawn them from the blueprint library, then control them via direct control commands, autopilot, or Ackermann steering.
Code Reference
Source Location
- Repository: CARLA
- Files:
LibCarla/source/carla/client/Vehicle.h(153 lines)LibCarla/source/carla/client/Vehicle.cpp(156 lines)
Signature
namespace carla {
namespace client {
class Vehicle : public Actor {
public:
using Control = rpc::VehicleControl;
using AckermannControl = rpc::VehicleAckermannControl;
using PhysicsControl = rpc::VehiclePhysicsControl;
using LightState = rpc::VehicleLightState::LightState;
using VehicleDoor = rpc::VehicleDoor;
using WheelLocation = carla::rpc::VehicleWheelLocation;
explicit Vehicle(ActorInitializer init);
void SetAutopilot(bool enabled = true, uint16_t tm_port = TM_DEFAULT_PORT);
void ShowDebugTelemetry(bool enabled = true);
void ApplyControl(const Control &control);
void ApplyAckermannControl(const AckermannControl &control);
rpc::AckermannControllerSettings GetAckermannControllerSettings() const;
void ApplyAckermannControllerSettings(const rpc::AckermannControllerSettings &settings);
void ApplyPhysicsControl(const PhysicsControl &physics_control);
void OpenDoor(const VehicleDoor door_idx);
void CloseDoor(const VehicleDoor door_idx);
void SetLightState(const LightState &light_state);
void SetWheelSteerDirection(WheelLocation wheel_location, float angle_in_deg);
float GetWheelSteerAngle(WheelLocation wheel_location);
Control GetControl() const;
PhysicsControl GetPhysicsControl() const;
LightState GetLightState() const;
float GetSpeedLimit() const;
rpc::TrafficLightState GetTrafficLightState() const;
bool IsAtTrafficLight();
SharedPtr<TrafficLight> GetTrafficLight() const;
void EnableCarSim(std::string simfile_path);
void UseCarSimRoad(bool enabled);
void EnableChronoPhysics(uint64_t MaxSubsteps, float MaxSubstepDeltaTime,
std::string VehicleJSON, std::string PowertrainJSON,
std::string TireJSON, std::string BaseJSONPath);
rpc::VehicleFailureState GetFailureState() const;
private:
const bool _is_control_sticky;
Control _control;
};
} // namespace client
} // namespace carla
Import
#include "carla/client/Vehicle.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| init | ActorInitializer | Yes | Actor initialization data with sticky_control attribute check |
| control | Control (rpc::VehicleControl) | No | Throttle, steer, brake, hand_brake, reverse, gear |
| physics_control | PhysicsControl | No | Full physics configuration (mass, engine, wheels, etc.) |
| enabled | bool | No | Toggle for autopilot or telemetry |
| tm_port | uint16_t | No | Traffic Manager port (default: TM_DEFAULT_PORT) |
| door_idx | VehicleDoor | No | Door index for open/close operations |
| light_state | LightState | No | Vehicle light configuration |
Outputs
| Name | Type | Description |
|---|---|---|
| control | Control | Last applied vehicle control from tick snapshot |
| physics_control | PhysicsControl | Current physics control (simulator call) |
| light_state | LightState | Current vehicle light state |
| speed_limit | float | Current speed limit from last tick |
| traffic_light_state | rpc::TrafficLightState | State of the affecting traffic light |
| traffic_light | SharedPtr<TrafficLight> | The traffic light actor affecting this vehicle |
| failure_state | rpc::VehicleFailureState | Current vehicle failure state |
Usage Examples
// Spawn a vehicle
auto bp = blueprint_library->Find("vehicle.tesla.model3");
auto vehicle = std::static_pointer_cast<carla::client::Vehicle>(
world.SpawnActor(*bp, spawn_point));
// Enable autopilot
vehicle->SetAutopilot(true);
// Manual control
carla::rpc::VehicleControl control;
control.throttle = 0.5f;
control.steer = 0.0f;
control.brake = 0.0f;
vehicle->ApplyControl(control);
// Check traffic light
if (vehicle->IsAtTrafficLight()) {
auto tl = vehicle->GetTrafficLight();
if (tl->GetState() == carla::rpc::TrafficLightState::Red) {
control.brake = 1.0f;
vehicle->ApplyControl(control);
}
}
// Open driver door
vehicle->OpenDoor(carla::rpc::VehicleDoor::FL);
Related Pages
- Environment:CARLA_simulator_Carla_Simulation_Runtime
- CARLA_simulator_Carla_Actor_Class - Base Actor class
- CARLA_simulator_Carla_TrafficLight_Class - Traffic lights that affect vehicles
- CARLA_simulator_Carla_Python_Control_Bindings - Python bindings for vehicle control types
- CARLA_simulator_Carla_RssCheck - RSS safety checks for vehicles