CARLA
TrafficManagerLocal.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 <atomic>
10 #include <chrono>
11 #include <mutex>
12 #include <thread>
13 #include <vector>
14 
17 #include "carla/client/World.h"
18 #include "carla/Memory.h"
19 #include "carla/rpc/Command.h"
20 
29 
35 
36 namespace carla {
37 namespace traffic_manager {
38 
39 namespace chr = std::chrono;
40 
41 using namespace std::chrono_literals;
42 
43 using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
44 using TLGroup = std::vector<carla::SharedPtr<carla::client::TrafficLight>>;
45 using LocalMapPtr = std::shared_ptr<InMemoryMap>;
47 
48 /// The function of this class is to integrate all the various stages of
49 /// the traffic manager appropriately using messengers.
51 
52 private:
53  /// PID controller parameters.
54  std::vector<float> longitudinal_PID_parameters;
56  std::vector<float> lateral_PID_parameters;
57  std::vector<float> lateral_highway_PID_parameters;
58  /// CARLA client connection object.
60  /// CARLA client and object.
62  /// Set of all actors registered with traffic manager.
64  /// State counter to track changes in registered actors.
66  /// List of vehicles registered with the traffic manager in
67  /// current update cycle.
68  std::vector<ActorId> vehicle_id_list;
69  /// Pointer to local map cache.
71  /// Structures to hold waypoint buffers for all vehicles.
73  /// Object for tracking paths of the traffic vehicles.
75  /// Type containing the current state of all actors involved in the simulation.
77  /// Time instance used to calculate dt in asynchronous mode.
79  /// Parameterization object.
81  /// Array to hold output data of localization stage.
83  /// Array to hold output data of collision avoidance.
85  /// Array to hold output data of traffic light response.
87  /// Array to hold output data of motion planning.
89  /// Variable to keep track of currently reserved array space for frames.
90  uint64_t current_reserved_capacity {0u};
91  /// Various stages representing core operations of traffic manager.
97  /// Traffic manager server instance.
99  /// Switch to turn on / turn off traffic manager.
100  std::atomic<bool> run_traffic_manger{true};
101  /// Flags to signal step begin and end.
102  std::atomic<bool> step_begin{false};
103  std::atomic<bool> step_end{false};
104  /// Mutex for progressing synchronous execution.
106  /// Condition variables for progressing synchronous execution.
107  std::condition_variable step_begin_trigger;
108  std::condition_variable step_end_trigger;
109  /// Single worker thread for sequential execution of sub-components.
110  std::unique_ptr<std::thread> worker_thread;
111  /// Structure holding random devices per vehicle.
113  /// Randomization seed.
114  uint64_t seed {static_cast<uint64_t>(time(NULL))};
115  bool is_custom_seed {false};
116  std::vector<ActorId> marked_for_removal;
117  /// Mutex to prevent vehicle registration during frame array re-allocation.
118  std::mutex registration_mutex;
119 
120  /// Method to check if all traffic lights are frozen in a group.
121  bool CheckAllFrozen(TLGroup tl_to_freeze);
122 
123 public:
124  /// Private constructor for singleton lifecycle management.
125  TrafficManagerLocal(std::vector<float> longitudinal_PID_parameters,
126  std::vector<float> longitudinal_highway_PID_parameters,
127  std::vector<float> lateral_PID_parameters,
128  std::vector<float> lateral_highway_PID_parameters,
129  float perc_decrease_from_limit,
130  cc::detail::EpisodeProxy &episode_proxy,
131  uint16_t &RPCportTM);
132 
133  /// Destructor.
134  virtual ~TrafficManagerLocal();
135 
136  /// Method to setup InMemoryMap.
137  void SetupLocalMap();
138 
139  /// To start the TrafficManager.
140  void Start();
141 
142  /// Initiates thread to run the TrafficManager sequentially.
143  void Run();
144 
145  /// To stop the TrafficManager.
146  void Stop();
147 
148  /// To release the traffic manager.
149  void Release();
150 
151  /// To reset the traffic manager.
152  void Reset();
153 
154  /// This method registers a vehicle with the traffic manager.
155  void RegisterVehicles(const std::vector<ActorPtr> &actor_list);
156 
157  /// This method unregisters a vehicle from traffic manager.
158  void UnregisterVehicles(const std::vector<ActorPtr> &actor_list);
159 
160  /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
161  /// If less than 0, it's a % increase.
162  void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage);
163 
164  /// Methos to set a global % decrease in velocity with respect to the speed limit.
165  /// If less than 0, it's a % increase.
166  void SetGlobalPercentageSpeedDifference(float const percentage);
167 
168  /// Method to set collision detection rules between vehicles.
169  void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision);
170 
171  /// Method to force lane change on a vehicle.
172  /// Direction flag can be set to true for left and false for right.
173  void SetForceLaneChange(const ActorPtr &actor, const bool direction);
174 
175  /// Enable/disable automatic lane change on a vehicle.
176  void SetAutoLaneChange(const ActorPtr &actor, const bool enable);
177 
178  /// Method to specify how much distance a vehicle should maintain to
179  /// the leading vehicle.
180  void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance);
181 
182  /// Method to specify the % chance of ignoring collisions with any walker.
183  void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc);
184 
185  /// Method to specify the % chance of ignoring collisions with any vehicle.
186  void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc);
187 
188  /// Method to specify the % chance of running any traffic light.
189  void SetPercentageRunningLight(const ActorPtr &actor, const float perc);
190 
191  /// Method to specify the % chance of running any traffic sign.
192  void SetPercentageRunningSign(const ActorPtr &actor, const float perc);
193 
194  /// Method to switch traffic manager into synchronous execution.
195  void SetSynchronousMode(bool mode);
196 
197  /// Method to set Tick timeout for synchronous execution.
198  void SetSynchronousModeTimeOutInMiliSecond(double time);
199 
200  /// Method to provide synchronous tick.
201  bool SynchronousTick();
202 
203  /// Get CARLA episode information.
204  carla::client::detail::EpisodeProxy &GetEpisodeProxy();
205 
206  /// Get list of all registered vehicles.
207  std::vector<ActorId> GetRegisteredVehiclesIDs();
208 
209  /// Method to specify how much distance a vehicle should maintain to
210  /// the Global leading vehicle.
211  void SetGlobalDistanceToLeadingVehicle(const float distance);
212 
213  /// Method to set probabilistic preference to keep on the right lane.
214  void SetKeepRightPercentage(const ActorPtr &actor, const float percentage);
215 
216  /// Method to set hybrid physics mode.
217  void SetHybridPhysicsMode(const bool mode_switch);
218 
219  /// Method to set hybrid physics radius.
220  void SetHybridPhysicsRadius(const float radius);
221 
222  /// Method to set randomization seed.
223  void SetRandomDeviceSeed(const uint64_t _seed);
224 
225  /// Method to set Open Street Map mode.
226  void SetOSMMode(const bool mode_switch);
227 
228  /// Method to set automatic respawn of dormant vehicles.
229  void SetRespawnDormantVehicles(const bool mode_switch);
230 
231  // Method to set boundaries to respawn of dormant vehicles.
232  void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound);
233 
234  // Method to set limits for boundaries when respawning dormant vehicles.
235  void SetMaxBoundaries(const float lower, const float upper);
236 
237  void ShutDown() {};
238 };
239 
240 } // namespace traffic_manager
241 } // namespace carla
CollisionFrame collision_frame
Array to hold output data of collision avoidance.
TrafficManagerServer server
Traffic manager server instance.
std::vector< carla::rpc::Command > ControlFrame
carla::SharedPtr< cc::Actor > ActorPtr
std::vector< bool > TLFrame
std::vector< float > longitudinal_PID_parameters
PID controller parameters.
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
std::condition_variable step_begin_trigger
Condition variables for progressing synchronous execution.
std::shared_ptr< InMemoryMap > LocalMapPtr
Definition: ALSM.h:35
This class holds the state of all the vehicles in the simlation.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unordered_map< carla::rpc::ActorId, RandomGenerator > RandomGeneratorMap
std::unordered_map< carla::ActorId, Buffer > BufferMap
ALSM: Agent Lifecycle and State Managerment This class has functionality to update the local cache of...
Definition: ALSM.h:40
Parameters parameters
Parameterization object.
std::vector< carla::SharedPtr< carla::client::TrafficLight > > TLGroup
carla::client::detail::EpisodeProxy episode_proxy
CARLA client connection object.
std::mutex step_execution_mutex
Mutex for progressing synchronous execution.
SimulationState simulation_state
Type containing the current state of all actors involved in the simulation.
cc::World world
CARLA client and object.
AtomicActorSet registered_vehicles
Set of all actors registered with traffic manager.
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
This class has functionality for responding to traffic lights and managing entry into non-signalized ...
std::mutex registration_mutex
Mutex to prevent vehicle registration during frame array re-allocation.
std::vector< ActorId > vehicle_id_list
List of vehicles registered with the traffic manager in current update cycle.
TimePoint previous_update_instance
Time instance used to calculate dt in asynchronous mode.
ControlFrame control_frame
Array to hold output data of motion planning.
RandomGeneratorMap random_devices
Structure holding random devices per vehicle.
BufferMap buffer_map
Structures to hold waypoint buffers for all vehicles.
TrackTraffic track_traffic
Object for tracking paths of the traffic vehicles.
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
std::vector< LocalizationData > LocalizationFrame
TLFrame tl_frame
Array to hold output data of traffic light response.
LocalizationFrame localization_frame
Array to hold output data of localization stage.
LocalMapPtr local_map
Pointer to local map cache.
LocalizationStage localization_stage
Various stages representing core operations of traffic manager.
EpisodeProxyImpl< EpisodeProxyPointerType::Strong > EpisodeProxy
Definition: EpisodeProxy.h:69
int registered_vehicles_state
State counter to track changes in registered actors.
This class has functionality to maintain a horizon of waypoints ahead of the vehicle for it to follow...
std::unique_ptr< std::thread > worker_thread
Single worker thread for sequential execution of sub-components.
This class has functionality to detect potential collision with a nearby actor.
std::vector< CollisionHazardData > CollisionFrame