From 59754156e821d077f978f769596914bdbb8396e1 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Tue, 19 Apr 2022 13:49:54 -0700 Subject: [PATCH 1/4] add ContentFilteredTopic example. Signed-off-by: Tomoya Fujita --- .../topics/minimal_subscriber/CMakeLists.txt | 3 + rclcpp/topics/minimal_subscriber/README.md | 3 + .../minimal_subscriber/content_filtering.cpp | 103 ++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 rclcpp/topics/minimal_subscriber/content_filtering.cpp diff --git a/rclcpp/topics/minimal_subscriber/CMakeLists.txt b/rclcpp/topics/minimal_subscriber/CMakeLists.txt index 2edca97f..617c7001 100644 --- a/rclcpp/topics/minimal_subscriber/CMakeLists.txt +++ b/rclcpp/topics/minimal_subscriber/CMakeLists.txt @@ -33,6 +33,8 @@ ament_target_dependencies(subscriber_member_function_with_unique_network_flow_en add_executable(subscriber_not_composable not_composable.cpp) ament_target_dependencies(subscriber_not_composable rclcpp std_msgs) +add_executable(subscriber_content_filtering content_filtering.cpp) +ament_target_dependencies(subscriber_content_filtering rclcpp std_msgs) add_library(wait_set_subscriber_library SHARED wait_set_subscriber.cpp @@ -65,6 +67,7 @@ install(TARGETS subscriber_member_function_with_type_adapter subscriber_member_function_with_unique_network_flow_endpoints subscriber_not_composable + subscriber_content_filtering DESTINATION lib/${PROJECT_NAME}) if(BUILD_TESTING) diff --git a/rclcpp/topics/minimal_subscriber/README.md b/rclcpp/topics/minimal_subscriber/README.md index 5caa8006..07f3f15d 100644 --- a/rclcpp/topics/minimal_subscriber/README.md +++ b/rclcpp/topics/minimal_subscriber/README.md @@ -8,6 +8,7 @@ This package contains a few different strategies for creating nodes which receiv * `static_wait_set_subscriber.cpp` uses a `rclcpp::StaticWaitSet` to wait and poll for data * `time_triggered_wait_set_subscriber.cpp` uses a `rclcpp::Waitset` and a timer to poll for data periodically + * `content_filtering.cpp` uses a ROS 2 ContentFilteredTopic Note that `not_composable.cpp` instantiates a `rclcpp::Node` _without_ subclassing it. This was the typical usage model in ROS 1, but this style of coding is not compatible with composing multiple nodes into a single process. @@ -20,3 +21,5 @@ We provide multiple examples of different coding styles which achieve this behav The following examples `wait_set_subscriber.cpp`, `static_wait_set_subscriber.cpp` and `time_triggered_wait_set_subscriber.cpp` show how to use a subscription in a node using a `rclcpp` wait-set. This is not a common use case in ROS 2 so this is not the recommended strategy to use by-default. This strategy makes sense in some specific situations, for example when the developer needs to have more control over callback order execution, to create custom triggering conditions or to use the timeouts provided by the wait-sets. + +The following example `content_filtering.cpp` show how to use ROS 2 ContentFilteredTopic. diff --git a/rclcpp/topics/minimal_subscriber/content_filtering.cpp b/rclcpp/topics/minimal_subscriber/content_filtering.cpp new file mode 100644 index 00000000..25fc9c4d --- /dev/null +++ b/rclcpp/topics/minimal_subscriber/content_filtering.cpp @@ -0,0 +1,103 @@ +// Copyright 2022 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/string.hpp" + +using std::placeholders::_1; + +class MinimalContentFilteringSubscriber : public rclcpp::Node +{ +public: + MinimalContentFilteringSubscriber() + : Node("minimal_contentfiltering_subscriber"), + current_filtering_expression_("data = %0"), + current_expression_parameter_("'Hello, world! 1'"), + count_(10) + { + rclcpp::SubscriptionOptions sub_options; + current_expression_parameter_ = "'Hello, world! " + std::to_string(count_) + "'"; + sub_options.content_filter_options.filter_expression = current_filtering_expression_; + sub_options.content_filter_options.expression_parameters = { + current_expression_parameter_ + }; + + subscription_ = this->create_subscription( + "topic", 10, std::bind(&MinimalContentFilteringSubscriber::topic_callback, this, _1), + sub_options); + + if (!subscription_->is_cft_enabled()) { + RCLCPP_WARN( + this->get_logger(), "Content filter is not enabled since it's not supported"); + } else { + RCLCPP_INFO( + this->get_logger(), + "Subscribed to topic \"%s\" with content filtering", subscription_->get_topic_name()); + print_expression_parameter(); + } + } + +private: + void topic_callback(const std_msgs::msg::String & msg) + { + RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str()); + // update filtering expression parameter + if (subscription_->is_cft_enabled()) { + count_ = count_ + 10; + current_expression_parameter_ = "'Hello, world! " + std::to_string(count_) + "'"; + try { + subscription_->set_content_filter( + current_filtering_expression_, {current_expression_parameter_} + ); + } catch (const std::exception & e) { + RCLCPP_ERROR(this->get_logger(), "%s", e.what()); + } + } + print_expression_parameter(); + } + + void print_expression_parameter(void) const + { + // print filtering expression and parameter stored in subscription + if (subscription_->is_cft_enabled()) { + rclcpp::ContentFilterOptions options; + try { + options = subscription_->get_content_filter(); + RCLCPP_INFO( + this->get_logger(), + "Content filtering expression and parameter are \"%s\" and \"%s\"", + options.filter_expression.c_str(), options.expression_parameters[0].c_str()); + } catch (const std::exception & e) { + RCLCPP_ERROR(this->get_logger(), "%s", e.what()); + } + } + } + + rclcpp::Subscription::SharedPtr subscription_; + std::string current_filtering_expression_; + std::string current_expression_parameter_; + size_t count_; +}; + +int main(int argc, char * argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} From 59ca01af2ee18496313a48d3c07c396dda2de742 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Fri, 22 Apr 2022 09:24:47 -0700 Subject: [PATCH 2/4] fix README. Signed-off-by: Tomoya Fujita Co-authored-by: William Woodall --- rclcpp/topics/minimal_subscriber/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rclcpp/topics/minimal_subscriber/README.md b/rclcpp/topics/minimal_subscriber/README.md index 07f3f15d..de38c169 100644 --- a/rclcpp/topics/minimal_subscriber/README.md +++ b/rclcpp/topics/minimal_subscriber/README.md @@ -8,7 +8,7 @@ This package contains a few different strategies for creating nodes which receiv * `static_wait_set_subscriber.cpp` uses a `rclcpp::StaticWaitSet` to wait and poll for data * `time_triggered_wait_set_subscriber.cpp` uses a `rclcpp::Waitset` and a timer to poll for data periodically - * `content_filtering.cpp` uses a ROS 2 ContentFilteredTopic + * `content_filtering.cpp` used the content filtering feature for Subscriptions Note that `not_composable.cpp` instantiates a `rclcpp::Node` _without_ subclassing it. This was the typical usage model in ROS 1, but this style of coding is not compatible with composing multiple nodes into a single process. @@ -22,4 +22,4 @@ The following examples `wait_set_subscriber.cpp`, `static_wait_set_subscriber.cp This is not a common use case in ROS 2 so this is not the recommended strategy to use by-default. This strategy makes sense in some specific situations, for example when the developer needs to have more control over callback order execution, to create custom triggering conditions or to use the timeouts provided by the wait-sets. -The following example `content_filtering.cpp` show how to use ROS 2 ContentFilteredTopic. +The example `content_filtering.cpp` shows how to use the content filtering feature for Subscriptions. From fb859b30f4f22a0963e4d323677319b341a2e824 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Fri, 22 Apr 2022 10:11:40 -0700 Subject: [PATCH 3/4] fix uncrustify errors. Signed-off-by: Tomoya Fujita --- rclcpp/topics/minimal_subscriber/content_filtering.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rclcpp/topics/minimal_subscriber/content_filtering.cpp b/rclcpp/topics/minimal_subscriber/content_filtering.cpp index 25fc9c4d..74de76e4 100644 --- a/rclcpp/topics/minimal_subscriber/content_filtering.cpp +++ b/rclcpp/topics/minimal_subscriber/content_filtering.cpp @@ -26,9 +26,9 @@ class MinimalContentFilteringSubscriber : public rclcpp::Node public: MinimalContentFilteringSubscriber() : Node("minimal_contentfiltering_subscriber"), - current_filtering_expression_("data = %0"), - current_expression_parameter_("'Hello, world! 1'"), - count_(10) + current_filtering_expression_("data = %0"), + current_expression_parameter_("'Hello, world! 1'"), + count_(10) { rclcpp::SubscriptionOptions sub_options; current_expression_parameter_ = "'Hello, world! " + std::to_string(count_) + "'"; @@ -63,7 +63,7 @@ class MinimalContentFilteringSubscriber : public rclcpp::Node try { subscription_->set_content_filter( current_filtering_expression_, {current_expression_parameter_} - ); + ); } catch (const std::exception & e) { RCLCPP_ERROR(this->get_logger(), "%s", e.what()); } @@ -83,7 +83,7 @@ class MinimalContentFilteringSubscriber : public rclcpp::Node "Content filtering expression and parameter are \"%s\" and \"%s\"", options.filter_expression.c_str(), options.expression_parameters[0].c_str()); } catch (const std::exception & e) { - RCLCPP_ERROR(this->get_logger(), "%s", e.what()); + RCLCPP_ERROR(this->get_logger(), "%s", e.what()); } } } From 6bc314e1126eac38e6a13f6ed7b7d4294adb433c Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Fri, 22 Apr 2022 13:10:45 -0700 Subject: [PATCH 4/4] fix README. Signed-off-by: Tomoya Fujita --- rclcpp/topics/minimal_subscriber/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rclcpp/topics/minimal_subscriber/README.md b/rclcpp/topics/minimal_subscriber/README.md index de38c169..fb03cf69 100644 --- a/rclcpp/topics/minimal_subscriber/README.md +++ b/rclcpp/topics/minimal_subscriber/README.md @@ -8,7 +8,7 @@ This package contains a few different strategies for creating nodes which receiv * `static_wait_set_subscriber.cpp` uses a `rclcpp::StaticWaitSet` to wait and poll for data * `time_triggered_wait_set_subscriber.cpp` uses a `rclcpp::Waitset` and a timer to poll for data periodically - * `content_filtering.cpp` used the content filtering feature for Subscriptions + * `content_filtering.cpp` uses the content filtering feature for Subscriptions Note that `not_composable.cpp` instantiates a `rclcpp::Node` _without_ subclassing it. This was the typical usage model in ROS 1, but this style of coding is not compatible with composing multiple nodes into a single process.