|
| 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 "rclcpp/rclcpp.hpp" |
| 16 | +#include "rclcpp_components/register_node_macro.hpp" |
| 17 | +#include "rcpputils/join.hpp" |
| 18 | + |
| 19 | +#include "std_msgs/msg/float32.hpp" |
| 20 | + |
| 21 | +#include "demo_nodes_cpp/visibility_control.h" |
| 22 | + |
| 23 | +namespace demo_nodes_cpp |
| 24 | +{ |
| 25 | +// Emergency temperature data less than -30 or greater than 100 |
| 26 | +constexpr std::array<float, 2> EMERGENCY_TEMPERATURE {-30.0f, 100.0f}; |
| 27 | + |
| 28 | +// Create a ContentFilteringSubscriber class that subclasses the generic rclcpp::Node base class. |
| 29 | +// The main function below will instantiate the class as a ROS node. |
| 30 | +class ContentFilteringSubscriber : public rclcpp::Node |
| 31 | +{ |
| 32 | +public: |
| 33 | + DEMO_NODES_CPP_PUBLIC |
| 34 | + explicit ContentFilteringSubscriber(const rclcpp::NodeOptions & options) |
| 35 | + : Node("content_filtering_subscriber", options) |
| 36 | + { |
| 37 | + setvbuf(stdout, NULL, _IONBF, BUFSIZ); |
| 38 | + // Create a callback function for when messages are received. |
| 39 | + auto callback = |
| 40 | + [this](const std_msgs::msg::Float32 & msg) -> void |
| 41 | + { |
| 42 | + if (msg.data < EMERGENCY_TEMPERATURE[0] || msg.data > EMERGENCY_TEMPERATURE[1]) { |
| 43 | + RCLCPP_INFO( |
| 44 | + this->get_logger(), |
| 45 | + "I receive an emergency temperature data: [%f]", msg.data); |
| 46 | + } else { |
| 47 | + RCLCPP_INFO(this->get_logger(), "I receive a temperature data: [%f]", msg.data); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // Initialize a subscription with a content filter to receive emergency temperature data that |
| 52 | + // are less than -30 or greater than 100. |
| 53 | + rclcpp::SubscriptionOptions sub_options; |
| 54 | + sub_options.content_filter_options.filter_expression = "data < %0 OR data > %1"; |
| 55 | + sub_options.content_filter_options.expression_parameters = { |
| 56 | + std::to_string(EMERGENCY_TEMPERATURE[0]), |
| 57 | + std::to_string(EMERGENCY_TEMPERATURE[1]) |
| 58 | + }; |
| 59 | + |
| 60 | + sub_ = create_subscription<std_msgs::msg::Float32>("temperature", 10, callback, sub_options); |
| 61 | + |
| 62 | + if (!sub_->is_cft_enabled()) { |
| 63 | + RCLCPP_WARN( |
| 64 | + this->get_logger(), "Content filter is not enabled since it's not supported"); |
| 65 | + } else { |
| 66 | + RCLCPP_INFO( |
| 67 | + this->get_logger(), |
| 68 | + "subscribed to topic \"%s\" with content filter options \"%s, {%s}\"", |
| 69 | + sub_->get_topic_name(), |
| 70 | + sub_options.content_filter_options.filter_expression.c_str(), |
| 71 | + rcpputils::join(sub_options.content_filter_options.expression_parameters, ", ").c_str()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +private: |
| 76 | + rclcpp::Subscription<std_msgs::msg::Float32>::SharedPtr sub_; |
| 77 | +}; |
| 78 | + |
| 79 | +} // namespace demo_nodes_cpp |
| 80 | + |
| 81 | +RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::ContentFilteringSubscriber) |
0 commit comments