CARLA
Loading...
Searching...
No Matches
VehicleLightStage.cpp
Go to the documentation of this file.
1
2#include <algorithm>
3
6
8
9namespace carla {
10namespace traffic_manager {
11
12using namespace constants::VehicleLight;
13using namespace constants::WorldInfoRefresh;
14
16 const std::vector<ActorId> &vehicle_id_list,
17 const BufferMap &buffer_map,
18 const Parameters &parameters,
19 const cc::World &world,
20 ControlFrame& control_frame)
21 : vehicle_id_list(vehicle_id_list),
22 buffer_map(buffer_map),
23 parameters(parameters),
24 world(world),
25 control_frame(control_frame) {}
26
27void VehicleLightStage::UpdateWorldInfo(const double current_time, const bool synchronous_mode) {
28 const bool any_vehicle_updates_lights = std::any_of(
29 vehicle_id_list.begin(),
30 vehicle_id_list.end(),
31 [this](const ActorId actor_id) { return parameters.GetUpdateVehicleLights(actor_id); });
32 if (!any_vehicle_updates_lights) {
33 all_light_states.clear();
35 last_light_states_update = -std::numeric_limits<double>::infinity();
37 return;
38 }
39
40 // Set only once the read has succeeded; it guards the unknown-state path.
42 if (synchronous_mode ||
46 last_light_states_update = current_time;
48 }
49
50 if (synchronous_mode ||
55 }
56 last_weather_update = current_time;
57 }
58}
59
61 const ActorId actor_id,
62 const rpc::VehicleLightState::flag_type light_state) {
63 for (auto &vls : all_light_states) {
64 if (vls.first == actor_id) {
65 vls.second = light_state;
66 return;
67 }
68 }
69 all_light_states.emplace_back(actor_id, light_state);
70}
71
72void VehicleLightStage::Update(const unsigned long index) {
73 ActorId actor_id = vehicle_id_list.at(index);
74
76 return; // this vehicle is not set to have automatic lights update
77
78 rpc::VehicleLightState::flag_type light_states = uint32_t(-1);
79 bool brake_lights = false;
80 bool left_turn_indicator = false;
81 bool right_turn_indicator = false;
82 bool position = false;
83 bool low_beam = false;
84 bool high_beam = false;
85 bool fog_lights = false;
86
87 // search the current light state of the vehicle
88 bool found_light_state = false;
89 for (auto&& vls : all_light_states) {
90 if (vls.first == actor_id) {
91 light_states = vls.second;
92 found_light_state = true;
93 break;
94 }
95 }
96
97 if (!found_light_state) {
98 // Unknown state: deriving a command from the sentinel would switch on the
99 // unmanaged bits (reverse, interior, special) for good.
101 last_light_states_update = -std::numeric_limits<double>::infinity();
102 }
104 missing_from_last_refresh.insert(actor_id);
105 }
106 return;
107 }
108
109 // Determine if the vehicle is truning left or right by checking the close waypoints
110
111 const Buffer& waypoint_buffer = buffer_map.at(actor_id);
112 cg::Location front_location = waypoint_buffer.front()->GetLocation();
113
114 for (const SimpleWaypointPtr& waypoint : waypoint_buffer) {
115 if (waypoint->CheckJunction()) {
116 RoadOption target_ro = waypoint->GetRoadOption();
117 if (target_ro == RoadOption::Left) left_turn_indicator = true;
118 else if (target_ro == RoadOption::Right) right_turn_indicator = true;
119 break;
120 }
121 if (cg::Math::DistanceSquared(front_location, waypoint->GetLocation()) > MAX_DISTANCE_LIGHT_CHECK) {
122 break;
123 }
124 }
125
126 // Determine brake light state
127 for (size_t cc = 0; cc < control_frame.size(); cc++) {
128 if (auto* maybe_ctrl = std::get_if<carla::rpc::Command::ApplyVehicleControl>(&control_frame[cc].command)) {
130 if (ctrl.actor == actor_id) {
131 brake_lights = (ctrl.control.brake > 0.5); // hard braking, avoid blinking for throttle control
132 break;
133 }
134 }
135 }
136
137 // Determine position, fog and beams. Do nothing if the weather is disabled.
138 if (is_weather_enabled) {
139
140 // Turn on beams & positions from sunset to dawn
143 {
144 position = true;
145 low_beam = true;
146 }
149 {
150 position = true;
151 }
152 // Turn on lights under heavy rain
154 position = true;
155 low_beam = true;
156 }
157 // Turn on fog lights
159 position = true;
160 low_beam = true;
161 fog_lights = true;
162 }
163 }
164
165 // Determine the new vehicle light state
166 rpc::VehicleLightState::flag_type new_light_states = light_states;
167 if (brake_lights)
169 else
170 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Brake);
171
172 if (left_turn_indicator)
174 else
175 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::LeftBlinker);
176
177 if (right_turn_indicator)
179 else
180 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::RightBlinker);
181
182 if (position)
184 else
185 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Position);
186
187 if (low_beam)
189 else
190 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::LowBeam);
191
192 if (high_beam)
194 else
195 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::HighBeam);
196
197 if (fog_lights)
199 else
200 new_light_states &= ~rpc::VehicleLightState::flag_type(rpc::VehicleLightState::LightState::Fog);
201
202 // Update the vehicle light state if it has changed
203 if (new_light_states != light_states) {
204 control_frame.push_back(carla::rpc::Command::SetVehicleLightState(actor_id, new_light_states));
205 // Otherwise the same command is re-queued every step until the next refresh.
206 SetCachedLightState(actor_id, new_light_states);
207 }
208}
209
211 all_light_states.erase(
212 std::remove_if(
213 all_light_states.begin(),
214 all_light_states.end(),
215 [actor_id](const rpc::VehicleLightStateList::value_type &light_state) {
216 return light_state.first == actor_id;
217 }),
218 all_light_states.end());
219 missing_from_last_refresh.erase(actor_id);
220}
221
223 all_light_states.clear();
225 last_light_states_update = -std::numeric_limits<double>::infinity();
226 last_weather_update = -std::numeric_limits<double>::infinity();
228 is_weather_enabled = false;
229}
230
231} // namespace traffic_manager
232} // namespace carla
rpc::WeatherParameters GetWeather() const
Retrieve the weather parameters currently active in the world.
Definition World.cpp:94
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:42
bool IsWeatherEnabled() const
Query if the Weather is enabled or not.
Definition World.cpp:110
bool GetUpdateVehicleLights(const ActorId &actor_id) const
Method to get if the vehicle lights should be updates automatically.
rpc::VehicleLightStateList all_light_states
All vehicle light states.
void RemoveActor(const ActorId actor_id) override
void SetCachedLightState(const ActorId actor_id, const rpc::VehicleLightState::flag_type light_state)
double last_light_states_update
Simulation time of the last refresh of each cached world query.
rpc::WeatherParameters weather
Current weather parameters.
VehicleLightStage(const std::vector< ActorId > &vehicle_id_list, const BufferMap &buffer_map, const Parameters &parameters, const cc::World &world, ControlFrame &control_frame)
std::unordered_set< ActorId > missing_from_last_refresh
Vehicles the server left out of the list it last returned.
const std::vector< ActorId > & vehicle_id_list
void UpdateWorldInfo(const double current_time, const bool synchronous_mode)
Synchronous mode refreshes every step.
void Update(const unsigned long index) override
static const float SUN_ALTITUDE_DEGREES_JUST_BEFORE_SUNSET
Definition Constants.h:216
bool IsRefreshDue(const double now, const double last, const double period)
Also true when the clock jumps backwards (new episode) or on NaN, so an unusable clock refreshes rath...
Definition Constants.h:57
bool IsEarlyRefreshDue(const bool refreshed_this_step, const bool missing_from_the_last_refresh)
A vehicle the server already omitted from a fresh list (dormant on a large map) must not re-arm the u...
Definition Constants.h:63
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
std::unordered_map< carla::ActorId, Buffer > BufferMap
std::vector< carla::rpc::Command > ControlFrame
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
This file contains definitions of common data structures used in traffic manager.