YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GenericSensor_nwc_ros2.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 YARP_DEV_GENERICSENSORS_NWC_ROS2_H
7#define YARP_DEV_GENERICSENSORS_NWC_ROS2_H
8
13#include <yarp/os/Log.h>
15#include <yarp/os/LogStream.h>
16#include <mutex>
18
19#include <rclcpp/rclcpp.hpp>
20#include <Ros2Utils.h>
21#include <Ros2Spinner.h>
22
23
24// The log component is defined in each device, with a specialized name
26
27
28
41template <class ROS_MSG>
45{
46protected:
47 double m_periodInS{0.01};
49 std::string m_framename;
51 const size_t m_sens_index = 0;
52 mutable std::mutex m_dataMutex;
54 rclcpp::Node::SharedPtr m_node;
55 typename rclcpp::Subscription<ROS_MSG>::SharedPtr m_subscription;
56
57 // Subscription callback. To be implemented for each derived device
58 virtual void subscription_callback(const std::shared_ptr<ROS_MSG> msg)=0;
59
60public:
63
64 /* DevideDriver methods */
65 bool open(yarp::os::Searchable &params) override;
66 bool close() override;
67};
68
69template <class ROS_MSG>
71{
72 m_node = nullptr;
73 m_subscription=nullptr;
74 m_timestamp=0;
75}
76
77template <class ROS_MSG>
79
80template <class ROS_MSG>
82{
83 parseParams(config);
84 if (m_node_name.c_str()[0] == '/') {
85 yCError(GENERICSENSOR_NWC_ROS2) << "node name cannot begin with /";
86 return false;
87 }
88
89 if (m_topic_name.c_str()[0] != '/') {
90 yCError(GENERICSENSOR_NWC_ROS2) << "Missing '/' in topic_name parameter";
91 return false;
92 }
93
94 m_sensor_name = config.find("sensor_name").asString();
95
96 m_node = NodeCreator::createNode(m_node_name);
97 m_subscription = m_node->create_subscription<ROS_MSG>(m_topic_name, rclcpp::QoS(10),
99 this, std::placeholders::_1));
100
101 if (m_node == nullptr) {
102 yCError(GENERICSENSOR_NWC_ROS2) << "Opening " << m_node_name << " Node creation failed, check your yarp-ROS network configuration\n";
103 return false;
104 }
105
106 if (m_subscription == nullptr) {
107 yCError(GENERICSENSOR_NWC_ROS2) << "Opening " << m_topic_name << " Topic failed, check your yarp-ROS network configuration\n";
108 return false;
109 }
110
112 m_spinner = new Ros2Spinner(m_node);
113 m_spinner->start();
114
115 yCInfo(GENERICSENSOR_NWC_ROS2) << "Device opened";
116
117 return true;
118}
119
120template <class ROS_MSG>
122{
123 yCInfo(GENERICSENSOR_NWC_ROS2) << "Closing...";
124 delete m_spinner;
125 yCInfo(GENERICSENSOR_NWC_ROS2) << "Closed";
126 return true;
127}
128
129#endif
const yarp::os::LogComponent & GENERICSENSOR_NWC_ROS2()
This class is the parameters parser for class GenericSensor_nwc_ros2.
This abstract template needs to be specialized in a ROS2 subscription, for a specific ROS2 message/se...
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
yarp::dev::MAS_status m_internalStatus
rclcpp::Subscription< ROS_MSG >::SharedPtr m_subscription
virtual ~GenericSensor_nwc_ros2()
rclcpp::Node::SharedPtr m_node
virtual void subscription_callback(const std::shared_ptr< ROS_MSG > msg)=0
bool close() override
Close the DeviceDriver.
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
Interface implemented by all device drivers.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_DECLARE_LOG_COMPONENT(name)
MAS_status
Status of a given analog sensor exposed by a multiple analog sensors interface.
@ MAS_WAITING_FOR_FIRST_READ
The sensor is read through the network, and the device is waiting to receive the first measurement.