CARLA
Loading...
Searching...
No Matches
Parameters.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
9
10namespace carla {
11namespace traffic_manager {
12
14
15 /// Set default synchronous mode time out.
16 synchronous_time_out = std::chrono::duration<int, std::milli>(10);
17}
18
20
21//////////////////////////////////// SETTERS //////////////////////////////////
22
23void Parameters::SetHybridPhysicsMode(const bool mode_switch) {
24
25 hybrid_physics_mode.store(mode_switch);
26}
27
28void Parameters::SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) {
29
30 float new_percentage = std::min(100.0f, percentage);
31 percentage_difference_from_speed_limit.AddEntry({actor->GetId(), new_percentage});
32 if (exact_desired_speed.Contains(actor->GetId())) {
33 exact_desired_speed.RemoveEntry(actor->GetId());
34 }
35}
36
37void Parameters::SetLaneOffset(const ActorPtr &actor, const float offset) {
38 const auto entry = std::make_pair(actor->GetId(), offset);
39 lane_offset.AddEntry(entry);
40}
41
42void Parameters::SetDesiredSpeed(const ActorPtr &actor, const float value) {
43
44 float new_value = std::max(0.0f, value);
45 exact_desired_speed.AddEntry({actor->GetId(), new_value});
48 }
49}
50
52 float new_percentage = std::min(100.0f, percentage);
54}
55
56void Parameters::SetGlobalLaneOffset(const float offset) {
57 global_lane_offset = offset;
58}
59
60void Parameters::SetLargeVehicleWideTurn(const ActorPtr &actor, const bool enable) {
61 const auto entry = std::make_pair(actor->GetId(), enable);
63}
64
68
69void Parameters::SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
70 const ActorId reference_id = reference_actor->GetId();
71 const ActorId other_id = other_actor->GetId();
72
73 if (detect_collision) {
74 if (ignore_collision.Contains(reference_id)) {
75 std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
76 if (actor_set->Contains(other_id)) {
77 actor_set->Remove({other_id});
78 }
79 }
80 } else {
81 if (ignore_collision.Contains(reference_id)) {
82 std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
83 if (!actor_set->Contains(other_id)) {
84 actor_set->Insert({other_actor});
85 }
86 } else {
87 std::shared_ptr<AtomicActorSet> actor_set = std::make_shared<AtomicActorSet>();
88 actor_set->Insert({other_actor});
89 auto entry = std::make_pair(reference_id, actor_set);
90 ignore_collision.AddEntry(entry);
91 }
92 }
93}
94
95void Parameters::SetForceLaneChange(const ActorPtr &actor, const bool direction) {
96
97 const ChangeLaneInfo lane_change_info = {true, direction};
98 const auto entry = std::make_pair(actor->GetId(), lane_change_info);
99 force_lane_change.AddEntry(entry);
100}
101
102void Parameters::SetKeepSlowLanePercentage(const ActorPtr &actor, const float percentage) {
103
104 const auto entry = std::make_pair(actor->GetId(), percentage);
106}
107
108
109void Parameters::SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage) {
110
111 const auto entry = std::make_pair(actor->GetId(), percentage);
113}
114
115void Parameters::SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage) {
116
117 const auto entry = std::make_pair(actor->GetId(), percentage);
119
120}
121
122void Parameters::SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update) {
123
124 const auto entry = std::make_pair(actor->GetId(), do_update);
126}
127
128void Parameters::SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
129
130 const auto entry = std::make_pair(actor->GetId(), enable);
132}
133
134void Parameters::SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
135
136 float new_distance = std::max(0.0f, distance);
137 const auto entry = std::make_pair(actor->GetId(), new_distance);
139}
140
141void Parameters::SetSynchronousMode(const bool mode_switch) {
142 synchronous_mode.store(mode_switch);
143}
144
146 synchronous_time_out = std::chrono::duration<double, std::milli>(time);
147}
148
150
151 distance_margin.store(dist);
152}
153
154void Parameters::SetPercentageRunningLight(const ActorPtr &actor, const float perc) {
155
156 float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
157 const auto entry = std::make_pair(actor->GetId(), new_perc);
159}
160
161void Parameters::SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
162
163 float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
164 const auto entry = std::make_pair(actor->GetId(), new_perc);
166}
167
168void Parameters::SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
169
170 float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
171 const auto entry = std::make_pair(actor->GetId(), new_perc);
173}
174
175void Parameters::SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
176
177 float new_perc = cg::Math::Clamp(perc,0.0f,100.0f);
178 const auto entry = std::make_pair(actor->GetId(), new_perc);
180}
181
182void Parameters::SetHybridPhysicsRadius(const float radius) {
183 float new_radius = std::max(radius, 0.0f);
184 hybrid_physics_radius.store(new_radius);
185}
186
187void Parameters::SetOSMMode(const bool mode_switch) {
188 osm_mode.store(mode_switch);
189}
190
191void Parameters::SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer) {
192 const auto entry = std::make_pair(actor->GetId(), path);
193 custom_path.AddEntry(entry);
194 const auto entry2 = std::make_pair(actor->GetId(), empty_buffer);
195 upload_path.AddEntry(entry2);
196}
197
198void Parameters::RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
199 if (!remove_path) {
200 upload_path.RemoveEntry(actor_id);
201 } else {
202 custom_path.RemoveEntry(actor_id);
203 }
204}
205
206void Parameters::UpdateUploadPath(const ActorId &actor_id, const Path path) {
207 custom_path.RemoveEntry(actor_id);
208 const auto entry = std::make_pair(actor_id, path);
209 custom_path.AddEntry(entry);
210}
211
212void Parameters::SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer) {
213 const auto entry = std::make_pair(actor->GetId(), route);
214 custom_route.AddEntry(entry);
215 const auto entry2 = std::make_pair(actor->GetId(), empty_buffer);
216 upload_route.AddEntry(entry2);
217}
218
219void Parameters::RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
220 if (!remove_path) {
221 upload_route.RemoveEntry(actor_id);
222 } else {
223 custom_route.RemoveEntry(actor_id);
224 }
225}
226
227void Parameters::UpdateImportedRoute(const ActorId &actor_id, const Route route) {
228 custom_route.RemoveEntry(actor_id);
229 const auto entry = std::make_pair(actor_id, route);
230 custom_route.AddEntry(entry);
231}
232
233//////////////////////////////////// GETTERS //////////////////////////////////
234
236
237 return hybrid_physics_radius.load();
238}
239
241 return synchronous_mode.load();
242}
243
247
248float Parameters::GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const {
249
250 float percentage_difference = global_percentage_difference_from_limit;
251
253 percentage_difference = percentage_difference_from_speed_limit.GetValue(actor_id);
254 } else if (exact_desired_speed.Contains(actor_id)) {
255 return exact_desired_speed.GetValue(actor_id);
256 }
257
258 return speed_limit * (1.0f - percentage_difference / 100.0f);
259}
260
261float Parameters::GetLaneOffset(const ActorId &actor_id) const {
262 float offset = global_lane_offset;
263
264 if (lane_offset.Contains(actor_id)) {
265 offset = lane_offset.GetValue(actor_id);
266 }
267
268 return offset;
269}
270
271bool Parameters::GetLargeVehicleWideTurn(const ActorId &actor_id) const {
272 bool enable = global_large_vehicle_wide_turn;
273
274 if (large_vehicle_wide_turn.Contains(actor_id)) {
275 enable = large_vehicle_wide_turn.GetValue(actor_id);
276 }
277
278 return enable;
279}
280
281bool Parameters::GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const {
282
283 bool avoid_collision = true;
284
285 if (ignore_collision.Contains(reference_actor_id) &&
286 ignore_collision.GetValue(reference_actor_id)->Contains(other_actor_id)) {
287 avoid_collision = false;
288 }
289
290 return avoid_collision;
291}
292
294
295 ChangeLaneInfo change_lane_info {false, false};
296
297 if (force_lane_change.Contains(actor_id)) {
298 change_lane_info = force_lane_change.GetValue(actor_id);
299 }
300
301 force_lane_change.RemoveEntry(actor_id);
302
303 return change_lane_info;
304}
305
307
308 float percentage = -1.0f;
309
310 if (perc_keep_slow_lane.Contains(actor_id)) {
311 percentage = perc_keep_slow_lane.GetValue(actor_id);
312 }
313
314 return percentage;
315}
316
318
319 float percentage = -1.0f;
320
321 if (perc_random_left.Contains(actor_id)) {
322 percentage = perc_random_left.GetValue(actor_id);
323 }
324
325 return percentage;
326}
327
329
330 float percentage = -1.0f;
331
332 if (perc_random_right.Contains(actor_id)) {
333 percentage = perc_random_right.GetValue(actor_id);
334 }
335
336 return percentage;
337}
338
339bool Parameters::GetAutoLaneChange(const ActorId &actor_id) const {
340
341 bool auto_lane_change_policy = true;
342
343 if (auto_lane_change.Contains(actor_id)) {
344 auto_lane_change_policy = auto_lane_change.GetValue(actor_id);
345 }
346
347 return auto_lane_change_policy;
348}
349
351
352 float specific_distance_margin = 0.0f;
354 specific_distance_margin = distance_to_leading_vehicle.GetValue(actor_id);
355 } else {
356 specific_distance_margin = distance_margin;
357 }
358
359 return specific_distance_margin;
360}
361
362float Parameters::GetPercentageRunningLight(const ActorId &actor_id) const {
363
364 float percentage = 0.0f;
365
366 if (perc_run_traffic_light.Contains(actor_id)) {
367 percentage = perc_run_traffic_light.GetValue(actor_id);
368 }
369
370 return percentage;
371}
372
373float Parameters::GetPercentageRunningSign(const ActorId &actor_id) const {
374
375 float percentage = 0.0f;
376
377 if (perc_run_traffic_sign.Contains(actor_id)) {
378 percentage = perc_run_traffic_sign.GetValue(actor_id);
379 }
380
381 return percentage;
382}
383
385
386 float percentage = 0.0f;
387
388 if (perc_ignore_walkers.Contains(actor_id)) {
389 percentage = perc_ignore_walkers.GetValue(actor_id);
390 }
391
392 return percentage;
393}
394
395bool Parameters::GetUpdateVehicleLights(const ActorId &actor_id) const {
396 bool do_update = false;
397
398 if (auto_update_vehicle_lights.Contains(actor_id)) {
399 do_update = auto_update_vehicle_lights.GetValue(actor_id);
400 }
401
402 return do_update;
403}
404
406
407 float percentage = 0.0f;
408
409 if (perc_ignore_vehicles.Contains(actor_id)) {
410 percentage = perc_ignore_vehicles.GetValue(actor_id);
411 }
412
413 return percentage;
414}
415
417
418 return hybrid_physics_mode.load();
419}
420
421
423
424 return osm_mode.load();
425}
426
427bool Parameters::GetUploadPath(const ActorId &actor_id) const {
428
429 bool custom_path_bool = false;
430
431 if (upload_path.Contains(actor_id)) {
432 custom_path_bool = upload_path.GetValue(actor_id);
433 }
434
435 return custom_path_bool;
436}
437
438Path Parameters::GetCustomPath(const ActorId &actor_id) const {
439
440 Path custom_path_import;
441
442 if (custom_path.Contains(actor_id)) {
443 custom_path_import = custom_path.GetValue(actor_id);
444 }
445
446 return custom_path_import;
447}
448
449
450bool Parameters::GetUploadRoute(const ActorId &actor_id) const {
451
452 bool custom_route_bool = false;
453
454 if (upload_route.Contains(actor_id)) {
455 custom_route_bool = upload_route.GetValue(actor_id);
456 }
457
458 return custom_route_bool;
459}
460
462
463 Route custom_route_import;
464
465 if (custom_route.Contains(actor_id)) {
466 custom_route_import = custom_route.GetValue(actor_id);
467 }
468
469 return custom_route_import;
470}
471
472
473} // namespace traffic_manager
474} // namespace carla
static constexpr T Clamp(T a, T min=T(0), T max=T(1))
Definition Math.h:49
void RemoveEntry(const Key &key)
Definition AtomicMap.h:50
const Value & GetValue(const Key &key) const
Definition AtomicMap.h:44
bool Contains(const Key &key) const
Definition AtomicMap.h:38
void AddEntry(const std::pair< Key, Value > &entry)
Definition AtomicMap.h:27
float GetRandomLeftLaneChangePercentage(const ActorId &actor_id)
Method to query percentage probability of a random right lane change for a vehicle.
void SetGlobalLargeVehicleWideTurn(const bool enable)
Method to enable/disable the wide-turn manoeuvre globally for all large vehicles.
float GetPercentageIgnoreWalkers(const ActorId &actor_id) const
Method to get % to ignore any walker.
AtomicMap< ActorId, ChangeLaneInfo > force_lane_change
Map containing force lane change commands.
Definition Parameters.h:59
ChangeLaneInfo GetForceLaneChange(const ActorId &actor_id)
Method to query lane change command for a vehicle.
AtomicMap< ActorId, bool > auto_lane_change
Map containing auto lane change commands.
Definition Parameters.h:61
bool GetLargeVehicleWideTurn(const ActorId &actor_id) const
Method to query whether the wide-turn manoeuvre is enabled for a large vehicle (per-vehicle override,...
float GetLaneOffset(const ActorId &actor_id) const
Method to query lane offset for a vehicle.
AtomicMap< ActorId, bool > upload_path
Parameter specifying if importing a custom path.
Definition Parameters.h:89
void SetLaneOffset(const ActorPtr &actor, const float offset)
Method to set a lane offset displacement from the center line.
float GetDistanceToLeadingVehicle(const ActorId &actor_id) const
Method to query distance to leading vehicle for a given vehicle.
void SetKeepSlowLanePercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the slow lane.
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
bool GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const
Method to query collision avoidance rule between a pair of vehicles.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
Route GetImportedRoute(const ActorId &actor_id) const
Method to get a custom route.
float global_lane_offset
Global lane offset.
Definition Parameters.h:49
float global_percentage_difference_from_limit
Global target velocity limit % difference.
Definition Parameters.h:47
std::atomic< bool > synchronous_mode
Synchronous mode switch.
Definition Parameters.h:79
float GetKeepSlowLanePercentage(const ActorId &actor_id)
Method to query percentage probability of keep slow lane rule for a vehicle.
double GetSynchronousModeTimeOutInMiliSecond() const
Get synchronous mode time out.
std::atomic< float > distance_margin
Distance margin.
Definition Parameters.h:81
std::chrono::duration< double, std::milli > synchronous_time_out
Synchronous mode time out variable.
Definition Parameters.h:283
std::atomic< float > hybrid_physics_radius
Hybrid physics radius.
Definition Parameters.h:85
bool GetUploadRoute(const ActorId &actor_id) const
Method to get if we are uploading a route.
AtomicMap< ActorId, bool > large_vehicle_wide_turn
Per-vehicle enable flag for the large-vehicle wide-turn manoeuvre.
Definition Parameters.h:51
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
bool GetOSMMode() const
Method to get Open Street Map mode.
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetGlobalDistanceToLeadingVehicle(const float dist)
Method to set the distance to leading vehicle for all registered vehicles.
AtomicMap< ActorId, float > perc_random_left
Map containing % of random left lane change.
Definition Parameters.h:73
AtomicMap< ActorId, float > perc_ignore_walkers
Map containing % of ignoring walkers.
Definition Parameters.h:67
float GetHybridPhysicsRadius() const
Method to retrieve hybrid physics radius.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
float GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const
Method to query target velocity for a vehicle.
void SetGlobalLaneOffset(float const offset)
Method to set a global lane offset displacement from the center line.
AtomicMap< ActorId, float > exact_desired_speed
Target velocity map for individual vehicles, based on a desired velocity.
Definition Parameters.h:45
AtomicMap< ActorId, bool > upload_route
Parameter specifying if importing a custom route.
Definition Parameters.h:93
bool GetUploadPath(const ActorId &actor_id) const
Method to get if we are uploading a path.
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
float GetRandomRightLaneChangePercentage(const ActorId &actor_id)
Method to query percentage probability of a random left lane change for a vehicle.
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
AtomicMap< ActorId, float > perc_run_traffic_light
Map containing % of running a traffic light.
Definition Parameters.h:63
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to set % to run any traffic sign.
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
std::atomic< bool > hybrid_physics_mode
Hybrid physics mode switch.
Definition Parameters.h:83
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
bool GetAutoLaneChange(const ActorId &actor_id) const
Method to query auto lane change rule for a vehicle.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Set Synchronous mode time out.
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
AtomicMap< ActorId, float > perc_random_right
Map containing % of random right lane change.
Definition Parameters.h:75
float GetPercentageRunningSign(const ActorId &actor_id) const
Method to get % to run any traffic light.
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
AtomicMap< ActorId, std::shared_ptr< AtomicActorSet > > ignore_collision
Map containing a set of actors to be ignored during collision detection.
Definition Parameters.h:55
AtomicMap< ActorId, float > perc_keep_slow_lane
Map containing % of keep slow lane rule.
Definition Parameters.h:71
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to set % to run any traffic light.
AtomicMap< ActorId, float > perc_run_traffic_sign
Map containing % of running a traffic sign.
Definition Parameters.h:65
std::atomic< bool > osm_mode
Parameter specifying Open Street Map mode.
Definition Parameters.h:87
Path GetCustomPath(const ActorId &actor_id) const
Method to get a custom path.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
AtomicMap< ActorId, float > distance_to_leading_vehicle
Map containing distance to leading vehicle command.
Definition Parameters.h:57
float GetPercentageIgnoreVehicles(const ActorId &actor_id) const
Method to get % to ignore any vehicle.
void SetDesiredSpeed(const ActorPtr &actor, const float value)
Set a vehicle's exact desired velocity.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
bool GetSynchronousMode() const
Method to get synchronous mode.
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
AtomicMap< ActorId, float > lane_offset
Lane offset map for individual vehicles.
Definition Parameters.h:43
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
float GetPercentageRunningLight(const ActorId &actor_id) const
Method to get % to run any traffic light.
bool global_large_vehicle_wide_turn
Global enable flag for the large-vehicle wide-turn manoeuvre.
Definition Parameters.h:53
bool GetUpdateVehicleLights(const ActorId &actor_id) const
Method to get if the vehicle lights should be updates automatically.
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Set a vehicle's % decrease in velocity with respect to the speed limit.
AtomicMap< ActorId, bool > auto_update_vehicle_lights
Map containing the automatic vehicle lights update flag.
Definition Parameters.h:77
AtomicMap< ActorId, Route > custom_route
Structure to hold all custom routes.
Definition Parameters.h:95
AtomicMap< ActorId, float > perc_ignore_vehicles
Map containing % of ignoring vehicles.
Definition Parameters.h:69
AtomicMap< ActorId, float > percentage_difference_from_speed_limit
Target velocity map for individual vehicles, based on a % diffrerence from speed limit.
Definition Parameters.h:41
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
AtomicMap< ActorId, Path > custom_path
Structure to hold all custom paths.
Definition Parameters.h:91
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
void SetLargeVehicleWideTurn(const ActorPtr &actor, const bool enable)
Method to enable/disable the wide-turn manoeuvre for a single large vehicle (bus/truck).
void SetSynchronousMode(const bool mode_switch=true)
Method to set synchronous mode.
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Method to set the automatic vehicle light state update flag.
carla::SharedPtr< cc::Actor > ActorPtr
std::vector< uint8_t > Route
std::vector< cg::Location > Path
This file contains definitions of common data structures used in traffic manager.