12#include <sensor_msgs/image_encodings.hpp>
19std::
string yarp2RosPixelCode(
int code)
24 return sensor_msgs::image_encodings::BGR8;
26 return sensor_msgs::image_encodings::BGRA8;
28 return sensor_msgs::image_encodings::BAYER_BGGR16;
30 return sensor_msgs::image_encodings::BAYER_BGGR8;
32 return sensor_msgs::image_encodings::BAYER_GBRG16;
34 return sensor_msgs::image_encodings::BAYER_GBRG8;
36 return sensor_msgs::image_encodings::BAYER_GRBG16;
38 return sensor_msgs::image_encodings::BAYER_GRBG8;
40 return sensor_msgs::image_encodings::BAYER_RGGB16;
42 return sensor_msgs::image_encodings::BAYER_RGGB8;
44 return sensor_msgs::image_encodings::MONO8;
46 return sensor_msgs::image_encodings::MONO16;
48 return sensor_msgs::image_encodings::RGB8;
50 return sensor_msgs::image_encodings::RGBA8;
52 return sensor_msgs::image_encodings::TYPE_32FC1;
54 return sensor_msgs::image_encodings::RGB8;
60 PeriodicThread(s_default_period)
87 yCError(FRAMEGRABBER_NWS_ROS2,
"Device is already opened");
93 publisher_image = m_node->create_publisher<sensor_msgs::msg::Image>(
m_topic_name, 10);
98 publisher_cameraInfo = m_node->create_publisher<sensor_msgs::msg::CameraInfo>(cameraInfoTopicName, 10);
100 yCInfo(FRAMEGRABBER_NWS_ROS2) <<
"Running, waiting for attach...";
110 yCError(FRAMEGRABBER_NWS_ROS2) <<
"Device " << poly <<
" to attach to is not valid ... cannot proceed";
114 PeriodicThread::setPeriod(m_period);
116 poly->
view(iRgbVisualParams);
117 poly->
view(iFrameGrabberImage);
118 poly->
view(iPreciselyTimed);
120 if (iFrameGrabberImage ==
nullptr) {
121 yCError(FRAMEGRABBER_NWS_ROS2) <<
"IFrameGrabberImage interface is not available on the device";
125 if (iRgbVisualParams ==
nullptr) {
126 yCWarning(FRAMEGRABBER_NWS_ROS2) <<
"IRgbVisualParams interface is not available on the device";
129 return PeriodicThread::start();
139 iRgbVisualParams =
nullptr;
140 iFrameGrabberImage =
nullptr;
141 iPreciselyTimed =
nullptr;
167 if (iPreciselyTimed) {
173 if (iFrameGrabberImage)
175 if (iFrameGrabberImage->
getImage(*yarpimg))
177 sensor_msgs::msg::Image rosimg;
179 rosimg.width = yarpimg->
width();
180 rosimg.height = yarpimg->
height();
181 rosimg.encoding = yarp2RosPixelCode(yarpimg->
getPixelCode());
186 rosimg.is_bigendian = 0;
188 publisher_image->publish(rosimg);
192 yCError(FRAMEGRABBER_NWS_ROS2) <<
"Image not captured (getImage failed). Check hardware configuration.";
197 yCError(FRAMEGRABBER_NWS_ROS2) <<
"Invalid call to interface iFrameGrabberImage";
200 if (iRgbVisualParams)
202 sensor_msgs::msg::CameraInfo cameraInfo;
203 if (setCamInfo(cameraInfo)) {
204 publisher_cameraInfo->publish(cameraInfo);
209 yCError(FRAMEGRABBER_NWS_ROS2) <<
"Invalid call to interface iRgbVisualParams";
217 param(T& inVar, std::string inName) :
219 parname(
std::move(inName))
227bool FrameGrabber_nws_ros2::setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo)
231 yCErrorThreadOnce(FRAMEGRABBER_NWS_ROS2) <<
"Unable to get intrinsic param from rgb sensor!";
235 if (!camData.
check(
"distortionModel")) {
236 yCWarning(FRAMEGRABBER_NWS_ROS2) <<
"Missing distortion model";
240 std::string distModel = camData.
find(
"distortionModel").
asString();
241 if (distModel !=
"plumb_bob") {
242 yCError(FRAMEGRABBER_NWS_ROS2) <<
"Distortion model not supported";
257 std::vector<param<double>> parVector;
258 parVector.emplace_back(phyF,
"physFocalLength");
259 parVector.emplace_back(fx,
"focalLengthX");
260 parVector.emplace_back(fy,
"focalLengthY");
261 parVector.emplace_back(cx,
"principalPointX");
262 parVector.emplace_back(cy,
"principalPointY");
263 parVector.emplace_back(k1,
"k1");
264 parVector.emplace_back(k2,
"k2");
265 parVector.emplace_back(t1,
"t1");
266 parVector.emplace_back(t2,
"t2");
267 parVector.emplace_back(k3,
"k3");
269 for(
auto& par : parVector) {
270 if(!camData.
check(par.parname)) {
271 yCWarning(FRAMEGRABBER_NWS_ROS2) <<
"Driver has not the param:" << par.parname;
280 cameraInfo.width = iRgbVisualParams->
getRgbWidth();
282 cameraInfo.distortion_model = distModel;
284 cameraInfo.d.resize(5);
285 cameraInfo.d[0] = k1;
286 cameraInfo.d[1] = k2;
287 cameraInfo.d[2] = t1;
288 cameraInfo.d[3] = t2;
289 cameraInfo.d[4] = k3;
291 cameraInfo.k[0] = fx; cameraInfo.k[1] = 0; cameraInfo.k[2] = cx;
292 cameraInfo.k[3] = 0; cameraInfo.k[4] = fy; cameraInfo.k[5] = cy;
293 cameraInfo.k[6] = 0; cameraInfo.k[7] = 0; cameraInfo.k[8] = 1;
304 cameraInfo.r[0] = 1; cameraInfo.r[1] = 0; cameraInfo.r[2] = 0;
305 cameraInfo.r[3] = 0; cameraInfo.r[4] = 1; cameraInfo.r[5] = 0;
306 cameraInfo.r[6] = 0; cameraInfo.r[7] = 0; cameraInfo.r[8] = 1;
308 cameraInfo.p[0] = fx; cameraInfo.p[1] = 0; cameraInfo.p[2] = cx; cameraInfo.p[3] = 0;
309 cameraInfo.p[4] = 0; cameraInfo.p[5] = fy; cameraInfo.p[6] = cy; cameraInfo.p[7] = 0;
310 cameraInfo.p[8] = 0; cameraInfo.p[9] = 0; cameraInfo.p[10] = 1; cameraInfo.p[11] = 0;
312 cameraInfo.binning_x = cameraInfo.binning_y = 0;
313 cameraInfo.roi.height = cameraInfo.roi.width = cameraInfo.roi.x_offset = cameraInfo.roi.y_offset = 0;
314 cameraInfo.roi.do_rectify =
false;
@ 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
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
void run() override
Loop function.
bool threadInit() override
Initialization method.
bool detach() override
Detach the object (you must have first called attach).
void threadRelease() override
Release method.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
~FrameGrabber_nws_ros2() override
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
static rclcpp::Node::SharedPtr createNode(std::string name)
bool view(T *&x)
Get an interface to the device driver.
virtual bool getImage(ImageType &image)=0
Get an image from the frame grabber.
virtual yarp::os::Stamp getLastInputStamp()=0
Return the time stamp relative to the last acquisition.
virtual bool getRgbIntrinsicParam(yarp::os::Property &intrinsic)=0
Get the intrinsic parameters of the rgb camera.
virtual int getRgbHeight()=0
Return the height of each frame.
virtual int getRgbWidth()=0
Return the width of each frame.
A container for a device driver.
bool isValid() const
Check if device is valid.
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.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::string asString() const
Get string value.
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.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
#define yCErrorThreadOnce(component,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.