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