YARP
Yet Another Robot Platform
RGBDSensorFromRosTopic.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <algorithm>
7 
8 #include <yarp/os/LogComponent.h>
9 #include <yarp/os/Value.h>
10 #include <yarp/sig/ImageUtils.h>
11 
12 #include "RGBDSensorFromRosTopic.h"
13 
14 using namespace yarp::dev;
15 using namespace yarp::sig;
16 using namespace yarp::os;
17 
18 
19 namespace {
20 YARP_LOG_COMPONENT(RGBD_ROS_TOPIC, "yarp.device.RGBDSensorFromRosTopic")
21 }
22 
23 
25 {
26  if (!config.check("depth_topic_name")) {
27  yCError(RGBD_ROS_TOPIC) << "missing depth_topic_name parameter, using default one";
28  return false;
29  }
30  std::string depth_topic_name = config.find("depth_topic_name").asString();
31  if(depth_topic_name[0] != '/'){
32  yCError(RGBD_ROS_TOPIC) << "depth_topic_name must begin with an initial /";
33  return false;
34  }
35 
36  if (!config.check("color_topic_name")) {
37  yCError(RGBD_ROS_TOPIC) << "missing color_topic_name parameter, using default one";
38  return false;
39  }
40  std::string color_topic_name = config.find("color_topic_name").asString();
41  if(color_topic_name[0] != '/'){
42  yCError(RGBD_ROS_TOPIC) << "color_topic_name must begin with an initial /";
43  return false;
44  }
45  std::string rgb_info_topic_name = color_topic_name.substr(0,color_topic_name.rfind('/')) + "/camera_info";
46  std::string depth_info_topic_name = depth_topic_name.substr(0,depth_topic_name.rfind('/')) + "/camera_info";
47 
48  if (!config.check("node_name")) {
49  yCError(RGBD_ROS_TOPIC) << "missing node_name parameter, using default one";
50  return false;
51  }
52  std::string node_name = config.find("node_name").asString();
53  if(node_name[0] != '/'){
54  yCError(RGBD_ROS_TOPIC) << "node_name must begin with an initial /";
55  return false;
56  }
57  m_ros_node = new yarp::os::Node(node_name);
58 
59  //m_rgb_input_processor.useCallback(); ///@@@<-SEGFAULT
60  //m_depth_input_processor.useCallback(); ///@@@<-SEGFAULT
61  m_rgb_input_processor = new yarp::dev::RGBDRosConversionUtils::commonImageProcessor(color_topic_name, rgb_info_topic_name);
62  m_depth_input_processor = new yarp::dev::RGBDRosConversionUtils::commonImageProcessor(depth_topic_name, depth_info_topic_name);
63  m_rgb_input_processor->useCallback();
64  m_depth_input_processor->useCallback();
65 
66  return true;
67 }
68 
70 {
71  if (m_rgb_input_processor)
72  {
73  delete m_rgb_input_processor;
74  m_rgb_input_processor =nullptr;
75  }
76  if (m_depth_input_processor)
77  {
78  delete m_depth_input_processor;
79  m_depth_input_processor = nullptr;
80  }
81  if (m_ros_node)
82  {
83  delete m_ros_node;
84  m_ros_node=nullptr;
85  }
86  return true;
87 }
88 
90 {
91  if (m_rgb_input_processor==nullptr)
92  {
93  return 0;
94  }
95  return static_cast<int>(m_rgb_input_processor->getHeight());
96 }
97 
99 {
100  if (m_rgb_input_processor==nullptr)
101  {
102  return 0;
103  }
104  return static_cast<int>(m_rgb_input_processor->getWidth());
105 }
106 
108 {
109  YARP_UNUSED(configurations);
110  yCWarning(RGBD_ROS_TOPIC) << "getRgbSupportedConfigurations not implemented yet";
111  return false;
112 }
113 
114 bool RGBDSensorFromRosTopic::getRgbResolution(int &width, int &height)
115 {
116  if (m_rgb_input_processor == nullptr)
117  {
118  width=0;
119  height=0;
120  return true;
121  }
122  width = static_cast<int>(m_rgb_input_processor->getWidth());
123  height = static_cast<int>(m_rgb_input_processor->getHeight());
124  return true;
125 }
126 
128 {
129  YARP_UNUSED(width);
130  YARP_UNUSED(height);
131  yCWarning(RGBD_ROS_TOPIC) << "setDepthResolution not supported";
132  return false;
133 }
134 
135 bool RGBDSensorFromRosTopic::setRgbResolution(int width, int height)
136 {
137  YARP_UNUSED(width);
138  YARP_UNUSED(height);
139  yCWarning(RGBD_ROS_TOPIC) << "setRgbResolution not supported";
140  return false;
141 }
142 
143 
144 bool RGBDSensorFromRosTopic::setRgbFOV(double horizontalFov, double verticalFov)
145 {
146  YARP_UNUSED(horizontalFov);
147  YARP_UNUSED(verticalFov);
148  yCWarning(RGBD_ROS_TOPIC) << "setRgbFOV not supported";
149  return false;
150 }
151 
152 bool RGBDSensorFromRosTopic::setDepthFOV(double horizontalFov, double verticalFov)
153 {
154  YARP_UNUSED(horizontalFov);
155  YARP_UNUSED(verticalFov);
156  yCWarning(RGBD_ROS_TOPIC) << "setDepthFOV not supported";
157  return false;
158 }
159 
161 {
162  YARP_UNUSED(accuracy);
163  yCWarning(RGBD_ROS_TOPIC) << "setDepthAccuracy not supported";
164  return false;
165 }
166 
167 bool RGBDSensorFromRosTopic::getRgbFOV(double &horizontalFov, double &verticalFov)
168 {
169  if (m_rgb_input_processor == nullptr)
170  {
171  horizontalFov=0;
172  verticalFov=0;
173  return true;
174  }
175  return m_rgb_input_processor->getFOV(horizontalFov, verticalFov);
176 }
177 
179 {
180  YARP_UNUSED(mirror);
181  yCWarning(RGBD_ROS_TOPIC) << "Mirroring not supported";
182  return false;
183 }
184 
186 {
187  YARP_UNUSED(mirror);
188  yCWarning(RGBD_ROS_TOPIC) << "Mirroring not supported";
189  return false;
190 }
191 
193 {
194  if (m_rgb_input_processor == nullptr)
195  {
196  intrinsic.clear();
197  return true;
198  }
199  return m_rgb_input_processor->getIntrinsicParam(intrinsic);
200 }
201 
203 {
204  if (m_depth_input_processor == nullptr)
205  {
206  return 0;
207  }
208  return static_cast<int>(m_depth_input_processor->getHeight());
209 }
210 
212 {
213  if (m_depth_input_processor == nullptr)
214  {
215  return 0;
216  }
217  return static_cast<int>(m_depth_input_processor->getWidth());
218 }
219 
220 bool RGBDSensorFromRosTopic::getDepthFOV(double& horizontalFov, double& verticalFov)
221 {
222  if (m_depth_input_processor == nullptr)
223  {
224  horizontalFov = 0;
225  verticalFov = 0;
226  return true;
227  }
228  return m_depth_input_processor->getFOV(horizontalFov, verticalFov);
229 }
230 
232 {
233  if (m_depth_input_processor == nullptr)
234  {
235  intrinsic.clear();
236  return true;
237  }
238  return m_depth_input_processor->getIntrinsicParam(intrinsic);
239 }
240 
242 {
243  yCWarning(RGBD_ROS_TOPIC) << "getDepthAccuracy not supported";
244  return 0;
245 }
246 
247 bool RGBDSensorFromRosTopic::getDepthClipPlanes(double& nearPlane, double& farPlane)
248 {
249  YARP_UNUSED(nearPlane);
250  YARP_UNUSED(farPlane);
251  yCWarning(RGBD_ROS_TOPIC) << "getDepthClipPlanes not supported";
252  return false;
253 }
254 
255 bool RGBDSensorFromRosTopic::setDepthClipPlanes(double nearPlane, double farPlane)
256 {
257  YARP_UNUSED(nearPlane);
258  YARP_UNUSED(farPlane);
259  yCWarning(RGBD_ROS_TOPIC) << "setDepthClipPlanes not supported";
260  return false;
261 }
262 
264 {
265  YARP_UNUSED(mirror);
266  yCWarning(RGBD_ROS_TOPIC) << "getDepthMirroring not supported";
267  return false;
268 }
269 
271 {
272  YARP_UNUSED(mirror);
273  yCWarning(RGBD_ROS_TOPIC) << "setDepthMirroring not supported";
274  return false;
275 }
276 
278 {
279  YARP_UNUSED(extrinsic);
280  yCWarning(RGBD_ROS_TOPIC) << "getExtrinsicParam not supported";
281  return false;
282 }
283 
285 {
286  std::lock_guard<std::mutex> guard(m_mutex);
287  bool rgb_ok = false;
288  if (m_rgb_input_processor!=nullptr)
289  { rgb_ok = m_rgb_input_processor->getLastRGBData(rgbImage, *timeStamp); }
290  return rgb_ok;
291 }
292 
294 {
295  std::lock_guard<std::mutex> guard(m_mutex);
296  bool depth_ok =false;
297  if (m_depth_input_processor != nullptr)
298  { depth_ok = m_depth_input_processor->getLastDepthData(depthImage, *timeStamp); }
299  return depth_ok;
300 }
301 
302 bool RGBDSensorFromRosTopic::getImages(FlexImage& colorFrame, ImageOf<PixelFloat>& depthFrame, Stamp* colorStamp, Stamp* depthStamp)
303 {
304  bool rgb_ok = false;
305  bool depth_ok = false;
306  std::lock_guard<std::mutex> guard(m_mutex);
307  if (m_rgb_input_processor != nullptr)
308  { rgb_ok = m_rgb_input_processor->getLastRGBData(colorFrame, *colorStamp); }
309  if (m_depth_input_processor != nullptr)
310  { depth_ok = m_depth_input_processor->getLastDepthData(depthFrame, *depthStamp); }
311  return (rgb_ok && depth_ok);
312 }
313 
315 {
316  return RGBD_SENSOR_OK_IN_USE;
317 }
318 
320 {
321  YARP_UNUSED(timeStamp);
322  return m_lastError;
323 }
324 
325 /*
326 //IFrameGrabberControls
327 bool RGBDSensorFromRosTopic::getCameraDescription(CameraDescriptor* camera)
328 {
329  camera->deviceDescription = "Ros Camera";
330  camera->busType = BusType::BUS_UNKNOWN;
331  return true;
332 }
333 
334 bool RGBDSensorFromRosTopic::hasFeature(int feature, bool* hasFeature)
335 {
336  yCWarning(RGBD_ROS_TOPIC) << "hasFeature not supported";
337  return false;
338 }
339 
340 bool RGBDSensorFromRosTopic::setFeature(int feature, double value)
341 {
342  yCWarning(RGBD_ROS_TOPIC) << "setFeature not supported";
343  return false;
344 }
345 
346 bool RGBDSensorFromRosTopic::getFeature(int feature, double *value)
347 {
348  yCWarning(RGBD_ROS_TOPIC) << "getFeature not supported";
349  return false; return true;
350 }
351 
352 bool RGBDSensorFromRosTopic::setFeature(int feature, double value1, double value2)
353 {
354  yCWarning(RGBD_ROS_TOPIC) << "setFeature not supported";
355  return false;
356 }
357 
358 bool RGBDSensorFromRosTopic::getFeature(int feature, double *value1, double *value2)
359 {
360  yCWarning(RGBD_ROS_TOPIC) << "getFeature not supported";
361  return false;
362 }
363 
364 bool RGBDSensorFromRosTopic::hasOnOff( int feature, bool *HasOnOff)
365 {
366  yCWarning(RGBD_ROS_TOPIC) << "hasOnOff not supported";
367  return false;
368 }
369 
370 bool RGBDSensorFromRosTopic::setActive( int feature, bool onoff)
371 {
372  yCWarning(RGBD_ROS_TOPIC) << "setActive not supported";
373  return false;
374 }
375 
376 bool RGBDSensorFromRosTopic::getActive( int feature, bool *isActive)
377 {
378  yCWarning(RGBD_ROS_TOPIC) << "getActive not supported";
379  return false;
380 }
381 
382 bool RGBDSensorFromRosTopic::hasAuto(int feature, bool *hasAuto)
383 {
384  yCWarning(RGBD_ROS_TOPIC) << "hasAuto not supported";
385  return false;
386 }
387 
388 bool RGBDSensorFromRosTopic::hasManual( int feature, bool* hasManual)
389 {
390  yCWarning(RGBD_ROS_TOPIC) << "hasManual not supported";
391  return false;
392 }
393 
394 bool RGBDSensorFromRosTopic::hasOnePush(int feature, bool* hasOnePush)
395 {
396  yCWarning(RGBD_ROS_TOPIC) << "hasOnePush not supported";
397  return false;
398 }
399 
400 bool RGBDSensorFromRosTopic::setMode(int feature, FeatureMode mode)
401 {
402  yCWarning(RGBD_ROS_TOPIC) << "setMode not supported";
403  return false;
404 }
405 
406 bool RGBDSensorFromRosTopic::getMode(int feature, FeatureMode* mode)
407 {
408  yCWarning(RGBD_ROS_TOPIC) << "getMode not supported";
409  *mode = MODE_UNKNOWN;
410  return false;
411 }
412 
413 bool RGBDSensorFromRosTopic::setOnePush(int feature)
414 {
415  yCWarning(RGBD_ROS_TOPIC) << "setOnePush not supported";
416  return false;
417 }
418 */
bool getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
bool setDepthClipPlanes(double nearPlane, double farPlane) override
Set the clipping planes of the sensor.
bool getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
bool setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
int getDepthHeight() override
Return the height of each frame.
bool setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
bool getDepthIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the depth camera.
double getDepthAccuracy() override
Get the minimum detectable variation in distance [meter].
bool getRgbImage(FlexImage &rgbImage, Stamp *timeStamp=nullptr) override
Get the rgb frame from the device.
bool getDepthClipPlanes(double &nearPlane, double &farPlane) override
Get the clipping planes of the sensor.
bool getRgbIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool getImages(FlexImage &colorFrame, depthImage &depthFrame, Stamp *colorStamp=nullptr, Stamp *depthStamp=nullptr) override
bool getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
bool setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
int getRgbHeight() override
Return the height of each frame.
int getRgbWidth() override
Return the width of each frame.
int getDepthWidth() override
Return the height of each frame.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
RGBDSensor_status getSensorStatus() override
Get the surrent status of the sensor, using enum type.
bool setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool getDepthImage(depthImage &depthImage, Stamp *timeStamp=nullptr) override
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
std::string getLastErrorMsg(Stamp *timeStamp=nullptr) override
Return an error message in case of error.
bool close() override
Close the DeviceDriver.
The Node class.
Definition: Node.h:24
A class for storing options and configuration information.
Definition: Property.h:34
void clear()
Remove all associations.
Definition: Property.cpp:1057
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
Image class with user control of representation details.
Definition: Image.h:414
Typed image class.
Definition: Image.h:658
A class for a Matrix.
Definition: Matrix.h:43
Provides:
Definition: Vector.h:119
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22
#define YARP_UNUSED(var)
Definition: api.h:162