rtabmap: supporting RGB+camera_info only with in RGBD/Enabled=true and Mem/IncrementalMemory=false (used to localize using only RGB against a prebuilt map)

This commit is contained in:
matlabbe
2017-07-10 12:17:17 -04:00
parent 1a1919e5e0
commit e944786ee5
4 changed files with 178 additions and 54 deletions
@@ -67,6 +67,7 @@ public:
bool isSubscribedToOdomInfo() const {return subscribedToOdomInfo_;} bool isSubscribedToOdomInfo() const {return subscribedToOdomInfo_;}
bool isDataSubscribed() const {return isSubscribedToDepth() || isSubscribedToStereo() || isSubscribedToRGBD();} bool isDataSubscribed() const {return isSubscribedToDepth() || isSubscribedToStereo() || isSubscribedToRGBD();}
int getQueueSize() const {return queueSize_;} int getQueueSize() const {return queueSize_;}
bool isApproxSync() const {return approxSync_;}
protected: protected:
void setupCallbacks(ros::NodeHandle & nh, ros::NodeHandle & pnh, const std::string & name); void setupCallbacks(ros::NodeHandle & nh, ros::NodeHandle & pnh, const std::string & name);
+7
View File
@@ -255,6 +255,13 @@ private:
// for loop closure detection only // for loop closure detection only
image_transport::Subscriber defaultSub_; image_transport::Subscriber defaultSub_;
// for rgb/localization
image_transport::SubscriberFilter rgbSub_;
message_filters::Subscriber<nav_msgs::Odometry> rgbOdomSub_;
message_filters::Subscriber<sensor_msgs::CameraInfo> rgbCameraInfoSub_;
DATA_SYNCS2(rgb, sensor_msgs::Image, sensor_msgs::CameraInfo);
DATA_SYNCS3(rgbOdom, sensor_msgs::Image, sensor_msgs::CameraInfo, nav_msgs::Odometry);
ros::Subscriber userDataAsyncSub_; ros::Subscriber userDataAsyncSub_;
cv::Mat userData_; cv::Mat userData_;
+109 -14
View File
@@ -105,6 +105,8 @@ CoreWrapper::CoreWrapper() :
mapToOdom_(rtabmap::Transform::getIdentity()), mapToOdom_(rtabmap::Transform::getIdentity()),
transformThread_(0), transformThread_(0),
tfThreadRunning_(false), tfThreadRunning_(false),
SYNC_INIT(rgb),
SYNC_INIT(rgbOdom),
stereoToDepth_(false), stereoToDepth_(false),
odomSensorSync_(false), odomSensorSync_(false),
rate_(Parameters::defaultRtabmapDetectionRate()), rate_(Parameters::defaultRtabmapDetectionRate()),
@@ -478,21 +480,84 @@ void CoreWrapper::onInit()
if(!this->isDataSubscribed()) if(!this->isDataSubscribed())
{ {
bool isRGBD = uStr2Bool(parameters_.at(Parameters::kRGBDEnabled()).c_str()); bool isRGBD = uStr2Bool(parameters_.at(Parameters::kRGBDEnabled()).c_str());
if(isRGBD) bool incremental = uStr2Bool(parameters_.at(Parameters::kMemIncrementalMemory()).c_str());
if(isRGBD && !incremental)
{ {
NODELET_WARN("ROS param subscribe_depth, subscribe_stereo and subscribe_rgbd are false, but RTAB-Map " NODELET_INFO("\"%s\" is true and \"%s\" is false, subscribing to RGB + camera info...",
"parameter \"%s\" is true! Please set subscribe_depth, subscribe_stereo or subscribe_rgbd " Parameters::kRGBDEnabled().c_str(),
"to true to use rtabmap node for RGB-D SLAM, or set \"%s\" to false for loop closure " Parameters::kMemIncrementalMemory().c_str());
"detection on images-only.", Parameters::kRGBDEnabled().c_str(), Parameters::kRGBDEnabled().c_str()); ros::NodeHandle rgb_nh(nh, "rgb");
ros::NodeHandle rgb_pnh(pnh, "rgb");
image_transport::ImageTransport rgb_it(rgb_nh);
image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), rgb_pnh);
rgbSub_.subscribe(rgb_it, rgb_nh.resolveName("image"), 1, hintsRgb);
rgbCameraInfoSub_.subscribe(rgb_nh, "camera_info", 1);
std::string odomFrameId;
pnh.getParam("odom_frame_id", odomFrameId);
if(!odomFrameId.empty())
{
// odom from TF
if(isApproxSync())
{
rgbApproximateSync_ = new message_filters::Synchronizer<rgbApproximateSyncPolicy>(
rgbApproximateSyncPolicy(getQueueSize()), rgbSub_, rgbCameraInfoSub_);
rgbApproximateSync_->registerCallback(boost::bind(&CoreWrapper::rgbCallback, this, _1, _2));
}
else
{
rgbExactSync_ = new message_filters::Synchronizer<rgbExactSyncPolicy>(
rgbExactSyncPolicy(getQueueSize()), rgbSub_, rgbCameraInfoSub_);
rgbExactSync_->registerCallback(boost::bind(&CoreWrapper::rgbCallback, this, _1, _2));
}
NODELET_INFO("\n%s subscribed to:\n %s\n %s",
getName().c_str(),
rgbSub_.getTopic().c_str(),
rgbCameraInfoSub_.getTopic().c_str());
}
else
{
rgbOdomSub_.subscribe(nh, "odom", 1);
if(isApproxSync())
{
rgbOdomApproximateSync_ = new message_filters::Synchronizer<rgbOdomApproximateSyncPolicy>(
rgbOdomApproximateSyncPolicy(getQueueSize()), rgbSub_, rgbCameraInfoSub_, rgbOdomSub_);
rgbOdomApproximateSync_->registerCallback(boost::bind(&CoreWrapper::rgbOdomCallback, this, _1, _2, _3));
}
else
{
rgbOdomExactSync_ = new message_filters::Synchronizer<rgbOdomExactSyncPolicy>(
rgbOdomExactSyncPolicy(getQueueSize()), rgbSub_, rgbCameraInfoSub_, rgbOdomSub_);
rgbOdomExactSync_->registerCallback(boost::bind(&CoreWrapper::rgbOdomCallback, this, _1, _2, _3));
}
NODELET_INFO("\n%s subscribed to:\n %s\n %s\n %s",
getName().c_str(),
rgbSub_.getTopic().c_str(),
rgbCameraInfoSub_.getTopic().c_str(),
rgbOdomSub_.getTopic().c_str());
}
} }
else
{
if(isRGBD)
{
NODELET_WARN("ROS param subscribe_depth, subscribe_stereo and subscribe_rgbd are false, but RTAB-Map "
"parameter \"%s\" and \"%s\" are true! Please set subscribe_depth, subscribe_stereo or subscribe_rgbd "
"to true to use rtabmap node for RGB-D SLAM, set \"%s\" to false for loop closure "
"detection on images-only or set \"%s\" to false to localize a single RGB camera against pre-built 3D map.",
Parameters::kRGBDEnabled().c_str(),
Parameters::kMemIncrementalMemory().c_str(),
Parameters::kRGBDEnabled().c_str(),
Parameters::kMemIncrementalMemory().c_str());
}
ros::NodeHandle rgb_nh(nh, "rgb");
ros::NodeHandle rgb_pnh(pnh, "rgb");
image_transport::ImageTransport rgb_it(rgb_nh);
image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), rgb_pnh);
defaultSub_ = rgb_it.subscribe("image", 1, &CoreWrapper::defaultCallback, this);
ros::NodeHandle rgb_nh(nh, "rgb"); NODELET_INFO("\n%s subscribed to:\n %s", getName().c_str(), defaultSub_.getTopic().c_str());
ros::NodeHandle rgb_pnh(pnh, "rgb"); }
image_transport::ImageTransport rgb_it(rgb_nh);
image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), rgb_pnh);
defaultSub_ = rgb_it.subscribe("image", 1, &CoreWrapper::defaultCallback, this);
NODELET_INFO("\n%s subscribed to:\n %s", getName().c_str(), defaultSub_.getTopic().c_str());
} }
userDataAsyncSub_ = nh.subscribe("user_data_async", 1, &CoreWrapper::userDataAsyncCallback, this); userDataAsyncSub_ = nh.subscribe("user_data_async", 1, &CoreWrapper::userDataAsyncCallback, this);
@@ -508,6 +573,9 @@ CoreWrapper::~CoreWrapper()
delete transformThread_; delete transformThread_;
} }
SYNC_DEL(rgb);
SYNC_DEL(rgbOdom);
this->saveParameters(configPath_); this->saveParameters(configPath_);
ros::NodeHandle nh; ros::NodeHandle nh;
@@ -636,6 +704,33 @@ void CoreWrapper::defaultCallback(const sensor_msgs::ImageConstPtr & imageMsg)
} }
} }
void CoreWrapper::rgbCallback(
const sensor_msgs::ImageConstPtr& imageMsg,
const sensor_msgs::CameraInfoConstPtr& cameraInfoMsg)
{
rtabmap_ros::UserDataConstPtr userDataMsg; // Null
nav_msgs::OdometryConstPtr odomMsg; // Null
sensor_msgs::LaserScanConstPtr scanMsg; // Null
sensor_msgs::PointCloud2ConstPtr scan3dMsg; // Null
rtabmap_ros::OdomInfoConstPtr odomInfoMsg; // null
cv_bridge::CvImageConstPtr depthMsg;// Null
commonSingleDepthCallback(odomMsg, userDataMsg, cv_bridge::toCvShare(imageMsg), depthMsg, *cameraInfoMsg, scanMsg, scan3dMsg, odomInfoMsg);
}
void CoreWrapper::rgbOdomCallback(
const sensor_msgs::ImageConstPtr& imageMsg,
const sensor_msgs::CameraInfoConstPtr& cameraInfoMsg,
const nav_msgs::OdometryConstPtr & odomMsg)
{
rtabmap_ros::UserDataConstPtr userDataMsg; // Null
sensor_msgs::LaserScanConstPtr scanMsg; // Null
sensor_msgs::PointCloud2ConstPtr scan3dMsg; // null
rtabmap_ros::OdomInfoConstPtr odomInfoMsg; // null
cv_bridge::CvImageConstPtr depthMsg;// Null
commonSingleDepthCallback(odomMsg, userDataMsg, cv_bridge::toCvShare(imageMsg), depthMsg, *cameraInfoMsg, scanMsg, scan3dMsg, odomInfoMsg);
}
bool CoreWrapper::odomUpdate(const nav_msgs::OdometryConstPtr & odomMsg) bool CoreWrapper::odomUpdate(const nav_msgs::OdometryConstPtr & odomMsg)
{ {
if(!paused_) if(!paused_)
@@ -847,7 +942,7 @@ void CoreWrapper::commonDepthCallbackImpl(
} }
UASSERT(uContains(parameters_, rtabmap::Parameters::kMemSaveDepth16Format())); UASSERT(uContains(parameters_, rtabmap::Parameters::kMemSaveDepth16Format()));
if(depth.type() == CV_32FC1 && uStr2Bool(parameters_.at(Parameters::kMemSaveDepth16Format()))) if(!depth.empty() && depth.type() == CV_32FC1 && uStr2Bool(parameters_.at(Parameters::kMemSaveDepth16Format())))
{ {
depth = rtabmap::util2d::cvtDepthFromFloat(depth); depth = rtabmap::util2d::cvtDepthFromFloat(depth);
static bool shown = false; static bool shown = false;
@@ -865,7 +960,7 @@ void CoreWrapper::commonDepthCallbackImpl(
Transform scanLocalTransform = Transform::getIdentity(); Transform scanLocalTransform = Transform::getIdentity();
pcl::PointCloud<pcl::PointXYZ> scanCloud2d; pcl::PointCloud<pcl::PointXYZ> scanCloud2d;
bool genMaxScanPts = 0; bool genMaxScanPts = 0;
if(scan2dMsg.get() == 0 && scan3dMsg.get() == 0 && genScan_) if(scan2dMsg.get() == 0 && scan3dMsg.get() == 0 && !depth.empty() && genScan_)
{ {
scanCloud2d = util3d::laserScanFromDepthImages( scanCloud2d = util3d::laserScanFromDepthImages(
depth, depth,
+61 -40
View File
@@ -1106,18 +1106,21 @@ bool convertRGBDMsgs(
double waitForTransform) double waitForTransform)
{ {
UASSERT(imageMsgs.size()>0 && UASSERT(imageMsgs.size()>0 &&
imageMsgs.size() == depthMsgs.size() && (imageMsgs.size() == depthMsgs.size() || depthMsgs.empty()) &&
imageMsgs.size() == cameraInfoMsgs.size()); imageMsgs.size() == cameraInfoMsgs.size());
int imageWidth = imageMsgs[0]->image.cols; int imageWidth = imageMsgs[0]->image.cols;
int imageHeight = imageMsgs[0]->image.rows; int imageHeight = imageMsgs[0]->image.rows;
int depthWidth = depthMsgs[0]->image.cols; int depthWidth = depthMsgs.size()?depthMsgs[0]->image.cols:0;
int depthHeight = depthMsgs[0]->image.rows; int depthHeight = depthMsgs.size()?depthMsgs[0]->image.rows:0;
UASSERT_MSG( if(depthMsgs.size())
imageWidth % depthWidth == 0 && imageHeight % depthHeight == 0 && {
imageWidth/depthWidth == imageHeight/depthHeight, UASSERT_MSG(
uFormat("rgb=%dx%d depth=%dx%d", imageWidth, imageHeight, depthWidth, depthHeight).c_str()); imageWidth % depthWidth == 0 && imageHeight % depthHeight == 0 &&
imageWidth/depthWidth == imageHeight/depthHeight,
uFormat("rgb=%dx%d depth=%dx%d", imageWidth, imageHeight, depthWidth, depthHeight).c_str());
}
int cameraCount = imageMsgs.size(); int cameraCount = imageMsgs.size();
for(unsigned int i=0; i<imageMsgs.size(); ++i) for(unsigned int i=0; i<imageMsgs.size(); ++i)
@@ -1129,14 +1132,19 @@ bool convertRGBDMsgs(
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0 || imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0 ||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BGRA8) == 0 || imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BGRA8) == 0 ||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGBA8) == 0 || imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGBA8) == 0 ||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BAYER_GRBG8) == 0) || imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BAYER_GRBG8) == 0))
!(depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_16UC1) == 0 ||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_32FC1) == 0 ||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::MONO16) == 0))
{ {
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8,bgra8,rgba8 and "
"image_depth=32FC1,16UC1,mono16. Current rgb=%s and depth=%s", ROS_ERROR("Input rgb type must be image=mono8,mono16,rgb8,bgr8,bgra8,rgba8. Current rgb=%s",
imageMsgs[i]->encoding.c_str(), imageMsgs[i]->encoding.c_str());
return false;
}
if(depthMsgs.size() &&
!(depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_16UC1) == 0 ||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_32FC1) == 0 ||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::MONO16) == 0))
{
ROS_ERROR("Input depth type must be image_depth=32FC1,16UC1,mono16. Current depth=%s",
depthMsgs[i]->encoding.c_str()); depthMsgs[i]->encoding.c_str());
return false; return false;
} }
@@ -1147,38 +1155,47 @@ bool convertRGBDMsgs(
imageMsgs[i]->image.cols, imageMsgs[i]->image.cols,
imageHeight, imageHeight,
imageMsgs[i]->image.rows).c_str()); imageMsgs[i]->image.rows).c_str());
UASSERT_MSG(depthMsgs[i]->image.cols == depthWidth && depthMsgs[i]->image.rows == depthHeight, ros::Time stamp;
uFormat("depthWidth=%d vs %d imageHeight=%d vs %d", if(depthMsgs.size())
depthWidth, {
depthMsgs[i]->image.cols, UASSERT_MSG(depthMsgs[i]->image.cols == depthWidth && depthMsgs[i]->image.rows == depthHeight,
depthHeight, uFormat("depthWidth=%d vs %d imageHeight=%d vs %d",
depthMsgs[i]->image.rows).c_str()); depthWidth,
depthMsgs[i]->image.cols,
depthHeight,
depthMsgs[i]->image.rows).c_str());
stamp = depthMsgs[i]->header.stamp;
}
else
{
stamp = imageMsgs[i]->header.stamp;
}
// use depth's stamp so that geometry is sync to odom, use rgb frame as we assume depth is registered (normally depth msg should have same frame than rgb) // use depth's stamp so that geometry is sync to odom, use rgb frame as we assume depth is registered (normally depth msg should have same frame than rgb)
rtabmap::Transform localTransform = rtabmap_ros::getTransform(frameId, imageMsgs[i]->header.frame_id, depthMsgs[i]->header.stamp, listener, waitForTransform); rtabmap::Transform localTransform = rtabmap_ros::getTransform(frameId, imageMsgs[i]->header.frame_id, stamp, listener, waitForTransform);
if(localTransform.isNull()) if(localTransform.isNull())
{ {
ROS_ERROR("TF of received depth image %d at time %fs is not set!", i, depthMsgs[i]->header.stamp.toSec()); ROS_ERROR("TF of received image %d at time %fs is not set!", i, stamp.toSec());
return false; return false;
} }
// sync with odometry stamp // sync with odometry stamp
if(!odomFrameId.empty() && odomStamp != depthMsgs[i]->header.stamp) if(!odomFrameId.empty() && odomStamp != stamp)
{ {
rtabmap::Transform sensorT = getTransform( rtabmap::Transform sensorT = getTransform(
frameId, frameId,
odomFrameId, odomFrameId,
odomStamp, odomStamp,
depthMsgs[i]->header.stamp, stamp,
listener, listener,
waitForTransform); waitForTransform);
if(sensorT.isNull()) if(sensorT.isNull())
{ {
ROS_WARN("Could not get odometry value for depth image stamp (%fs). Latest odometry " ROS_WARN("Could not get odometry value for depth image stamp (%fs). Latest odometry "
"stamp is %fs. The depth image pose will not be synchronized with odometry.", depthMsgs[i]->header.stamp.toSec(), odomStamp.toSec()); "stamp is %fs. The depth image pose will not be synchronized with odometry.", stamp.toSec(), odomStamp.toSec());
} }
else else
{ {
//ROS_WARN("RGBD correction = %s (time diff=%fs)", sensorT.prettyPrint().c_str(), fabs(depthMsgs[i]->header.stamp.toSec()-odomStamp.toSec())); //ROS_WARN("RGBD correction = %s (time diff=%fs)", sensorT.prettyPrint().c_str(), fabs(stamp.toSec()-odomStamp.toSec()));
localTransform = sensorT * localTransform; localTransform = sensorT * localTransform;
} }
} }
@@ -1198,19 +1215,12 @@ bool convertRGBDMsgs(
{ {
ptrImage = cv_bridge::cvtColor(imageMsgs[i], "bgr8"); ptrImage = cv_bridge::cvtColor(imageMsgs[i], "bgr8");
} }
cv_bridge::CvImageConstPtr ptrDepth = depthMsgs[i];
cv::Mat subDepth = ptrDepth->image;
// initialize // initialize
if(rgb.empty()) if(rgb.empty())
{ {
rgb = cv::Mat(imageHeight, imageWidth*cameraCount, ptrImage->image.type()); rgb = cv::Mat(imageHeight, imageWidth*cameraCount, ptrImage->image.type());
} }
if(depth.empty())
{
depth = cv::Mat(depthHeight, depthWidth*cameraCount, subDepth.type());
}
if(ptrImage->image.type() == rgb.type()) if(ptrImage->image.type() == rgb.type())
{ {
ptrImage->image.copyTo(cv::Mat(rgb, cv::Rect(i*imageWidth, 0, imageWidth, imageHeight))); ptrImage->image.copyTo(cv::Mat(rgb, cv::Rect(i*imageWidth, 0, imageWidth, imageHeight)));
@@ -1221,14 +1231,25 @@ bool convertRGBDMsgs(
return false; return false;
} }
if(subDepth.type() == depth.type()) if(depthMsgs.size())
{ {
subDepth.copyTo(cv::Mat(depth, cv::Rect(i*depthWidth, 0, depthWidth, depthHeight))); cv_bridge::CvImageConstPtr ptrDepth = depthMsgs[i];
} cv::Mat subDepth = ptrDepth->image;
else
{ if(depth.empty())
ROS_ERROR("Some Depth images are not the same type!"); {
return false; depth = cv::Mat(depthHeight, depthWidth*cameraCount, subDepth.type());
}
if(subDepth.type() == depth.type())
{
subDepth.copyTo(cv::Mat(depth, cv::Rect(i*depthWidth, 0, depthWidth, depthHeight)));
}
else
{
ROS_ERROR("Some Depth images are not the same type!");
return false;
}
} }
cameraModels.push_back(rtabmap_ros::cameraModelFromROS(cameraInfoMsgs[i], localTransform)); cameraModels.push_back(rtabmap_ros::cameraModelFromROS(cameraInfoMsgs[i], localTransform));