CARLA
Loading...
Searching...
No Matches
Vehicle.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8
12#include "carla/Memory.h"
14
16
17namespace carla {
18
20
21namespace client {
22
23
24 template <typename AttributesT>
25 static bool GetControlIsSticky(const AttributesT &attributes) {
26 for (auto &&attribute : attributes) {
27 if (attribute.GetId() == "sticky_control") {
28 return attribute.template As<bool>();
29 }
30 }
31 return true;
32 }
33
35 : Actor(std::move(init)),
36 _is_control_sticky(GetControlIsSticky(GetAttributes())) {}
37
38 void Vehicle::SetAutopilot(bool enabled, uint16_t tm_port) {
39 TM tm(GetEpisode(), tm_port);
40 if (enabled) {
41 tm.RegisterVehicles({shared_from_this()});
42 } else {
43 tm.UnregisterVehicles({shared_from_this()});
44 }
45 }
46
47 void Vehicle::ShowDebugTelemetry(bool enabled) {
48 GetEpisode().Lock()->ShowVehicleDebugTelemetry(*this, enabled);
49 }
50
51 void Vehicle::ApplyControl(const Control &control) {
52 if (!_is_control_sticky || (control != _control)) {
53 GetEpisode().Lock()->ApplyControlToVehicle(*this, control);
54 _control = control;
55 }
56 }
57
59 GetEpisode().Lock()->ApplyAckermannControlToVehicle(*this, control);
60 }
61
63 return GetEpisode().Lock()->GetAckermannControllerSettings(*this);
64 }
65
67 GetEpisode().Lock()->ApplyAckermannControllerSettings(*this, settings);
68 }
69
70 void Vehicle::ApplyPhysicsControl(const PhysicsControl &physics_control) {
71 GetEpisode().Lock()->ApplyPhysicsControlToVehicle(*this, physics_control);
72 }
73
74 void Vehicle::OpenDoor(const VehicleDoor door_idx) {
75 GetEpisode().Lock()->OpenVehicleDoor(*this, rpc::VehicleDoor(door_idx));
76 }
77
78 void Vehicle::CloseDoor(const VehicleDoor door_idx) {
79 GetEpisode().Lock()->CloseVehicleDoor(*this, rpc::VehicleDoor(door_idx));
80 }
81
82 void Vehicle::SetLightState(const LightState &light_state) {
83 GetEpisode().Lock()->SetLightStateToVehicle(*this, rpc::VehicleLightState(light_state));
84 }
85
86 void Vehicle::SetWheelSteerDirection(WheelLocation wheel_location, float angle_in_deg) {
87 GetEpisode().Lock()->SetWheelSteerDirection(*this, wheel_location, angle_in_deg);
88 }
89
91 return GetEpisode().Lock()->GetWheelSteerAngle(*this, wheel_location);
92 }
93
94 void Vehicle::SetWheelPitchAngle(WheelLocation wheel_location, float angle_in_deg) {
95 GetEpisode().Lock()->SetWheelPitchAngle(*this, wheel_location, angle_in_deg);
96 }
97
99 return GetEpisode().Lock()->GetWheelPitchAngle(*this, wheel_location);
100 }
101
102 void Vehicle::SetWheelSuspensionOffset(WheelLocation wheel_location, float offset) {
103 GetEpisode().Lock()->SetWheelSuspensionOffset(*this, wheel_location, offset);
104 }
105
107 return GetEpisode().Lock()->GetWheelSuspensionOffset(*this, wheel_location);
108 }
109
111 GetEpisode().Lock()->SetWheelAnimationOverride(*this, enabled);
112 }
113
115 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.control;
116 }
117
119 return GetEpisode().Lock()->GetVehiclePhysicsControl(*this);
120 }
121
123 return GetEpisode().Lock()->GetVehicleTelemetryData(*this);
124 }
125
127 return GetEpisode().Lock()->GetVehicleLightState(*this).GetLightStateEnum();
128 }
129
130 std::vector<geom::Transform> Vehicle::GetVehicleBoneWorldTransforms() const {
131 return GetEpisode().Lock()->GetVehicleBoneWorldTransforms(*this);
132 }
133
135 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.speed_limit;
136 }
137
139 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_state;
140 }
141
143 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.has_traffic_light;
144 }
145
147 auto id = GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_id;
148 return std::static_pointer_cast<TrafficLight>(GetWorld().GetActor(id));
149 }
150
151 void Vehicle::EnableCarSim(std::string simfile_path) {
152 GetEpisode().Lock()->EnableCarSim(*this, simfile_path);
153 }
154
155 void Vehicle::UseCarSimRoad(bool enabled) {
156 GetEpisode().Lock()->UseCarSimRoad(*this, enabled);
157 }
158
160 uint64_t MaxSubsteps,
161 float MaxSubstepDeltaTime,
162 std::string VehicleJSON,
163 std::string PowertrainJSON,
164 std::string TireJSON,
165 std::string BaseJSONPath) {
166 GetEpisode().Lock()->EnableChronoPhysics(*this,
167 MaxSubsteps,
168 MaxSubstepDeltaTime,
169 VehicleJSON,
170 PowertrainJSON,
171 TireJSON,
172 BaseJSONPath);
173 }
174
176 return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.failure_state;
177 }
178
179} // namespace client
180} // namespace carla
Used to initialize Actor classes.
Represents an actor in the simulation.
void SetWheelSteerDirection(WheelLocation wheel_location, float angle_in_deg)
Sets a Rotation to a wheel of the vehicle (affects the bone of the car skeleton, not the physics)
Definition Vehicle.cpp:86
TelemetryData GetTelemetryData() const
Return the runtime telemetry of this vehicle: forward speed, last applied control inputs,...
Definition Vehicle.cpp:122
void ApplyControl(const Control &control)
Apply control to this vehicle.
Definition Vehicle.cpp:51
void SetWheelAnimationOverride(bool enabled)
Enables or disables the wheel animation override, in which the wheels show only the values set throug...
Definition Vehicle.cpp:110
PhysicsControl GetPhysicsControl() const
Return the physics control last applied to this vehicle.
Definition Vehicle.cpp:118
float GetWheelSteerAngle(WheelLocation wheel_location)
Return a Rotation from a wheel of the vehicle.
Definition Vehicle.cpp:90
void CloseDoor(const VehicleDoor door_idx)
Close a door in this vehicle.
Definition Vehicle.cpp:78
Vehicle(ActorInitializer init)
Definition Vehicle.cpp:34
LightState GetLightState() const
Return the current open lights (LightState) of this vehicle.
Definition Vehicle.cpp:126
bool IsAtTrafficLight()
Return whether a traffic light is affecting this vehicle.
Definition Vehicle.cpp:142
void EnableCarSim(std::string simfile_path)
Enables CarSim simulation if it is availiable.
Definition Vehicle.cpp:151
rpc::TrafficLightState GetTrafficLightState() const
Return the state of the traffic light currently affecting this vehicle.
Definition Vehicle.cpp:138
void ApplyAckermannControl(const AckermannControl &control)
Apply control to this vehicle.
Definition Vehicle.cpp:58
float GetWheelSuspensionOffset(WheelLocation wheel_location)
Return the suspension compression of a wheel, in meters.
Definition Vehicle.cpp:106
void ApplyPhysicsControl(const PhysicsControl &physics_control)
Apply physics control to this vehicle.
Definition Vehicle.cpp:70
rpc::AckermannControllerSettings GetAckermannControllerSettings() const
Return the last Ackermann controller settings applied to this vehicle.
Definition Vehicle.cpp:62
float GetSpeedLimit() const
Return the speed limit currently affecting this vehicle.
Definition Vehicle.cpp:134
void SetWheelPitchAngle(WheelLocation wheel_location, float angle_in_deg)
Sets the spin angle of a wheel, in degrees (affects the bone of the car skeleton, not the physics)
Definition Vehicle.cpp:94
SharedPtr< TrafficLight > GetTrafficLight() const
Retrieve the traffic light actor currently affecting this vehicle.
Definition Vehicle.cpp:146
void SetWheelSuspensionOffset(WheelLocation wheel_location, float offset)
Sets the suspension compression of a wheel, in meters (affects the bone of the car skeleton,...
Definition Vehicle.cpp:102
Control GetControl() const
Return the control last applied to this vehicle.
Definition Vehicle.cpp:114
std::vector< geom::Transform > GetVehicleBoneWorldTransforms() const
Definition Vehicle.cpp:130
void UseCarSimRoad(bool enabled)
Enables the use of CarSim internal road definition instead of unreal's.
Definition Vehicle.cpp:155
void ApplyAckermannControllerSettings(const rpc::AckermannControllerSettings &settings)
Apply Ackermann control settings to this vehicle.
Definition Vehicle.cpp:66
void OpenDoor(const VehicleDoor door_idx)
Open a door in this vehicle.
Definition Vehicle.cpp:74
void EnableChronoPhysics(uint64_t MaxSubsteps, float MaxSubstepDeltaTime, std::string VehicleJSON="", std::string PowertrainJSON="", std::string TireJSON="", std::string BaseJSONPath="")
Definition Vehicle.cpp:159
float GetWheelPitchAngle(WheelLocation wheel_location)
Return the spin angle of a wheel, in degrees.
Definition Vehicle.cpp:98
void SetLightState(const LightState &light_state)
Sets a LightState to this vehicle.
Definition Vehicle.cpp:82
rpc::VehicleFailureState GetFailureState() const
Returns the failure state of the vehicle.
Definition Vehicle.cpp:175
const bool _is_control_sticky
Definition Vehicle.h:174
void SetAutopilot(bool enabled=true, uint16_t tm_port=TM_DEFAULT_PORT)
Switch on/off this vehicle's autopilot.
Definition Vehicle.cpp:38
void ShowDebugTelemetry(bool enabled=true)
Switch on/off this vehicle's autopilot.
Definition Vehicle.cpp:47
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
This class integrates all the various stages of the traffic manager appropriately using messengers.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
static bool GetControlIsSticky(const AttributesT &attributes)
Definition Vehicle.cpp:25
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15