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::ApplyControl(const Control &control) {
48  if (!_is_control_sticky || (control != _control)) {
49  GetEpisode().Lock()->ApplyControlToVehicle(*this, control);
50  _control = control;
51  }
52  }
53 
54  void Vehicle::ApplyPhysicsControl(const PhysicsControl &physics_control) {
55  GetEpisode().Lock()->ApplyPhysicsControlToVehicle(*this, physics_control);
56  }
57 
58  void Vehicle::SetLightState(const LightState &light_state) {
59  GetEpisode().Lock()->SetLightStateToVehicle(*this, rpc::VehicleLightState(light_state));
60  }
61 
63  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.control;
64  }
65 
67  return GetEpisode().Lock()->GetVehiclePhysicsControl(*this);
68  }
69 
71  return GetEpisode().Lock()->GetVehicleLightState(*this).GetLightStateEnum();
72  }
73 
74  float Vehicle::GetSpeedLimit() const {
75  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.speed_limit;
76  }
77 
79  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_state;
80  }
81 
83  return GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.has_traffic_light;
84  }
85 
87  auto id = GetEpisode().Lock()->GetActorSnapshot(*this).state.vehicle_data.traffic_light_id;
88  return boost::static_pointer_cast<TrafficLight>(GetWorld().GetActor(id));
89  }
90 
91  void Vehicle::EnableCarSim(std::string simfile_path) {
92  GetEpisode().Lock()->EnableCarSim(*this, simfile_path);
93  }
94 
95  void Vehicle::UseCarSimRoad(bool enabled) {
96  GetEpisode().Lock()->UseCarSimRoad(*this, enabled);
97  }
98 
99 } // namespace client
100 } // namespace carla
SharedPtr< TrafficLight > GetTrafficLight() const
Retrieve the traffic light actor currently affecting this vehicle.
Definition: Vehicle.cpp:86
Vehicle(ActorInitializer init)
Definition: Vehicle.cpp:34
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:91
float GetSpeedLimit() const
Return the speed limit currently affecting this vehicle.
Definition: Vehicle.cpp:74
const bool _is_control_sticky
Definition: Vehicle.h:98
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:99
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:70
Used to initialize Actor classes.
Definition: ActorState.h:93
Control GetControl() const
Return the control last applied to this vehicle.
Definition: Vehicle.cpp:62
bool IsAtTrafficLight()
Return whether a traffic light is affecting this vehicle.
Definition: Vehicle.cpp:82
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
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:78
void ApplyControl(const Control &control)
Apply control to this vehicle.
Definition: Vehicle.cpp:47
void SetLightState(const LightState &light_state)
Sets a LightState to this vehicle.
Definition: Vehicle.cpp:58
PhysicsControl GetPhysicsControl() const
Return the physics control last applied to this vehicle.
Definition: Vehicle.cpp:66
void ApplyPhysicsControl(const PhysicsControl &physics_control)
Apply physics control to this vehicle.
Definition: Vehicle.cpp:54
Represents an actor in the simulation.
Definition: client/Actor.h:18
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
void UseCarSimRoad(bool enabled)
Enables the use of CarSim internal road definition instead of unreal&#39;s.
Definition: Vehicle.cpp:95