From bf8291b7c2823b0d2f149179c30cbb918933b457 Mon Sep 17 00:00:00 2001 From: Dominic Reber Date: Fri, 4 Oct 2024 10:24:37 +0000 Subject: [PATCH] fix: use shared ptr buffer for custom input --- .../modulo_controllers/BaseControllerInterface.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/source/modulo_controllers/include/modulo_controllers/BaseControllerInterface.hpp b/source/modulo_controllers/include/modulo_controllers/BaseControllerInterface.hpp index e9fd1364..bde57816 100644 --- a/source/modulo_controllers/include/modulo_controllers/BaseControllerInterface.hpp +++ b/source/modulo_controllers/include/modulo_controllers/BaseControllerInterface.hpp @@ -527,7 +527,7 @@ inline void BaseControllerInterface::set_parameter_value(const std::string& name template inline void BaseControllerInterface::add_input(const std::string& name, const std::string& topic_name) { if constexpr (modulo_core::concepts::CustomT) { - auto buffer = realtime_tools::RealtimeBuffer(); + auto buffer = std::make_shared>>(); auto input = ControllerInput(buffer); auto parsed_name = validate_and_declare_signal(name, "input", topic_name); if (!parsed_name.empty()) { @@ -536,9 +536,10 @@ inline void BaseControllerInterface::add_input(const std::string& name, const st name, [this](const std::string& name, const std::string& topic) { auto subscription = get_node()->create_subscription(topic, qos_, [this, name](const std::shared_ptr message) { - auto buffer = - std::any_cast>>(inputs_.at(name).buffer); - buffer.writeFromNonRT(message); + auto buffer_variant = std::get(inputs_.at(name).buffer); + auto buffer = std::any_cast>>>( + buffer_variant); + buffer->writeFromNonRT(message); inputs_.at(name).timestamp = std::chrono::steady_clock::now(); }); subscriptions_.push_back(subscription); @@ -656,8 +657,9 @@ inline std::optional BaseControllerInterface::read_input(const std::string& n if constexpr (modulo_core::concepts::CustomT) { try { - auto buffer = std::any_cast>>(inputs_.at(name).buffer); - return **(buffer.readFromNonRT()); + auto buffer_variant = std::get(inputs_.at(name).buffer); + auto buffer = std::any_cast>>>(buffer_variant); + return **(buffer->readFromNonRT()); } catch (const std::bad_any_cast& ex) { RCLCPP_ERROR(get_node()->get_logger(), "Failed to read custom input: %s", ex.what()); }