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