|
| 1 | +/* |
| 2 | + * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT) |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This software may be modified and distributed under the terms of the |
| 6 | + * BSD-3-Clause license. See the accompanying LICENSE file for details. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "Ros2SubscriptionTest.h" |
| 10 | + |
| 11 | +#include <yarp/os/LogComponent.h> |
| 12 | +#include <yarp/os/LogStream.h> |
| 13 | + |
| 14 | +#include <chrono> |
| 15 | +#include <functional> |
| 16 | +#include <memory> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +using namespace std::chrono_literals; |
| 20 | +using std::placeholders::_1; |
| 21 | + |
| 22 | +YARP_LOG_COMPONENT(ROS2SUBSCRIPTIONTEST, "yarp.ros2.ros2SubscriptionTest", yarp::os::Log::TraceType); |
| 23 | + |
| 24 | +class MinimalSubscriber : public rclcpp::Node |
| 25 | +{ |
| 26 | +public: |
| 27 | + MinimalSubscriber(std::string name, Ros2SubscriptionTest *subscriptionTest, std::string topic) |
| 28 | + : Node(name) |
| 29 | + { |
| 30 | + subscription_ = this->create_subscription<std_msgs::msg::String>( |
| 31 | + topic, 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); |
| 32 | + m_subscriptionTest = subscriptionTest; |
| 33 | + } |
| 34 | + |
| 35 | +private: |
| 36 | + void topic_callback(const std_msgs::msg::String::SharedPtr msg) const |
| 37 | + { |
| 38 | + m_subscriptionTest->local_callback(msg); |
| 39 | + } |
| 40 | + Ros2SubscriptionTest *m_subscriptionTest; |
| 41 | + rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_; |
| 42 | +}; |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +//Ros2Init::Ros2Init() |
| 49 | +//{ |
| 50 | +// rclcpp::init(/*argc*/ 0, /*argv*/ nullptr); |
| 51 | +// node = std::make_shared<rclcpp::Node>("yarprobotinterface_node"); |
| 52 | +//} |
| 53 | +// |
| 54 | +//Ros2Init& Ros2Init::get() |
| 55 | +//{ |
| 56 | +// static Ros2Init instance; |
| 57 | +// return instance; |
| 58 | +//} |
| 59 | + |
| 60 | + |
| 61 | +Ros2SubscriptionTest::Ros2SubscriptionTest() |
| 62 | +{ |
| 63 | +} |
| 64 | + |
| 65 | +bool Ros2SubscriptionTest::open(yarp::os::Searchable& config) |
| 66 | +{ |
| 67 | + if (config.check("node_name")) |
| 68 | + { |
| 69 | + m_node_name = config.find("node_name").asString(); |
| 70 | + } else { |
| 71 | + yCError(ROS2SUBSCRIPTIONTEST) << "missing node_name parameter"; |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + if (config.check("topic_name")) |
| 76 | + { |
| 77 | + m_topic = config.find("topic_name").asString(); |
| 78 | + } else { |
| 79 | + yCError(ROS2SUBSCRIPTIONTEST) << "missing topic_name parameter"; |
| 80 | + return false; |
| 81 | + } |
| 82 | + start(); |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +bool Ros2SubscriptionTest::close() |
| 87 | +{ |
| 88 | + yCTrace(ROS2SUBSCRIPTIONTEST); |
| 89 | + yCInfo(ROS2SUBSCRIPTIONTEST, "Ros2SubscriptionTest::close"); |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +void Ros2SubscriptionTest::local_callback(const std_msgs::msg::String::SharedPtr msg) const |
| 94 | +{ |
| 95 | + yInfo() << "hello, I'm " + m_node_name + m_topic + ", I heard: '%s'" << msg->data.c_str(); |
| 96 | +} |
| 97 | + |
| 98 | +void Ros2SubscriptionTest::run() |
| 99 | +{ |
| 100 | + if(!rclcpp::ok()) |
| 101 | + { |
| 102 | + rclcpp::init(/*argc*/ 0, /*argv*/ nullptr); |
| 103 | + } |
| 104 | + rclcpp::spin(std::make_shared<MinimalSubscriber>(m_node_name, this, m_topic)); |
| 105 | + rclcpp::shutdown(); |
| 106 | +} |
0 commit comments