2#include "boost/pointer_cast.hpp"
15namespace traffic_manager {
19 AtomicActorSet ®istered_vehicles,
21 TrackTraffic &track_traffic,
22 std::vector<ActorId>& marked_for_removal,
23 const Parameters ¶meters,
26 SimulationState &simulation_state,
27 LocalizationStage &localization_stage,
28 CollisionStage &collision_stage,
29 TrafficLightStage &traffic_light_stage,
30 MotionPlanStage &motion_plan_stage,
31 VehicleLightStage &vehicle_light_stage)
32 : registered_vehicles(registered_vehicles),
33 buffer_map(buffer_map),
34 track_traffic(track_traffic),
35 marked_for_removal(marked_for_removal),
36 parameters(parameters),
39 simulation_state(simulation_state),
40 localization_stage(localization_stage),
41 collision_stage(collision_stage),
42 traffic_light_stage(traffic_light_stage),
43 motion_plan_stage(motion_plan_stage),
44 vehicle_light_stage(vehicle_light_stage) {}
48 bool hybrid_physics_mode = parameters.GetHybridPhysicsMode();
50 std::set<ActorId> world_vehicle_ids;
51 std::set<ActorId> world_pedestrian_ids;
53 std::vector<ActorId> unregistered_list_to_be_deleted;
55 current_timestamp = world.GetSnapshot().GetTimestamp();
56 ActorList world_actors = world.GetActors();
59 const ALSM::DestroyeddActors destroyed_actors = IdentifyDestroyedActors(world_actors);
62 const ActorIdSet &destroyed_registered = destroyed_actors.first;
63 for (
const auto &deletion_id: destroyed_registered) {
64 RemoveActor(deletion_id,
true);
67 const ActorIdSet &destroyed_unregistered = destroyed_actors.second;
68 for (
auto deletion_id : destroyed_unregistered) {
69 RemoveActor(deletion_id,
false);
73 if (hero_actors.size() != 0u) {
76 for (
auto &hero_actor_info: hero_actors) {
78 if (destroyed_unregistered.find(hero_actor_info.first) != destroyed_unregistered.end()) {
79 hero_actors_to_delete.insert(hero_actor_info.first);
82 if (destroyed_registered.find(hero_actor_info.first) != destroyed_registered.end()) {
83 hero_actors_to_delete.insert(hero_actor_info.first);
88 for (
auto &deletion_id: hero_actors_to_delete) {
89 hero_actors.erase(deletion_id);
94 IdentifyNewActors(world_actors);
97 ALSM::IdleInfo max_idle_time = std::make_pair(0u, current_timestamp.elapsed_seconds);
98 UpdateRegisteredActorsData(hybrid_physics_mode, max_idle_time);
101 if (IsVehicleStuck(max_idle_time.first)
102 && (current_timestamp.elapsed_seconds - elapsed_last_actor_destruction) > DELTA_TIME_BETWEEN_DESTRUCTIONS
103 && hero_actors.find(max_idle_time.first) == hero_actors.end()) {
106 registered_vehicles.Destroy(max_idle_time.first);
107 RemoveActor(max_idle_time.first,
true);
108 elapsed_last_actor_destruction = current_timestamp.elapsed_seconds;
112 if (parameters.GetOSMMode()) {
114 for (
const ActorId& actor_id: marked_for_removal) {
115 registered_vehicles.Destroy(actor_id);
116 RemoveActor(actor_id,
true);
118 marked_for_removal.clear();
122 UpdateUnregisteredActorsData();
126void ALSM::IdentifyNewActors(
const ActorList &actor_list) {
128 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
130 ActorId actor_id = actor->GetId();
132 if (actor->GetTypeId().front() ==
'v') {
134 if (hero_actors.size() == 0u || hero_actors.find(actor_id) == hero_actors.end()) {
136 for (
auto&& attribute: actor->GetAttributes()) {
138 if (attribute.GetId() ==
"role_name" && attribute.GetValue() ==
"hero") {
140 hero_actors.insert({actor_id, actor});
146 if (!registered_vehicles.Contains(actor_id)
147 && unregistered_actors.find(actor_id) == unregistered_actors.end()) {
149 unregistered_actors.insert({actor_id, actor});
155ALSM::DestroyeddActors ALSM::IdentifyDestroyedActors(
const ActorList &actor_list) {
157 ALSM::DestroyeddActors destroyed_actors;
158 ActorIdSet &deleted_registered = destroyed_actors.first;
159 ActorIdSet &deleted_unregistered = destroyed_actors.second;
163 for (
auto iter = actor_list->begin(); iter != actor_list->end(); ++iter) {
164 current_actors.insert((*iter)->GetId());
168 std::vector<ActorId> registered_ids = registered_vehicles.GetIDList();
169 for (
const ActorId &actor_id : registered_ids) {
171 if (current_actors.find(actor_id) == current_actors.end()) {
173 deleted_registered.insert(actor_id);
178 for (
const auto &actor_info: unregistered_actors) {
179 const ActorId &actor_id = actor_info.first;
181 if (current_actors.find(actor_id) == current_actors.end()
182 || registered_vehicles.Contains(actor_id)) {
184 deleted_unregistered.insert(actor_id);
188 return destroyed_actors;
191void ALSM::UpdateRegisteredActorsData(
const bool hybrid_physics_mode, ALSM::IdleInfo &max_idle_time) {
194 std::vector<ActorPtr> vehicle_list = registered_vehicles.GetList();
196 bool hero_actor_present = hero_actors.size() != 0u;
198 float physics_radius = parameters.GetHybridPhysicsRadius();
200 float physics_radius_square =
SQUARE(physics_radius);
202 bool is_respawn_vehicles = parameters.GetRespawnDormantVehicles();
204 if (is_respawn_vehicles && !hero_actor_present) {
208 for (
auto &hero_actor_info: hero_actors){
210 if (is_respawn_vehicles) {
211 track_traffic.SetHeroLocation(hero_actor_info.second->GetTransform().location);
214 UpdateData(hybrid_physics_mode, hero_actor_info.second, hero_actor_present, physics_radius_square);
217 for (
const Actor &vehicle : vehicle_list) {
219 ActorId actor_id = vehicle->GetId();
221 if (hero_actors.find(actor_id) == hero_actors.end()) {
223 UpdateData(hybrid_physics_mode, vehicle, hero_actor_present, physics_radius_square);
225 UpdateIdleTime(max_idle_time, actor_id);
230void ALSM::UpdateData(
const bool hybrid_physics_mode,
const Actor &vehicle,
231 const bool hero_actor_present,
const float physics_radius_square) {
234 ActorId actor_id = vehicle->GetId();
240 bool state_entry_present = simulation_state.ContainsActor(actor_id);
243 if (idle_time.find(actor_id) == idle_time.end() && current_timestamp.elapsed_seconds != 0.0) {
244 idle_time.insert({actor_id, current_timestamp.elapsed_seconds});
248 bool in_range_of_hero_actor =
false;
249 if (hero_actor_present && hybrid_physics_mode) {
250 for (
auto &hero_actor_info: hero_actors) {
251 const ActorId &hero_actor_id = hero_actor_info.first;
252 if (simulation_state.ContainsActor(hero_actor_id)) {
253 const cg::Location &hero_location = simulation_state.GetLocation(hero_actor_id);
254 if (cg::Math::DistanceSquared(vehicle_location, hero_location) < physics_radius_square) {
255 in_range_of_hero_actor =
true;
263 bool enable_physics = hybrid_physics_mode ? in_range_of_hero_actor :
true;
264 if (!has_physics_enabled.count(actor_id) || has_physics_enabled[actor_id] != enable_physics) {
266 if (hero_actors.find(actor_id) == hero_actors.end()) {
267 vehicle->SetSimulatePhysics(enable_physics);
268 has_physics_enabled[actor_id] = enable_physics;
270 if (enable_physics ==
true && state_entry_present) {
271 vehicle->SetTargetVelocity(simulation_state.GetVelocity(actor_id));
279 if (state_entry_present && !simulation_state.IsPhysicsEnabled(actor_id)){
280 cg::Location previous_location = simulation_state.GetLocation(actor_id);
281 cg::Location previous_end_location = simulation_state.GetHybridEndLocation(actor_id);
282 cg::Vector3D displacement = (previous_end_location - previous_location);
283 vehicle_velocity = displacement * INV_HYBRID_DT;
287 auto vehicle_ptr = boost::static_pointer_cast<cc::Vehicle>(vehicle);
288 KinematicState kinematic_state{vehicle_location, vehicle_rotation,
289 vehicle_velocity, vehicle_ptr->GetSpeedLimit(),
293 TrafficLightState tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
296 if (state_entry_present) {
297 simulation_state.UpdateKinematicState(actor_id, kinematic_state);
298 simulation_state.UpdateTrafficLightState(actor_id, tl_state);
302 cg::Vector3D dimensions = vehicle_ptr->GetBoundingBox().extent;
303 StaticAttributes attributes{ActorType::Vehicle, dimensions.
x, dimensions.
y, dimensions.
z};
306 simulation_state.AddActor(actor_id, kinematic_state, attributes, tl_state);
311void ALSM::UpdateUnregisteredActorsData() {
313 for (
auto &actor_info: unregistered_actors) {
315 const ActorId actor_id = actor_info.first;
316 const ActorPtr actor_ptr = actor_info.second;
317 const std::string type_id = actor_ptr->GetTypeId();
319 const cg::Transform actor_transform = actor_ptr->GetTransform();
322 const cg::Vector3D actor_velocity = actor_ptr->GetVelocity();
323 const bool actor_is_dormant = actor_ptr->IsDormant();
325 KinematicState kinematic_state {actor_location, actor_rotation, actor_velocity, -1.0f,
true, actor_is_dormant,
cg::Location()};
327 TrafficLightState tl_state;
328 ActorType actor_type = ActorType::Any;
330 std::vector<SimpleWaypointPtr> nearest_waypoints;
333 bool state_entry_not_present = !simulation_state.ContainsActor(actor_id);
334 if (type_id.front() ==
'v') {
335 auto vehicle_ptr = boost::static_pointer_cast<cc::Vehicle>(actor_ptr);
336 kinematic_state.speed_limit = vehicle_ptr->GetSpeedLimit();
338 tl_state = {vehicle_ptr->GetTrafficLightState(), vehicle_ptr->IsAtTrafficLight()};
340 if (state_entry_not_present) {
341 dimensions = vehicle_ptr->GetBoundingBox().extent;
342 actor_type = ActorType::Vehicle;
343 StaticAttributes attributes {actor_type, dimensions.
x, dimensions.
y, dimensions.
z};
346 simulation_state.AddActor(actor_id, kinematic_state, attributes, tl_state);
349 simulation_state.UpdateKinematicState(actor_id, kinematic_state);
350 simulation_state.UpdateTrafficLightState(actor_id, tl_state);
354 cg::Vector3D extent = vehicle_ptr->GetBoundingBox().extent;
355 cg::Vector3D heading_vector = vehicle_ptr->GetTransform().GetForwardVector();
357 std::vector<cg::Location> corners = {actor_location +
cg::Location(extent.
x * heading_vector),
362 nearest_waypoints.push_back(nearest_waypoint);
365 else if (type_id.front() ==
'w') {
366 auto walker_ptr = boost::static_pointer_cast<cc::Walker>(actor_ptr);
368 if (state_entry_not_present) {
369 dimensions = walker_ptr->GetBoundingBox().extent;
370 actor_type = ActorType::Pedestrian;
371 StaticAttributes attributes {actor_type, dimensions.
x, dimensions.
y, dimensions.
z};
374 simulation_state.AddActor(actor_id, kinematic_state, attributes, tl_state);
377 simulation_state.UpdateKinematicState(actor_id, kinematic_state);
382 nearest_waypoints.push_back(nearest_waypoint);
385 track_traffic.UpdateUnregisteredGridPosition(actor_id, nearest_waypoints);
389void ALSM::UpdateIdleTime(std::pair<ActorId, double>& max_idle_time,
const ActorId& actor_id) {
391 if (idle_time.find(actor_id) != idle_time.end()) {
393 double &idle_duration = idle_time.at(actor_id);
395 if (simulation_state.GetVelocity(actor_id).SquaredLength() >
SQUARE(STOPPED_VELOCITY_THRESHOLD)) {
397 idle_duration = current_timestamp.elapsed_seconds;
401 if (max_idle_time.first == 0u || max_idle_time.second > idle_duration) {
402 max_idle_time = std::make_pair(actor_id, idle_duration);
408bool ALSM::IsVehicleStuck(
const ActorId& actor_id) {
410 if (idle_time.find(actor_id) != idle_time.end()) {
412 double delta_idle_time = current_timestamp.elapsed_seconds - idle_time.at(actor_id);
414 TrafficLightState tl_state = simulation_state.GetTLS(actor_id);
416 if ((delta_idle_time >= RED_TL_BLOCKED_TIME_THRESHOLD)
417 || (delta_idle_time >= BLOCKED_TIME_THRESHOLD && tl_state.tl_state != TLS::Red))
426void ALSM::RemoveActor(
const ActorId actor_id,
const bool registered_actor) {
428 if (registered_actor) {
430 registered_vehicles.Remove({actor_id});
432 buffer_map.erase(actor_id);
433 idle_time.erase(actor_id);
434 localization_stage.RemoveActor(actor_id);
435 collision_stage.RemoveActor(actor_id);
436 traffic_light_stage.RemoveActor(actor_id);
437 motion_plan_stage.RemoveActor(actor_id);
438 vehicle_light_stage.RemoveActor(actor_id);
442 unregistered_actors.erase(actor_id);
443 hero_actors.erase(actor_id);
447 track_traffic.DeleteActor(actor_id);
449 simulation_state.RemoveActor(actor_id);
455 unregistered_actors.clear();
458 elapsed_last_actor_destruction = 0.0;
459 current_timestamp = world.GetSnapshot().GetTimestamp();
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld Actor
#define SQUARE(a)
This file contains various constants used in traffic manager arranged into sensible namespaces for re...
carla::SharedPtr< cc::Actor > ActorPtr
使用别名简化代码中的命名
std::shared_ptr< InMemoryMap > LocalMapPtr
本地地图指针类型,使用智能指针管理InMemoryMap对象
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
std::unordered_set< ActorId > ActorIdSet
std::unordered_map< carla::ActorId, Buffer > BufferMap