2 #include "boost/pointer_cast.hpp" 15 namespace 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) {
82 hero_actors.erase(deletion_id);
108 marked_for_removal.clear();
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();
189 if (hero_actors.find(actor_id) == hero_actors.end()) {
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;
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);
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) {
std::pair< ActorId, double > IdleInfo
MotionPlanStage & motion_plan_stage
CollisionStage & collision_stage
SharedPtr< ActorList > GetActors() const
Return a list with all the actors currently present in the world.
void RemoveActor(const ActorId actor_id, const bool registered_actor)
void UpdateTrafficLightState(ActorId actor_id, TrafficLightState state)
carla::SharedPtr< cc::Actor > ActorPtr
std::pair< ActorIdSet, ActorIdSet > DestroyeddActors
std::vector< ActorId > & marked_for_removal
void SetHeroLocation(const cg::Location location)
float GetSpeedLimit() const
Return the speed limit currently affecting this vehicle.
bool GetOSMMode() const
Method to get Open Street Map mode.
float SquaredLength() const
TrafficLightState GetTLS(const ActorId actor_id) const
std::shared_ptr< InMemoryMap > LocalMapPtr
This class holds the state of all the vehicles in the simlation.
This file contains definitions of common data structures used in traffic manager. ...
std::unordered_set< ActorId > ActorIdSet
std::vector< ActorId > GetIDList()
void RemoveActor(const ActorId actor_id) override
void RemoveActor(const ActorId actor_id)
static auto DistanceSquared(const Vector3D &a, const Vector3D &b)
DestroyeddActors IdentifyDestroyedActors(const ActorList &actor_list)
std::unordered_map< carla::ActorId, Buffer > BufferMap
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 UpdateKinematicState(ActorId actor_id, KinematicState state)
VehicleLightStage & vehicle_light_stage
cg::Location GetLocation(const ActorId actor_id) const
static const double INV_HYBRID_DT
LocalizationStage & localization_stage
std::unordered_map< ActorId, bool > has_physics_enabled
void UpdateUnregisteredActorsData()
static const double BLOCKED_TIME_THRESHOLD
bool GetRespawnDormantVehicles() const
Method to retrieve if we are automatically respawning vehicles.
const Parameters & parameters
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
void UpdateUnregisteredGridPosition(const ActorId actor_id, const std::vector< SimpleWaypointPtr > waypoints)
carla::SharedPtr< cc::Actor > Actor
std::vector< ActorPtr > GetList()
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
double elapsed_last_actor_destruction
void UpdateIdleTime(std::pair< ActorId, double > &max_idle_time, const ActorId &actor_id)
bool GetHybridPhysicsMode() const
Method to retrieve hybrid physics mode.
void UpdateData(const bool hybrid_physics_mode, const Actor &vehicle, const bool hero_actor_present, const float physics_radius_square)
cg::Location GetHybridEndLocation(const ActorId actor_id) const
bool ContainsActor(ActorId actor_id) const
void RemoveActor(const ActorId actor_id) override
static const double DELTA_TIME_BETWEEN_DESTRUCTIONS
static const float STOPPED_VELOCITY_THRESHOLD
float GetHybridPhysicsRadius() const
Method to retrieve hybrid physics radius.
This class has functionality for responding to traffic lights and managing entry into non-signalized ...
void RemoveActor(const ActorId actor_id) override
void DeleteActor(ActorId actor_id)
Method to delete actor data from tracking.
bool IsVehicleStuck(const ActorId &actor_id)
bool Contains(ActorId id)
AtomicActorSet & registered_vehicles
bool IsPhysicsEnabled(const ActorId actor_id) const
void IdentifyNewActors(const ActorList &actor_list)
TrafficLightStage & traffic_light_stage
void RemoveActor(const ActorId actor_id) override
void AddActor(ActorId actor_id, KinematicState kinematic_state, StaticAttributes attributes, TrafficLightState tl_state)
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
void Destroy(ActorId actor_id)
SimulationState & simulation_state
const LocalMapPtr & local_map
This class has functionality to maintain a horizon of waypoints ahead of the vehicle for it to follow...
ActorMap unregistered_actors
TrackTraffic & track_traffic
carla::SharedPtr< cc::ActorList > ActorList
cg::Vector3D GetVelocity(const ActorId actor_id) const
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 RemoveActor(ActorId actor_id)
static const double RED_TL_BLOCKED_TIME_THRESHOLD
This class has functionality to detect potential collision with a nearby actor.
double elapsed_seconds
Simulated seconds elapsed since the beginning of the current episode.
void Remove(std::vector< ActorId > actor_id_list)
cc::Timestamp current_timestamp
void UpdateRegisteredActorsData(const bool hybrid_physics_mode, IdleInfo &max_idle_time)