|
| 1 | +// Copyright 2022 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <functional> |
| 16 | +#include <memory> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "rclcpp/rclcpp.hpp" |
| 20 | +#include "std_msgs/msg/string.hpp" |
| 21 | + |
| 22 | +using std::placeholders::_1; |
| 23 | + |
| 24 | +class MinimalContentFilteringSubscriber : public rclcpp::Node |
| 25 | +{ |
| 26 | +public: |
| 27 | + MinimalContentFilteringSubscriber() |
| 28 | + : Node("minimal_contentfiltering_subscriber"), |
| 29 | + current_filtering_expression_("data = %0"), |
| 30 | + current_expression_parameter_("'Hello, world! 1'"), |
| 31 | + count_(10) |
| 32 | + { |
| 33 | + rclcpp::SubscriptionOptions sub_options; |
| 34 | + current_expression_parameter_ = "'Hello, world! " + std::to_string(count_) + "'"; |
| 35 | + sub_options.content_filter_options.filter_expression = current_filtering_expression_; |
| 36 | + sub_options.content_filter_options.expression_parameters = { |
| 37 | + current_expression_parameter_ |
| 38 | + }; |
| 39 | + |
| 40 | + subscription_ = this->create_subscription<std_msgs::msg::String>( |
| 41 | + "topic", 10, std::bind(&MinimalContentFilteringSubscriber::topic_callback, this, _1), |
| 42 | + sub_options); |
| 43 | + |
| 44 | + if (!subscription_->is_cft_enabled()) { |
| 45 | + RCLCPP_WARN( |
| 46 | + this->get_logger(), "Content filter is not enabled since it's not supported"); |
| 47 | + } else { |
| 48 | + RCLCPP_INFO( |
| 49 | + this->get_logger(), |
| 50 | + "Subscribed to topic \"%s\" with content filtering", subscription_->get_topic_name()); |
| 51 | + print_expression_parameter(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +private: |
| 56 | + void topic_callback(const std_msgs::msg::String & msg) |
| 57 | + { |
| 58 | + RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str()); |
| 59 | + // update filtering expression parameter |
| 60 | + if (subscription_->is_cft_enabled()) { |
| 61 | + count_ = count_ + 10; |
| 62 | + current_expression_parameter_ = "'Hello, world! " + std::to_string(count_) + "'"; |
| 63 | + try { |
| 64 | + subscription_->set_content_filter( |
| 65 | + current_filtering_expression_, {current_expression_parameter_} |
| 66 | + ); |
| 67 | + } catch (const std::exception & e) { |
| 68 | + RCLCPP_ERROR(this->get_logger(), "%s", e.what()); |
| 69 | + } |
| 70 | + } |
| 71 | + print_expression_parameter(); |
| 72 | + } |
| 73 | + |
| 74 | + void print_expression_parameter(void) const |
| 75 | + { |
| 76 | + // print filtering expression and parameter stored in subscription |
| 77 | + if (subscription_->is_cft_enabled()) { |
| 78 | + rclcpp::ContentFilterOptions options; |
| 79 | + try { |
| 80 | + options = subscription_->get_content_filter(); |
| 81 | + RCLCPP_INFO( |
| 82 | + this->get_logger(), |
| 83 | + "Content filtering expression and parameter are \"%s\" and \"%s\"", |
| 84 | + options.filter_expression.c_str(), options.expression_parameters[0].c_str()); |
| 85 | + } catch (const std::exception & e) { |
| 86 | + RCLCPP_ERROR(this->get_logger(), "%s", e.what()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; |
| 92 | + std::string current_filtering_expression_; |
| 93 | + std::string current_expression_parameter_; |
| 94 | + size_t count_; |
| 95 | +}; |
| 96 | + |
| 97 | +int main(int argc, char * argv[]) |
| 98 | +{ |
| 99 | + rclcpp::init(argc, argv); |
| 100 | + rclcpp::spin(std::make_shared<MinimalContentFilteringSubscriber>()); |
| 101 | + rclcpp::shutdown(); |
| 102 | + return 0; |
| 103 | +} |
0 commit comments