From a190752c36c5145d47f7a9a95c973dad99d638cb Mon Sep 17 00:00:00 2001 From: Lei Liu Date: Mon, 5 Jul 2021 13:42:16 +0800 Subject: [PATCH 1/2] Add loaned message listener. Signed-off-by: Lei Liu --- demo_nodes_cpp/CMakeLists.txt | 4 ++ .../src/topics/listener_loaned_message.cpp | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 demo_nodes_cpp/src/topics/listener_loaned_message.cpp diff --git a/demo_nodes_cpp/CMakeLists.txt b/demo_nodes_cpp/CMakeLists.txt index e4b572551..143c189de 100644 --- a/demo_nodes_cpp/CMakeLists.txt +++ b/demo_nodes_cpp/CMakeLists.txt @@ -72,6 +72,7 @@ add_library(topics_library SHARED src/topics/talker_loaned_message.cpp src/topics/talker_serialized_message.cpp src/topics/listener.cpp + src/topics/listener_loaned_message.cpp src/topics/listener_serialized_message.cpp src/topics/listener_best_effort.cpp) add_demo_dependencies(timers_library) @@ -121,6 +122,9 @@ rclcpp_components_register_node(topics_library rclcpp_components_register_node(topics_library PLUGIN "demo_nodes_cpp::Listener" EXECUTABLE listener) +rclcpp_components_register_node(topics_library + PLUGIN "demo_nodes_cpp::LoanedMessageListener" + EXECUTABLE listener_loaned_message) rclcpp_components_register_node(topics_library PLUGIN "demo_nodes_cpp::SerializedMessageListener" EXECUTABLE listener_serialized_message) diff --git a/demo_nodes_cpp/src/topics/listener_loaned_message.cpp b/demo_nodes_cpp/src/topics/listener_loaned_message.cpp new file mode 100644 index 000000000..cb5d867fd --- /dev/null +++ b/demo_nodes_cpp/src/topics/listener_loaned_message.cpp @@ -0,0 +1,53 @@ +// Copyright 2014 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 "rclcpp/rclcpp.hpp" +#include "rclcpp_components/register_node_macro.hpp" + +#include "std_msgs/msg/float64.hpp" +#include "demo_nodes_cpp/visibility_control.h" + +namespace demo_nodes_cpp +{ + // Create a Listener class that subclasses the generic rclcpp::Node base class. + // The main function below will instantiate the class as a ROS node. + class LoanedMessageListener : public rclcpp::Node + { + public: + DEMO_NODES_CPP_PUBLIC + explicit LoanedMessageListener(const rclcpp::NodeOptions &options) + : Node("listener_loaned_message", options) + { + // Create a callback function for when messages are received. + // Variations of this function also exist using, for example UniquePtr for zero-copy transport. + setvbuf(stdout, NULL, _IONBF, BUFSIZ); + auto callback = + [this](const std_msgs::msg::Float64::SharedPtr msg) -> void + { + RCLCPP_INFO(this->get_logger(), "I heard: [%f]", msg->data); + }; + // Create a subscription to the topic which can be matched with one or more compatible ROS + // publishers. + // Note that not all publishers on the same topic with the same type will be compatible: + // they must have compatible Quality of Service policies. + sub_ = create_subscription("chatter_pod", 10, callback); + } + + private: + rclcpp::Subscription::SharedPtr sub_; + }; + +} // namespace demo_nodes_cpp + +RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::LoanedMessageListener) From c2e5e13d3bfd219150ec1d17b9d8b2efba879ee2 Mon Sep 17 00:00:00 2001 From: Lei Liu Date: Mon, 5 Jul 2021 14:52:35 +0800 Subject: [PATCH 2/2] Update indent. Signed-off-by: Lei Liu --- .../src/topics/listener_loaned_message.cpp | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/demo_nodes_cpp/src/topics/listener_loaned_message.cpp b/demo_nodes_cpp/src/topics/listener_loaned_message.cpp index cb5d867fd..f5665a801 100644 --- a/demo_nodes_cpp/src/topics/listener_loaned_message.cpp +++ b/demo_nodes_cpp/src/topics/listener_loaned_message.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Open Source Robotics Foundation, Inc. +// Copyright 2021 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. @@ -20,34 +20,34 @@ namespace demo_nodes_cpp { - // Create a Listener class that subclasses the generic rclcpp::Node base class. - // The main function below will instantiate the class as a ROS node. - class LoanedMessageListener : public rclcpp::Node +// Create a Listener class that subclasses the generic rclcpp::Node base class. +// The main function below will instantiate the class as a ROS node. +class LoanedMessageListener : public rclcpp::Node +{ +public: + DEMO_NODES_CPP_PUBLIC + explicit LoanedMessageListener(const rclcpp::NodeOptions & options) + : Node("listener_loaned_message", options) { - public: - DEMO_NODES_CPP_PUBLIC - explicit LoanedMessageListener(const rclcpp::NodeOptions &options) - : Node("listener_loaned_message", options) - { - // Create a callback function for when messages are received. - // Variations of this function also exist using, for example UniquePtr for zero-copy transport. - setvbuf(stdout, NULL, _IONBF, BUFSIZ); - auto callback = - [this](const std_msgs::msg::Float64::SharedPtr msg) -> void + // Create a callback function for when messages are received. + // Variations of this function also exist using, for example UniquePtr for zero-copy transport. + setvbuf(stdout, NULL, _IONBF, BUFSIZ); + auto callback = + [this](const std_msgs::msg::Float64::SharedPtr msg) -> void { RCLCPP_INFO(this->get_logger(), "I heard: [%f]", msg->data); }; - // Create a subscription to the topic which can be matched with one or more compatible ROS - // publishers. - // Note that not all publishers on the same topic with the same type will be compatible: - // they must have compatible Quality of Service policies. - sub_ = create_subscription("chatter_pod", 10, callback); - } + // Create a subscription to the topic which can be matched with one or more compatible ROS + // publishers. + // Note that not all publishers on the same topic with the same type will be compatible: + // they must have compatible Quality of Service policies. + sub_ = create_subscription("chatter_pod", 10, callback); + } - private: - rclcpp::Subscription::SharedPtr sub_; - }; +private: + rclcpp::Subscription::SharedPtr sub_; +}; -} // namespace demo_nodes_cpp +} // namespace demo_nodes_cpp RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::LoanedMessageListener)