CARLA
TrafficManager.h
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 
7 #pragma once
8 
9 #include <map>
10 #include <mutex>
11 #include <vector>
12 
13 #include "carla/client/Actor.h"
16 
17 namespace carla {
18 namespace traffic_manager {
19 
21 
23 
24 /// This class integrates all the various stages of
25 /// the traffic manager appropriately using messengers.
27 
28 public:
29  /// Public constructor for singleton life cycle management.
30  explicit TrafficManager(
32  uint16_t port = TM_DEFAULT_PORT);
33 
35  _port = other._port;
36  }
37 
39 
40  TrafficManager(TrafficManager &&) = default;
41 
42  TrafficManager &operator=(const TrafficManager &) = default;
44 
45  static void Release();
46 
47  static void Reset();
48 
49  static void Tick();
50 
51  uint16_t Port() const {
52  return _port;
53  }
54 
55  bool IsValidPort() const {
56  // The first 1024 ports are reserved by the OS
57  return (_port > 1023);
58  }
59 
60  /// Method to set Open Street Map mode.
61  void SetOSMMode(const bool mode_switch) {
62  TrafficManagerBase* tm_ptr = GetTM(_port);
63  if (tm_ptr != nullptr) {
64  tm_ptr->SetOSMMode(mode_switch);
65  }
66  }
67 
68  /// Method to set our own imported path.
69  void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer) {
70  TrafficManagerBase* tm_ptr = GetTM(_port);
71  if (tm_ptr != nullptr) {
72  tm_ptr->SetCustomPath(actor, path, empty_buffer);
73  }
74  }
75 
76  /// Method to remove a path.
77  void RemoveUploadPath(const ActorId &actor_id, const bool remove_path) {
78  TrafficManagerBase* tm_ptr = GetTM(_port);
79  if (tm_ptr != nullptr) {
80  tm_ptr->RemoveUploadPath(actor_id, remove_path);
81  }
82  }
83 
84  /// Method to update an already set path.
85  void UpdateUploadPath(const ActorId &actor_id, const Path path) {
86  TrafficManagerBase* tm_ptr = GetTM(_port);
87  if (tm_ptr != nullptr) {
88  tm_ptr->UpdateUploadPath(actor_id, path);
89  }
90  }
91 
92  /// Method to set our own imported route.
93  void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer) {
94  TrafficManagerBase* tm_ptr = GetTM(_port);
95  if (tm_ptr != nullptr) {
96  tm_ptr->SetImportedRoute(actor, route, empty_buffer);
97  }
98  }
99 
100  /// Method to remove a route.
101  void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path) {
102  TrafficManagerBase* tm_ptr = GetTM(_port);
103  if (tm_ptr != nullptr) {
104  tm_ptr->RemoveImportedRoute(actor_id, remove_path);
105  }
106  }
107 
108  /// Method to update an already set route.
109  void UpdateImportedRoute(const ActorId &actor_id, const Route route) {
110  TrafficManagerBase* tm_ptr = GetTM(_port);
111  if (tm_ptr != nullptr) {
112  tm_ptr->UpdateImportedRoute(actor_id, route);
113  }
114  }
115 
116  /// Method to set if we are automatically respawning vehicles.
117  void SetRespawnDormantVehicles(const bool mode_switch) {
118  TrafficManagerBase* tm_ptr = GetTM(_port);
119  if (tm_ptr != nullptr) {
120  tm_ptr->SetRespawnDormantVehicles(mode_switch);
121  }
122  }
123  /// Method to set boundaries for respawning vehicles.
124  void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound) {
125  TrafficManagerBase* tm_ptr = GetTM(_port);
126  if (tm_ptr != nullptr) {
127  tm_ptr->SetBoundariesRespawnDormantVehicles(lower_bound, upper_bound);
128  }
129  }
130 
131  /// Method to set boundaries for respawning vehicles.
132  void SetMaxBoundaries(const float lower, const float upper) {
133  TrafficManagerBase* tm_ptr = GetTM(_port);
134  if (tm_ptr != nullptr) {
135  tm_ptr->SetMaxBoundaries(lower, upper);
136  }
137  }
138 
139  /// This method sets the hybrid physics mode.
140  void SetHybridPhysicsMode(const bool mode_switch) {
141  TrafficManagerBase* tm_ptr = GetTM(_port);
142  if(tm_ptr != nullptr){
143  tm_ptr->SetHybridPhysicsMode(mode_switch);
144  }
145  }
146 
147  /// This method sets the hybrid physics radius.
148  void SetHybridPhysicsRadius(const float radius) {
149  TrafficManagerBase* tm_ptr = GetTM(_port);
150  if(tm_ptr != nullptr){
151  tm_ptr->SetHybridPhysicsRadius(radius);
152  }
153  }
154 
155  /// This method registers a vehicle with the traffic manager.
156  void RegisterVehicles(const std::vector<ActorPtr> &actor_list) {
157  TrafficManagerBase* tm_ptr = GetTM(_port);
158  if(tm_ptr != nullptr){
159  tm_ptr->RegisterVehicles(actor_list);
160  }
161  }
162 
163  /// This method unregisters a vehicle from traffic manager.
164  void UnregisterVehicles(const std::vector<ActorPtr> &actor_list) {
165  TrafficManagerBase* tm_ptr = GetTM(_port);
166  if(tm_ptr != nullptr){
167  tm_ptr->UnregisterVehicles(actor_list);
168  }
169  }
170 
171  /// Set a vehicle's % decrease in velocity with respect to the speed limit.
172  /// If less than 0, it's a % increase.
173  void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) {
174  TrafficManagerBase* tm_ptr = GetTM(_port);
175  if(tm_ptr != nullptr){
176  tm_ptr->SetPercentageSpeedDifference(actor, percentage);
177  }
178  }
179 
180  /// Set a vehicle's exact desired velocity.
181  void SetDesiredSpeed(const ActorPtr &actor, const float value) {
182  TrafficManagerBase* tm_ptr = GetTM(_port);
183  if(tm_ptr != nullptr){
184  tm_ptr->SetDesiredSpeed(actor, value);
185  }
186  }
187 
188  /// Set a global % decrease in velocity with respect to the speed limit.
189  /// If less than 0, it's a % increase.
190  void SetGlobalPercentageSpeedDifference(float const percentage){
191  TrafficManagerBase* tm_ptr = GetTM(_port);
192  if(tm_ptr != nullptr){
193  tm_ptr->SetGlobalPercentageSpeedDifference(percentage);
194  }
195  }
196 
197  /// Set the automatic management of the vehicle lights
198  void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update){
199  TrafficManagerBase* tm_ptr = GetTM(_port);
200  if(tm_ptr != nullptr){
201  tm_ptr->SetUpdateVehicleLights(actor, do_update);
202  }
203  }
204 
205  /// Method to set collision detection rules between vehicles.
206  void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
207  TrafficManagerBase* tm_ptr = GetTM(_port);
208  if(tm_ptr != nullptr){
209  tm_ptr->SetCollisionDetection(reference_actor, other_actor, detect_collision);
210  }
211  }
212 
213  /// Method to force lane change on a vehicle.
214  /// Direction flag can be set to true for left and false for right.
215  void SetForceLaneChange(const ActorPtr &actor, const bool direction) {
216  TrafficManagerBase* tm_ptr = GetTM(_port);
217  if(tm_ptr != nullptr){
218  tm_ptr->SetForceLaneChange(actor, direction);
219  }
220  }
221 
222  /// Enable/disable automatic lane change on a vehicle.
223  void SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
224  TrafficManagerBase* tm_ptr = GetTM(_port);
225  if(tm_ptr != nullptr){
226  tm_ptr->SetAutoLaneChange(actor, enable);
227  }
228  }
229 
230  /// Method to specify how much distance a vehicle should maintain to
231  /// the leading vehicle.
232  void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
233  TrafficManagerBase* tm_ptr = GetTM(_port);
234  if(tm_ptr != nullptr){
235  tm_ptr->SetDistanceToLeadingVehicle(actor, distance);
236  }
237  }
238 
239  /// Method to specify the % chance of ignoring collisions with any walker.
240  void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
241  TrafficManagerBase* tm_ptr = GetTM(_port);
242  if(tm_ptr != nullptr){
243  tm_ptr->SetPercentageIgnoreWalkers(actor, perc);
244  }
245  }
246 
247  /// Method to specify the % chance of ignoring collisions with any vehicle.
248  void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
249  TrafficManagerBase* tm_ptr = GetTM(_port);
250  if(tm_ptr != nullptr){
251  tm_ptr->SetPercentageIgnoreVehicles(actor, perc);
252  }
253  }
254 
255  /// Method to specify the % chance of running a sign.
256  void SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
257  TrafficManagerBase* tm_ptr = GetTM(_port);
258  if(tm_ptr != nullptr){
259  tm_ptr->SetPercentageRunningSign(actor, perc);
260  }
261  }
262 
263  /// Method to specify the % chance of running a light.
264  void SetPercentageRunningLight(const ActorPtr &actor, const float perc){
265  TrafficManagerBase* tm_ptr = GetTM(_port);
266  if(tm_ptr != nullptr){
267  tm_ptr->SetPercentageRunningLight(actor, perc);
268  }
269  }
270 
271  /// Method to switch traffic manager into synchronous execution.
272  void SetSynchronousMode(bool mode) {
273  TrafficManagerBase* tm_ptr = GetTM(_port);
274  if(tm_ptr != nullptr){
275  tm_ptr->SetSynchronousMode(mode);
276  }
277  }
278 
279  /// Method to set tick timeout for synchronous execution.
281  TrafficManagerBase* tm_ptr = GetTM(_port);
282  if(tm_ptr != nullptr){
284  }
285  }
286 
287  /// Method to provide synchronous tick.
289  TrafficManagerBase* tm_ptr = GetTM(_port);
290  if(tm_ptr != nullptr){
291  return tm_ptr->SynchronousTick();
292  }
293  return false;
294  }
295 
296  /// Method to Set Global distance to Leading vehicle
297  void SetGlobalDistanceToLeadingVehicle(const float distance) {
298  TrafficManagerBase* tm_ptr = GetTM(_port);
299  if(tm_ptr != nullptr){
300  tm_ptr->SetGlobalDistanceToLeadingVehicle(distance);
301  }
302  }
303 
304  /// Method to set % to keep on the right lane.
305  void SetKeepRightPercentage(const ActorPtr &actor, const float percentage) {
306  TrafficManagerBase* tm_ptr = GetTM(_port);
307  if(tm_ptr != nullptr){
308  tm_ptr->SetKeepRightPercentage(actor, percentage);
309  }
310  }
311 
312  /// Method to set % to randomly do a left lane change.
313  void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage) {
314  TrafficManagerBase* tm_ptr = GetTM(_port);
315  if(tm_ptr != nullptr){
316  tm_ptr->SetRandomLeftLaneChangePercentage(actor, percentage);
317  }
318  }
319 
320  /// Method to set % to randomly do a right lane change.
321  void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage) {
322  TrafficManagerBase* tm_ptr = GetTM(_port);
323  if(tm_ptr != nullptr){
324  tm_ptr->SetRandomRightLaneChangePercentage(actor, percentage);
325  }
326  }
327 
328  /// Method to set randomization seed.
329  void SetRandomDeviceSeed(const uint64_t seed) {
330  TrafficManagerBase* tm_ptr = GetTM(_port);
331  if(tm_ptr != nullptr){
332  tm_ptr->SetRandomDeviceSeed(seed);
333  }
334  }
335 
336  void ShutDown();
337 
338  /// Method to get the next action.
339  Action GetNextAction(const ActorId &actor_id) {
340  Action next_action;
341  TrafficManagerBase* tm_ptr = GetTM(_port);
342  if (tm_ptr != nullptr) {
343  next_action = tm_ptr->GetNextAction(actor_id);
344  return next_action;
345  }
346  return next_action;
347  }
348 
349  /// Method to get the action buffer.
351  ActionBuffer action_buffer;
352  TrafficManagerBase* tm_ptr = GetTM(_port);
353  if (tm_ptr != nullptr) {
354  action_buffer = tm_ptr->GetActionBuffer(actor_id);
355  return action_buffer;
356  }
357  return action_buffer;
358  }
359 
360 private:
361 
364  uint16_t port);
365 
366 
369  uint16_t port);
370 
371  TrafficManagerBase* GetTM(uint16_t port) const {
372  std::lock_guard<std::mutex> lock(_mutex);
373  auto it = _tm_map.find(port);
374  if (it != _tm_map.end()) {
375  return it->second;
376  }
377  return nullptr;
378  }
379 
380  static std::map<uint16_t, TrafficManagerBase*> _tm_map;
381  static std::mutex _mutex;
382 
383  uint16_t _port = 0;
384 
385 };
386 
387 } // namespace traffic_manager
388 } // namespace carla
virtual void SetHybridPhysicsMode(const bool mode_switch)=0
Method to set hybrid physics mode.
void SetSynchronousModeTimeOutInMiliSecond(double time)
Method to set tick timeout for synchronous execution.
virtual void SetAutoLaneChange(const ActorPtr &actor, const bool enable)=0
Enable/disable automatic lane change on a vehicle.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any vehicle.
virtual void SetForceLaneChange(const ActorPtr &actor, const bool direction)=0
Method to force lane change on a vehicle.
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
virtual bool SynchronousTick()=0
Method to provide synchronous tick.
static const unsigned short TM_DEFAULT_PORT
Definition: Constants.h:22
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
carla::SharedPtr< cc::Actor > ActorPtr
virtual void SetGlobalDistanceToLeadingVehicle(const float dist)=0
Method to set Global Distance to Leading Vehicle.
virtual void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any walker.
std::vector< cg::Location > Path
bool CreateTrafficManagerClient(carla::client::detail::EpisodeProxy episode_proxy, uint16_t port)
virtual void SetMaxBoundaries(const float lower, const float upper)=0
Method to set limits for boundaries when respawning vehicles.
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static std::map< uint16_t, TrafficManagerBase * > _tm_map
virtual void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)=0
Set a vehicle&#39;s % decrease in velocity with respect to the speed limit.
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to specify the % chance of running a light.
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
virtual void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method unregisters a vehicle from traffic manager.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to Set Global distance to Leading vehicle.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Set a vehicle&#39;s % decrease in velocity with respect to the speed limit.
TrafficManager & operator=(const TrafficManager &)=default
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
virtual Action GetNextAction(const ActorId &actor_id)=0
Method to get the vehicle&#39;s next action.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
virtual void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a left lane change.
virtual void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)=0
Method to remove a route.
virtual void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)=0
Method to set our own imported route.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set path.
std::pair< RoadOption, WaypointPtr > Action
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the right lane.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any walker.
virtual void SetOSMMode(const bool mode_switch)=0
Method to set Open Street Map mode.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the action buffer.
virtual void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)=0
Method to remove a path.
virtual void SetGlobalPercentageSpeedDifference(float const percentage)=0
Set a global % decrease in velocity with respect to the speed limit.
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
virtual void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to keep on the right lane.
carla::ActorId ActorId
This class integrates all the various stages of the traffic manager appropriately using messengers...
bool SynchronousTick()
Method to provide synchronous tick.
void SetHybridPhysicsMode(const bool mode_switch)
This method sets the hybrid physics mode.
void SetDesiredSpeed(const ActorPtr &actor, const float value)
Set a vehicle&#39;s exact desired velocity.
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set if we are automatically respawning vehicles.
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
virtual void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)=0
Method to specify how much distance a vehicle should maintain to the leading vehicle.
virtual void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)=0
Method to set the automatic management of the vehicle lights.
virtual void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of ignoring collisions with any vehicle.
void SetSynchronousMode(bool mode)
Method to switch traffic manager into synchronous execution.
virtual void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)=0
Method to set our own imported path.
std::vector< uint8_t > Route
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
void SetMaxBoundaries(const float lower, const float upper)
Method to set boundaries for respawning vehicles.
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to specify the % chance of running a sign.
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
TrafficManager(const TrafficManager &other)
virtual void SetRespawnDormantVehicles(const bool mode_switch)=0
Method to set automatic respawn of dormant vehicles.
TrafficManagerBase * GetTM(uint16_t port) const
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
virtual void UpdateUploadPath(const ActorId &actor_id, const Path path)=0
Method to update an already set path.
virtual void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)=0
Method to set collision detection rules between vehicles.
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Set the automatic management of the vehicle lights.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
virtual void SetPercentageRunningLight(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic light.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a path.
Action GetNextAction(const ActorId &actor_id)
Method to get the next action.
virtual void UpdateImportedRoute(const ActorId &actor_id, const Route route)=0
Method to update an already set route.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
virtual void SetHybridPhysicsRadius(const float radius)=0
Method to set hybrid physics radius.
virtual void SetRandomDeviceSeed(const uint64_t seed)=0
Method to set randomization seed.
virtual void SetDesiredSpeed(const ActorPtr &actor, const float value)=0
Set a vehicle&#39;s exact desired velocity.
std::vector< Action > ActionBuffer
virtual void SetSynchronousModeTimeOutInMiliSecond(double time)=0
Method to set Tick timeout for synchronous execution.
virtual void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)=0
Method to set boundaries for respawning vehicles.
void CreateTrafficManagerServer(carla::client::detail::EpisodeProxy episode_proxy, uint16_t port)
virtual void SetPercentageRunningSign(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic sign.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
virtual ActionBuffer GetActionBuffer(const ActorId &actor_id)=0
Method to get the vehicle&#39;s action buffer.
virtual void SetSynchronousMode(bool mode)=0
Method to switch traffic manager into synchronous execution.
virtual void RegisterVehicles(const std::vector< ActorPtr > &actor_list)=0
This method registers a vehicle with the traffic manager.
virtual void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to randomly do a right lane change.
void SetHybridPhysicsRadius(const float radius)
This method sets the hybrid physics radius.