Skip to content

Commit 59cd939

Browse files
authored
Merge pull request #77 from kaidegast/master
Fix RgbdSensor_nws_ros2 camera data and distortion type handling
2 parents f4bbeaa + 3d431cf commit 59cd939

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

src/devices/rgbdSensor_nws_ros2/RgbdSensor_nws_ros2.cpp

+32-36
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ bool RgbdSensor_nws_ros2::detach()
223223

224224
bool RgbdSensor_nws_ros2::setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo,
225225
const std::string& frame_id,
226-
const yarp::os::Stamp& stamp,
227226
const SensorType& sensorType)
228227
{
229228
double phyF = 0.0;
@@ -259,22 +258,21 @@ bool RgbdSensor_nws_ros2::setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo,
259258
}
260259

261260
distModel = camData.find("distortionModel").asString();
262-
if (distModel != "plumb_bob")
263-
{
264-
yCError(RGBDSENSOR_NWS_ROS2) << "Distortion model not supported";
265-
return false;
266-
}
267261

268262
parVector.emplace_back(phyF,"physFocalLength");
269263
parVector.emplace_back(fx,"focalLengthX");
270264
parVector.emplace_back(fy,"focalLengthY");
271265
parVector.emplace_back(cx,"principalPointX");
272266
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");
267+
268+
if (distModel != "none")
269+
{
270+
parVector.emplace_back(k1,"k1");
271+
parVector.emplace_back(k2,"k2");
272+
parVector.emplace_back(t1,"t1");
273+
parVector.emplace_back(t2,"t2");
274+
parVector.emplace_back(k3,"k3");
275+
}
278276

279277
for(auto& par : parVector) {
280278
if(!camData.check(par.parname)) {
@@ -285,18 +283,19 @@ bool RgbdSensor_nws_ros2::setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo,
285283
}
286284

287285
cameraInfo.header.frame_id = frame_id;
288-
cameraInfo.header.stamp.sec = static_cast<int>(stamp.getTime()); // FIXME
289-
cameraInfo.header.stamp.nanosec = static_cast<int>(1000000000UL * (stamp.getTime() - int(stamp.getTime()))); // FIXME
290286
cameraInfo.width = sensorType == COLOR_SENSOR ? sensor_p->getRgbWidth() : sensor_p->getDepthWidth();
291287
cameraInfo.height = sensorType == COLOR_SENSOR ? sensor_p->getRgbHeight() : sensor_p->getDepthHeight();
292288
cameraInfo.distortion_model = distModel;
293289

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;
290+
if (distModel != "none")
291+
{
292+
cameraInfo.d.resize(5);
293+
cameraInfo.d[0] = k1;
294+
cameraInfo.d[1] = k2;
295+
cameraInfo.d[2] = t1;
296+
cameraInfo.d[3] = t2;
297+
cameraInfo.d[4] = k3;
298+
}
300299

301300
cameraInfo.k[0] = fx; cameraInfo.k[1] = 0; cameraInfo.k[2] = cx;
302301
cameraInfo.k[3] = 0; cameraInfo.k[4] = fy; cameraInfo.k[5] = cy;
@@ -356,7 +355,14 @@ bool RgbdSensor_nws_ros2::writeData()
356355
oldDepthStamp = depthStamp;
357356
}
358357

359-
// TBD: We should check here somehow if the timestamp was correctly updated and, if not, update it ourselves.
358+
// Call setCamInfo only the first time
359+
if (m_firstTime) {
360+
setCamInfo(m_camInfoData.colorCamInfo, "color_frame", COLOR_SENSOR);
361+
setCamInfo(m_camInfoData.depthCamInfo, "depth_frame", DEPTH_SENSOR);
362+
m_firstTime = false;
363+
}
364+
365+
// Use m_camInfoData for subsequent calls
360366
if (rgb_data_ok) {
361367
sensor_msgs::msg::Image rColorImage;
362368
rColorImage.data.resize(colorImage.getRawImageSize());
@@ -372,15 +378,10 @@ bool RgbdSensor_nws_ros2::writeData()
372378

373379
rosPublisher_color->publish(rColorImage);
374380

375-
sensor_msgs::msg::CameraInfo camInfoC;
376-
if (setCamInfo(camInfoC, m_color_frame_id, colorStamp, COLOR_SENSOR)) {
377-
if(m_forceInfoSync) {
378-
camInfoC.header.stamp = rColorImage.header.stamp;
379-
}
380-
rosPublisher_colorCaminfo->publish(camInfoC);
381-
} else {
382-
yCWarning(RGBDSENSOR_NWS_ROS2, "Missing color camera parameters... camera info messages will be not sent");
381+
if (m_forceInfoSync) {
382+
m_camInfoData.colorCamInfo.header.stamp = rColorImage.header.stamp;
383383
}
384+
rosPublisher_colorCaminfo->publish(m_camInfoData.colorCamInfo);
384385
}
385386

386387
if (depth_data_ok)
@@ -399,15 +400,10 @@ bool RgbdSensor_nws_ros2::writeData()
399400

400401
rosPublisher_depth->publish(rDepthImage);
401402

402-
sensor_msgs::msg::CameraInfo camInfoD;
403-
if (setCamInfo(camInfoD, m_depth_frame_id, depthStamp, DEPTH_SENSOR)) {
404-
if(m_forceInfoSync) {
405-
camInfoD.header.stamp = rDepthImage.header.stamp;
406-
}
407-
rosPublisher_depthCaminfo->publish(camInfoD);
408-
} else {
409-
yCWarning(RGBDSENSOR_NWS_ROS2, "Missing depth camera parameters... camera info messages will be not sent");
403+
if (m_forceInfoSync) {
404+
m_camInfoData.depthCamInfo.header.stamp = rDepthImage.header.stamp;
410405
}
406+
rosPublisher_depthCaminfo->publish(m_camInfoData.depthCamInfo);
411407
}
412408

413409
return true;

src/devices/rgbdSensor_nws_ros2/RgbdSensor_nws_ros2.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
* Documentation to be added
3131
*
3232
*/
33+
struct CameraInfoData {
34+
sensor_msgs::msg::CameraInfo colorCamInfo;
35+
sensor_msgs::msg::CameraInfo depthCamInfo;
36+
};
37+
3338
class RgbdSensor_nws_ros2 :
3439
public yarp::dev::DeviceDriver,
3540
public yarp::dev::WrapperSingle,
@@ -86,16 +91,14 @@ class RgbdSensor_nws_ros2 :
8691
yarp::dev::IRGBDSensor* sensor_p {nullptr};
8792
yarp::dev::IFrameGrabberControls* fgCtrl {nullptr};
8893
bool m_forceInfoSync {true};
94+
bool m_firstTime {true};
95+
96+
CameraInfoData m_camInfoData;
8997

9098
bool writeData();
9199
bool setCamInfo(sensor_msgs::msg::CameraInfo& cameraInfo,
92100
const std::string& frame_id,
93-
const yarp::os::Stamp& stamp,
94101
const SensorType& sensorType);
95-
96-
// static std::string yarp2RosPixelCode(int code);
97-
98-
99102
bool fromConfig(yarp::os::Searchable &config);
100103
bool initialize_ROS2(yarp::os::Searchable& config);
101104

0 commit comments

Comments
 (0)