2#include "boost/pointer_cast.hpp"
15namespace traffic_manager {
21 std::vector<ActorId>& marked_for_removal,
31 : registered_vehicles(registered_vehicles),
32 buffer_map(buffer_map),
33 track_traffic(track_traffic),
34 marked_for_removal(marked_for_removal),
35 parameters(parameters),
38 simulation_state(simulation_state),
39 localization_stage(localization_stage),
40 collision_stage(collision_stage),
41 traffic_light_stage(traffic_light_stage),
42 motion_plan_stage(motion_plan_stage),
43 vehicle_light_stage(vehicle_light_stage) {}
49 std::set<ActorId> world_vehicle_ids;
50 std::set<ActorId> world_pedestrian_ids;
51 std::vector<ActorId> unregistered_list_to_be_deleted;
59 const ActorIdSet &destroyed_registered = destroyed_actors.first;
60 for (
const auto &deletion_id: destroyed_registered) {
64 const ActorIdSet &destroyed_unregistered = destroyed_actors.second;
65 for (
auto deletion_id : destroyed_unregistered) {
73 if (destroyed_unregistered.find(hero_actor_info.first) != destroyed_unregistered.end()) {
74 hero_actors_to_delete.insert(hero_actor_info.first);
76 if (destroyed_registered.find(hero_actor_info.first) != destroyed_registered.end()) {
77 hero_actors_to_delete.insert(hero_actor_info.first);
81 for (
auto &deletion_id: hero_actors_to_delete) {
116 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
118 ActorId actor_id = actor->GetId();
120 if (actor->GetTypeId().front() ==
'v') {
122 for (
auto&& attribute: actor->GetAttributes()) {
123 if (attribute.GetId() ==
"role_name" && attribute.GetValue() ==
"hero") {
140 ActorIdSet &deleted_registered = destroyed_actors.first;
141 ActorIdSet &deleted_unregistered = destroyed_actors.second;
145 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
146 current_actors.insert((*iter)->GetId());
151 for (
const ActorId &actor_id : registered_ids) {
152 if (current_actors.find(actor_id) == current_actors.end()) {
153 deleted_registered.insert(actor_id);
159 const ActorId &actor_id = actor_info.first;
160 if (current_actors.find(actor_id) == current_actors.end()
162 deleted_unregistered.insert(actor_id);
166 return destroyed_actors;
172 bool hero_actor_present =
hero_actors.size() != 0u;
174 float physics_radius_square =
SQUARE(physics_radius);
176 if (is_respawn_vehicles && !hero_actor_present) {
181 if (is_respawn_vehicles) {
184 UpdateData(hybrid_physics_mode, hero_actor_info.second, hero_actor_present, physics_radius_square);
187 for (
const Actor &vehicle : vehicle_list) {
188 ActorId actor_id = vehicle->GetId();
190 UpdateData(hybrid_physics_mode, vehicle, hero_actor_present, physics_radius_square);
197 const bool hero_actor_present,
const float physics_radius_square) {
199 ActorId actor_id = vehicle->GetId();
212 bool in_range_of_hero_actor =
false;
213 if (hero_actor_present && hybrid_physics_mode) {
215 const ActorId &hero_actor_id = hero_actor_info.first;
218 if (cg::Math::DistanceSquared(vehicle_location, hero_location) < physics_radius_square) {
219 in_range_of_hero_actor =
true;
226 bool enable_physics = hybrid_physics_mode ? in_range_of_hero_actor :
true;
229 vehicle->SetSimulatePhysics(enable_physics);
231 if (enable_physics ==
true && state_entry_present) {
243 cg::Vector3D displacement = (previous_end_location - previous_location);
248 auto vehicle_ptr = boost::static_pointer_cast<cc::Vehicle>(vehicle);
249 KinematicState kinematic_state{vehicle_location, vehicle_rotation,
250 vehicle_velocity, vehicle_ptr->GetSpeedLimit(),
254 TrafficLightState tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
257 if (state_entry_present) {
262 cg::Vector3D dimensions = vehicle_ptr->GetBoundingBox().extent;
273 const ActorId actor_id = actor_info.first;
274 const ActorPtr actor_ptr = actor_info.second;
275 const std::string type_id = actor_ptr->GetTypeId();
277 const cg::Transform actor_transform = actor_ptr->GetTransform();
280 const cg::Vector3D actor_velocity = actor_ptr->GetVelocity();
281 const bool actor_is_dormant = actor_ptr->IsDormant();
282 KinematicState kinematic_state {actor_location, actor_rotation, actor_velocity, -1.0f,
true, actor_is_dormant,
cg::Location()};
287 std::vector<SimpleWaypointPtr> nearest_waypoints;
290 if (type_id.front() ==
'v') {
291 auto vehicle_ptr = boost::static_pointer_cast<cc::Vehicle>(actor_ptr);
292 kinematic_state.speed_limit = vehicle_ptr->GetSpeedLimit();
294 tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
296 if (state_entry_not_present) {
297 dimensions = vehicle_ptr->GetBoundingBox().extent;
308 cg::Vector3D extent = vehicle_ptr->GetBoundingBox().extent;
309 cg::Vector3D heading_vector = vehicle_ptr->GetTransform().GetForwardVector();
310 std::vector<cg::Location> corners = {actor_location +
cg::Location(extent.
x * heading_vector),
315 nearest_waypoints.push_back(nearest_waypoint);
318 else if (type_id.front() ==
'w') {
319 auto walker_ptr = boost::static_pointer_cast<cc::Walker>(actor_ptr);
321 if (state_entry_not_present) {
322 dimensions = walker_ptr->GetBoundingBox().extent;
333 nearest_waypoints.push_back(nearest_waypoint);
342 double &idle_duration =
idle_time.at(actor_id);
348 if (max_idle_time.first == 0u || max_idle_time.second > idle_duration) {
349 max_idle_time = std::make_pair(actor_id, idle_duration);
368 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.
float SquaredLength() const
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, LocalizationStage &localization_stage, CollisionStage &collision_stage, TrafficLightStage &traffic_light_stage, MotionPlanStage &motion_plan_stage, VehicleLightStage &vehicle_light_stage)
void IdentifyNewActors(const ActorList &actor_list)
void RemoveActor(const ActorId actor_id, const bool registered_actor)
AtomicActorSet & registered_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)
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
carla::SharedPtr< cc::Actor > ActorPtr
carla::SharedPtr< cc::Actor > Actor
carla::SharedPtr< cc::ActorList > ActorList
std::shared_ptr< InMemoryMap > LocalMapPtr
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
std::unordered_set< ActorId > ActorIdSet
std::unordered_map< carla::ActorId, Buffer > BufferMap
This file contains definitions of common data structures used in traffic manager.