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 
8 
9 namespace carla {
10 namespace traffic_manager {
11 
13 
14  /// Set default synchronous mode time out.
15  synchronous_time_out = std::chrono::duration<int, std::milli>(10);
16 }
17 
19 
20 //////////////////////////////////// SETTERS //////////////////////////////////
21 
22 void Parameters::SetHybridPhysicsMode(const bool mode_switch) {
23 
24  hybrid_physics_mode.store(mode_switch);
25 }
26 
27 void Parameters::SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) {
28 
29  float new_percentage = std::min(100.0f, percentage);
30  percentage_difference_from_speed_limit.AddEntry({actor->GetId(), new_percentage});
31 }
32 
33 void Parameters::SetGlobalPercentageSpeedDifference(const float percentage) {
34  float new_percentage = std::min(100.0f, percentage);
36 }
37 
38 void Parameters::SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
39  const ActorId reference_id = reference_actor->GetId();
40  const ActorId other_id = other_actor->GetId();
41 
42  if (detect_collision) {
43  if (ignore_collision.Contains(reference_id)) {
44  std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
45  if (actor_set->Contains(other_id)) {
46  actor_set->Remove({other_id});
47  }
48  }
49  } else {
50  if (ignore_collision.Contains(reference_id)) {
51  std::shared_ptr<AtomicActorSet> actor_set = ignore_collision.GetValue(reference_id);
52  if (!actor_set->Contains(other_id)) {
53  actor_set->Insert({other_actor});
54  }
55  } else {
56  std::shared_ptr<AtomicActorSet> actor_set = std::make_shared<AtomicActorSet>();
57  actor_set->Insert({other_actor});
58  auto entry = std::make_pair(reference_id, actor_set);
59  ignore_collision.AddEntry(entry);
60  }
61  }
62 }
63 
64 void Parameters::SetForceLaneChange(const ActorPtr &actor, const bool direction) {
65 
66  const ChangeLaneInfo lane_change_info = {true, direction};
67  const auto entry = std::make_pair(actor->GetId(), lane_change_info);
68  force_lane_change.AddEntry(entry);
69 }
70 
71 void Parameters::SetKeepRightPercentage(const ActorPtr &actor, const float percentage) {
72 
73  const auto entry = std::make_pair(actor->GetId(), percentage);
75 }
76 
77 void Parameters::SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
78 
79  const auto entry = std::make_pair(actor->GetId(), enable);
81 }
82 
83 void Parameters::SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
84 
85  float new_distance = std::max(0.0f, distance);
86  const auto entry = std::make_pair(actor->GetId(), new_distance);
88 }
89 
90 void Parameters::SetSynchronousMode(const bool mode_switch) {
91  synchronous_mode.store(mode_switch);
92 }
93 
95  synchronous_time_out = std::chrono::duration<double, std::milli>(time);
96 }
97 
99 
100  distance_margin.store(dist);
101 }
102 
103 void Parameters::SetPercentageRunningLight(const ActorPtr &actor, const float perc) {
104 
105  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
106  const auto entry = std::make_pair(actor->GetId(), new_perc);
108 }
109 
110 void Parameters::SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
111 
112  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
113  const auto entry = std::make_pair(actor->GetId(), new_perc);
115 }
116 
117 void Parameters::SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
118 
119  float new_perc = cg::Math::Clamp(perc, 0.0f, 100.0f);
120  const auto entry = std::make_pair(actor->GetId(), new_perc);
122 }
123 
124 void Parameters::SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
125 
126  float new_perc = cg::Math::Clamp(perc,0.0f,100.0f);
127  const auto entry = std::make_pair(actor->GetId(), new_perc);
129 }
130 
131 void Parameters::SetHybridPhysicsRadius(const float radius) {
132  float new_radius = std::max(radius, 0.0f);
133  hybrid_physics_radius.store(new_radius);
134 }
135 
136 void Parameters::SetOSMMode(const bool mode_switch) {
137  osm_mode.store(mode_switch);
138 }
139 
140 //////////////////////////////////// GETTERS //////////////////////////////////
141 
143 
144  return hybrid_physics_radius.load();
145 }
146 
148  return synchronous_mode.load();
149 }
150 
152  return synchronous_time_out.count();
153 }
154 
155 float Parameters::GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const {
156 
157  float percentage_difference = global_percentage_difference_from_limit;
158 
160  percentage_difference = percentage_difference_from_speed_limit.GetValue(actor_id);
161  }
162 
163  return speed_limit * (1.0f - percentage_difference / 100.0f);
164 }
165 
166 bool Parameters::GetCollisionDetection(const ActorId &reference_actor_id, const ActorId &other_actor_id) const {
167 
168  bool avoid_collision = true;
169 
170  if (ignore_collision.Contains(reference_actor_id) &&
171  ignore_collision.GetValue(reference_actor_id)->Contains(other_actor_id)) {
172  avoid_collision = false;
173  }
174 
175  return avoid_collision;
176 }
177 
179 
180  ChangeLaneInfo change_lane_info {false, false};
181 
182  if (force_lane_change.Contains(actor_id)) {
183  change_lane_info = force_lane_change.GetValue(actor_id);
184  }
185 
186  force_lane_change.RemoveEntry(actor_id);
187 
188  return change_lane_info;
189 }
190 
192 
193  float percentage = -1.0f;
194 
195  if (perc_keep_right.Contains(actor_id)) {
196  percentage = perc_keep_right.GetValue(actor_id);
197  }
198 
199  perc_keep_right.RemoveEntry(actor_id);
200 
201  return percentage;
202 }
203 
204 bool Parameters::GetAutoLaneChange(const ActorId &actor_id) const {
205 
206  bool auto_lane_change_policy = true;
207 
208  if (auto_lane_change.Contains(actor_id)) {
209  auto_lane_change_policy = auto_lane_change.GetValue(actor_id);
210  }
211 
212  return auto_lane_change_policy;
213 }
214 
215 float Parameters::GetDistanceToLeadingVehicle(const ActorId &actor_id) const {
216 
217  float specific_distance_margin = 0.0f;
218  if (distance_to_leading_vehicle.Contains(actor_id)) {
219  specific_distance_margin = distance_to_leading_vehicle.GetValue(actor_id);
220  } else {
221  specific_distance_margin = distance_margin;
222  }
223 
224  return specific_distance_margin;
225 }
226 
227 float Parameters::GetPercentageRunningLight(const ActorId &actor_id) const {
228 
229  float percentage = 0.0f;
230 
231  if (perc_run_traffic_light.Contains(actor_id)) {
232  percentage = perc_run_traffic_light.GetValue(actor_id);
233  }
234 
235  return percentage;
236 }
237 
238 float Parameters::GetPercentageRunningSign(const ActorId &actor_id) const {
239 
240  float percentage = 0.0f;
241 
242  if (perc_run_traffic_sign.Contains(actor_id)) {
243  percentage = perc_run_traffic_sign.GetValue(actor_id);
244  }
245 
246  return percentage;
247 }
248 
249 float Parameters::GetPercentageIgnoreWalkers(const ActorId &actor_id) const {
250 
251  float percentage = 0.0f;
252 
253  if (perc_ignore_walkers.Contains(actor_id)) {
254  percentage = perc_ignore_walkers.GetValue(actor_id);
255  }
256 
257  return percentage;
258 }
259 
260 float Parameters::GetPercentageIgnoreVehicles(const ActorId &actor_id) const {
261 
262  float percentage = 0.0f;
263 
264  if (perc_ignore_vehicles.Contains(actor_id)) {
265  percentage = perc_ignore_vehicles.GetValue(actor_id);
266  }
267 
268  return percentage;
269 }
270 
272 
273  return hybrid_physics_mode.load();
274 }
275 
277 
278  return osm_mode.load();
279 }
280 
281 } // namespace traffic_manager
282 } // namespace carla
float GetPercentageRunningLight(const ActorId &actor_id) const
Method to get % to run any traffic light.
Definition: Parameters.cpp:227
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:166
AtomicMap< ActorId, float > perc_run_traffic_sign
Map containing % of running a traffic sign.
Definition: Parameters.h:52
float GetVehicleTargetVelocity(const ActorId &actor_id, const float speed_limit) const
Method to query target velocity for a vehicle.
Definition: Parameters.cpp:155
const Value & GetValue(const Key &key) const
Definition: AtomicMap.h:44
std::atomic< float > hybrid_physics_radius
Hybrid physics radius.
Definition: Parameters.h:66
void SetGlobalDistanceToLeadingVehicle(const float dist)
Method to set the distance to leading vehicle for all registered vehicles.
Definition: Parameters.cpp:98
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:83
carla::SharedPtr< cc::Actor > ActorPtr
bool GetOSMMode() const
Method to get Open Street Map mode.
Definition: Parameters.cpp:276
std::atomic< bool > osm_mode
Parameter specifying Open Street Map mode.
Definition: Parameters.h:68
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:77
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:204
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
std::chrono::duration< double, std::milli > synchronous_time_out
Synchronous mode time out variable.
Definition: Parameters.h:182
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set probabilistic preference to keep on the right lane.
Definition: Parameters.cpp:71
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:64
AtomicMap< ActorId, ChangeLaneInfo > force_lane_change
Map containing force lane change commands.
Definition: Parameters.h:46
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:27
AtomicMap< ActorId, float > perc_keep_right
Map containing % of keep right rule.
Definition: Parameters.h:58
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:191
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:38
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
Definition: Parameters.cpp:271
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:142
bool GetSynchronousMode() const
Method to get synchronous mode.
Definition: Parameters.cpp:147
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
Definition: Parameters.cpp:117
float GetPercentageIgnoreVehicles(const ActorId &actor_id) const
Method to get % to ignore any vehicle.
Definition: Parameters.cpp:260
float GetDistanceToLeadingVehicle(const ActorId &actor_id) const
Method to query distance to leading vehicle for a given vehicle.
Definition: Parameters.cpp:215
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
Definition: Parameters.cpp:33
float GetPercentageRunningSign(const ActorId &actor_id) const
Method to get % to run any traffic light.
Definition: Parameters.cpp:238
AtomicMap< ActorId, float > perc_ignore_walkers
Map containing % of ignoring walkers.
Definition: Parameters.h:54
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
Definition: Parameters.cpp:124
void AddEntry(const std::pair< Key, Value > &entry)
Definition: AtomicMap.h:27
float GetPercentageIgnoreWalkers(const ActorId &actor_id) const
Method to get % to ignore any walker.
Definition: Parameters.cpp:249
std::atomic< bool > synchronous_mode
Synchronous mode switch.
Definition: Parameters.h:60
void SetSynchronousMode(const bool mode_switch=true)
Method to set synchronous mode.
Definition: Parameters.cpp:90
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
Definition: Parameters.cpp:131
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:103
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:110
double GetSynchronousModeTimeOutInMiliSecond() const
Get synchronous mode time out.
Definition: Parameters.cpp:151
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:94
ChangeLaneInfo GetForceLaneChange(const ActorId &actor_id)
Method to query lane change command for a vehicle.
Definition: Parameters.cpp:178
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
Definition: Parameters.cpp:22
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
Definition: Parameters.cpp:136