From ffc45a3df97267f406ea1037ffbdf4eb16167f16 Mon Sep 17 00:00:00 2001 From: Masaya Kataoka Date: Mon, 6 Jan 2025 17:15:12 +0900 Subject: [PATCH 1/2] add despawn_function argument Signed-off-by: Masaya Kataoka --- .../include/traffic_simulator/api/api.hpp | 3 ++- .../traffic_simulator/traffic/traffic_controller.hpp | 2 ++ .../include/traffic_simulator/traffic/traffic_sink.hpp | 4 +++- .../traffic_simulator/src/traffic/traffic_controller.cpp | 5 +++-- simulation/traffic_simulator/src/traffic/traffic_sink.cpp | 8 ++++++-- 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/simulation/traffic_simulator/include/traffic_simulator/api/api.hpp b/simulation/traffic_simulator/include/traffic_simulator/api/api.hpp index be13d11637e..b8038357d0c 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/api/api.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/api/api.hpp @@ -73,7 +73,8 @@ class API node, entity_manager_ptr_->getHdmapUtils(), getROS2Parameter("architecture_type", "awf/universe/20240605"))), traffic_controller_ptr_(std::make_shared( - entity_manager_ptr_, configuration.auto_sink_entity_types)), + [this](const std::string & name) { despawn(name); }, entity_manager_ptr_, + configuration.auto_sink_entity_types)), clock_pub_(rclcpp::create_publisher( node, "/clock", rclcpp::QoS(rclcpp::KeepLast(1)).best_effort(), rclcpp::PublisherOptionsWithAllocator())), diff --git a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp index 3ed3561f75a..5a88aef47e4 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp @@ -43,6 +43,7 @@ class TrafficController { public: explicit TrafficController( + const std::function & despawn_function, const std::shared_ptr entity_manager_ptr, const std::set auto_sink_entity_types /*= {}*/); @@ -57,6 +58,7 @@ class TrafficController private: auto appendAutoSinks(const std::set & auto_sink_entity_types) -> void; + const std::function despawn_function_; const std::shared_ptr entity_manager_ptr_; std::vector> modules_; }; diff --git a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp index e3a99f23a86..32580103dfd 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp @@ -81,6 +81,7 @@ class TrafficSink : public TrafficModuleBase * @param config TrafficSink configuration */ explicit TrafficSink( + const std::function & despawn_function, const std::shared_ptr entity_manager_ptr, const TrafficSinkConfig & config); /** @@ -95,8 +96,9 @@ class TrafficSink : public TrafficModuleBase private: auto isEntitySinkable(const std::string & entity_name) const noexcept(false) -> bool; - const TrafficSinkConfig config_; + const std::function despawn_function_; const std::shared_ptr entity_manager_ptr_; + const TrafficSinkConfig config_; }; } // namespace traffic } // namespace traffic_simulator diff --git a/simulation/traffic_simulator/src/traffic/traffic_controller.cpp b/simulation/traffic_simulator/src/traffic/traffic_controller.cpp index 5e764e933c1..05891c300e4 100644 --- a/simulation/traffic_simulator/src/traffic/traffic_controller.cpp +++ b/simulation/traffic_simulator/src/traffic/traffic_controller.cpp @@ -37,9 +37,10 @@ namespace traffic_simulator namespace traffic { TrafficController::TrafficController( + const std::function & despawn_function, const std::shared_ptr entity_manager_ptr, const std::set auto_sink_entity_types) -: entity_manager_ptr_(entity_manager_ptr), modules_() +: despawn_function_(despawn_function), entity_manager_ptr_(entity_manager_ptr), modules_() { if (not auto_sink_entity_types.empty()) { appendAutoSinks(auto_sink_entity_types); @@ -59,7 +60,7 @@ auto TrafficController::appendAutoSinks(const std::set & auto_sink const auto pose = pose::toMapPose(lanelet_pose, hdmap_utils_ptr); const auto traffic_sink_config = TrafficSinkConfig( sink_radius, pose.position, auto_sink_entity_types, std::make_optional(lanelet_id)); - addModule(entity_manager_ptr_, traffic_sink_config); + addModule(despawn_function_, entity_manager_ptr_, traffic_sink_config); } } } diff --git a/simulation/traffic_simulator/src/traffic/traffic_sink.cpp b/simulation/traffic_simulator/src/traffic/traffic_sink.cpp index dc41a3a3a72..cc0e0653b6f 100644 --- a/simulation/traffic_simulator/src/traffic/traffic_sink.cpp +++ b/simulation/traffic_simulator/src/traffic/traffic_sink.cpp @@ -40,8 +40,12 @@ namespace traffic_simulator namespace traffic { TrafficSink::TrafficSink( + const std::function & despawn_function, const std::shared_ptr entity_manager_ptr, const TrafficSinkConfig & config) -: TrafficModuleBase(), config_(config), entity_manager_ptr_(entity_manager_ptr) +: TrafficModuleBase(), + despawn_function_(despawn_function), + entity_manager_ptr_(entity_manager_ptr), + config_(config) { } @@ -50,7 +54,7 @@ auto TrafficSink::execute( { for (const auto & entity_name : entity_manager_ptr_->getEntityNames()) { if (isEntitySinkable(entity_name)) { - entity_manager_ptr_->despawnEntity(entity_name); + despawn_function_(entity_name); } } } From 6139a87dcbf0ec238f9f12fd4070d1fb1e7cb325 Mon Sep 17 00:00:00 2001 From: Masaya Kataoka Date: Wed, 8 Jan 2025 16:19:51 +0900 Subject: [PATCH 2/2] rename despawn_function to despawn Signed-off-by: Masaya Kataoka --- .../traffic_simulator/traffic/traffic_controller.hpp | 4 ++-- .../include/traffic_simulator/traffic/traffic_sink.hpp | 2 +- .../traffic_simulator/src/traffic/traffic_controller.cpp | 6 +++--- .../traffic_simulator/src/traffic/traffic_sink.cpp | 9 +++------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp index 5a88aef47e4..dbdc7d8563c 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_controller.hpp @@ -43,7 +43,7 @@ class TrafficController { public: explicit TrafficController( - const std::function & despawn_function, + const std::function & despawn_, const std::shared_ptr entity_manager_ptr, const std::set auto_sink_entity_types /*= {}*/); @@ -58,7 +58,7 @@ class TrafficController private: auto appendAutoSinks(const std::set & auto_sink_entity_types) -> void; - const std::function despawn_function_; + const std::function despawn_; const std::shared_ptr entity_manager_ptr_; std::vector> modules_; }; diff --git a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp index 32580103dfd..885417d3c4a 100644 --- a/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp +++ b/simulation/traffic_simulator/include/traffic_simulator/traffic/traffic_sink.hpp @@ -96,7 +96,7 @@ class TrafficSink : public TrafficModuleBase private: auto isEntitySinkable(const std::string & entity_name) const noexcept(false) -> bool; - const std::function despawn_function_; + const std::function despawn_; const std::shared_ptr entity_manager_ptr_; const TrafficSinkConfig config_; }; diff --git a/simulation/traffic_simulator/src/traffic/traffic_controller.cpp b/simulation/traffic_simulator/src/traffic/traffic_controller.cpp index 05891c300e4..4d79b8c3755 100644 --- a/simulation/traffic_simulator/src/traffic/traffic_controller.cpp +++ b/simulation/traffic_simulator/src/traffic/traffic_controller.cpp @@ -37,10 +37,10 @@ namespace traffic_simulator namespace traffic { TrafficController::TrafficController( - const std::function & despawn_function, + const std::function & despawn, const std::shared_ptr entity_manager_ptr, const std::set auto_sink_entity_types) -: despawn_function_(despawn_function), entity_manager_ptr_(entity_manager_ptr), modules_() +: despawn_(despawn), entity_manager_ptr_(entity_manager_ptr), modules_() { if (not auto_sink_entity_types.empty()) { appendAutoSinks(auto_sink_entity_types); @@ -60,7 +60,7 @@ auto TrafficController::appendAutoSinks(const std::set & auto_sink const auto pose = pose::toMapPose(lanelet_pose, hdmap_utils_ptr); const auto traffic_sink_config = TrafficSinkConfig( sink_radius, pose.position, auto_sink_entity_types, std::make_optional(lanelet_id)); - addModule(despawn_function_, entity_manager_ptr_, traffic_sink_config); + addModule(despawn_, entity_manager_ptr_, traffic_sink_config); } } } diff --git a/simulation/traffic_simulator/src/traffic/traffic_sink.cpp b/simulation/traffic_simulator/src/traffic/traffic_sink.cpp index cc0e0653b6f..447e71f7fcc 100644 --- a/simulation/traffic_simulator/src/traffic/traffic_sink.cpp +++ b/simulation/traffic_simulator/src/traffic/traffic_sink.cpp @@ -40,12 +40,9 @@ namespace traffic_simulator namespace traffic { TrafficSink::TrafficSink( - const std::function & despawn_function, + const std::function & despawn, const std::shared_ptr entity_manager_ptr, const TrafficSinkConfig & config) -: TrafficModuleBase(), - despawn_function_(despawn_function), - entity_manager_ptr_(entity_manager_ptr), - config_(config) +: TrafficModuleBase(), despawn_(despawn), entity_manager_ptr_(entity_manager_ptr), config_(config) { } @@ -54,7 +51,7 @@ auto TrafficSink::execute( { for (const auto & entity_name : entity_manager_ptr_->getEntityNames()) { if (isEntitySinkable(entity_name)) { - despawn_function_(entity_name); + despawn_(entity_name); } } }