CARLA
Parameters.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 
10 namespace carla {
11 namespace 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 
23 void Parameters::SetHybridPhysicsMode(const bool mode_switch) {
24 
25  hybrid_physics_mode.store(mode_switch);
26 }
27 
28 void Parameters::SetRespawnDormantVehicles(const bool mode_switch) {
29 
30  respawn_dormant_vehicles.store(mode_switch);
31 }
32 
33 void Parameters::SetMaxBoundaries(const float lower, const float upper) {
34  min_lower_bound = lower;
35  max_upper_bound = upper;
36 }
37 
38 void Parameters::SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
39  respawn_lower_bound = min_lower_bound > lower_bound ? min_lower_bound : lower_bound;
40  respawn_upper_bound = max_upper_bound < upper_bound ? max_upper_bound : upper_bound;
41 }
42 
43 void Parameters::SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) {
44 
45  float new_percentage = std::min(100.0f, percentage);
46  percentage_difference_from_speed_limit.AddEntry({actor->GetId(), new_percentage});
47 }
48 
49 void Parameters::SetGlobalPercentageSpeedDifference(const float percentage) {
50  float new_percentage = std::min(100.0f, percentage);
52 }
53 
54 void Parameters::SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
55  const ActorId reference_id = reference_actor->GetId();
56  const ActorId other_id = other_actor->GetId();
57 
58  if (detect_collision) {
59  if (ignore_collision.Contains(reference_id)) {
60  std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
61  if (actor_set->Contains(other_id)) {
62  actor_set->Remove({other_id});
63  }
64  }
65  } else {
66  if (ignore_collision.Contains(reference_id)) {
67  std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
68  if (!actor_set->Contains(other_id)) {
69  actor_set->Insert({other_actor});
70  }
71  } else {
72  std::shared_ptr<AtomicActorSet> actor_set = std::make_shared<AtomicActorSet>();
73  actor_set->Insert({other_actor});
74  auto entry = std::make_pair(reference_id, actor_set);
75  ignore_collision.AddEntry(entry);
76  }
77  }
78 }
79 
80 void Parameters::SetForceLaneChange(const ActorPtr &actor, const bool direction) {
81 
82  const ChangeLaneInfo lane_change_info = {true, direction};
83  const auto entry = std::make_pair(actor->GetId(), lane_change_info);
84  force_lane_change.AddEntry(entry);
85 }
86 
87 void Parameters::SetKeepRightPercentage(const ActorPtr &actor, const float percentage) {
88 
89  const auto entry = std::make_pair(actor->GetId(), percentage);
91 }
92 
93 void Parameters::SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
94 
95  const auto entry = std::make_pair(actor->GetId(), enable);
97 }
98 
99 void Parameters::SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
100 
101  float new_distance = std::max(0.0f, distance);
102  const auto entry = std::make_pair(actor->GetId(), new_distance);
104 }
105 
106 void Parameters::SetSynchronousMode(const bool mode_switch) {
107  synchronous_mode.store(mode_switch);
108 }
109 
111  synchronous_time_out = std::chrono::duration<double, std::milli>(time);
112 }
113 
115 
116  distance_margin.store(dist);
117 }
118 
119 void Parameters::SetPercentageRunningLight(const ActorPtr &actor, const float perc) {
120 
121  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
122  const auto entry = std::make_pair(actor->GetId(), new_perc);
124 }
125 
126 void Parameters::SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
127 
128  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
129  const auto entry = std::make_pair(actor->GetId(), new_perc);
131 }
132 
133 void Parameters::SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
134 
135  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
136  const auto entry = std::make_pair(actor->GetId(), new_perc);
138 }
139 
140 void Parameters::SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
141 
142  float new_perc = cg::Math::Clamp(perc,0.0f,100.0f);
143  const auto entry = std::make_pair(actor->GetId(), new_perc);
145 }
146 
147 void Parameters::SetHybridPhysicsRadius(const float radius) {
148  float new_radius = std::max(radius, 0.0f);
149  hybrid_physics_radius.store(new_radius);
150 }
151 
152 void Parameters::SetOSMMode(const bool mode_switch) {
153  osm_mode.store(mode_switch);
154 }
155 
156 //////////////////////////////////// GETTERS //////////////////////////////////
157 
159 
160  return hybrid_physics_radius.load();
161 }
162 
164  return synchronous_mode.load();
165 }
166 
168  return synchronous_time_out.count();
169 }
170 
171 float Parameters::GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const {
172 
173  float percentage_difference = global_percentage_difference_from_limit;
174 
176  percentage_difference = percentage_difference_from_speed_limit.GetValue(actor_id);
177  }
178 
179  return speed_limit * (1.0f - percentage_difference / 100.0f);
180 }
181 
182 bool Parameters::GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const {
183 
184  bool avoid_collision = true;
185 
186  if (ignore_collision.Contains(reference_actor_id) &&
187  ignore_collision.GetValue(reference_actor_id)->Contains(other_actor_id)) {
188  avoid_collision = false;
189  }
190 
191  return avoid_collision;
192 }
193 
195 
196  ChangeLaneInfo change_lane_info {false, false};
197 
198  if (force_lane_change.Contains(actor_id)) {
199  change_lane_info = force_lane_change.GetValue(actor_id);
200  }
201 
202  force_lane_change.RemoveEntry(actor_id);
203 
204  return change_lane_info;
205 }
206 
208 
209  float percentage = -1.0f;
210 
211  if (perc_keep_right.Contains(actor_id)) {
212  percentage = perc_keep_right.GetValue(actor_id);
213  }
214 
215  perc_keep_right.RemoveEntry(actor_id);
216 
217  return percentage;
218 }
219 
220 bool Parameters::GetAutoLaneChange(const ActorId &actor_id) const {
221 
222  bool auto_lane_change_policy = true;
223 
224  if (auto_lane_change.Contains(actor_id)) {
225  auto_lane_change_policy = auto_lane_change.GetValue(actor_id);
226  }
227 
228  return auto_lane_change_policy;
229 }
230 
231 float Parameters::GetDistanceToLeadingVehicle(const ActorId &actor_id) const {
232 
233  float specific_distance_margin = 0.0f;
234  if (distance_to_leading_vehicle.Contains(actor_id)) {
235  specific_distance_margin = distance_to_leading_vehicle.GetValue(actor_id);
236  } else {
237  specific_distance_margin = distance_margin;
238  }
239 
240  return specific_distance_margin;
241 }
242 
243 float Parameters::GetPercentageRunningLight(const ActorId &actor_id) const {
244 
245  float percentage = 0.0f;
246 
247  if (perc_run_traffic_light.Contains(actor_id)) {
248  percentage = perc_run_traffic_light.GetValue(actor_id);
249  }
250 
251  return percentage;
252 }
253 
254 float Parameters::GetPercentageRunningSign(const ActorId &actor_id) const {
255 
256  float percentage = 0.0f;
257 
258  if (perc_run_traffic_sign.Contains(actor_id)) {
259  percentage = perc_run_traffic_sign.GetValue(actor_id);
260  }
261 
262  return percentage;
263 }
264 
265 float Parameters::GetPercentageIgnoreWalkers(const ActorId &actor_id) const {
266 
267  float percentage = 0.0f;
268 
269  if (perc_ignore_walkers.Contains(actor_id)) {
270  percentage = perc_ignore_walkers.GetValue(actor_id);
271  }
272 
273  return percentage;
274 }
275 
276 float Parameters::GetPercentageIgnoreVehicles(const ActorId &actor_id) const {
277 
278  float percentage = 0.0f;
279 
280  if (perc_ignore_vehicles.Contains(actor_id)) {
281  percentage = perc_ignore_vehicles.GetValue(actor_id);
282  }
283 
284  return percentage;
285 }
286 
288 
289  return hybrid_physics_mode.load();
290 }
291 
293 
294  return respawn_dormant_vehicles.load();
295 }
296 
298 
299  return respawn_lower_bound.load();
300 }
301 
303 
304  return respawn_upper_bound.load();
305 }
306 
307 
309 
310  return osm_mode.load();
311 }
312 
313 } // namespace traffic_manager
314 } // namespace carla
float GetPercentageRunningLight(const ActorId &actor_id) const
Method to get % to run any traffic light.
Definition: Parameters.cpp:243
void RemoveEntry(const Key &key)
Definition: AtomicMap.h:50
bool GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const
Method to query collision avoidance rule between a pair of vehicles.
Definition: Parameters.cpp:182
AtomicMap< ActorId, float > perc_run_traffic_sign
Map containing % of running a traffic sign.
Definition: Parameters.h:52
std::atomic< float > respawn_lower_bound
Minimum distance to respawn vehicles with respect to the hero vehicle.
Definition: Parameters.h:68
float GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const
Method to query target velocity for a vehicle.
Definition: Parameters.cpp:171
float min_lower_bound
Minimum possible distance to respawn vehicles with respect to the hero vehicle.
Definition: Parameters.h:72
const Value & GetValue(const Key &key) const
Definition: AtomicMap.h:44
std::atomic< float > hybrid_physics_radius
Hybrid physics radius.
Definition: Parameters.h:76
void SetGlobalDistanceToLeadingVehicle(const float dist)
Method to set the distance to leading vehicle for all registered vehicles.
Definition: Parameters.cpp:114
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
Definition: Parameters.cpp:99
carla::SharedPtr< cc::Actor > ActorPtr
bool GetOSMMode() const
Method to get Open Street Map mode.
Definition: Parameters.cpp:308
std::atomic< bool > osm_mode
Parameter specifying Open Street Map mode.
Definition: Parameters.h:78
bool Contains(const Key &key) const
Definition: AtomicMap.h:38
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
Definition: Parameters.cpp:93
static T Clamp(T a, T min=T(0), T max=T(1))
Definition: Math.h:49
AtomicMap< ActorId, bool > auto_lane_change
Map containing auto lane change commands.
Definition: Parameters.h:48
bool GetAutoLaneChange(const ActorId &actor_id) const
Method to query auto lane change rule for a vehicle.
Definition: Parameters.cpp:220
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::chrono::duration< double, std::milli > synchronous_time_out
Synchronous mode time out variable.
Definition: Parameters.h:210
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set probabilistic preference to keep on the right lane.
Definition: Parameters.cpp:87
AtomicMap< ActorId, float > perc_run_traffic_light
Map containing % of running a traffic light.
Definition: Parameters.h:50
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
Definition: Parameters.cpp:80
AtomicMap< ActorId, ChangeLaneInfo > force_lane_change
Map containing force lane change commands.
Definition: Parameters.h:46
void SetMaxBoundaries(const float lower, const float upper)
Method to set limits for boundaries when respawning vehicles.
Definition: Parameters.cpp:33
float max_upper_bound
Maximum possible distance to respawn vehicles with respect to the hero vehicle.
Definition: Parameters.h:74
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Set a vehicle&#39;s % decrease in velocity with respect to the speed limit.
Definition: Parameters.cpp:43
AtomicMap< ActorId, float > perc_keep_right
Map containing % of keep right rule.
Definition: Parameters.h:58
bool GetRespawnDormantVehicles() const
Method to retrieve if we are automatically respawning vehicles.
Definition: Parameters.cpp:292
std::atomic< float > distance_margin
Distance margin.
Definition: Parameters.h:62
float GetKeepRightPercentage(const ActorId &actor_id)
Method to query percentage probability of keep right rule for a vehicle.
Definition: Parameters.cpp:207
AtomicMap< ActorId, float > percentage_difference_from_speed_limit
Target velocity map for individual vehicles.
Definition: Parameters.h:38
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
Definition: Parameters.cpp:54
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
Definition: Parameters.cpp:287
carla::ActorId ActorId
std::atomic< bool > hybrid_physics_mode
Hybrid physics mode switch.
Definition: Parameters.h:64
AtomicMap< ActorId, float > distance_to_leading_vehicle
Map containing distance to leading vehicle command.
Definition: Parameters.h:44
float GetHybridPhysicsRadius() const
Method to retrieve hybrid physics radius.
Definition: Parameters.cpp:158
bool GetSynchronousMode() const
Method to get synchronous mode.
Definition: Parameters.cpp:163
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
Definition: Parameters.cpp:133
float GetPercentageIgnoreVehicles(const ActorId &actor_id) const
Method to get % to ignore any vehicle.
Definition: Parameters.cpp:276
float GetDistanceToLeadingVehicle(const ActorId &actor_id) const
Method to query distance to leading vehicle for a given vehicle.
Definition: Parameters.cpp:231
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
Definition: Parameters.cpp:49
float GetPercentageRunningSign(const ActorId &actor_id) const
Method to get % to run any traffic light.
Definition: Parameters.cpp:254
AtomicMap< ActorId, float > perc_ignore_walkers
Map containing % of ignoring walkers.
Definition: Parameters.h:54
float GetUpperBoundaryRespawnDormantVehicles() const
Method to retrieve maximum distance from hero vehicle when respawning vehicles.
Definition: Parameters.cpp:302
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
Definition: Parameters.cpp:140
void AddEntry(const std::pair< Key, Value > &entry)
Definition: AtomicMap.h:27
float GetLowerBoundaryRespawnDormantVehicles() const
Method to retrieve minimum distance from hero vehicle when respawning vehicles.
Definition: Parameters.cpp:297
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
Definition: Parameters.cpp:38
float GetPercentageIgnoreWalkers(const ActorId &actor_id) const
Method to get % to ignore any walker.
Definition: Parameters.cpp:265
std::atomic< bool > synchronous_mode
Synchronous mode switch.
Definition: Parameters.h:60
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set if we are automatically respawning vehicles.
Definition: Parameters.cpp:28
void SetSynchronousMode(const bool mode_switch=true)
Method to set synchronous mode.
Definition: Parameters.cpp:106
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
Definition: Parameters.cpp:147
AtomicMap< ActorId, std::shared_ptr< AtomicActorSet > > ignore_collision
Map containing a set of actors to be ignored during collision detection.
Definition: Parameters.h:42
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to set % to run any traffic light.
Definition: Parameters.cpp:119
float global_percentage_difference_from_limit
Global target velocity limit % difference.
Definition: Parameters.h:40
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to set % to run any traffic sign.
Definition: Parameters.cpp:126
double GetSynchronousModeTimeOutInMiliSecond() const
Get synchronous mode time out.
Definition: Parameters.cpp:167
AtomicMap< ActorId, float > perc_ignore_vehicles
Map containing % of ignoring vehicles.
Definition: Parameters.h:56
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Set Synchronous mode time out.
Definition: Parameters.cpp:110
ChangeLaneInfo GetForceLaneChange(const ActorId &actor_id)
Method to query lane change command for a vehicle.
Definition: Parameters.cpp:194
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
Definition: Parameters.cpp:23
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
Definition: Parameters.cpp:152
std::atomic< bool > respawn_dormant_vehicles
Automatic respawn mode switch.
Definition: Parameters.h:66
std::atomic< float > respawn_upper_bound
Maximum distance to respawn vehicles with respect to the hero vehicle.
Definition: Parameters.h:70