YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Ros2Subscriber.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef ROS2_SUBSCRIBER_H
7#define ROS2_SUBSCRIBER_H
8
9#include <iostream>
10#include <cstring>
11
12#include <yarp/os/LogStream.h>
13
15#include <rclcpp/rclcpp.hpp>
16
17
18
19template <class CallbackClass, typename MsgClass>
21{
22public:
23 Ros2Subscriber(rclcpp::Node::SharedPtr node, CallbackClass* callbackClass);
24
25 void subscribe_to_topic(std::string topic_name);
26private:
27 rclcpp::Node::SharedPtr m_node;
28 CallbackClass* m_callbackClass;
29 std::vector<typename rclcpp::Subscription<MsgClass>::SharedPtr> m_subscription;
30};
31
32template <class CallbackClass, typename MsgClass>
33Ros2Subscriber<CallbackClass, MsgClass>::Ros2Subscriber(rclcpp::Node::SharedPtr node, CallbackClass* callbackClass)
34{
35 m_node = node;
36 m_callbackClass = callbackClass;
37}
38
39
40template <class CallbackClass, typename MsgClass>
42 yInfo() << "Ros2Subscriber creating topic: " << topic_name;
43 m_subscription.push_back(m_node->create_subscription<MsgClass>(
44 topic_name,
45 10,
46 [this, topic_name](const typename MsgClass::SharedPtr msg) {
47 m_callbackClass->callback(msg, topic_name);
48 }));
49}
50#endif //ROS2_SUBSCRIBER_H
#define yInfo(...)
Definition Log.h:319
Ros2Subscriber(rclcpp::Node::SharedPtr node, CallbackClass *callbackClass)
void subscribe_to_topic(std::string topic_name)