From 5890d0bac9ef42b0cd5878617f1afd699c9ba7ef Mon Sep 17 00:00:00 2001 From: Michael Wrock Date: Fri, 15 Nov 2024 12:12:13 -0800 Subject: [PATCH] changed to async --- .../sam2_segmentation/sam2_segmentation.hpp | 23 +++++++++++++------ .../behaviors/src/sam2_segmentation.cpp | 22 +++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/picknik_ur_site_config/behaviors/include/sam2_segmentation/sam2_segmentation.hpp b/src/picknik_ur_site_config/behaviors/include/sam2_segmentation/sam2_segmentation.hpp index b6cc016b..07363c01 100644 --- a/src/picknik_ur_site_config/behaviors/include/sam2_segmentation/sam2_segmentation.hpp +++ b/src/picknik_ur_site_config/behaviors/include/sam2_segmentation/sam2_segmentation.hpp @@ -1,15 +1,17 @@ #pragma once -#include + +#include #include #include +#include namespace custom_behaviors { /** * @brief Segment an image using the SAM 2 model */ -class SAM2Segmentation : public moveit_studio::behaviors::SharedResourcesNode +class SAM2Segmentation : public moveit_studio::behaviors::AsyncBehaviorBase { public: /** @@ -36,11 +38,9 @@ class SAM2Segmentation : public moveit_studio::behaviors::SharedResourcesNode doWork() override; + private: void set_onnx_from_ros_image(const sensor_msgs::msg::Image& image_msg); @@ -49,6 +49,15 @@ class SAM2Segmentation : public moveit_studio::behaviors::SharedResourcesNode sam2_; moveit_pro_ml::ONNXImage onnx_image_; sensor_msgs::msg::Image image_; + + /** @brief Classes derived from AsyncBehaviorBase must implement getFuture() so that it returns a shared_future class member */ + std::shared_future>& getFuture() override + { + return future_; + } + + /** @brief Classes derived from AsyncBehaviorBase must have this shared_future as a class member */ + std::shared_future> future_; }; } // namespace sam2_segmentation diff --git a/src/picknik_ur_site_config/behaviors/src/sam2_segmentation.cpp b/src/picknik_ur_site_config/behaviors/src/sam2_segmentation.cpp index e4801a20..2c54b138 100644 --- a/src/picknik_ur_site_config/behaviors/src/sam2_segmentation.cpp +++ b/src/picknik_ur_site_config/behaviors/src/sam2_segmentation.cpp @@ -23,7 +23,7 @@ namespace custom_behaviors { SAM2Segmentation::SAM2Segmentation(const std::string& name, const BT::NodeConfiguration& config, const std::shared_ptr& shared_resources) - : SharedResourcesNode(name, config, shared_resources) + : moveit_studio::behaviors::AsyncBehaviorBase(name, config, shared_resources) { std::filesystem::path package_path = ament_index_cpp::get_package_share_directory("picknik_ur_site_config"); const std::filesystem::path encoder_onnx_file = package_path / "models" / "sam2_hiera_large_encoder.onnx"; @@ -79,7 +79,7 @@ namespace custom_behaviors } - BT::NodeStatus SAM2Segmentation::tick() + tl::expected SAM2Segmentation::doWork() { const auto ports = moveit_studio::behaviors::getRequiredInputs(getInput(kPortImage), getInput