YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GenericSensorRosPublisher.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6
7#ifndef YARP_DEV_GENERICSENSORSROSPUBLISHER_H
8#define YARP_DEV_GENERICSENSORSROSPUBLISHER_H
9
11#include <yarp/os/Publisher.h>
12#include <yarp/os/Node.h>
16#include <yarp/os/Log.h>
18#include <yarp/os/LogStream.h>
19
20
21// The log component is defined in each device, with a specialized name
23
24
25
42template <class ROS_MSG>
47{
48protected:
49 double m_periodInS{0.01};
50 std::string m_publisherName;
51 std::string m_rosNodeName;
57 std::string m_framename;
58 const size_t m_sens_index = 0;
59
60public:
63
64 /* DevideDriver methods */
65 bool open(yarp::os::Searchable &params) override;
66 bool close() override;
67
68 /* IMultipleWrapper methods */
69 bool attachAll(const yarp::dev::PolyDriverList &p) override;
70 bool detachAll() override;
71
72 /* PeriodicRateThread methods */
73 void threadRelease() override;
74 void run() override;
75
76protected:
77 virtual bool viewInterfaces() = 0;
78};
79
80template <class ROS_MSG>
82 PeriodicThread(0.02)
83{
84 m_rosNode = nullptr;
85 m_poly = nullptr;
88}
89
90template <class ROS_MSG>
92
93template <class ROS_MSG>
95{
96 if (!config.check("topic")) {
97 yCError(GENERICSENSORROSPUBLISHER, "Missing `topic` parameter, exiting.");
98 return false;
99 }
100
101 if (!config.check("period")) {
102 yCError(GENERICSENSORROSPUBLISHER, "Missing `period` parameter, exiting.");
103 return false;
104 }
105
106 if (config.find("period").isFloat32()==false && config.find("period").isFloat64()==false) {
107 yCError(GENERICSENSORROSPUBLISHER, "`period` parameter is present but it is not a float, exiting.");
108 return false;
109 }
110
111 m_periodInS = config.find("period").asFloat64();
112
113 if (m_periodInS <= 0) {
114 yCError(GENERICSENSORROSPUBLISHER, "`period` parameter is present (%f) but it is not a positive value, exiting.", m_periodInS);
115 return false;
116 }
117
118 std::string name = config.find("topic").asString();
119
120 // TODO(traversaro) Add port name validation when ready,
121 // see https://github.com/robotology/yarp/pull/1508
122
123 m_rosNodeName = name+ "_node";
124 m_publisherName = name;
125
126 if (config.check("node_name"))
127 {
128 m_rosNodeName = config.find("node_name").asString();
129 }
130
131 if (m_rosNodeName == "")
132 {
133 yCError(GENERICSENSORROSPUBLISHER) << "Invalid node name: " << m_rosNodeName;
134 return false;
135 }
136
137 m_rosNode = new yarp::os::Node(m_rosNodeName); // add a ROS node
138
139 if (m_rosNode == nullptr) {
140 yCError(GENERICSENSORROSPUBLISHER) << "Opening " << m_rosNodeName << " Node, check your yarp-ROS network configuration\n";
141 return false;
142 }
143
144 if (!m_publisher.topic(m_publisherName)) {
145 yCError(GENERICSENSORROSPUBLISHER) << "Opening " << m_publisherName << " Topic, check your yarp-ROS network configuration\n";
146 return false;
147 }
148
149 return true;
150}
151
152template <class ROS_MSG>
154{
155 return this->detachAll();
156
157 m_publisher.close();
158 if (m_rosNode)
159 {
160 delete m_rosNode;
161 m_rosNode = nullptr;
162 }
163}
164
165template <class ROS_MSG>
167{
168 // Attach the device
169 if (p.size() > 1)
170 {
171 yCError(GENERICSENSORROSPUBLISHER, "This device only supports exposing a "
172 "single MultipleAnalogSensors device on YARP ports, but %d devices have been passed in attachAll.",
173 p.size());
174 yCError(GENERICSENSORROSPUBLISHER, "Please use the multipleanalogsensorsremapper device to combine several device in a new device.");
175 detachAll();
176 return false;
177 }
178
179 if (p.size() == 0)
180 {
181 yCError(GENERICSENSORROSPUBLISHER, "No device passed to attachAll, please pass a device to expose on YARP ports.");
182 return false;
183 }
184
185 m_poly = p[0]->poly;
186
187 if (!m_poly)
188 {
189 yCError(GENERICSENSORROSPUBLISHER, "Null pointer passed to attachAll.");
190 return false;
191 }
192
193 // View all the interfaces
194 bool ok = viewInterfaces();
195 if (!ok)
196 {
197 yCError(GENERICSENSORROSPUBLISHER, "viewInterfaces() method failed.");
198 return false;
199 }
200
201 // Set rate period
202 ok &= this->setPeriod(m_periodInS);
203 ok &= this->start();
204
205 return ok;
206}
207
208template <class ROS_MSG>
210{
211 // Stop the thread on detach
212 if (this->isRunning()) {
213 this->stop();
214 }
215 return true;
216}
217
218template <class ROS_MSG>
222
223template <class ROS_MSG>
228
229#endif
const yarp::os::LogComponent & GENERICSENSORROSPUBLISHER()
This abstract template needs to be specialized in a ROS Publisher, for a specific ROS mesagge/sensor ...
virtual ~GenericSensorRosPublisher()
void run() override
Loop function.
bool detachAll() override
Detach the object (you must have first called attach).
virtual bool viewInterfaces()=0
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
yarp::os::Publisher< ROS_MSG > m_publisher
void threadRelease() override
Release method.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
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
A mini-server for performing network communication in the background.
The Node class.
Definition Node.h:23
An abstraction for a periodic thread.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
#define yCError(component,...)
#define YARP_DECLARE_LOG_COMPONENT(name)