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;
26
28
29
30
34
36{
37 parseParams(config);
38 m_topic_depth_camera_info = m_depth_topic_name.substr(0, m_depth_topic_name.rfind('/')) + "/camera_info";
39 m_topic_rgb_camera_info = m_color_topic_name.substr(0, m_color_topic_name.rfind('/')) + "/camera_info";
40
41 m_verbose = m_verbose_on == 1;
42
45 m_sub1->subscribe_to_topic(m_topic_rgb_camera_info);
46 m_sub1->subscribe_to_topic(m_topic_depth_camera_info);
50
51 m_spinner = new Ros2Spinner(m_node);
52 m_spinner->start();
53
54 yCInfo(RGBDSENSOR_NWC_ROS2) << "opened";
55
56 return true;
57}
58
60{
61 yCInfo(RGBDSENSOR_NWC_ROS2, "closing...");
62 delete m_sub1;
63 delete m_sub2;
64 delete m_spinner;
65 yCInfo(RGBDSENSOR_NWC_ROS2, "closed");
66 return true;
67}
68
69void RgbdSensor_nwc_ros2::callback(sensor_msgs::msg::Image::SharedPtr msg, std::string topic)
70{
71 if (topic == m_color_topic_name) {
73 } else if (topic == m_depth_topic_name) {
75 }
76}
77
78
79void RgbdSensor_nwc_ros2::callback(sensor_msgs::msg::CameraInfo::SharedPtr msg, std::string topic)
80{
81 if (topic == m_topic_rgb_camera_info) {
83 } else if (topic == m_topic_depth_camera_info)
84 {
86 }
87}
88
89void RgbdSensor_nwc_ros2::depth_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
90{
91 yCTrace(RGBDSENSOR_NWC_ROS2, "callback depth image");
92 std::lock_guard<std::mutex> depth_image_guard(m_depth_image_mutex);
94 m_depth_image_valid = true;
95}
96
97void RgbdSensor_nwc_ros2::depth_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
98{
99 std::lock_guard<std::mutex> depth_camera_info_guard(m_depth_camera_info_mutex);
100 yarp::dev::Ros2RGBDConversionUtils::updateStamp(msg, m_depth_image_frame, m_current_depth_stamp);
101 saveIntrinsics(msg, m_depth_params);
102 m_max_depth_height = msg->height;
103 m_max_depth_width = msg->width;
104 m_depth_stamp_valid = true;
105}
106
107void RgbdSensor_nwc_ros2::color_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
108{
109 yCTrace(RGBDSENSOR_NWC_ROS2, "callback color image");
110 std::lock_guard<std::mutex> rgb_image_guard(m_rgb_image_mutex);
112 m_rgb_image_valid = true;
113}
114
115void RgbdSensor_nwc_ros2::color_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
116{
117 std::lock_guard<std::mutex> rgb_camera_info_guard(m_rgb_camera_info_mutex);
118 yarp::dev::Ros2RGBDConversionUtils::updateStamp(msg, m_rgb_image_frame, m_current_rgb_stamp);
119 saveIntrinsics(msg, m_rgb_params);
120 m_max_rgb_height = msg->height;
121 m_max_rgb_width = msg->width;
122 m_rgb_stamp_valid = true;
123}
124
125// TODO FIXME eable distortion inverse brown conrady or check distortion in realsense
126void RgbdSensor_nwc_ros2::saveIntrinsics(sensor_msgs::msg::CameraInfo::SharedPtr msg, yarp::sig::IntrinsicParams& params)
127{
128 params.focalLengthX = msg->k[0];
129 params.focalLengthY = msg->k[4];
130 params.principalPointX = msg->k[2];
131 params.principalPointY = msg->k[5];
132 // distortion model
133 if (msg->distortion_model=="plumb_bob")
134 {
135 if (params.distortionModel.k1 == 0 &&
136 params.distortionModel.k2 == 0 &&
137 params.distortionModel.k3 == 0 &&
138 params.distortionModel.t1 == 0 &&
139 params.distortionModel.t2 == 0)
140 {
142 }
144 params.distortionModel.k1 = msg->d[0];
145 params.distortionModel.k2 = msg->d[1];
146 params.distortionModel.t1 = msg->d[2];
147 params.distortionModel.t2 = msg->d[3];
148 params.distortionModel.k3 = msg->d[4];
149 }
150 else
151 {
152 yCError(RGBDSENSOR_NWC_ROS2) << "Unsupported distortion model";
154 }
155}
156
157
158
160{
161 if (!m_rgb_image_valid) return 0;
162 return m_current_rgb_image.height();
163}
164
166{
167 if (!m_rgb_image_valid) return 0;
168 return m_current_rgb_image.width();
169}
170
172{
173 yCWarning(RGBDSENSOR_NWC_ROS2) << "getRgbSupportedConfigurations not implemented yet";
174 return false;
175}
176
177bool RgbdSensor_nwc_ros2::getRgbResolution(int &width, int &height)
178{
179 if (!m_rgb_image_valid)
180 {
181 width=0;
182 height=0;
183 return false;
184 }
185 width = m_current_rgb_image.width();
186 height = m_current_rgb_image.height();
187 return true;
188}
189
191{
192 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthResolution not supported";
193 return false;
194}
195
196bool RgbdSensor_nwc_ros2::setRgbResolution(int width, int height)
197{
198 yCWarning(RGBDSENSOR_NWC_ROS2) << "setRgbResolution not supported";
199 return false;
200}
201
202bool RgbdSensor_nwc_ros2::setRgbFOV(double horizontalFov, double verticalFov)
203{
204 yCWarning(RGBDSENSOR_NWC_ROS2) << "setRgbFOV not supported";
205 return false;
206}
207
208bool RgbdSensor_nwc_ros2::setDepthFOV(double horizontalFov, double verticalFov)
209{
210 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthFOV not supported";
211 return false;
212}
213
215{
216 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthAccuracy not supported";
217 return false;
218}
219
220// not sure that it is correct, maybe I should save the full image dimension
221bool RgbdSensor_nwc_ros2::getRgbFOV(double &horizontalFov, double &verticalFov)
222{
223 if (!m_rgb_image_valid && ! m_rgb_stamp_valid)
224 {
225 horizontalFov=0;
226 verticalFov=0;
227 return false;
228 yCError(RGBDSENSOR_NWC_ROS2) << "get rgb IntrinsicParam missing information";
229 }
230 horizontalFov = 2 * atan(m_max_rgb_width / (2 * m_rgb_params.focalLengthX)) * 180.0 / M_PI;
231 verticalFov = 2 * atan(m_max_rgb_height / (2 * m_rgb_params.focalLengthY)) * 180.0 / M_PI;
232 return true;
233}
234
235
237{
238 yCWarning(RGBDSENSOR_NWC_ROS2) << "Mirroring not supported";
239 return false;
240}
241
243{
244 yCWarning(RGBDSENSOR_NWC_ROS2) << "Mirroring not supported";
245 return false;
246}
247
249{
250 if (m_rgb_stamp_valid)
251 {
252 intrinsic.clear();
253 m_rgb_params.toProperty(intrinsic);
254 return true;
255 }
256 return false;
257}
258
260{
261 if (!m_depth_image_valid) return 0;
262 return m_current_depth_image.height();
263}
264
266{
267 if (!m_depth_image_valid) return 0;
268 return m_current_depth_image.width();
269}
270
271bool RgbdSensor_nwc_ros2::getDepthFOV(double& horizontalFov, double& verticalFov)
272{
273 if (!m_depth_image_valid && !m_depth_stamp_valid)
274 {
275 horizontalFov=0;
276 verticalFov=0;
277 return false;
278 yCError(RGBDSENSOR_NWC_ROS2) << "get depth IntrinsicParam missing information";
279 }
280 horizontalFov = 2 * atan(m_max_rgb_width / (2 * m_depth_params.focalLengthX)) * 180.0 / M_PI;
281 verticalFov = 2 * atan(m_max_rgb_height / (2 * m_depth_params.focalLengthY)) * 180.0 / M_PI;
282 return true;
283}
284
286{
287 if(m_depth_stamp_valid)
288 {
289 intrinsic.clear();
290 m_depth_params.toProperty(intrinsic);
291 return true;
292 }
293 return false;
294}
295
297{
298 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthAccuracy not supported";
299 return 0;
300}
301
302bool RgbdSensor_nwc_ros2::getDepthClipPlanes(double& nearPlane, double& farPlane)
303{
304 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthClipPlanes not supported";
305 return false;
306}
307
308bool RgbdSensor_nwc_ros2::setDepthClipPlanes(double nearPlane, double farPlane)
309{
310 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthClipPlanes not supported";
311 return false;
312}
313
315{
316 yCWarning(RGBDSENSOR_NWC_ROS2) << "getDepthMirroring not supported";
317 return false;
318}
319
321{
322 yCWarning(RGBDSENSOR_NWC_ROS2) << "setDepthMirroring not supported";
323 return false;
324}
325
327{
328 yCWarning(RGBDSENSOR_NWC_ROS2) << "getExtrinsicParam not supported";
329 return false;
330}
331
332
333/*
334 * TODO FIXME to update with header instead of stamp
335 */
337{
338 std::lock_guard<std::mutex> guard_rgb_image(m_rgb_image_mutex);
339 std::lock_guard<std::mutex> guard_rgb_camera_info(m_rgb_camera_info_mutex);
340 bool rgb_ok = false;
341 if (m_rgb_image_valid)
342 {
343 if (m_rgb_stamp_valid)
344 {
345 if (rgb_image_stamp == nullptr)
346 {
347 yarp::os::Stamp stamp(m_current_rgb_stamp.getCount(),m_current_rgb_stamp.getTime());
348 rgb_image_stamp = &stamp;
349 } else {
350 rgb_image_stamp->update(m_current_rgb_stamp.getTime());
351 }
352 yarp::dev::Ros2RGBDConversionUtils::deepCopyFlexImage(m_current_rgb_image, rgb_image);
353 rgb_ok = true;
354 }
355 else
356 {
357 yCError(RGBDSENSOR_NWC_ROS2, "missing rgb camera info");
358 }
359 }
360 else
361 {
362 yCError(RGBDSENSOR_NWC_ROS2, "missing rgb image");
363 }
364 return rgb_ok;
365}
366
367/*
368 * TODO FIXME to update with header instead of stamp
369 */
371{
372 std::lock_guard<std::mutex> guard_depth_image(m_depth_image_mutex);
373 std::lock_guard<std::mutex> guard_depth_camera_info(m_depth_camera_info_mutex);
374 bool depth_ok =false;
375
376 if (m_depth_image_valid)
377 {
378 if (m_depth_stamp_valid)
379 {
380 if (depth_image_stamp == nullptr)
381 {
382 yarp::os::Stamp stamp(m_current_depth_stamp.getCount(),m_current_depth_stamp.getTime());
383 depth_image_stamp = &stamp;
384 } else {
385 depth_image_stamp->update(m_current_depth_stamp.getTime());
386 }
387 yarp::dev::Ros2RGBDConversionUtils::deepCopyImageOf(m_current_depth_image, depth_image);
388 depth_ok = true;
389 }
390 else
391 {
392 yCError(RGBDSENSOR_NWC_ROS2, "missing depth camera info");
393 }
394 }
395 else
396 {
397 yCError(RGBDSENSOR_NWC_ROS2, "missing depth image");
398 }
399 return depth_ok;
400}
401/*
402 * TODO FIXME to update with header instead of stamp
403 */
404bool RgbdSensor_nwc_ros2::getImages(yarp::sig::FlexImage& rgb_image, depthImage& depth_image, yarp::os::Stamp* rgb_image_stamp, yarp::os::Stamp* depth_image_stamp)
405{
406 bool rgb_ok, depth_ok;
407 rgb_ok = getRgbImage(rgb_image, rgb_image_stamp);
408 depth_ok = getDepthImage(depth_image, depth_image_stamp);
409 return (rgb_ok && depth_ok);
410}
411
412
417
418
420{
421 yCError(RGBDSENSOR_NWC_ROS2) << "get last error not yet implemented";
422 return "get last error not yet implemented";
423}
#define M_PI
const yarp::os::LogComponent & RGBDSENSOR_NWC_ROS2()
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
int getDepthWidth() override
Return the height of each frame.
void color_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
std::string getLastErrorMsg(yarp::os::Stamp *timeStamp=NULL) override
Return an error message in case of error.
double getDepthAccuracy() override
Get the minimum detectable variation in distance [meter].
void depth_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
bool getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
bool close() override
Close the DeviceDriver.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
bool setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
bool getDepthImage(depthImage &depth_image, yarp::os::Stamp *depth_image_stamp=nullptr) override
bool getRgbImage(yarp::sig::FlexImage &rgb_image, yarp::os::Stamp *rgb_image_stamp=NULL) override
Get the rgb frame from the device.
bool getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
int getRgbWidth() override
Return the width of each frame.
int getRgbHeight() override
Return the height of each frame.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void depth_info_callback(const sensor_msgs::msg::CameraInfo::SharedPtr msg)
bool getDepthClipPlanes(double &nearPlane, double &farPlane) override
Get the clipping planes of the sensor.
yarp::dev::IRGBDSensor::RGBDSensor_status getSensorStatus() override
Get the surrent status of the sensor, using enum type.
void color_raw_callback(const sensor_msgs::msg::Image::SharedPtr msg)
bool setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
bool setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool getDepthIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the depth camera.
bool 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 getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
void callback(sensor_msgs::msg::CameraInfo::SharedPtr msg, std::string topic)
bool setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
bool setDepthClipPlanes(double nearPlane, double farPlane) override
Set the clipping planes of the sensor.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb 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:33
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:21
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
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:363
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
A class for a Matrix.
Definition Matrix.h:39
#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)
@ YARP_UNSUPPORTED
Unsupported distortion model.
@ YARP_PLUMB_BOB
Plumb bob distortion model.
@ YARP_DISTORTION_NONE
Rectilinear images.
constexpr char accuracy[]
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).
double focalLengthY
Result of the product of the physical focal length(mm) and the size sy of the individual imager eleme...
DistortionModel distortionModel
Distortion model of the image.
double focalLengthX
Result of the product of the physical focal length(mm) and the size sx of the individual imager eleme...
void toProperty(yarp::os::Property &intrinsic) const
toProperty, convert the struct to a Property.
double principalPointX
Horizontal coordinate of the principal point of the image, as a pixel offset from the left edge.
double principalPointY
Vertical coordinate of the principal point of the image, as a pixel offset from the top edge.