16#include <sensor_msgs/image_encodings.hpp>
18using namespace std::chrono_literals;
41 return sensor_msgs::image_encodings::BGR8;
43 return sensor_msgs::image_encodings::BGRA8;
45 return sensor_msgs::image_encodings::BAYER_BGGR16;
47 return sensor_msgs::image_encodings::BAYER_BGGR8;
49 return sensor_msgs::image_encodings::BAYER_GBRG16;
51 return sensor_msgs::image_encodings::BAYER_GBRG8;
53 return sensor_msgs::image_encodings::BAYER_GRBG16;
55 return sensor_msgs::image_encodings::BAYER_GRBG8;
57 return sensor_msgs::image_encodings::BAYER_RGGB16;
59 return sensor_msgs::image_encodings::BAYER_RGGB8;
61 return sensor_msgs::image_encodings::MONO8;
63 return sensor_msgs::image_encodings::MONO16;
65 return sensor_msgs::image_encodings::RGB8;
67 return sensor_msgs::image_encodings::RGBA8;
69 return sensor_msgs::image_encodings::TYPE_32FC1;
71 return sensor_msgs::image_encodings::RGB8;
87 yCDebug(RGBDSENSOR_NWS_ROS2) <<
"Parameters are: " << config.
toString();
89 if(!fromConfig(config)) {
90 yCError(RGBDSENSOR_NWS_ROS2) <<
"Failed to open, check previous log for error messages.";
94 if(!initialize_ROS2(config)) {
95 yCError(RGBDSENSOR_NWS_ROS2) <<
"Error initializing ROS topic";
108 yCError(RGBDSENSOR_NWS_ROS2) <<
"node_name cannot begin with an initial /";
113 yCError(RGBDSENSOR_NWS_ROS2) <<
"depth_topic_name must begin with an initial /";
119 yCError(RGBDSENSOR_NWS_ROS2) <<
"color_topic_name must begin with an initial /";
133 rosPublisher_color = m_node->create_publisher<sensor_msgs::msg::Image>(
m_color_topic_name, 10);
134 rosPublisher_depth = m_node->create_publisher<sensor_msgs::msg::Image>(
m_depth_topic_name, 10);
135 rosPublisher_colorCaminfo = m_node->create_publisher<sensor_msgs::msg::CameraInfo>(m_color_info_topic_name, 10);
136 rosPublisher_depthCaminfo = m_node->create_publisher<sensor_msgs::msg::CameraInfo>(m_depth_info_topic_name, 10);
143 yCTrace(RGBDSENSOR_NWS_ROS2,
"Close");
156 if (sensor_p!=
nullptr) {
161 yCError(RGBDSENSOR_NWS_ROS2,
"Image not captured.. check hardware configuration");
168 yCInfo(RGBDSENSOR_NWS_ROS2) <<
"Device not ready, waiting...";
171 yCWarning(RGBDSENSOR_NWS_ROS2) <<
"Device is taking too long to start..";
176 yCError(RGBDSENSOR_NWS_ROS2,
"Sensor returned error");
179 yCError(RGBDSENSOR_NWS_ROS2,
"Sensor interface is not valid");
192 poly->
view(sensor_p);
196 if(sensor_p ==
nullptr)
198 yCError(RGBDSENSOR_NWS_ROS2) <<
"Attached device has no valid IRGBDSensor interface.";
202 if(fgCtrl ==
nullptr)
204 yCWarning(RGBDSENSOR_NWS_ROS2) <<
"Attached device has no valid IFrameGrabberControls interface.";
207 return PeriodicThread::start();
224bool RgbdSensor_nws_ros2::setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo,
225 const std::string& frame_id,
227 const SensorType& sensorType)
240 std::string distModel;
241 std::string currentSensor;
243 std::vector<param<double> > parVector;
246 currentSensor = sensorType == COLOR_SENSOR ?
"Rgb" :
"Depth";
251 yCError(RGBDSENSOR_NWS_ROS2) <<
"Unable to get intrinsic param from" << currentSensor <<
"sensor!";
255 if(!camData.
check(
"distortionModel"))
257 yCWarning(RGBDSENSOR_NWS_ROS2) <<
"Missing distortion model";
262 if (distModel !=
"plumb_bob")
264 yCError(RGBDSENSOR_NWS_ROS2) <<
"Distortion model not supported";
268 parVector.emplace_back(phyF,
"physFocalLength");
269 parVector.emplace_back(fx,
"focalLengthX");
270 parVector.emplace_back(fy,
"focalLengthY");
271 parVector.emplace_back(cx,
"principalPointX");
272 parVector.emplace_back(cy,
"principalPointY");
273 parVector.emplace_back(k1,
"k1");
274 parVector.emplace_back(k2,
"k2");
275 parVector.emplace_back(t1,
"t1");
276 parVector.emplace_back(t2,
"t2");
277 parVector.emplace_back(k3,
"k3");
279 for(
auto& par : parVector) {
280 if(!camData.
check(par.parname)) {
281 yCWarning(RGBDSENSOR_NWS_ROS2) <<
"Driver has not the param:" << par.parname;
287 cameraInfo.header.frame_id = frame_id;
288 cameraInfo.header.stamp.sec =
static_cast<int>(stamp.
getTime());
289 cameraInfo.header.stamp.nanosec =
static_cast<int>(1000000000UL * (stamp.
getTime() - int(stamp.
getTime())));
292 cameraInfo.distortion_model = distModel;
294 cameraInfo.d.resize(5);
295 cameraInfo.d[0] = k1;
296 cameraInfo.d[1] = k2;
297 cameraInfo.d[2] = t1;
298 cameraInfo.d[3] = t2;
299 cameraInfo.d[4] = k3;
301 cameraInfo.k[0] = fx; cameraInfo.k[1] = 0; cameraInfo.k[2] = cx;
302 cameraInfo.k[3] = 0; cameraInfo.k[4] = fy; cameraInfo.k[5] = cy;
303 cameraInfo.k[6] = 0; cameraInfo.k[7] = 0; cameraInfo.k[8] = 1;
314 cameraInfo.r[0] = 1; cameraInfo.r[1] = 0; cameraInfo.r[2] = 0;
315 cameraInfo.r[3] = 0; cameraInfo.r[4] = 1; cameraInfo.r[5] = 0;
316 cameraInfo.r[6] = 0; cameraInfo.r[7] = 0; cameraInfo.r[8] = 1;
318 cameraInfo.p[0] = fx; cameraInfo.p[1] = 0; cameraInfo.p[2] = cx; cameraInfo.p[3] = 0;
319 cameraInfo.p[4] = 0; cameraInfo.p[5] = fy; cameraInfo.p[6] = cy; cameraInfo.p[7] = 0;
320 cameraInfo.p[8] = 0; cameraInfo.p[9] = 0; cameraInfo.p[10] = 1; cameraInfo.p[11] = 0;
322 cameraInfo.binning_x = cameraInfo.binning_y = 0;
323 cameraInfo.roi.height = cameraInfo.roi.width = cameraInfo.roi.x_offset = cameraInfo.roi.y_offset = 0;
324 cameraInfo.roi.do_rectify =
false;
329bool RgbdSensor_nws_ros2::writeData()
342 bool rgb_data_ok =
true;
343 bool depth_data_ok =
true;
345 if (((colorStamp.
getTime() - oldColorStamp.
getTime()) > 0) ==
false) {
349 oldColorStamp = colorStamp;
352 if (((depthStamp.
getTime() - oldDepthStamp.
getTime()) > 0) ==
false) {
356 oldDepthStamp = depthStamp;
361 sensor_msgs::msg::Image rColorImage;
363 rColorImage.width = colorImage.
width();
364 rColorImage.height = colorImage.
height();
369 rColorImage.header.stamp.sec =
static_cast<int>(colorStamp.
getTime());
370 rColorImage.header.stamp.nanosec =
static_cast<int>(1000000000UL * (colorStamp.
getTime() - int(colorStamp.
getTime())));
371 rColorImage.is_bigendian = 0;
373 rosPublisher_color->publish(rColorImage);
375 sensor_msgs::msg::CameraInfo camInfoC;
377 if(m_forceInfoSync) {
378 camInfoC.header.stamp = rColorImage.header.stamp;
380 rosPublisher_colorCaminfo->publish(camInfoC);
382 yCWarning(RGBDSENSOR_NWS_ROS2,
"Missing color camera parameters... camera info messages will be not sent");
388 sensor_msgs::msg::Image rDepthImage;
396 rDepthImage.header.stamp.sec =
static_cast<int>(depthStamp.
getTime());
397 rDepthImage.header.stamp.nanosec =
static_cast<int>(1000000000UL * (depthStamp.
getTime() - int(depthStamp.
getTime())));
398 rDepthImage.is_bigendian = 0;
400 rosPublisher_depth->publish(rDepthImage);
402 sensor_msgs::msg::CameraInfo camInfoD;
404 if(m_forceInfoSync) {
405 camInfoD.header.stamp = rDepthImage.header.stamp;
407 rosPublisher_depthCaminfo->publish(camInfoD);
409 yCWarning(RGBDSENSOR_NWS_ROS2,
"Missing depth camera parameters... camera info messages will be not sent");
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR16
@ VOCAB_PIXEL_ENCODING_BAYER_BGGR8
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB8
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG8
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG16
@ VOCAB_PIXEL_ENCODING_BAYER_GRBG16
@ VOCAB_PIXEL_ENCODING_BAYER_GBRG8
@ VOCAB_PIXEL_ENCODING_BAYER_RGGB16
yarp::sig::ImageOf< yarp::sig::PixelFloat > depthImage
static rclcpp::Node::SharedPtr createNode(std::string name)
std::string m_depth_topic_name
std::string m_color_topic_name
std::string m_color_frame_id
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
std::string m_depth_frame_id
bool detach() override
Detach the object (you must have first called attach).
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
bool view(T *&x)
Get an interface to the device driver.
int getRgbWidth() override=0
Return the width of each frame.
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override=0
Get the intrinsic parameters of the rgb camera.
int getDepthWidth() override=0
Return the height of each frame.
int getRgbHeight() override=0
Return the height of each frame.
virtual bool getImages(yarp::sig::FlexImage &colorFrame, yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthFrame, yarp::os::Stamp *colorStamp=nullptr, yarp::os::Stamp *depthStamp=nullptr)=0
Get the both the color and depth frame in a single call.
virtual RGBDSensor_status getSensorStatus()=0
Get the surrent status of the sensor, using enum type.
int getDepthHeight() override=0
Return the height of each frame.
bool getDepthIntrinsicParam(yarp::os::Property &intrinsic) override=0
Get the intrinsic parameters of the depth camera.
A container for a device driver.
bool isRunning() const
Returns true when the thread is started, false otherwise.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
A base class for nested structures that can be searched.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
An abstraction for a time stamp and/or sequence number.
double getTime() const
Get the time stamp.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::string asString() const
Get string value.
Image class with user control of representation details.
int getPixelCode() const override
Gets pixel type identifier.
size_t width() const
Gets width of image in pixels.
size_t getRowSize() const
Size of the underlying image buffer rows.
unsigned char * getRawImage() const
Access to the internal image buffer.
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
size_t height() const
Gets height of image in pixels.
virtual int getPixelCode() const
Gets pixel type identifier.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
const std::string depthInfoTopicName_param
const std::string depthTopicName_param
const std::string colorTopicName_param
const std::string nodeName_param
const std::string frameId_param
const std::string colorInfoTopicName_param
std::string yarp2RosPixelCode(int code)
The main, catch-all namespace for YARP.