6#include "boost/pointer_cast.hpp"
20namespace traffic_manager {
26 std::vector<ActorId>& marked_for_removal,
31 std::unordered_map<
ActorId, std::pair<float, bool>> &large_vehicles,
37 : registered_vehicles(registered_vehicles),
38 buffer_map(buffer_map),
39 track_traffic(track_traffic),
40 marked_for_removal(marked_for_removal),
41 parameters(parameters),
44 simulation_state(simulation_state),
45 localization_stage(localization_stage),
46 collision_stage(collision_stage),
47 traffic_light_stage(traffic_light_stage),
48 motion_plan_stage(motion_plan_stage),
49 vehicle_light_stage(vehicle_light_stage),
50 large_vehicles(large_vehicles) {}
56 std::set<ActorId> world_vehicle_ids;
57 std::set<ActorId> world_pedestrian_ids;
58 std::vector<ActorId> unregistered_list_to_be_deleted;
66 const ActorIdSet &destroyed_registered = destroyed_actors.first;
67 for (
const auto &deletion_id: destroyed_registered) {
71 const ActorIdSet &destroyed_unregistered = destroyed_actors.second;
72 for (
auto deletion_id : destroyed_unregistered) {
80 if (destroyed_unregistered.find(hero_actor_info.first) != destroyed_unregistered.end()) {
81 hero_actors_to_delete.insert(hero_actor_info.first);
83 if (destroyed_registered.find(hero_actor_info.first) != destroyed_registered.end()) {
84 hero_actors_to_delete.insert(hero_actor_info.first);
88 for (
auto &deletion_id: hero_actors_to_delete) {
123 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
125 ActorId actor_id = actor->GetId();
127 if (actor->GetTypeId().front() ==
'v') {
129 for (
auto&& attribute: actor->GetAttributes()) {
130 if (attribute.GetId() ==
"role_name" && attribute.GetValue() ==
"hero") {
147 ActorIdSet &deleted_registered = destroyed_actors.first;
148 ActorIdSet &deleted_unregistered = destroyed_actors.second;
152 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
153 current_actors.insert((*iter)->GetId());
158 for (
const ActorId &actor_id : registered_ids) {
159 if (current_actors.find(actor_id) == current_actors.end()) {
160 deleted_registered.insert(actor_id);
166 const ActorId &actor_id = actor_info.first;
167 if (current_actors.find(actor_id) == current_actors.end()
169 deleted_unregistered.insert(actor_id);
173 return destroyed_actors;
179 bool hero_actor_present =
hero_actors.size() != 0u;
181 float physics_radius_square =
SQUARE(physics_radius);
183 if (is_respawn_vehicles && !hero_actor_present) {
187 std::vector<ActorId> vanished_heroes;
188 std::vector<ActorId> vanished_vehicles;
192 if (is_respawn_vehicles) {
195 UpdateData(hybrid_physics_mode, hero_actor_info.second, hero_actor_present, physics_radius_square);
196 }
catch (
const std::exception &e) {
197 log_debug(
"Traffic Manager: dropping hero vehicle", hero_actor_info.first,
":", e.what());
198 vanished_heroes.push_back(hero_actor_info.first);
202 for (
const Actor &vehicle : vehicle_list) {
203 ActorId actor_id = vehicle->GetId();
206 UpdateData(hybrid_physics_mode, vehicle, hero_actor_present, physics_radius_square);
207 }
catch (
const std::exception &e) {
208 log_debug(
"Traffic Manager: dropping vehicle", actor_id,
":", e.what());
209 vanished_vehicles.push_back(actor_id);
215 for (
const ActorId actor_id : vanished_heroes) {
218 for (
const ActorId actor_id : vanished_vehicles) {
224 const bool hero_actor_present,
const float physics_radius_square) {
226 ActorId actor_id = vehicle->GetId();
239 bool in_range_of_hero_actor =
false;
240 if (hero_actor_present && hybrid_physics_mode) {
242 const ActorId &hero_actor_id = hero_actor_info.first;
245 if (cg::Math::DistanceSquared(vehicle_location, hero_location) < physics_radius_square) {
246 in_range_of_hero_actor =
true;
253 bool enable_physics = hybrid_physics_mode ? in_range_of_hero_actor :
true;
256 vehicle->SetSimulatePhysics(enable_physics);
258 if (enable_physics ==
true && state_entry_present) {
270 cg::Vector3D displacement = (previous_end_location - previous_location);
275 auto vehicle_ptr = std::static_pointer_cast<cc::Vehicle>(vehicle);
276 KinematicState kinematic_state{vehicle_location, vehicle_rotation,
277 vehicle_velocity, vehicle_ptr->GetSpeedLimit(),
281 TrafficLightState tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
284 if (state_entry_present) {
289 cg::Vector3D dimensions = vehicle_ptr->GetBoundingBox().extent;
296 const auto physics_control = vehicle_ptr->GetPhysicsControl();
297 float max_steer_angle = 0.0f;
298 for (
const auto &wheel : physics_control.wheels) {
299 max_steer_angle = std::max(max_steer_angle, wheel.max_steer_angle);
301 if (max_steer_angle > 1.0f) {
302 attributes.max_steer_angle = max_steer_angle;
304 }
catch (
const std::exception &) {}
314 const ActorId actor_id = actor_info.first;
315 const ActorPtr actor_ptr = actor_info.second;
316 const std::string type_id = actor_ptr->GetTypeId();
318 const cg::Transform actor_transform = actor_ptr->GetTransform();
321 const cg::Vector3D actor_velocity = actor_ptr->GetVelocity();
322 const bool actor_is_dormant = actor_ptr->IsDormant();
323 KinematicState kinematic_state {actor_location, actor_rotation, actor_velocity, -1.0f,
true, actor_is_dormant,
cg::Location()};
328 std::vector<SimpleWaypointPtr> nearest_waypoints;
331 if (type_id.front() ==
'v') {
332 auto vehicle_ptr = std::static_pointer_cast<cc::Vehicle>(actor_ptr);
333 kinematic_state.speed_limit = vehicle_ptr->GetSpeedLimit();
335 tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
337 if (state_entry_not_present) {
338 dimensions = vehicle_ptr->GetBoundingBox().extent;
349 cg::Vector3D extent = vehicle_ptr->GetBoundingBox().extent;
350 cg::Vector3D heading_vector = vehicle_ptr->GetTransform().GetForwardVector();
351 std::vector<cg::Location> corners = {actor_location +
cg::Location(extent.
x * heading_vector),
356 if (nearest_waypoint !=
nullptr) {
357 nearest_waypoints.push_back(nearest_waypoint);
361 else if (type_id.front() ==
'w') {
362 auto walker_ptr = std::static_pointer_cast<cc::Walker>(actor_ptr);
364 if (state_entry_not_present) {
365 dimensions = walker_ptr->GetBoundingBox().extent;
376 if (nearest_waypoint !=
nullptr) {
377 nearest_waypoints.push_back(nearest_waypoint);
387 double &idle_duration =
idle_time.at(actor_id);
393 if (max_idle_time.first == 0u || max_idle_time.second > idle_duration) {
394 max_idle_time = std::make_pair(actor_id, idle_duration);
413 const ActorId actor_id = actor->GetId();
414 for (
auto &&attribute : actor->GetAttributes()) {
415 if (attribute.GetId() !=
"base_type") {
418 std::string value = attribute.GetValue();
423 [](
unsigned char c) { return std::tolower(c); });
424 const std::string_view value_view{value};
425 const auto match = std::find(
439 if (registered_actor) {
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
double elapsed_seconds
Simulated seconds elapsed since the beginning of the current episode.
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
WorldSnapshot GetSnapshot() const
Return a snapshot of the world at this moment.
Euler angles of a rotation in CARLA's / Unreal's left-handed frame: x forward, y right,...
void AddActor(const Actor actor)
void IdentifyNewActors(const ActorList &actor_list)
void RemoveActor(const ActorId actor_id, const bool registered_actor)
AtomicActorSet & registered_vehicles
static constexpr std::array< std::string_view, 2 > large_vehicle_types
std::unordered_map< ActorId, std::pair< float, bool > > & large_vehicles
LocalizationStage & localization_stage
const LocalMapPtr & local_map
std::unordered_map< ActorId, bool > has_physics_enabled
VehicleLightStage & vehicle_light_stage
std::pair< ActorId, double > IdleInfo
bool IsVehicleStuck(const ActorId &actor_id)
MotionPlanStage & motion_plan_stage
std::pair< ActorIdSet, ActorIdSet > DestroyeddActors
CollisionStage & collision_stage
void UpdateData(const bool hybrid_physics_mode, const Actor &vehicle, const bool hero_actor_present, const float physics_radius_square)
void UpdateUnregisteredActorsData()
ActorMap unregistered_actors
cc::Timestamp current_timestamp
const Parameters & parameters
std::vector< ActorId > & marked_for_removal
SimulationState & simulation_state
void UpdateRegisteredActorsData(const bool hybrid_physics_mode, IdleInfo &max_idle_time)
ALSM(AtomicActorSet ®istered_vehicles, BufferMap &buffer_map, TrackTraffic &track_traffic, std::vector< ActorId > &marked_for_removal, const Parameters ¶meters, const cc::World &world, const LocalMapPtr &local_map, SimulationState &simulation_state, std::unordered_map< ActorId, std::pair< float, bool > > &large_vehicles, LocalizationStage &localization_stage, CollisionStage &collision_stage, TrafficLightStage &traffic_light_stage, MotionPlanStage &motion_plan_stage, VehicleLightStage &vehicle_light_stage)
TrafficLightStage & traffic_light_stage
void UpdateIdleTime(std::pair< ActorId, double > &max_idle_time, const ActorId &actor_id)
TrackTraffic & track_traffic
DestroyeddActors IdentifyDestroyedActors(const ActorList &actor_list)
double elapsed_last_actor_destruction
void Destroy(ActorId actor_id)
void Remove(std::vector< ActorId > actor_id_list)
std::vector< ActorId > GetIDList()
std::vector< ActorPtr > GetList()
bool Contains(ActorId id)
This class has functionality to detect potential collision with a nearby actor.
void RemoveActor(const ActorId actor_id) override
This class has functionality to maintain a horizon of waypoints ahead of the vehicle for it to follow...
void RemoveActor(const ActorId actor_id) override
void RemoveActor(const ActorId actor_id)
bool GetOSMMode() const
Method to get Open Street Map mode.
float GetHybridPhysicsRadius() const
Method to retrieve hybrid physics radius.
bool GetRespawnDormantVehicles() const
Method to retrieve if we are automatically respawning vehicles.
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
This class holds the state of all the vehicles in the simlation.
bool ContainsActor(ActorId actor_id) const
cg::Location GetLocation(const ActorId actor_id) const
cg::Location GetHybridEndLocation(const ActorId actor_id) const
void UpdateKinematicState(ActorId actor_id, KinematicState state)
void AddActor(ActorId actor_id, KinematicState kinematic_state, StaticAttributes attributes, TrafficLightState tl_state)
void UpdateTrafficLightState(ActorId actor_id, TrafficLightState state)
cg::Vector3D GetVelocity(const ActorId actor_id) const
bool IsPhysicsEnabled(const ActorId actor_id) const
void RemoveActor(ActorId actor_id)
TrafficLightState GetTLS(const ActorId actor_id) const
void UpdateUnregisteredGridPosition(const ActorId actor_id, const std::vector< SimpleWaypointPtr > waypoints)
void DeleteActor(ActorId actor_id)
Method to delete actor data from tracking.
void SetHeroLocation(const cg::Location location)
This class has functionality for responding to traffic lights and managing entry into non-signalized ...
void RemoveActor(const ActorId actor_id) override
This class has functionality for turning on/off the vehicle lights according to the current vehicle s...
void RemoveActor(const ActorId actor_id) override
static const double INV_HYBRID_DT
static const double RED_TL_BLOCKED_TIME_THRESHOLD
static const double BLOCKED_TIME_THRESHOLD
static const float STOPPED_VELOCITY_THRESHOLD
static const double DELTA_TIME_BETWEEN_DESTRUCTIONS
std::shared_ptr< InMemoryMap > LocalMapPtr
carla::SharedPtr< cc::Actor > Actor
std::unordered_set< ActorId > ActorIdSet
std::unordered_map< carla::ActorId, Buffer > BufferMap
carla::SharedPtr< cc::ActorList > ActorList
carla::SharedPtr< cc::Actor > ActorPtr
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
This file contains definitions of common data structures used in traffic manager.
static void log_debug(Args &&... args)
constexpr float SquaredLength() const