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  /// This method sets the hybrid physics mode.
69  void SetHybridPhysicsMode(const bool mode_switch) {
70  TrafficManagerBase* tm_ptr = GetTM(_port);
71  if(tm_ptr != nullptr){
72  tm_ptr->SetHybridPhysicsMode(mode_switch);
73  }
74  }
75 
76  /// This method sets the hybrid physics radius.
77  void SetHybridPhysicsRadius(const float radius) {
78  TrafficManagerBase* tm_ptr = GetTM(_port);
79  if(tm_ptr != nullptr){
80  tm_ptr->SetHybridPhysicsRadius(radius);
81  }
82  }
83 
84  /// This method registers a vehicle with the traffic manager.
85  void RegisterVehicles(const std::vector<ActorPtr> &actor_list) {
86  TrafficManagerBase* tm_ptr = GetTM(_port);
87  if(tm_ptr != nullptr){
88  tm_ptr->RegisterVehicles(actor_list);
89  }
90  }
91 
92  /// This method unregisters a vehicle from traffic manager.
93  void UnregisterVehicles(const std::vector<ActorPtr> &actor_list) {
94  TrafficManagerBase* tm_ptr = GetTM(_port);
95  if(tm_ptr != nullptr){
96  tm_ptr->UnregisterVehicles(actor_list);
97  }
98  }
99 
100  /// Set a vehicle's % decrease in velocity with respect to the speed limit.
101  /// If less than 0, it's a % increase.
102  void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage) {
103  TrafficManagerBase* tm_ptr = GetTM(_port);
104  if(tm_ptr != nullptr){
105  tm_ptr->SetPercentageSpeedDifference(actor, percentage);
106  }
107  }
108 
109  /// Set a global % decrease in velocity with respect to the speed limit.
110  /// If less than 0, it's a % increase.
111  void SetGlobalPercentageSpeedDifference(float const percentage){
112  TrafficManagerBase* tm_ptr = GetTM(_port);
113  if(tm_ptr != nullptr){
114  tm_ptr->SetGlobalPercentageSpeedDifference(percentage);
115  }
116  }
117 
118  /// Method to set collision detection rules between vehicles.
119  void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
120  TrafficManagerBase* tm_ptr = GetTM(_port);
121  if(tm_ptr != nullptr){
122  tm_ptr->SetCollisionDetection(reference_actor, other_actor, detect_collision);
123  }
124  }
125 
126  /// Method to force lane change on a vehicle.
127  /// Direction flag can be set to true for left and false for right.
128  void SetForceLaneChange(const ActorPtr &actor, const bool direction) {
129  TrafficManagerBase* tm_ptr = GetTM(_port);
130  if(tm_ptr != nullptr){
131  tm_ptr->SetForceLaneChange(actor, direction);
132  }
133  }
134 
135  /// Enable/disable automatic lane change on a vehicle.
136  void SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
137  TrafficManagerBase* tm_ptr = GetTM(_port);
138  if(tm_ptr != nullptr){
139  tm_ptr->SetAutoLaneChange(actor, enable);
140  }
141  }
142 
143  /// Method to specify how much distance a vehicle should maintain to
144  /// the leading vehicle.
145  void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
146  TrafficManagerBase* tm_ptr = GetTM(_port);
147  if(tm_ptr != nullptr){
148  tm_ptr->SetDistanceToLeadingVehicle(actor, distance);
149  }
150  }
151 
152  /// Method to specify the % chance of ignoring collisions with any walker.
153  void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
154  TrafficManagerBase* tm_ptr = GetTM(_port);
155  if(tm_ptr != nullptr){
156  tm_ptr->SetPercentageIgnoreWalkers(actor, perc);
157  }
158  }
159 
160  /// Method to specify the % chance of ignoring collisions with any vehicle.
161  void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
162  TrafficManagerBase* tm_ptr = GetTM(_port);
163  if(tm_ptr != nullptr){
164  tm_ptr->SetPercentageIgnoreVehicles(actor, perc);
165  }
166  }
167 
168  /// Method to specify the % chance of running a sign.
169  void SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
170  TrafficManagerBase* tm_ptr = GetTM(_port);
171  if(tm_ptr != nullptr){
172  tm_ptr->SetPercentageRunningSign(actor, perc);
173  }
174  }
175 
176  /// Method to specify the % chance of running a light.
177  void SetPercentageRunningLight(const ActorPtr &actor, const float perc){
178  TrafficManagerBase* tm_ptr = GetTM(_port);
179  if(tm_ptr != nullptr){
180  tm_ptr->SetPercentageRunningLight(actor, perc);
181  }
182  }
183 
184  /// Method to switch traffic manager into synchronous execution.
185  void SetSynchronousMode(bool mode) {
186  TrafficManagerBase* tm_ptr = GetTM(_port);
187  if(tm_ptr != nullptr){
188  tm_ptr->SetSynchronousMode(mode);
189  }
190  }
191 
192  /// Method to set tick timeout for synchronous execution.
194  TrafficManagerBase* tm_ptr = GetTM(_port);
195  if(tm_ptr != nullptr){
197  }
198  }
199 
200  /// Method to provide synchronous tick.
202  TrafficManagerBase* tm_ptr = GetTM(_port);
203  if(tm_ptr != nullptr){
204  return tm_ptr->SynchronousTick();
205  }
206  return false;
207  }
208 
209  /// Method to Set Global distance to Leading vehicle
210  void SetGlobalDistanceToLeadingVehicle(const float distance) {
211  TrafficManagerBase* tm_ptr = GetTM(_port);
212  if(tm_ptr != nullptr){
213  tm_ptr->SetGlobalDistanceToLeadingVehicle(distance);
214  }
215  }
216 
217  /// Method to set probabilistic preference to keep on the right lane.
218  void SetKeepRightPercentage(const ActorPtr &actor, const float percentage) {
219  TrafficManagerBase* tm_ptr = GetTM(_port);
220  if(tm_ptr != nullptr){
221  tm_ptr->SetKeepRightPercentage(actor, percentage);
222  }
223  }
224 
225  /// Method to set randomization seed.
226  void SetRandomDeviceSeed(const uint64_t seed) {
227  TrafficManagerBase* tm_ptr = GetTM(_port);
228  if(tm_ptr != nullptr){
229  tm_ptr->SetRandomDeviceSeed(seed);
230  }
231  }
232 
233 private:
234 
237  uint16_t port);
238 
239 
242  uint16_t port);
243 
244  TrafficManagerBase* GetTM(uint16_t port) const {
245  std::lock_guard<std::mutex> lock(_mutex);
246  auto it = _tm_map.find(port);
247  if (it != _tm_map.end()) {
248  return it->second;
249  }
250  return nullptr;
251  }
252 
253  static std::map<uint16_t, TrafficManagerBase*> _tm_map;
254  static std::mutex _mutex;
255 
256  uint16_t _port = 0;
257 
258 };
259 
260 } // namespace traffic_manager
261 } // 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:20
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.
bool CreateTrafficManagerClient(carla::client::detail::EpisodeProxy episode_proxy, uint16_t port)
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:99
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 SetRandomDeviceSeed(const uint64_t seed)
Method to set randomization seed.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set probabilistic preference 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.
virtual void SetGlobalPercentageSpeedDifference(float const percentage)=0
Set a global % decrease in velocity with respect to the speed limit.
virtual void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)=0
Method to set probabilistic preference to keep on the right lane.
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.
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 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.
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
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)
TrafficManagerBase * GetTM(uint16_t port) const
virtual void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)=0
Method to set collision detection rules between vehicles.
virtual void SetPercentageRunningLight(const ActorPtr &actor, const float perc)=0
Method to specify the % chance of running any traffic light.
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 SetSynchronousModeTimeOutInMiliSecond(double time)=0
Method to set Tick timeout for synchronous execution.
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 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.
void SetHybridPhysicsRadius(const float radius)
This method sets the hybrid physics radius.