CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
trafficmanager
VehicleLightStage.cpp
Go to the documentation of this file.
1
2
#include <algorithm>
3
4
#include "
carla/trafficmanager/Constants.h
"
5
#include "
carla/trafficmanager/LocalizationUtils.h
"
6
7
#include "
carla/trafficmanager/VehicleLightStage.h
"
8
9
namespace
carla
{
10
namespace
traffic_manager
{
11
12
using namespace
constants::VehicleLight
;
13
using namespace
constants::WorldInfoRefresh
;
14
15
VehicleLightStage::VehicleLightStage
(
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
27
void
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();
34
missing_from_last_refresh
.clear();
35
last_light_states_update
= -std::numeric_limits<double>::infinity();
36
light_states_refreshed
=
false
;
37
return
;
38
}
39
40
// Set only once the read has succeeded; it guards the unknown-state path.
41
light_states_refreshed
=
false
;
42
if
(synchronous_mode ||
43
IsRefreshDue
(current_time,
last_light_states_update
, VEHICLE_LIGHT_STATES_REFRESH_PERIOD)) {
44
all_light_states
=
world
.GetVehiclesLightStates();
45
missing_from_last_refresh
.clear();
46
last_light_states_update
= current_time;
47
light_states_refreshed
=
true
;
48
}
49
50
if
(synchronous_mode ||
51
IsRefreshDue
(current_time,
last_weather_update
, WEATHER_REFRESH_PERIOD)) {
52
is_weather_enabled
=
world
.IsWeatherEnabled();
53
if
(
is_weather_enabled
) {
54
weather
=
world
.GetWeather();
55
}
56
last_weather_update
= current_time;
57
}
58
}
59
60
void
VehicleLightStage::SetCachedLightState
(
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
72
void
VehicleLightStage::Update
(
const
unsigned
long
index) {
73
ActorId
actor_id =
vehicle_id_list
.at(index);
74
75
if
(!
parameters
.
GetUpdateVehicleLights
(actor_id))
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.
100
if
(
IsEarlyRefreshDue
(
light_states_refreshed
,
missing_from_last_refresh
.contains(actor_id))) {
101
last_light_states_update
= -std::numeric_limits<double>::infinity();
102
}
103
if
(
light_states_refreshed
) {
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)) {
129
carla::rpc::Command::ApplyVehicleControl
& ctrl = *maybe_ctrl;
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
141
if
(
weather
.
sun_altitude_angle
< SUN_ALTITUDE_DEGREES_BEFORE_DAWN ||
142
weather
.
sun_altitude_angle
> SUN_ALTITUDE_DEGREES_AFTER_SUNSET)
143
{
144
position =
true
;
145
low_beam =
true
;
146
}
147
else
if
(
weather
.
sun_altitude_angle
< SUN_ALTITUDE_DEGREES_JUST_AFTER_DAWN ||
148
weather
.
sun_altitude_angle
> SUN_ALTITUDE_DEGREES_JUST_BEFORE_SUNSET)
149
{
150
position =
true
;
151
}
152
// Turn on lights under heavy rain
153
if
(
weather
.
precipitation
> HEAVY_PRECIPITATION_THRESHOLD) {
154
position =
true
;
155
low_beam =
true
;
156
}
157
// Turn on fog lights
158
if
(
weather
.
fog_density
> FOG_DENSITY_THRESHOLD) {
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)
168
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::Brake
);
169
else
170
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::Brake
);
171
172
if
(left_turn_indicator)
173
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::LeftBlinker
);
174
else
175
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::LeftBlinker
);
176
177
if
(right_turn_indicator)
178
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::RightBlinker
);
179
else
180
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::RightBlinker
);
181
182
if
(position)
183
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::Position
);
184
else
185
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::Position
);
186
187
if
(low_beam)
188
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::LowBeam
);
189
else
190
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::LowBeam
);
191
192
if
(high_beam)
193
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::HighBeam
);
194
else
195
new_light_states &=
~rpc
::VehicleLightState::flag_type(
rpc::VehicleLightState::LightState::HighBeam
);
196
197
if
(fog_lights)
198
new_light_states |=
rpc::VehicleLightState::flag_type
(
rpc::VehicleLightState::LightState::Fog
);
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
210
void
VehicleLightStage::RemoveActor
(
const
ActorId
actor_id) {
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
222
void
VehicleLightStage::Reset
() {
223
all_light_states
.clear();
224
missing_from_last_refresh
.clear();
225
last_light_states_update
= -std::numeric_limits<double>::infinity();
226
last_weather_update
= -std::numeric_limits<double>::infinity();
227
light_states_refreshed
=
false
;
228
is_weather_enabled
=
false
;
229
}
230
231
}
// namespace traffic_manager
232
}
// namespace carla
Constants.h
LocalizationUtils.h
VehicleLightStage.h
carla::geom::Math::DistanceSquared
static constexpr auto DistanceSquared(const Vector3D &a, const Vector3D &b)
Definition
Math.h:70
carla::rpc::VehicleControl::brake
float brake
Definition
LibCarla/source/carla/rpc/VehicleControl.h:43
carla::rpc::VehicleLightState::flag_type
uint32_t flag_type
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:29
carla::rpc::VehicleLightState::LightState::HighBeam
@ HighBeam
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:36
carla::rpc::VehicleLightState::LightState::Position
@ Position
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:34
carla::rpc::VehicleLightState::LightState::LowBeam
@ LowBeam
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:35
carla::rpc::VehicleLightState::LightState::Brake
@ Brake
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:37
carla::rpc::VehicleLightState::LightState::Fog
@ Fog
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:41
carla::rpc::VehicleLightState::LightState::LeftBlinker
@ LeftBlinker
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:39
carla::rpc::VehicleLightState::LightState::RightBlinker
@ RightBlinker
Definition
LibCarla/source/carla/rpc/VehicleLightState.h:38
carla::rpc::WeatherParameters::sun_altitude_angle
float sun_altitude_angle
Definition
LibCarla/source/carla/rpc/WeatherParameters.h:89
carla::rpc::WeatherParameters::fog_density
float fog_density
Definition
LibCarla/source/carla/rpc/WeatherParameters.h:90
carla::rpc::WeatherParameters::precipitation
float precipitation
Definition
LibCarla/source/carla/rpc/WeatherParameters.h:85
carla::traffic_manager::Parameters
Definition
Parameters.h:37
carla::traffic_manager::Parameters::GetUpdateVehicleLights
bool GetUpdateVehicleLights(const ActorId &actor_id) const
Method to get if the vehicle lights should be updates automatically.
Definition
Parameters.cpp:395
carla::traffic_manager::VehicleLightStage::all_light_states
rpc::VehicleLightStateList all_light_states
All vehicle light states.
Definition
VehicleLightStage.h:26
carla::traffic_manager::VehicleLightStage::Reset
void Reset() override
Definition
VehicleLightStage.cpp:222
carla::traffic_manager::VehicleLightStage::RemoveActor
void RemoveActor(const ActorId actor_id) override
Definition
VehicleLightStage.cpp:210
carla::traffic_manager::VehicleLightStage::SetCachedLightState
void SetCachedLightState(const ActorId actor_id, const rpc::VehicleLightState::flag_type light_state)
Definition
VehicleLightStage.cpp:60
carla::traffic_manager::VehicleLightStage::light_states_refreshed
bool light_states_refreshed
Definition
VehicleLightStage.h:34
carla::traffic_manager::VehicleLightStage::last_weather_update
double last_weather_update
Definition
VehicleLightStage.h:33
carla::traffic_manager::VehicleLightStage::last_light_states_update
double last_light_states_update
Simulation time of the last refresh of each cached world query.
Definition
VehicleLightStage.h:32
carla::traffic_manager::VehicleLightStage::weather
rpc::WeatherParameters weather
Current weather parameters.
Definition
VehicleLightStage.h:28
carla::traffic_manager::VehicleLightStage::control_frame
ControlFrame & control_frame
Definition
VehicleLightStage.h:24
carla::traffic_manager::VehicleLightStage::parameters
const Parameters & parameters
Definition
VehicleLightStage.h:22
carla::traffic_manager::VehicleLightStage::buffer_map
const BufferMap & buffer_map
Definition
VehicleLightStage.h:21
carla::traffic_manager::VehicleLightStage::VehicleLightStage
VehicleLightStage(const std::vector< ActorId > &vehicle_id_list, const BufferMap &buffer_map, const Parameters ¶meters, const cc::World &world, ControlFrame &control_frame)
Definition
VehicleLightStage.cpp:15
carla::traffic_manager::VehicleLightStage::missing_from_last_refresh
std::unordered_set< ActorId > missing_from_last_refresh
Vehicles the server left out of the list it last returned.
Definition
VehicleLightStage.h:36
carla::traffic_manager::VehicleLightStage::vehicle_id_list
const std::vector< ActorId > & vehicle_id_list
Definition
VehicleLightStage.h:20
carla::traffic_manager::VehicleLightStage::UpdateWorldInfo
void UpdateWorldInfo(const double current_time, const bool synchronous_mode)
Synchronous mode refreshes every step.
Definition
VehicleLightStage.cpp:27
carla::traffic_manager::VehicleLightStage::Update
void Update(const unsigned long index) override
Definition
VehicleLightStage.cpp:72
carla::traffic_manager::VehicleLightStage::is_weather_enabled
bool is_weather_enabled
Weather enabled.
Definition
VehicleLightStage.h:30
carla::traffic_manager::VehicleLightStage::world
const cc::World & world
Definition
VehicleLightStage.h:23
carla::rpc
Definition
DebugShapeDrawer.h:11
carla::traffic_manager::constants::VehicleLight
Definition
Constants.h:210
carla::traffic_manager::constants::WorldInfoRefresh
Definition
Constants.h:44
carla::traffic_manager::constants::WorldInfoRefresh::IsRefreshDue
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:56
carla::traffic_manager::constants::WorldInfoRefresh::IsEarlyRefreshDue
bool IsEarlyRefreshDue(const bool refreshed_this_step, const bool missing_from_the_last_refresh)
A vehicle the server already omitted from a fresh list must not re-arm the unscheduled read on every ...
Definition
Constants.h:62
carla::traffic_manager
Definition
Vehicle.h:25
carla::traffic_manager::ActorId
carla::ActorId ActorId
Definition
AtomicActorSet.h:21
carla::traffic_manager::ControlFrame
std::vector< carla::rpc::Command > ControlFrame
Definition
DataStructures.h:56
carla::traffic_manager::RoadOption
RoadOption
Definition
SimpleWaypoint.h:25
carla::traffic_manager::RoadOption::Right
@ Right
Definition
SimpleWaypoint.h:28
carla::traffic_manager::RoadOption::Left
@ Left
Definition
SimpleWaypoint.h:27
carla::traffic_manager::Buffer
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
Definition
CollisionStage.h:44
carla::traffic_manager::SimpleWaypointPtr
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
Definition
CachedSimpleWaypoint.cpp:12
carla::traffic_manager::BufferMap
std::unordered_map< carla::ActorId, Buffer > BufferMap
Definition
CollisionStage.h:45
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::geom::Location
Definition
geom/Location.h:23
carla::rpc::Command::ApplyVehicleControl
Definition
Command.h:77
carla::rpc::Command::ApplyVehicleControl::actor
ActorId actor
Definition
Command.h:82
carla::rpc::Command::ApplyVehicleControl::control
VehicleControl control
Definition
Command.h:83
carla::rpc::Command::SetVehicleLightState
Definition
Command.h:253
Generated by
1.18.0