16 namespace traffic_manager {
18 using namespace constants::FrameMemory;
21 std::vector<float> longitudinal_PID_parameters,
22 std::vector<float> longitudinal_highway_PID_parameters,
23 std::vector<float> lateral_PID_parameters,
24 std::vector<float> lateral_highway_PID_parameters,
25 float perc_difference_from_limit,
29 : longitudinal_PID_parameters(longitudinal_PID_parameters),
30 longitudinal_highway_PID_parameters(longitudinal_highway_PID_parameters),
31 lateral_PID_parameters(lateral_PID_parameters),
32 lateral_highway_PID_parameters(lateral_highway_PID_parameters),
34 episode_proxy(episode_proxy),
35 world(
cc::World(episode_proxy)),
68 longitudinal_PID_parameters,
69 longitudinal_highway_PID_parameters,
70 lateral_PID_parameters,
71 lateral_highway_PID_parameters,
86 alsm(
ALSM(registered_vehicles,
98 vehicle_light_stage)),
118 local_map = std::make_shared<InMemoryMap>(world_map);
121 if (!files.empty()) {
123 if (content.size() != 0) {
126 log_warning(
"No InMemoryMap cache found. Setting up local map. This may take a while...");
130 log_warning(
"No InMemoryMap cache found. Setting up local map. This may take a while...");
148 size_t last_frame = 0;
156 if (synchronous_mode) {
163 if (!synchronous_mode && hybrid_physics_mode) {
164 TimePoint current_instance = chr::system_clock::now();
166 chr::duration<float> time_to_wait = chr::duration<float>(
HYBRID_MODE_DT) - elapsed_time;
167 if (time_to_wait > chr::duration<float>(0.0f)) {
168 std::this_thread::sleep_for(time_to_wait);
170 previous_update_instance = current_instance;
174 if (!synchronous_mode) {
176 if (timestamp.
frame == last_frame) {
179 last_frame = timestamp.
frame;
194 uint64_t growth_factor =
static_cast<uint64_t
>(
static_cast<float>(number_of_vehicles) *
INV_GROWTH_STEP_SIZE);
199 tl_frame.reserve(new_frame_capacity);
212 tl_frame.resize(number_of_vehicles);
222 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
225 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
230 for (
unsigned long index = 0u; index <
vehicle_id_list.size(); ++index) {
236 registration_lock.unlock();
239 if (synchronous_mode) {
323 std::vector<ActorId> actor_id_list;
324 for (
auto &actor : actor_list) {
459 for (
auto &elem : tl_to_freeze) {
460 if (!elem->IsFrozen() || elem->GetState() !=
TLS::Red) {
470 if (previous_mode && !mode) {
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Method to set the automatic vehicle light state update flag.
CollisionFrame collision_frame
Array to hold output data of collision avoidance.
void SetLaneOffset(const ActorPtr &actor, const float offset)
Method to set a lane offset displacement from the center line.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
TrafficManagerServer server
Traffic manager server instance.
VehicleLightStage vehicle_light_stage
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic light.
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
Action ComputeNextAction(const ActorId &actor_id)
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
void Update(const unsigned long index)
static const uint64_t GROWTH_STEP_SIZE
std::atomic< bool > step_begin
Flags to signal step begin and end.
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void SetGlobalDistanceToLeadingVehicle(const float dist)
Method to set the distance to leading vehicle for all registered vehicles.
void RemoveActor(const ActorId actor_id, const bool registered_actor)
bool CheckAllFrozen(TLGroup tl_to_freeze)
Method to check if all traffic lights are frozen in a group.
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
std::atomic< bool > step_end
carla::SharedPtr< cc::Actor > ActorPtr
MotionPlanStage motion_plan_stage
uint64_t current_reserved_capacity
Variable to keep track of currently reserved array space for frames.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any vehicle.
void Release()
To release the traffic manager.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries to respawn of dormant vehicles.
ActionBuffer GetActionBuffer(const ActorId &actor_id)
Method to get the vehicle's action buffer.
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to specify the % chance of running any traffic sign.
chr::time_point< chr::system_clock, chr::nanoseconds > TimePoint
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
std::condition_variable step_begin_trigger
Condition variables for progressing synchronous execution.
std::vector< cg::Location > Path
void SetMaxBoundaries(const float lower, const float upper)
Method to set limits for boundaries when respawning dormant vehicles.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Method to set a vehicle's % decrease in velocity with respect to the speed limit. ...
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
This file contains definitions of common data structures used in traffic manager. ...
std::size_t frame
Number of frames elapsed since the simulator was launched.
SharedPtr< Map > GetMap() const
Return the map that describes this world.
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the right lane.
void SetForceLaneChange(const ActorPtr &actor, const bool direction)
Method to force lane change on a vehicle.
std::vector< ActorId > GetIDList()
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetKeepRightPercentage(const ActorPtr &actor, const float percentage)
Method to set % to keep on the right lane.
void SetMaxBoundaries(const float lower, const float upper)
Method to set limits for boundaries when respawning vehicles.
void Stop()
To stop the TrafficManager.
void SetGlobalDistanceToLeadingVehicle(const float distance)
Method to specify how much distance a vehicle should maintain to the Global leading vehicle...
ALSM: Agent Lifecycle and State Managerment This class has functionality to update the local cache of...
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.
void SetPercentageSpeedDifference(const ActorPtr &actor, const float percentage)
Set a vehicle's % decrease in velocity with respect to the speed limit.
void SetAutoLaneChange(const ActorPtr &actor, const bool enable)
Enable/disable automatic lane change on a vehicle.
void Update(const unsigned long index) override
Parameters parameters
Parameterization object.
std::vector< carla::SharedPtr< carla::client::TrafficLight > > TLGroup
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
void UpdateUploadPath(const ActorId &actor_id, const Path path)
Method to update an already set list of points.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to specify the % chance of ignoring collisions with any walker.
static const uint64_t INITIAL_SIZE
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
static const float INV_GROWTH_STEP_SIZE
void SetRandomDeviceSeed(const uint64_t _seed)
Method to set randomization seed.
std::pair< RoadOption, WaypointPtr > Action
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
void SetGlobalLaneOffset(float const offset)
Method to set a global lane offset displacement from the center line.
void SetSynchronousModeTimeOutInMiliSecond(double time)
Method to set Tick timeout for synchronous execution.
void Update(const unsigned long index) override
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
std::condition_variable step_end_trigger
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.
void Insert(std::vector< ActorPtr > actor_list)
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
void Update(const unsigned long index) override
carla::client::detail::EpisodeProxy & GetEpisodeProxy()
Get CARLA episode information.
void SetDistanceToLeadingVehicle(const ActorPtr &actor, const float distance)
Method to specify how much distance a vehicle should maintain to the leading vehicle.
std::vector< ActorId > GetRegisteredVehiclesIDs()
Get list of all registered vehicles.
cc::World world
CARLA client and object.
void UnregisterVehicles(const std::vector< ActorPtr > &actor_list)
This method unregisters a vehicle from traffic manager.
AtomicActorSet registered_vehicles
Set of all actors registered with traffic manager.
std::atomic< bool > run_traffic_manger
Switch to turn on / turn off traffic manager.
void ResetAllTrafficLights()
The function of this class is to integrate all the various stages of the traffic manager appropriatel...
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set automatic respawn of dormant vehicles.
std::vector< uint8_t > Route
RandomGenerator random_device
Structure holding random devices per vehicle.
This class has functionality for responding to traffic lights and managing entry into non-signalized ...
void Run()
Initiates thread to run the TrafficManager sequentially.
std::mutex registration_mutex
Mutex to prevent vehicle registration during frame array re-allocation.
bool GetSynchronousMode() const
Method to get synchronous mode.
void SetPercentageIgnoreVehicles(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
void SetupLocalMap()
Method to setup InMemoryMap.
static void log_warning(Args &&... args)
TrafficManagerLocal(std::vector< float > longitudinal_PID_parameters, std::vector< float > longitudinal_highway_PID_parameters, std::vector< float > lateral_PID_parameters, std::vector< float > lateral_highway_PID_parameters, float perc_decrease_from_limit, cc::detail::EpisodeProxy &episode_proxy, uint16_t &RPCportTM)
Private constructor for singleton lifecycle management.
void SetRandomLeftLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a left lane change.
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.
void SetGlobalPercentageSpeedDifference(float const percentage)
Set a global % decrease in velocity with respect to the speed limit.
void SetSynchronousMode(bool mode)
Method to switch traffic manager into synchronous execution.
void SetUpdateVehicleLights(const ActorPtr &actor, const bool do_update)
Method to set the automatic management of the vehicle lights.
ControlFrame control_frame
Array to hold output data of motion planning.
static const double HYBRID_MODE_DT
uint64_t seed
Randomization seed.
void Update(const unsigned long index) override
void SetCollisionDetection(const ActorPtr &reference_actor, const ActorPtr &other_actor, const bool detect_collision)
Method to set collision detection rules between vehicles.
void Start()
To start the TrafficManager.
BufferMap buffer_map
Structures to hold waypoint buffers for all vehicles.
void Reset()
To reset the traffic manager.
TrackTraffic track_traffic
Object for tracking paths of the traffic vehicles.
void SetCustomPath(const ActorPtr &actor, const Path path, const bool empty_buffer)
Method to set our own imported path.
void SetLaneOffset(const ActorPtr &actor, const float offset)
Method to set a lane offset displacement from the center line.
TLFrame tl_frame
Array to hold output data of traffic light response.
void SetPercentageIgnoreWalkers(const ActorPtr &actor, const float perc)
Method to set % to ignore any vehicle.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
void SetGlobalLaneOffset(float const offset)
Method to set a global lane offset displacement from the center line.
void RegisterVehicles(const std::vector< ActorPtr > &actor_list)
This method registers a vehicle with the traffic manager.
void RemoveImportedRoute(const ActorId &actor_id, const bool remove_path)
Method to remove a route.
TrafficLightStage traffic_light_stage
LocalizationFrame localization_frame
Array to hold output data of localization stage.
LocalMapPtr local_map
Pointer to local map cache.
void SetBoundariesRespawnDormantVehicles(const float lower_bound, const float upper_bound)
Method to set boundaries for respawning vehicles.
Action GetNextAction(const ActorId &actor_id)
Method to get the vehicle's next action.
void RemoveUploadPath(const ActorId &actor_id, const bool remove_path)
Method to remove a list of points.
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
void SetDesiredSpeed(const ActorPtr &actor, const float value)
Set a vehicle's exact desired velocity.
LocalizationStage localization_stage
Various stages representing core operations of traffic manager.
void SetRespawnDormantVehicles(const bool mode_switch)
Method to set if we are automatically respawning vehicles.
EpisodeProxyImpl< EpisodeProxyPointerType::Strong > EpisodeProxy
int registered_vehicles_state
State counter to track changes in registered actors.
void SetSynchronousMode(const bool mode_switch=true)
Method to set synchronous mode.
void SetHybridPhysicsRadius(const float radius)
Method to set hybrid physics radius.
void SetPercentageRunningLight(const ActorPtr &actor, const float perc)
Method to set % to run any traffic light.
void UpdateImportedRoute(const ActorId &actor_id, const Route route)
Method to update an already set route.
virtual ~TrafficManagerLocal()
Destructor.
This class has functionality to maintain a horizon of waypoints ahead of the vehicle for it to follow...
void SetPercentageRunningSign(const ActorPtr &actor, const float perc)
Method to set % to run any traffic sign.
std::unique_ptr< std::thread > worker_thread
Single worker thread for sequential execution of sub-components.
std::vector< Action > ActionBuffer
CollisionStage collision_stage
void SetRandomRightLaneChangePercentage(const ActorPtr &actor, const float percentage)
Method to set % to randomly do a right lane change.
This class has functionality for turning on/off the vehicle lights according to the current vehicle s...
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
void SetSynchronousModeTimeOutInMiliSecond(const double time)
Set Synchronous mode time out.
bool SynchronousTick()
Method to provide synchronous tick.
This class has functionality to detect potential collision with a nearby actor.
ActionBuffer ComputeActionBuffer(const ActorId &actor_id)
void SetHybridPhysicsMode(const bool mode_switch)
Method to set hybrid physics mode.
void SetGlobalPercentageSpeedDifference(float const percentage)
Method to set a global % decrease in velocity with respect to the speed limit.
void SetOSMMode(const bool mode_switch)
Method to set Open Street Map mode.
void SetImportedRoute(const ActorPtr &actor, const Route route, const bool empty_buffer)
Method to set our own imported route.
void SetDesiredSpeed(const ActorPtr &actor, const float value)
Set a vehicle's exact desired velocity.