YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RgbdSensor_nwc_ros2.cpp
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 _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
8#endif
9
10#include "RgbdSensor_nwc_ros2.h"
11
12#include <chrono>
13#include <functional>
14#include <memory>
15#include <string>
16#include <cmath>
17
19#include <yarp/os/LogStream.h>
20
21#include <Ros2Utils.h>
23
24using namespace std::chrono_literals;
25using namespace std::placeholders;
26using namespace yarp::dev;
27
29
30
31
35
37{
38 parseParams(config);
39 m_topic_depth_camera_info = m_depth_topic_name.substr(0, m_depth_topic_name.rfind('/')) + "/camera_info";
40 m_topic_rgb_camera_info = m_color_topic_name.substr(0, m_color_topic_name.rfind('/')) + "/camera_info";
41
42 m_verbose = m_verbose_on == 1;
43
44 if(m_namespace.empty())
45 {
47 }
48 else
49 {
51 }
52 if(!m_node)
53 {
54 yCError(RGBDSENSOR_NWC_ROS2) << "Cannot create node";
55 return false;
56 }
58 m_sub1->subscribe_to_topic(m_topic_rgb_camera_info);
59 m_sub1->subscribe_to_topic(m_topic_depth_camera_info);
63
64 m_spinner = new Ros2Spinner(m_node);
65 m_spinner->start();
66
67 yCInfo(RGBDSENSOR_NWC_ROS2) << "opened";
68
69 return true;
70}
71
73{
74 yCInfo(RGBDSENSOR_NWC_ROS2, "closing...");
75 delete m_sub1;
76 delete m_sub2;
77 delete m_spinner;
78 yCInfo(RGBDSENSOR_NWC_ROS2, "closed");
79 return true;
80}
81
82void RgbdSensor_nwc_ros2::callback(sensor_msgs::msg::Image::SharedPtr msg, std::string topic)
83{
84 if (topic == m_color_topic_name) {
86 } else if (topic == m_depth_topic_name) {
88 }
89}
90
91
92void RgbdSensor_nwc_ros2::callback(sensor_msgs::msg::CameraInfo::SharedPtr msg, std::string topic)
93{
94 if (topic == m_topic_rgb_camera_info) {
96 } else if (topic == m_topic_depth_camera_info)
97 {
99 }
100}
101
102void RgbdSensor_nwc_ros2::depth_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
103{
104 yCTrace(RGBDSENSOR_NWC_ROS2, "callback depth image");
105 std::lock_guard<std::mutex> depth_image_guard(m_depth_image_mutex);
107 m_depth_image_valid = true;
108}
109
110void RgbdSensor_nwc_ros2::depth_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
111{
112 std::lock_guard<std::mutex> depth_camera_info_guard(m_depth_camera_info_mutex);
113 yarp::dev::Ros2RGBDConversionUtils::updateStamp(msg, m_depth_image_frame, m_current_depth_stamp);
114 saveIntrinsics(msg, m_depth_params);
115 m_max_depth_height = msg->height;
116 m_max_depth_width = msg->width;
117 m_depth_stamp_valid = true;
118}
119
120void RgbdSensor_nwc_ros2::color_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
121{
122 yCTrace(RGBDSENSOR_NWC_ROS2, "callback color image");
123 std::lock_guard<std::mutex> rgb_image_guard(m_rgb_image_mutex);
125 m_rgb_image_valid = true;
126}
127
128void RgbdSensor_nwc_ros2::color_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
129{
130 std::lock_guard<std::mutex> rgb_camera_info_guard(m_rgb_camera_info_mutex);
131 yarp::dev::Ros2RGBDConversionUtils::updateStamp(msg, m_rgb_image_frame, m_current_rgb_stamp);
132 saveIntrinsics(msg, m_rgb_params);
133 m_max_rgb_height = msg->height;
134 m_max_rgb_width = msg->width;
135 m_rgb_stamp_valid = true;
136}
137
138// TODO FIXME eable distortion inverse brown conrady or check distortion in realsense
139void RgbdSensor_nwc_ros2::saveIntrinsics(sensor_msgs::msg::CameraInfo::SharedPtr msg, yarp::sig::IntrinsicParams& params)
140{
141 params.focalLengthX = msg->k[0];
142 params.focalLengthY = msg->k[4];
143 params.principalPointX = msg->k[2];
144 params.principalPointY = msg->k[5];
145 // distortion model
146 if (msg->distortion_model=="plumb_bob")
147 {
148 if (params.distortionModel.k1 == 0 &&
149 params.distortionModel.k2 == 0 &&
150 params.distortionModel.k3 == 0 &&
151 params.distortionModel.t1 == 0 &&
152 params.distortionModel.t2 == 0)
153 {
154 params.distortionModel.type = yarp::sig::YarpDistortion::YARP_DISTORTION_NONE;
155 }
156 params.distortionModel.type = yarp::sig::YarpDistortion::YARP_PLUMB_BOB;
157 params.distortionModel.k1 = msg->d[0];
158 params.distortionModel.k2 = msg->d[1];
159 params.distortionModel.t1 = msg->d[2];
160 params.distortionModel.t2 = msg->d[3];
161 params.distortionModel.k3 = msg->d[4];
162 }
163 else
164 {
165 yCError(RGBDSENSOR_NWC_ROS2) << "Unsupported distortion model";
166 params.distortionModel.type = yarp::sig::YarpDistortion::YARP_UNSUPPORTED;
167 }
168}
169
170
171
173{
174 if (!m_rgb_image_valid) return 0;
175 return m_current_rgb_image.height();
176}
177
179{
180 if (!m_rgb_image_valid) return 0;
181 return m_current_rgb_image.width();
182}
183
184yarp::dev::ReturnValue RgbdSensor_nwc_ros2::getRgbSupportedConfigurations(std::vector<yarp::dev::CameraConfig> &configurations)
185{
186 yCWarning(RGBDSENSOR_NWC_ROS2) << "getRgbSupportedConfigurations not implemented yet";
187 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
188}
189
191{
192 if (!m_rgb_image_valid)
193 {
194 width=0;
195 height=0;
196 return ReturnValue::return_code::return_value_error_not_ready;
197 }
198 width = m_current_rgb_image.width();
199 height = m_current_rgb_image.height();
200 return ReturnValue_ok;
201}
202
204{
205 if (!m_rgb_image_valid)
206 {
207 width=0;
208 height=0;
209 return ReturnValue::return_code::return_value_error_not_ready;
210 }
211 width = m_current_depth_image.width();
212 height = m_current_depth_image.height();
213 return ReturnValue_ok;
214}
215
217{
218 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthResolution not supported";
219 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
220}
221
223{
224 yCWarning(RGBDSENSOR_NWC_ROS2) << "setRgbResolution not supported";
225 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
226}
227
228yarp::dev::ReturnValue RgbdSensor_nwc_ros2::setRgbFOV(double horizontalFov, double verticalFov)
229{
230 yCWarning(RGBDSENSOR_NWC_ROS2) << "setRgbFOV not supported";
231 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
232}
233
234yarp::dev::ReturnValue RgbdSensor_nwc_ros2::setDepthFOV(double horizontalFov, double verticalFov)
235{
236 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthFOV not supported";
237 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
238}
239
241{
242 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthAccuracy not supported";
243 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
244}
245
246// not sure that it is correct, maybe I should save the full image dimension
247yarp::dev::ReturnValue RgbdSensor_nwc_ros2::getRgbFOV(double &horizontalFov, double &verticalFov)
248{
249 if (!m_rgb_image_valid && ! m_rgb_stamp_valid)
250 {
251 horizontalFov=0;
252 verticalFov=0;
253 return ReturnValue::return_code::return_value_error_not_ready;
254 yCError(RGBDSENSOR_NWC_ROS2) << "get rgb IntrinsicParam missing information";
255 }
256 horizontalFov = 2 * atan(m_max_rgb_width / (2 * m_rgb_params.focalLengthX)) * 180.0 / M_PI;
257 verticalFov = 2 * atan(m_max_rgb_height / (2 * m_rgb_params.focalLengthY)) * 180.0 / M_PI;
258 return ReturnValue_ok;
259}
260
261
263{
264 yCWarning(RGBDSENSOR_NWC_ROS2) << "Mirroring not supported";
265 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
266}
267
269{
270 yCWarning(RGBDSENSOR_NWC_ROS2) << "Mirroring not supported";
271 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
272}
273
275{
276 if (m_rgb_stamp_valid)
277 {
278 intrinsic.clear();
279 m_rgb_params.toProperty(intrinsic);
280 return ReturnValue_ok;
281 }
282 return ReturnValue::return_code::return_value_error_not_ready;
283}
284
286{
287 if (!m_depth_image_valid) return 0;
288 return m_current_depth_image.height();
289}
290
292{
293 if (!m_depth_image_valid) return 0;
294 return m_current_depth_image.width();
295}
296
297yarp::dev::ReturnValue RgbdSensor_nwc_ros2::getDepthFOV(double& horizontalFov, double& verticalFov)
298{
299 if (!m_depth_image_valid && !m_depth_stamp_valid)
300 {
301 horizontalFov=0;
302 verticalFov=0;
303 return ReturnValue::return_code::return_value_error_not_ready;
304 yCError(RGBDSENSOR_NWC_ROS2) << "get depth IntrinsicParam missing information";
305 }
306 horizontalFov = 2 * atan(m_max_rgb_width / (2 * m_depth_params.focalLengthX)) * 180.0 / M_PI;
307 verticalFov = 2 * atan(m_max_rgb_height / (2 * m_depth_params.focalLengthY)) * 180.0 / M_PI;
308 return ReturnValue_ok;
309}
310
312{
313 if(m_depth_stamp_valid)
314 {
315 intrinsic.clear();
316 m_depth_params.toProperty(intrinsic);
317 return ReturnValue_ok;
318 }
319 return ReturnValue::return_code::return_value_error_not_ready;
320}
321
323{
324 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthAccuracy not supported";
325 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
326}
327
329{
330 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthClipPlanes not supported";
331 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
332}
333
335{
336 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthClipPlanes not supported";
337 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
338}
339
341{
342 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthMirroring not supported";
343 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
344}
345
347{
348 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthMirroring not supported";
349 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
350}
351
353{
354 yCWarning(RGBDSENSOR_NWC_ROS2) << "getExtrinsicParam not supported";
355 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
356}
357
358
359/*
360 * TODO FIXME to update with header instead of stamp
361 */
363{
364 std::lock_guard<std::mutex> guard_rgb_image(m_rgb_image_mutex);
365 std::lock_guard<std::mutex> guard_rgb_camera_info(m_rgb_camera_info_mutex);
366 bool rgb_ok = false;
367 if (m_rgb_image_valid)
368 {
369 if (m_rgb_stamp_valid)
370 {
371 if (rgb_image_stamp == nullptr)
372 {
373 yarp::os::Stamp stamp(m_current_rgb_stamp.getCount(),m_current_rgb_stamp.getTime());
374 rgb_image_stamp = &stamp;
375 } else {
376 rgb_image_stamp->update(m_current_rgb_stamp.getTime());
377 }
378 yarp::dev::Ros2RGBDConversionUtils::deepCopyFlexImage(m_current_rgb_image, rgb_image);
379 rgb_ok = true;
380 }
381 else
382 {
383 yCError(RGBDSENSOR_NWC_ROS2, "missing rgb camera info");
384 }
385 }
386 else
387 {
388 yCError(RGBDSENSOR_NWC_ROS2, "missing rgb image");
389 }
390 if (rgb_ok) return ReturnValue_ok;
391 return ReturnValue::return_code::return_value_error_not_ready;
392}
393
394/*
395 * TODO FIXME to update with header instead of stamp
396 */
398{
399 std::lock_guard<std::mutex> guard_depth_image(m_depth_image_mutex);
400 std::lock_guard<std::mutex> guard_depth_camera_info(m_depth_camera_info_mutex);
401 bool depth_ok =false;
402
403 if (m_depth_image_valid)
404 {
405 if (m_depth_stamp_valid)
406 {
407 if (depth_image_stamp == nullptr)
408 {
409 yarp::os::Stamp stamp(m_current_depth_stamp.getCount(),m_current_depth_stamp.getTime());
410 depth_image_stamp = &stamp;
411 } else {
412 depth_image_stamp->update(m_current_depth_stamp.getTime());
413 }
414 yarp::dev::Ros2RGBDConversionUtils::deepCopyImageOf(m_current_depth_image, depth_image);
415 depth_ok = true;
416 }
417 else
418 {
419 yCError(RGBDSENSOR_NWC_ROS2, "missing depth camera info");
420 }
421 }
422 else
423 {
424 yCError(RGBDSENSOR_NWC_ROS2, "missing depth image");
425 }
426
427 if (depth_ok) return ReturnValue_ok;
428 return ReturnValue::return_code::return_value_error_not_ready;
429}
430/*
431 * TODO FIXME to update with header instead of stamp
432 */
434{
435 bool rgb_ok, depth_ok;
436 rgb_ok = getRgbImage(rgb_image, rgb_image_stamp);
437 depth_ok = getDepthImage(depth_image, depth_image_stamp);
438
439 if (rgb_ok && depth_ok) return ReturnValue_ok;
440 return ReturnValue::return_code::return_value_error_not_ready;
441}
442
443
449
450
452{
453 yCError(RGBDSENSOR_NWC_ROS2) << "get last error not yet implemented";
454 message = "get last error not yet implemented";
455 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
456}
#define ReturnValue_ok
Definition ReturnValue.h:82
const yarp::os::LogComponent & RGBDSENSOR_NWC_ROS2()
#define M_PI
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
int getDepthWidth() override
Return the height of each frame.
yarp::dev::ReturnValue getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
yarp::dev::ReturnValue getRgbSupportedConfigurations(std::vector< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
yarp::dev::ReturnValue getDepthClipPlanes(double &nearPlane, double &farPlane) override
Get the clipping planes of the sensor.
void color_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
yarp::dev::ReturnValue setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
void depth_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
yarp::dev::ReturnValue getDepthAccuracy(double &accuracy) override
Get the minimum detectable variation in distance [meter].
yarp::dev::ReturnValue getDepthImage(depthImage &depth_image, yarp::os::Stamp *depth_image_stamp=nullptr) override
yarp::dev::ReturnValue getImages(yarp::sig::FlexImage &rgb_image, depthImage &depth_image, yarp::os::Stamp *rgb_image_stamp=nullptr, yarp::os::Stamp *depth_image_stamp=nullptr) override
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getLastErrorMsg(std::string &message, yarp::os::Stamp *timeStamp=NULL) override
Return an error message in case of error.
yarp::dev::ReturnValue getDepthIntrinsicParam(yarp::os::Property &intrinsic) override
yarp::dev::ReturnValue getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
yarp::dev::ReturnValue getSensorStatus(yarp::dev::IRGBDSensor::RGBDSensor_status &status) override
Get the current status of the sensor, using enum type.
yarp::dev::ReturnValue getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
yarp::dev::ReturnValue getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
yarp::dev::ReturnValue getRgbImage(yarp::sig::FlexImage &rgb_image, yarp::os::Stamp *rgb_image_stamp=NULL) override
Get the rgb frame from the device.
int getRgbWidth() override
Return the width of each frame.
int getRgbHeight() override
Return the height of each frame.
yarp::dev::ReturnValue getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
yarp::dev::ReturnValue setDepthClipPlanes(double nearPlane, double farPlane) override
Set the clipping planes of the sensor.
void depth_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
yarp::dev::ReturnValue setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
void color_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
yarp::dev::ReturnValue getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
yarp::dev::ReturnValue setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
yarp::dev::ReturnValue setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
yarp::dev::ReturnValue setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
void callback(sensor_msgs::msg::CameraInfo::SharedPtr msg, std::string topic)
yarp::dev::ReturnValue setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
yarp::dev::ReturnValue getDepthResolution(int &width, int &height) override
Get the resolution of the depth image from the camera.
int getDepthHeight() override
Return the height of each frame.
void subscribe_to_topic(std::string topic_name)
@ TraceType
Definition Log.h:92
A class for storing options and configuration information.
Definition Property.h:32
void clear()
Remove all associations.
A base class for nested structures that can be searched.
Definition Searchable.h:31
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:18
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:49
double getTime() const
Get the time stamp.
Definition Stamp.cpp:34
int getCount() const
Get the sequence number.
Definition Stamp.cpp:29
bool start()
Start the new thread running.
Definition Thread.cpp:93
Image class with user control of representation details.
Definition Image.h:361
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
yarp::sig::DistortionModelData distortionModel
A class for a Matrix.
Definition Matrix.h:40
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
void deepCopyImageOf(const DepthImage &src, DepthImage &dest)
void updateStamp(sensor_msgs::msg::CameraInfo::SharedPtr ros_camera_info_src, std::string &frame_id_dest, yarp::os::Stamp &yarp_stamp)
void convertDepthImageRos2ToYarpImageOf(sensor_msgs::msg::Image::SharedPtr ros_image_src, yarp::sig::ImageOf< yarp::sig::PixelFloat > &dest)
void convertRGBImageRos2ToYarpFlexImage(sensor_msgs::msg::Image::SharedPtr ros_image_src, yarp::sig::FlexImage &dest)
void deepCopyFlexImage(const yarp::sig::FlexImage &src, yarp::sig::FlexImage &dest)
For streams capable of holding different kinds of content, check what they actually have.
constexpr char accuracy[]
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).