CARLA
VehicleLightStage.cpp
Go to the documentation of this file.
1 
4 
6 
7 namespace carla {
8 namespace traffic_manager {
9 
10 using namespace constants::VehicleLight;
11 
13  const std::vector<ActorId> &vehicle_id_list,
14  const BufferMap &buffer_map,
15  const Parameters &parameters,
16  const cc::World &world,
17  ControlFrame& control_frame)
18  : vehicle_id_list(vehicle_id_list),
19  buffer_map(buffer_map),
20  parameters(parameters),
21  world(world),
22  control_frame(control_frame) {}
23 
25  // Get the global weather and all the vehicle light states at once
28 }
29 
30 void VehicleLightStage::Update(const unsigned long index) {
31  ActorId actor_id = vehicle_id_list.at(index);
32 
33  if (!parameters.GetUpdateVehicleLights(actor_id))
34  return; // this vehicle is not set to have automatic lights update
35 
36  rpc::VehicleLightState::flag_type light_states = uint32_t(-1);
37  bool brake_lights = false;
38  bool left_turn_indicator = false;
39  bool right_turn_indicator = false;
40  bool position = false;
41  bool low_beam = false;
42  bool high_beam = false;
43  bool fog_lights = false;
44 
45  // search the current light state of the vehicle
46  for (auto&& vls : all_light_states) {
47  if (vls.first == actor_id) {
48  light_states = vls.second;
49  break;
50  }
51  }
52 
53  // Determine if the vehicle is truning left or right by checking the close waypoints
54 
55  const Buffer& waypoint_buffer = buffer_map.at(actor_id);
56  cg::Location front_location = waypoint_buffer.front()->GetLocation();
57 
58  for (const SimpleWaypointPtr& waypoint : waypoint_buffer) {
59  if (waypoint->CheckJunction()) {
60  RoadOption target_ro = waypoint->GetRoadOption();
61  if (target_ro == RoadOption::Left) left_turn_indicator = true;
62  else if (target_ro == RoadOption::Right) right_turn_indicator = true;
63  break;
64  }
65  if (cg::Math::DistanceSquared(front_location, waypoint->GetLocation()) > MAX_DISTANCE_LIGHT_CHECK) {
66  break;
67  }
68  }
69 
70  // Determine brake light state
71  for (size_t cc = 0; cc < control_frame.size(); cc++) {
72  if (control_frame[cc].command.type() == typeid(carla::rpc::Command::ApplyVehicleControl)) {
73  carla::rpc::Command::ApplyVehicleControl& ctrl = boost::get<carla::rpc::Command::ApplyVehicleControl>(control_frame[cc].command);
74  if (ctrl.actor == actor_id) {
75  brake_lights = (ctrl.control.brake > 0.5); // hard braking, avoid blinking for throttle control
76  break;
77  }
78  }
79  }
80 
81  // Determine position, fog and beams
82 
83  // Turn on beams & positions from sunset to dawn
86  {
87  position = true;
88  low_beam = true;
89  }
92  {
93  position = true;
94  }
95  // Turn on lights under heavy rain
97  position = true;
98  low_beam = true;
99  }
100  // Turn on fog lights
102  position = true;
103  low_beam = true;
104  fog_lights = true;
105  }
106 
107  // Determine the new vehicle light state
108  rpc::VehicleLightState::flag_type new_light_states = light_states;
109  if (brake_lights)
111  else
112  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Brake);
113 
114  if (left_turn_indicator)
116  else
117  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::LeftBlinker);
118 
119  if (right_turn_indicator)
121  else
122  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::RightBlinker);
123 
124  if (position)
126  else
127  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Position);
128 
129  if (low_beam)
131  else
132  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::LowBeam);
133 
134  if (high_beam)
136  else
137  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::HighBeam);
138 
139  if (fog_lights)
141  else
142  new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Fog);
143 
144  // Update the vehicle light state if it has changed
145  if (new_light_states != light_states)
146  control_frame.push_back(carla::rpc::Command::SetVehicleLightState(actor_id, new_light_states));
147 }
148 
150 }
151 
153 }
154 
155 } // namespace traffic_manager
156 } // namespace carla
static const float SUN_ALTITUDE_DEGREES_JUST_AFTER_DAWN
Definition: Constants.h:139
bool GetUpdateVehicleLights(const ActorId &actor_id) const
Method to get if the vehicle lights should be updates automatically.
Definition: Parameters.cpp:357
std::vector< carla::rpc::Command > ControlFrame
rpc::VehicleLightStateList all_light_states
All vehicle light states.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
Definition: Math.h:70
std::unordered_map< carla::ActorId, Buffer > BufferMap
rpc::VehicleLightStateList GetVehiclesLightStates() const
Returns a list of pairs where the firts element is the vehicle ID and the second one is the light sta...
Definition: World.cpp:40
const std::vector< ActorId > & vehicle_id_list
VehicleLightStage(const std::vector< ActorId > &vehicle_id_list, const BufferMap &buffer_map, const Parameters &parameters, const cc::World &world, ControlFrame &control_frame)
void Update(const unsigned long index) override
carla::ActorId ActorId
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition: World.cpp:86
rpc::WeatherParameters weather
Current weather parameters.
void RemoveActor(const ActorId actor_id) override
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
static const float SUN_ALTITUDE_DEGREES_JUST_BEFORE_SUNSET
Definition: Constants.h:140