CARLA
 
载入中...
搜索中...
未找到
TrafficManager.h
浏览该文件的文档.
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
17namespace carla {
18namespace traffic_manager {
19
21
23
24/// This class integrates all the various stages of
25/// the traffic manager appropriately using messengers.
27
28public:
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
41
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) {
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) {
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) {
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) {
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) {
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 /// Method to set a lane offset displacement from the center line.
181 /// Positive values imply a right offset while negative ones mean a left one.
182 void SetLaneOffset(const ActorPtr &actor, const float offset) {
183 TrafficManagerBase* tm_ptr = GetTM(_port);
184 if(tm_ptr != nullptr){
185 tm_ptr->SetLaneOffset(actor, offset);
186 }
187 }
188
189 /// Set a vehicle's exact desired velocity.
190 void SetDesiredSpeed(const ActorPtr &actor, const float value) {
191 TrafficManagerBase* tm_ptr = GetTM(_port);
192 if(tm_ptr != nullptr){
193 tm_ptr->SetDesiredSpeed(actor, value);
194 }
195 }
196
197 /// Set a global % decrease in velocity with respect to the speed limit.
198 /// If less than 0, it's a % increase.
199 void SetGlobalPercentageSpeedDifference(float const percentage){
200 TrafficManagerBase* tm_ptr = GetTM(_port);
201 if(tm_ptr != nullptr){
202 tm_ptr->SetGlobalPercentageSpeedDifference(percentage);
203 }
204 }
205
206 /// Method to set a global lane offset displacement from the center line.
207 /// Positive values imply a right offset while negative ones mean a left one.
208 void SetGlobalLaneOffset(float const offset){
209 TrafficManagerBase* tm_ptr = GetTM(_port);
210 if(tm_ptr != nullptr){
211 tm_ptr->SetGlobalLaneOffset(offset);
212 }
213 }
214
215 /// Set the automatic management of the vehicle lights
216 void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update){
217 TrafficManagerBase* tm_ptr = GetTM(_port);
218 if(tm_ptr != nullptr){
219 tm_ptr->SetUpdateVehicleLights(actor, do_update);
220 }
221 }
222
223 /// Method to set collision detection rules between vehicles.
224 void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision) {
225 TrafficManagerBase* tm_ptr = GetTM(_port);
226 if(tm_ptr != nullptr){
227 tm_ptr->SetCollisionDetection(reference_actor, other_actor, detect_collision);
228 }
229 }
230
231 /// Method to force lane change on a vehicle.
232 /// Direction flag can be set to true for left and false for right.
233 void SetForceLaneChange(const ActorPtr &actor, const bool direction) {
234 TrafficManagerBase* tm_ptr = GetTM(_port);
235 if(tm_ptr != nullptr){
236 tm_ptr->SetForceLaneChange(actor, direction);
237 }
238 }
239
240 /// Enable/disable automatic lane change on a vehicle.
241 void SetAutoLaneChange(const ActorPtr &actor, const bool enable) {
242 TrafficManagerBase* tm_ptr = GetTM(_port);
243 if(tm_ptr != nullptr){
244 tm_ptr->SetAutoLaneChange(actor, enable);
245 }
246 }
247
248 /// Method to specify how much distance a vehicle should maintain to
249 /// the leading vehicle.
250 void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance) {
251 TrafficManagerBase* tm_ptr = GetTM(_port);
252 if(tm_ptr != nullptr){
253 tm_ptr->SetDistanceToLeadingVehicle(actor, distance);
254 }
255 }
256
257 /// Method to specify the % chance of ignoring collisions with any walker.
258 void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc) {
259 TrafficManagerBase* tm_ptr = GetTM(_port);
260 if(tm_ptr != nullptr){
261 tm_ptr->SetPercentageIgnoreWalkers(actor, perc);
262 }
263 }
264
265 /// Method to specify the % chance of ignoring collisions with any vehicle.
266 void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc) {
267 TrafficManagerBase* tm_ptr = GetTM(_port);
268 if(tm_ptr != nullptr){
269 tm_ptr->SetPercentageIgnoreVehicles(actor, perc);
270 }
271 }
272
273 /// Method to specify the % chance of running a sign.
274 void SetPercentageRunningSign(const ActorPtr &actor, const float perc) {
275 TrafficManagerBase* tm_ptr = GetTM(_port);
276 if(tm_ptr != nullptr){
277 tm_ptr->SetPercentageRunningSign(actor, perc);
278 }
279 }
280
281 /// Method to specify the % chance of running a light.
282 void SetPercentageRunningLight(const ActorPtr &actor, const float perc){
283 TrafficManagerBase* tm_ptr = GetTM(_port);
284 if(tm_ptr != nullptr){
285 tm_ptr->SetPercentageRunningLight(actor, perc);
286 }
287 }
288
289 /// Method to switch traffic manager into synchronous execution.
290 void SetSynchronousMode(bool mode) {
291 TrafficManagerBase* tm_ptr = GetTM(_port);
292 if(tm_ptr != nullptr){
293 tm_ptr->SetSynchronousMode(mode);
294 }
295 }
296
297 /// Method to set tick timeout for synchronous execution.
299 TrafficManagerBase* tm_ptr = GetTM(_port);
300 if(tm_ptr != nullptr){
302 }
303 }
304
305 /// Method to provide synchronous tick.
307 TrafficManagerBase* tm_ptr = GetTM(_port);
308 if(tm_ptr != nullptr){
309 return tm_ptr->SynchronousTick();
310 }
311 return false;
312 }
313
314 /// Method to Set Global distance to Leading vehicle
315 void SetGlobalDistanceToLeadingVehicle(const float distance) {
316 TrafficManagerBase* tm_ptr = GetTM(_port);
317 if(tm_ptr != nullptr){
318 tm_ptr->SetGlobalDistanceToLeadingVehicle(distance);
319 }
320 }
321
322 /// Method to set % to keep on the right lane.
323 void SetKeepRightPercentage(const ActorPtr &actor, const float percentage) {
324 TrafficManagerBase* tm_ptr = GetTM(_port);
325 if(tm_ptr != nullptr){
326 tm_ptr->SetKeepRightPercentage(actor, percentage);
327 }
328 }
329
330 /// Method to set % to randomly do a left lane change.
331 void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage) {
332 TrafficManagerBase* tm_ptr = GetTM(_port);
333 if(tm_ptr != nullptr){
334 tm_ptr->SetRandomLeftLaneChangePercentage(actor, percentage);
335 }
336 }
337
338 /// Method to set % to randomly do a right lane change.
339 void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage) {
340 TrafficManagerBase* tm_ptr = GetTM(_port);
341 if(tm_ptr != nullptr){
342 tm_ptr->SetRandomRightLaneChangePercentage(actor, percentage);
343 }
344 }
345
346 /// Method to set randomization seed.
347 void SetRandomDeviceSeed(const uint64_t seed) {
348 TrafficManagerBase* tm_ptr = GetTM(_port);
349 if(tm_ptr != nullptr){
350 tm_ptr->SetRandomDeviceSeed(seed);
351 }
352 }
353
354 void ShutDown();
355
356 /// Method to get the next action.
357 Action GetNextAction(const ActorId &actor_id) {
358 Action next_action;
359 TrafficManagerBase* tm_ptr = GetTM(_port);
360 if (tm_ptr != nullptr) {
361 next_action = tm_ptr->GetNextAction(actor_id);
362 return next_action;
363 }
364 return next_action;
365 }
366
367 /// Method to get the action buffer.
369 ActionBuffer action_buffer;
370 TrafficManagerBase* tm_ptr = GetTM(_port);
371 if (tm_ptr != nullptr) {
372 action_buffer = tm_ptr->GetActionBuffer(actor_id);
373 return action_buffer;
374 }
375 return action_buffer;
376 }
377
378private:
379
382 uint16_t port);
383
384
387 uint16_t port);
388
389 TrafficManagerBase* GetTM(uint16_t port) const {
390 std::lock_guard<std::mutex> lock(_mutex);
391 auto it = _tm_map.find(port);
392 if (it != _tm_map.end()) {
393 return it->second;
394 }
395 return nullptr;
396 }
397
398 static std::map<uint16_t, TrafficManagerBase*> _tm_map;
399 static std::mutex _mutex;
400
401 uint16_t _port = 0;
402
403};
404
405} // namespace traffic_manager
406} // namespace carla
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
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 SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)=0
Method to set boundaries for respawning vehicles.
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 SetMaxBoundaries(const float lower, const float upper)=0
Method to set limits for boundaries when respawning vehicles.
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 void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)=0
Method to set % to keep on the right lane.
virtual void SetSynchronousMode(bool mode)=0
Method to switch traffic manager into synchronous execution.
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 SetRespawnDormantVehicles(const bool mode_switch)=0
Method to set automatic respawn of dormant vehicles.
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 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.
This class integrates all the various stages of the traffic manager appropriately using messengers.
static std::map< uint16_t, TrafficManagerBase * > _tm_map
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.
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 SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the right lane.
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 SetMaxBoundaries(const float lower, const float upper)
Method to set boundaries for respawning vehicles.
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.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning 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.
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 SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
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 SetRespawnDormantVehicles(const bool mode_switch)
Method to set if we are automatically respawning vehicles.
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
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.
static const unsigned short TM_DEFAULT_PORT
Definition Constants.h:22
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.
Definition Carla.cpp:133
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