CARLA
Vehicle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 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 
7 #include "carla/client/Vehicle.h"
8 
12 #include "carla/Memory.h"
14 
16 
17 namespace carla {
18 
20 
21 namespace 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 
95  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.control;
96  }
97 
99  return GetEpisode().Lock()->GetVehiclePhysicsControl(*this);
100  }
101 
103  return GetEpisode().Lock()->GetVehicleLightState(*this).GetLightStateEnum();
104  }
105 
106  float Vehicle::GetSpeedLimit() const {
107  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.speed_limit;
108  }
109 
111  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_state;
112  }
113 
115  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.has_traffic_light;
116  }
117 
119  auto id = GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_id;
120  return boost::static_pointer_cast<TrafficLight>(GetWorld().GetActor(id));
121  }
122 
123  void Vehicle::EnableCarSim(std::string simfile_path) {
124  GetEpisode().Lock()->EnableCarSim(*this, simfile_path);
125  }
126 
127  void Vehicle::UseCarSimRoad(bool enabled) {
128  GetEpisode().Lock()->UseCarSimRoad(*this, enabled);
129  }
130 
132  uint64_t MaxSubsteps,
133  float MaxSubstepDeltaTime,
134  std::string VehicleJSON,
135  std::string PowertrainJSON,
136  std::string TireJSON,
137  std::string BaseJSONPath) {
138  GetEpisode().Lock()->EnableChronoPhysics(*this,
139  MaxSubsteps,
140  MaxSubstepDeltaTime,
141  VehicleJSON,
142  PowertrainJSON,
143  TireJSON,
144  BaseJSONPath);
145  }
146 
148  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.failure_state;
149  }
150 
151 } // namespace client
152 } // namespace carla
SharedPtr< TrafficLight > GetTrafficLight() const
Retrieve the traffic light actor currently affecting this vehicle.
Definition: Vehicle.cpp:118
Vehicle(ActorInitializer init)
Definition: Vehicle.cpp:34
float GetWheelSteerAngle(WheelLocation wheel_location)
Return a Rotation from a wheel of the vehicle.
Definition: Vehicle.cpp:90
void SetAutopilot(bool enabled=true, uint16_t tm_port=TM_DEFAULT_PORT)
Switch on/off this vehicle&#39;s autopilot.
Definition: Vehicle.cpp:38
void EnableCarSim(std::string simfile_path)
Enables CarSim simulation if it is availiable.
Definition: Vehicle.cpp:123
float GetSpeedLimit() const
Return the speed limit currently affecting this vehicle.
Definition: Vehicle.cpp:106
rpc::VehicleFailureState GetFailureState() const
Returns the failure state of the vehicle.
Definition: Vehicle.cpp:147
void EnableChronoPhysics(uint64_t MaxSubsteps, float MaxSubstepDeltaTime, std::string VehicleJSON="", std::string PowertrainJSON="", std::string TireJSON="", std::string BaseJSONPath="")
Definition: Vehicle.cpp:131
const bool _is_control_sticky
Definition: Vehicle.h:145
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
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
LightState GetLightState() const
Return the current open lights (LightState) of this vehicle.
Definition: Vehicle.cpp:102
Used to initialize Actor classes.
Control GetControl() const
Return the control last applied to this vehicle.
Definition: Vehicle.cpp:94
bool IsAtTrafficLight()
Return whether a traffic light is affecting this vehicle.
Definition: Vehicle.cpp:114
void ApplyAckermannControllerSettings(const rpc::AckermannControllerSettings &settings)
Apply Ackermann control settings to this vehicle.
Definition: Vehicle.cpp:66
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
rpc::AckermannControllerSettings GetAckermannControllerSettings() const
Return the last Ackermann controller settings applied to this vehicle.
Definition: Vehicle.cpp:62
This class integrates all the various stages of the traffic manager appropriately using messengers...
rpc::TrafficLightState GetTrafficLightState() const
Return the state of the traffic light currently affecting this vehicle.
Definition: Vehicle.cpp:110
void ApplyControl(const Control &control)
Apply control to this vehicle.
Definition: Vehicle.cpp:51
void SetLightState(const LightState &light_state)
Sets a LightState to this vehicle.
Definition: Vehicle.cpp:82
PhysicsControl GetPhysicsControl() const
Return the physics control last applied to this vehicle.
Definition: Vehicle.cpp:98
void ShowDebugTelemetry(bool enabled=true)
Switch on/off this vehicle&#39;s autopilot.
Definition: Vehicle.cpp:47
void ApplyPhysicsControl(const PhysicsControl &physics_control)
Apply physics control to this vehicle.
Definition: Vehicle.cpp:70
Represents an actor in the simulation.
Definition: client/Actor.h:18
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
void OpenDoor(const VehicleDoor door_idx)
Open a door in this vehicle.
Definition: Vehicle.cpp:74
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
void ApplyAckermannControl(const AckermannControl &control)
Apply control to this vehicle.
Definition: Vehicle.cpp:58
void UseCarSimRoad(bool enabled)
Enables the use of CarSim internal road definition instead of unreal&#39;s.
Definition: Vehicle.cpp:127
void CloseDoor(const VehicleDoor door_idx)
Close a door in this vehicle.
Definition: Vehicle.cpp:78