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 
18 #include "carla/client/World.h"
19 #include "carla/Memory.h"
20 #include "carla/rpc/Command.h"
21 
30 
36 
37 namespace carla {
38 namespace traffic_manager {
39 
40 namespace chr = std::chrono;
41 
42 using namespace std::chrono_literals;
43 
44 using TimePoint = chr::time_point<chr::system_clock, chr::nanoseconds>;
45 using TLGroup = std::vector<carla::SharedPtr<carla::client::TrafficLight>>;
46 using LocalMapPtr = std::shared_ptr<InMemoryMap>;
48 
49 /// The function of this class is to integrate all the various stages of
50 /// the traffic manager appropriately using messengers.
52 
53 private:
54  /// PID controller parameters.
55  std::vector<float> longitudinal_PID_parameters;
57  std::vector<float> lateral_PID_parameters;
58  std::vector<float> lateral_highway_PID_parameters;
59  /// Carla's client connection object.
61  /// Carla client and object.
63  /// Set of all actors registered with traffic manager.
65  /// State counter to track changes in registered actors.
67  /// List of vehicles registered with the traffic manager in
68  /// current update cycle.
69  std::vector<ActorId> vehicle_id_list;
70  /// Pointer to local map cache.
72  /// Structures to hold waypoint buffers for all vehicles.
74  /// Carla's debug helper object.
76  /// Object for tracking paths of the traffic vehicles.
78  /// Type containing the current state of all actors involved in the simulation.
80  /// Time instance used to calculate dt in asynchronous mode.
82  /// Parameterization object.
84  /// Array to hold output data of localization stage.
86  /// Array to hold output data of collision avoidance.
88  /// Array to hold output data of traffic light response.
90  /// Array to hold output data of motion planning.
92  /// Variable to keep track of currently reserved array space for frames.
93  uint64_t current_reserved_capacity {0u};
94  /// Various stages representing core operations of traffic manager.
100  /// Traffic manager server instance.
102  /// Switch to turn on / turn off traffic manager.
103  std::atomic<bool> run_traffic_manger{true};
104  /// Flags to signal step begin and end.
105  std::atomic<bool> step_begin{false};
106  std::atomic<bool> step_end{false};
107  /// Mutex for progressing synchronous execution.
109  /// Condition variables for progressing synchronous execution.
110  std::condition_variable step_begin_trigger;
111  std::condition_variable step_end_trigger;
112  /// Single worker thread for sequential execution of sub-components.
113  std::unique_ptr<std::thread> worker_thread;
114  /// Structure holding random devices per vehicle.
116  /// Randomization seed.
117  uint64_t seed {static_cast<uint64_t>(time(NULL))};
118  std::vector<ActorId> marked_for_removal;
119  /// Mutex to prevent vehicle registration during frame array re-allocation.
120  std::mutex registration_mutex;
121 
122  /// Method to check if all traffic lights are frozen in a group.
123  bool CheckAllFrozen(TLGroup tl_to_freeze);
124 
125 public:
126  /// Private constructor for singleton lifecycle management.
127  TrafficManagerLocal(std::vector<float> longitudinal_PID_parameters,
128  std::vector<float> longitudinal_highway_PID_parameters,
129  std::vector<float> lateral_PID_parameters,
130  std::vector<float> lateral_highway_PID_parameters,
131  float perc_decrease_from_limit,
132  cc::detail::EpisodeProxy &episode_proxy,
133  uint16_t &RPCportTM);
134 
135  /// Destructor.
136  virtual ~TrafficManagerLocal();
137 
138  /// Method to setup InMemoryMap.
139  void SetupLocalMap();
140 
141  /// To start the TrafficManager.
142  void Start();
143 
144  /// Initiates thread to run the TrafficManager sequentially.
145  void Run();
146 
147  /// To stop the TrafficManager.
148  void Stop();
149 
150  /// To release the traffic manager.
151  void Release();
152 
153  /// To reset the traffic manager.
154  void Reset();
155 
156  /// This method registers a vehicle with the traffic manager.
157  void RegisterVehicles(const std::vector<ActorPtr> &actor_list);
158 
159  /// This method unregisters a vehicle from traffic manager.
160  void UnregisterVehicles(const std::vector<ActorPtr> &actor_list);
161 
162  /// Method to set a vehicle's % decrease in velocity with respect to the speed limit.
163  /// If less than 0, it's a % increase.
164  void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage);
165 
166  /// Methos to set a global % decrease in velocity with respect to the speed limit.
167  /// If less than 0, it's a % increase.
168  void SetGlobalPercentageSpeedDifference(float const percentage);
169 
170  /// Method to set collision detection rules between vehicles.
171  void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision);
172 
173  /// Method to force lane change on a vehicle.
174  /// Direction flag can be set to true for left and false for right.
175  void SetForceLaneChange(const ActorPtr &actor, const bool direction);
176 
177  /// Enable/disable automatic lane change on a vehicle.
178  void SetAutoLaneChange(const ActorPtr &actor, const bool enable);
179 
180  /// Method to specify how much distance a vehicle should maintain to
181  /// the leading vehicle.
182  void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance);
183 
184  /// Method to specify the % chance of ignoring collisions with any walker.
185  void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc);
186 
187  /// Method to specify the % chance of ignoring collisions with any vehicle.
188  void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc);
189 
190  /// Method to specify the % chance of running any traffic light.
191  void SetPercentageRunningLight(const ActorPtr &actor, const float perc);
192 
193  /// Method to specify the % chance of running any traffic sign.
194  void SetPercentageRunningSign(const ActorPtr &actor, const float perc);
195 
196  /// Method to switch traffic manager into synchronous execution.
197  void SetSynchronousMode(bool mode);
198 
199  /// Method to set Tick timeout for synchronous execution.
200  void SetSynchronousModeTimeOutInMiliSecond(double time);
201 
202  /// Method to provide synchronous tick.
203  bool SynchronousTick();
204 
205  /// Get CARLA episode information.
206  carla::client::detail::EpisodeProxy &GetEpisodeProxy();
207 
208  /// Get list of all registered vehicles.
209  std::vector<ActorId> GetRegisteredVehiclesIDs();
210 
211  /// Method to specify how much distance a vehicle should maintain to
212  /// the Global leading vehicle.
213  void SetGlobalDistanceToLeadingVehicle(const float distance);
214 
215  /// Method to set probabilistic preference to keep on the right lane.
216  void SetKeepRightPercentage(const ActorPtr &actor, const float percentage);
217 
218  /// Method to set hybrid physics mode.
219  void SetHybridPhysicsMode(const bool mode_switch);
220 
221  /// Method to set hybrid physics radius.
222  void SetHybridPhysicsRadius(const float radius);
223 
224  /// Method to set randomization seed.
225  void SetRandomDeviceSeed(const uint64_t _seed);
226 
227  /// Method to set Open Street Map mode.
228  void SetOSMMode(const bool mode_switch);
229 };
230 
231 } // namespace traffic_manager
232 } // 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:99
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&#39;s 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...
cc::DebugHelper debug_helper
Carla&#39;s debug helper object.
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