YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GenericSensor_nws_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_NWS_ROS2_H
7#define YARP_DEV_GENERICSENSORS_NWS_ROS2_H
8
13#include <yarp/os/Log.h>
15#include <yarp/os/LogStream.h>
17
18#include <rclcpp/rclcpp.hpp>
19#include <Ros2Utils.h>
20
21
22// The log component is defined in each device, with a specialized name
24
25
26
39template <class ROS_MSG>
45{
46protected:
47 rclcpp::Node::SharedPtr m_node;
48 typename rclcpp::Publisher<ROS_MSG>::SharedPtr m_publisher;
51 std::string m_framename;
52 const size_t m_sens_index = 0;
54
55public:
58
59 /* DevideDriver methods */
60 bool open(yarp::os::Searchable &params) override;
61 bool close() override;
62
63 /* IMultipleWrapper methods */
64 bool attachAll(const yarp::dev::PolyDriverList &p) override;
65 bool detachAll() override;
66
67 /* PeriodicRateThread methods */
68 void threadRelease() override;
69 void run() override;
70
71protected:
72 virtual bool viewInterfaces() = 0;
73};
74
75template <class ROS_MSG>
77 PeriodicThread(0.02)
78{
79 m_node = nullptr;
80 m_publisher=nullptr;
81 m_poly = nullptr;
83}
84
85template <class ROS_MSG>
87
88template <class ROS_MSG>
90{
91 parseParams(config);
92 if (m_node_name.c_str()[0] == '/') {
93 yCError(GENERICSENSOR_NWS_ROS2) << "node name cannot begin with /";
94 return false;
95 }
96
97 if (m_topic_name.c_str()[0] != '/') {
98 yCError(GENERICSENSOR_NWS_ROS2) << "Missing '/' in topic_name parameter";
99 return false;
100 }
101
102 m_node = NodeCreator::createNode(m_node_name); // add a ROS node
103 m_publisher = m_node->create_publisher<ROS_MSG>(m_topic_name,rclcpp::QoS(10));
104
105 if (m_node == nullptr) {
106 yCError(GENERICSENSOR_NWS_ROS2) << "Opening " << m_node_name << " Node, check your yarp-ROS network configuration\n";
107 return false;
108 }
109
110 if (m_publisher == nullptr) {
111 yCError(GENERICSENSOR_NWS_ROS2) << "Opening " << m_topic_name << " Topic, check your yarp-ROS network configuration\n";
112 return false;
113 }
114
115 yCInfo(GENERICSENSOR_NWS_ROS2) << "Running, waiting for attach...";
116
117 return true;
118}
119
120template <class ROS_MSG>
122{
123 return this->detachAll();
124}
125
126template <class ROS_MSG>
128{
129 // Attach the device
130 if (p.size() > 1)
131 {
132 yCError(GENERICSENSOR_NWS_ROS2, "This device only supports exposing a "
133 "single MultipleAnalogSensors device on YARP ports, but %d devices have been passed in attachAll.",
134 p.size());
135 yCError(GENERICSENSOR_NWS_ROS2, "Please use the multipleanalogsensorsremapper device to combine several device in a new device.");
136 detachAll();
137 return false;
138 }
139
140 if (p.size() == 0)
141 {
142 yCError(GENERICSENSOR_NWS_ROS2, "No device passed to attachAll, please pass a device to expose on YARP ports.");
143 return false;
144 }
145
146 m_poly = p[0]->poly;
147
148 if (!m_poly)
149 {
150 yCError(GENERICSENSOR_NWS_ROS2, "Null pointer passed to attachAll.");
151 return false;
152 }
153
154 // View all the interfaces
155 bool ok = viewInterfaces();
156
157 // Set rate period
158 ok &= this->setPeriod(m_period);
159 ok &= this->start();
160
161 return ok;
162}
163
164template <class ROS_MSG>
166{
167 // Stop the thread on detach
168 if (this->isRunning()) {
169 this->stop();
170 }
171 return true;
172}
173
174template <class ROS_MSG>
178
179template <class ROS_MSG>
184
185#endif
const yarp::os::LogComponent & GENERICSENSOR_NWS_ROS2()
This class is the parameters parser for class GenericSensor_nws_ros2.
This abstract template needs to be specialized in a ROS Publisher, for a specific ROS mesagge/sensor ...
rclcpp::Publisher< ROS_MSG >::SharedPtr m_publisher
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
yarp::dev::PolyDriver * m_poly
bool detachAll() override
Detach the object (you must have first called attach).
virtual bool viewInterfaces()=0
rclcpp::Node::SharedPtr m_node
virtual ~GenericSensor_nws_ros2()
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
yarp::dev::PolyDriver m_subdevicedriver
bool close() override
Close the DeviceDriver.
void run() override
Loop function.
void threadRelease() override
Release method.
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
Interface implemented by all device drivers.
Interface for an object that can wrap/attach to to another.
A container for a device driver.
Definition PolyDriver.h:23
An abstraction for a periodic thread.
A base class for nested structures that can be searched.
Definition Searchable.h:31
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_DECLARE_LOG_COMPONENT(name)