mirror of
https://github.com/introlab/rtabmap_ros.git
synced 2026-10-07 18:27:46 +08:00
First version rtabmap_launch working
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <pluginlib/class_list_macros.hpp>
|
||||
#include <nodelet/nodelet.h>
|
||||
|
||||
#include <sensor_msgs/Image.h>
|
||||
#include <sensor_msgs/CompressedImage.h>
|
||||
#include <sensor_msgs/image_encodings.h>
|
||||
#include <sensor_msgs/CameraInfo.h>
|
||||
|
||||
#include <image_transport/image_transport.h>
|
||||
#include <image_transport/subscriber_filter.h>
|
||||
|
||||
#include <message_filters/sync_policies/approximate_time.h>
|
||||
#include <message_filters/sync_policies/exact_time.h>
|
||||
#include <message_filters/subscriber.h>
|
||||
|
||||
#include <cv_bridge/cv_bridge.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "rtabmap_msgs/RGBDImage.h"
|
||||
|
||||
#include "rtabmap/core/Compression.h"
|
||||
#include "rtabmap/utilite/UConversion.h"
|
||||
|
||||
namespace rtabmap_sync
|
||||
{
|
||||
|
||||
class RgbSync : public nodelet::Nodelet
|
||||
{
|
||||
public:
|
||||
RgbSync() :
|
||||
compressedRate_(0),
|
||||
warningThread_(0),
|
||||
callbackCalled_(false),
|
||||
approxSync_(0),
|
||||
exactSync_(0)
|
||||
{}
|
||||
|
||||
virtual ~RgbSync()
|
||||
{
|
||||
if(approxSync_)
|
||||
delete approxSync_;
|
||||
if(exactSync_)
|
||||
delete exactSync_;
|
||||
|
||||
if(warningThread_)
|
||||
{
|
||||
callbackCalled_=true;
|
||||
warningThread_->join();
|
||||
delete warningThread_;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void onInit()
|
||||
{
|
||||
ros::NodeHandle & nh = getNodeHandle();
|
||||
ros::NodeHandle & pnh = getPrivateNodeHandle();
|
||||
|
||||
int queueSize = 10;
|
||||
bool approxSync = false;
|
||||
double approxSyncMaxInterval = 0.0;
|
||||
pnh.param("approx_sync", approxSync, approxSync);
|
||||
pnh.param("approx_sync_max_interval", approxSyncMaxInterval, approxSyncMaxInterval);
|
||||
pnh.param("queue_size", queueSize, queueSize);
|
||||
pnh.param("compressed_rate", compressedRate_, compressedRate_);
|
||||
|
||||
NODELET_INFO("%s: approx_sync = %s", getName().c_str(), approxSync?"true":"false");
|
||||
if(approxSync)
|
||||
NODELET_INFO("%s: approx_sync_max_interval = %f", getName().c_str(), approxSyncMaxInterval);
|
||||
NODELET_INFO("%s: queue_size = %d", getName().c_str(), queueSize);
|
||||
NODELET_INFO("%s: compressed_rate = %f", getName().c_str(), compressedRate_);
|
||||
|
||||
rgbdImagePub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image", 1);
|
||||
rgbdImageCompressedPub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image/compressed", 1);
|
||||
|
||||
if(approxSync)
|
||||
{
|
||||
approxSync_ = new message_filters::Synchronizer<MyApproxSyncPolicy>(MyApproxSyncPolicy(queueSize), imageSub_, cameraInfoSub_);
|
||||
if(approxSyncMaxInterval > 0.0)
|
||||
approxSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
approxSync_->registerCallback(boost::bind(&RgbSync::callback, this, boost::placeholders::_1, boost::placeholders::_2));
|
||||
}
|
||||
else
|
||||
{
|
||||
exactSync_ = new message_filters::Synchronizer<MyExactSyncPolicy>(MyExactSyncPolicy(queueSize), imageSub_, cameraInfoSub_);
|
||||
exactSync_->registerCallback(boost::bind(&RgbSync::callback, this, boost::placeholders::_1, boost::placeholders::_2));
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
imageSub_.subscribe(rgb_it, rgb_nh.resolveName("image_rect"), 1, hintsRgb);
|
||||
cameraInfoSub_.subscribe(rgb_nh, "camera_info", 1);
|
||||
|
||||
std::string subscribedTopicsMsg = uFormat("\n%s subscribed to (%s sync%s):\n %s \\\n %s",
|
||||
getName().c_str(),
|
||||
approxSync?"approx":"exact",
|
||||
approxSync&&approxSyncMaxInterval!=std::numeric_limits<double>::max()?uFormat(", max interval=%fs", approxSyncMaxInterval).c_str():"",
|
||||
imageSub_.getTopic().c_str(),
|
||||
cameraInfoSub_.getTopic().c_str());
|
||||
|
||||
warningThread_ = new boost::thread(boost::bind(&RgbSync::warningLoop, this, subscribedTopicsMsg, approxSync));
|
||||
NODELET_INFO("%s", subscribedTopicsMsg.c_str());
|
||||
}
|
||||
|
||||
void warningLoop(const std::string & subscribedTopicsMsg, bool approxSync)
|
||||
{
|
||||
ros::Duration r(5.0);
|
||||
while(!callbackCalled_)
|
||||
{
|
||||
r.sleep();
|
||||
if(!callbackCalled_)
|
||||
{
|
||||
ROS_WARN("%s: Did not receive data since 5 seconds! Make sure the input topics are "
|
||||
"published (\"$ rostopic hz my_topic\") and the timestamps in their "
|
||||
"header are set. %s%s",
|
||||
getName().c_str(),
|
||||
approxSync?"":"Parameter \"approx_sync\" is false, which means that input "
|
||||
"topics should have all the exact timestamp for the callback to be called.",
|
||||
subscribedTopicsMsg.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(
|
||||
const sensor_msgs::ImageConstPtr& image,
|
||||
const sensor_msgs::CameraInfoConstPtr& cameraInfo)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
if(rgbdImagePub_.getNumSubscribers() || rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
double stamp = image->header.stamp.toSec();
|
||||
|
||||
rtabmap_msgs::RGBDImage msg;
|
||||
msg.header.frame_id = cameraInfo->header.frame_id;
|
||||
msg.header.stamp = image->header.stamp;
|
||||
msg.rgb_camera_info = *cameraInfo;
|
||||
|
||||
if(rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
bool publishCompressed = true;
|
||||
if (compressedRate_ > 0.0)
|
||||
{
|
||||
if ( lastCompressedPublished_ + ros::Duration(1.0/compressedRate_) > ros::Time::now())
|
||||
{
|
||||
NODELET_DEBUG("throttle last update at %f skipping", lastCompressedPublished_.toSec());
|
||||
publishCompressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(publishCompressed)
|
||||
{
|
||||
lastCompressedPublished_ = ros::Time::now();
|
||||
|
||||
rtabmap_msgs::RGBDImage msgCompressed = msg;
|
||||
|
||||
cv_bridge::CvImageConstPtr imagePtr = cv_bridge::toCvShare(image);
|
||||
imagePtr->toCompressedImageMsg(msgCompressed.rgb_compressed, cv_bridge::JPG);
|
||||
|
||||
rgbdImageCompressedPub_.publish(msgCompressed);
|
||||
}
|
||||
}
|
||||
|
||||
if(rgbdImagePub_.getNumSubscribers())
|
||||
{
|
||||
msg.rgb = *image;
|
||||
rgbdImagePub_.publish(msg);
|
||||
}
|
||||
|
||||
if( stamp != image->header.stamp.toSec())
|
||||
{
|
||||
NODELET_ERROR("Input stamps changed between the beginning and the end of the callback! Make "
|
||||
"sure the node publishing the topics doesn't override the same data after publishing them. A "
|
||||
"solution is to use this node within another nodelet manager. Stamps: "
|
||||
"%f->%f",
|
||||
stamp, image->header.stamp.toSec());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
double compressedRate_;
|
||||
boost::thread * warningThread_;
|
||||
bool callbackCalled_;
|
||||
ros::Time lastCompressedPublished_;
|
||||
|
||||
ros::Publisher rgbdImagePub_;
|
||||
ros::Publisher rgbdImageCompressedPub_;
|
||||
|
||||
image_transport::SubscriberFilter imageSub_;
|
||||
message_filters::Subscriber<sensor_msgs::CameraInfo> cameraInfoSub_;
|
||||
|
||||
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::CameraInfo> MyApproxSyncPolicy;
|
||||
message_filters::Synchronizer<MyApproxSyncPolicy> * approxSync_;
|
||||
|
||||
typedef message_filters::sync_policies::ExactTime<sensor_msgs::Image, sensor_msgs::CameraInfo> MyExactSyncPolicy;
|
||||
message_filters::Synchronizer<MyExactSyncPolicy> * exactSync_;
|
||||
};
|
||||
|
||||
PLUGINLIB_EXPORT_CLASS(rtabmap_sync::RgbSync, nodelet::Nodelet);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,333 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <pluginlib/class_list_macros.hpp>
|
||||
#include <nodelet/nodelet.h>
|
||||
|
||||
#include <sensor_msgs/Image.h>
|
||||
#include <sensor_msgs/CompressedImage.h>
|
||||
#include <sensor_msgs/image_encodings.h>
|
||||
#include <sensor_msgs/CameraInfo.h>
|
||||
|
||||
#include <image_transport/image_transport.h>
|
||||
#include <image_transport/subscriber_filter.h>
|
||||
|
||||
#include <message_filters/sync_policies/approximate_time.h>
|
||||
#include <message_filters/sync_policies/exact_time.h>
|
||||
#include <message_filters/subscriber.h>
|
||||
|
||||
#include <cv_bridge/cv_bridge.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "rtabmap_msgs/RGBDImage.h"
|
||||
#include "rtabmap_conversions/MsgConversion.h"
|
||||
|
||||
#include "rtabmap/core/Compression.h"
|
||||
#include "rtabmap/core/util2d.h"
|
||||
#include "rtabmap/utilite/UConversion.h"
|
||||
|
||||
namespace rtabmap_sync
|
||||
{
|
||||
|
||||
class RGBDSync : public nodelet::Nodelet
|
||||
{
|
||||
public:
|
||||
RGBDSync() :
|
||||
depthScale_(1.0),
|
||||
decimation_(1),
|
||||
compressedRate_(0),
|
||||
warningThread_(0),
|
||||
callbackCalled_(false),
|
||||
approxSyncDepth_(0),
|
||||
exactSyncDepth_(0)
|
||||
{}
|
||||
|
||||
virtual ~RGBDSync()
|
||||
{
|
||||
if(approxSyncDepth_)
|
||||
delete approxSyncDepth_;
|
||||
if(exactSyncDepth_)
|
||||
delete exactSyncDepth_;
|
||||
|
||||
if(warningThread_)
|
||||
{
|
||||
callbackCalled_=true;
|
||||
warningThread_->join();
|
||||
delete warningThread_;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void onInit()
|
||||
{
|
||||
ros::NodeHandle & nh = getNodeHandle();
|
||||
ros::NodeHandle & pnh = getPrivateNodeHandle();
|
||||
|
||||
int queueSize = 10;
|
||||
bool approxSync = true;
|
||||
double approxSyncMaxInterval = 0.0;
|
||||
pnh.param("approx_sync", approxSync, approxSync);
|
||||
pnh.param("approx_sync_max_interval", approxSyncMaxInterval, approxSyncMaxInterval);
|
||||
pnh.param("queue_size", queueSize, queueSize);
|
||||
pnh.param("depth_scale", depthScale_, depthScale_);
|
||||
pnh.param("decimation", decimation_, decimation_);
|
||||
pnh.param("compressed_rate", compressedRate_, compressedRate_);
|
||||
|
||||
if(decimation_<1)
|
||||
{
|
||||
decimation_ = 1;
|
||||
}
|
||||
|
||||
NODELET_INFO("%s: approx_sync = %s", getName().c_str(), approxSync?"true":"false");
|
||||
if(approxSync)
|
||||
NODELET_INFO("%s: approx_sync_max_interval = %f", getName().c_str(), approxSyncMaxInterval);
|
||||
NODELET_INFO("%s: queue_size = %d", getName().c_str(), queueSize);
|
||||
NODELET_INFO("%s: depth_scale = %f", getName().c_str(), depthScale_);
|
||||
NODELET_INFO("%s: decimation = %d", getName().c_str(), decimation_);
|
||||
NODELET_INFO("%s: compressed_rate = %f", getName().c_str(), compressedRate_);
|
||||
|
||||
rgbdImagePub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image", 1);
|
||||
rgbdImageCompressedPub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image/compressed", 1);
|
||||
|
||||
if(approxSync)
|
||||
{
|
||||
approxSyncDepth_ = new message_filters::Synchronizer<MyApproxSyncDepthPolicy>(MyApproxSyncDepthPolicy(queueSize), imageSub_, imageDepthSub_, cameraInfoSub_);
|
||||
if(approxSyncMaxInterval > 0.0)
|
||||
approxSyncDepth_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
approxSyncDepth_->registerCallback(boost::bind(&RGBDSync::callback, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3));
|
||||
}
|
||||
else
|
||||
{
|
||||
exactSyncDepth_ = new message_filters::Synchronizer<MyExactSyncDepthPolicy>(MyExactSyncDepthPolicy(queueSize), imageSub_, imageDepthSub_, cameraInfoSub_);
|
||||
exactSyncDepth_->registerCallback(boost::bind(&RGBDSync::callback, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3));
|
||||
}
|
||||
|
||||
ros::NodeHandle rgb_nh(nh, "rgb");
|
||||
ros::NodeHandle depth_nh(nh, "depth");
|
||||
ros::NodeHandle rgb_pnh(pnh, "rgb");
|
||||
ros::NodeHandle depth_pnh(pnh, "depth");
|
||||
image_transport::ImageTransport rgb_it(rgb_nh);
|
||||
image_transport::ImageTransport depth_it(depth_nh);
|
||||
image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), rgb_pnh);
|
||||
image_transport::TransportHints hintsDepth("raw", ros::TransportHints(), depth_pnh);
|
||||
|
||||
imageSub_.subscribe(rgb_it, rgb_nh.resolveName("image"), 1, hintsRgb);
|
||||
imageDepthSub_.subscribe(depth_it, depth_nh.resolveName("image"), 1, hintsDepth);
|
||||
cameraInfoSub_.subscribe(rgb_nh, "camera_info", 1);
|
||||
|
||||
std::string subscribedTopicsMsg = uFormat("\n%s subscribed to (%s sync%s):\n %s \\\n %s \\\n %s",
|
||||
getName().c_str(),
|
||||
approxSync?"approx":"exact",
|
||||
approxSync&&approxSyncMaxInterval!=0.0?uFormat(", max interval=%fs", approxSyncMaxInterval).c_str():"",
|
||||
imageSub_.getTopic().c_str(),
|
||||
imageDepthSub_.getTopic().c_str(),
|
||||
cameraInfoSub_.getTopic().c_str());
|
||||
|
||||
warningThread_ = new boost::thread(boost::bind(&RGBDSync::warningLoop, this, subscribedTopicsMsg, approxSync));
|
||||
NODELET_INFO("%s", subscribedTopicsMsg.c_str());
|
||||
}
|
||||
|
||||
void warningLoop(const std::string & subscribedTopicsMsg, bool approxSync)
|
||||
{
|
||||
ros::Duration r(5.0);
|
||||
while(!callbackCalled_)
|
||||
{
|
||||
r.sleep();
|
||||
if(!callbackCalled_)
|
||||
{
|
||||
ROS_WARN("%s: Did not receive data since 5 seconds! Make sure the input topics are "
|
||||
"published (\"$ rostopic hz my_topic\") and the timestamps in their "
|
||||
"header are set. %s%s",
|
||||
getName().c_str(),
|
||||
approxSync?"":"Parameter \"approx_sync\" is false, which means that input "
|
||||
"topics should have all the exact timestamp for the callback to be called.",
|
||||
subscribedTopicsMsg.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(
|
||||
const sensor_msgs::ImageConstPtr& image,
|
||||
const sensor_msgs::ImageConstPtr& depth,
|
||||
const sensor_msgs::CameraInfoConstPtr& cameraInfo)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
if(rgbdImagePub_.getNumSubscribers() || rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
double rgbStamp = image->header.stamp.toSec();
|
||||
double depthStamp = depth->header.stamp.toSec();
|
||||
double infoStamp = cameraInfo->header.stamp.toSec();
|
||||
|
||||
double stampDiff = fabs(rgbStamp - depthStamp);
|
||||
if(stampDiff > 0.010)
|
||||
{
|
||||
NODELET_WARN("The time difference between rgb and depth frames is "
|
||||
"high (diff=%fs, rgb=%fs, depth=%fs). You may want "
|
||||
"to set approx_sync_max_interval lower than 0.01s to reject spurious bad synchronizations or use "
|
||||
"approx_sync=false if streams have all the exact same timestamp.",
|
||||
stampDiff,
|
||||
rgbStamp,
|
||||
depthStamp);
|
||||
}
|
||||
|
||||
rtabmap_msgs::RGBDImage msg;
|
||||
msg.header.frame_id = cameraInfo->header.frame_id;
|
||||
msg.header.stamp = image->header.stamp>depth->header.stamp?image->header.stamp:depth->header.stamp;
|
||||
if(decimation_>1 && !(depth->width % decimation_ == 0 && depth->height % decimation_ == 0))
|
||||
{
|
||||
ROS_WARN("Decimation of depth images should be exact (decimation=%d, size=(%d,%d))! "
|
||||
"Images won't be resized.", decimation_, depth->width, depth->height);
|
||||
decimation_ = 1;
|
||||
}
|
||||
if(decimation_>1)
|
||||
{
|
||||
rtabmap::CameraModel model = rtabmap_conversions::cameraModelFromROS(*cameraInfo);
|
||||
sensor_msgs::CameraInfo info;
|
||||
rtabmap_conversions::cameraModelToROS(model.scaled(1.0f/float(decimation_)), info);
|
||||
info.header = cameraInfo->header;
|
||||
msg.rgb_camera_info = info;
|
||||
msg.depth_camera_info = info;
|
||||
}
|
||||
else
|
||||
{
|
||||
msg.rgb_camera_info = *cameraInfo;
|
||||
msg.depth_camera_info = *cameraInfo;
|
||||
}
|
||||
|
||||
cv::Mat rgbMat;
|
||||
cv::Mat depthMat;
|
||||
cv_bridge::CvImageConstPtr imagePtr = cv_bridge::toCvShare(image);
|
||||
cv_bridge::CvImageConstPtr imageDepthPtr = cv_bridge::toCvShare(depth);
|
||||
rgbMat = imagePtr->image;
|
||||
depthMat = imageDepthPtr->image;
|
||||
|
||||
if(decimation_>1)
|
||||
{
|
||||
rgbMat = rtabmap::util2d::decimate(rgbMat, decimation_);
|
||||
depthMat = rtabmap::util2d::decimate(depthMat, decimation_);
|
||||
}
|
||||
|
||||
if(depthScale_ != 1.0)
|
||||
{
|
||||
depthMat*=depthScale_;
|
||||
}
|
||||
|
||||
if(rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
bool publishCompressed = true;
|
||||
if (compressedRate_ > 0.0)
|
||||
{
|
||||
if ( lastCompressedPublished_ + ros::Duration(1.0/compressedRate_) > ros::Time::now())
|
||||
{
|
||||
NODELET_DEBUG("throttle last update at %f skipping", lastCompressedPublished_.toSec());
|
||||
publishCompressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(publishCompressed)
|
||||
{
|
||||
lastCompressedPublished_ = ros::Time::now();
|
||||
|
||||
rtabmap_msgs::RGBDImage msgCompressed;
|
||||
msgCompressed.header = msg.header;
|
||||
msgCompressed.rgb_camera_info = msg.rgb_camera_info;
|
||||
msgCompressed.depth_camera_info = msg.depth_camera_info;
|
||||
|
||||
cv_bridge::CvImage cvImg;
|
||||
cvImg.header = image->header;
|
||||
cvImg.image = rgbMat;
|
||||
cvImg.encoding = image->encoding;
|
||||
cvImg.toCompressedImageMsg(msgCompressed.rgb_compressed, cv_bridge::JPG);
|
||||
|
||||
msgCompressed.depth_compressed.header = imageDepthPtr->header;
|
||||
msgCompressed.depth_compressed.data = rtabmap::compressImage(depthMat, ".png");
|
||||
|
||||
msgCompressed.depth_compressed.format = "png";
|
||||
|
||||
rgbdImageCompressedPub_.publish(msgCompressed);
|
||||
}
|
||||
}
|
||||
|
||||
if(rgbdImagePub_.getNumSubscribers())
|
||||
{
|
||||
cv_bridge::CvImage cvImg;
|
||||
cvImg.header = image->header;
|
||||
cvImg.image = rgbMat;
|
||||
cvImg.encoding = image->encoding;
|
||||
cvImg.toImageMsg(msg.rgb);
|
||||
|
||||
cv_bridge::CvImage cvDepth;
|
||||
cvDepth.header = depth->header;
|
||||
cvDepth.image = depthMat;
|
||||
cvDepth.encoding = depth->encoding;
|
||||
cvDepth.toImageMsg(msg.depth);
|
||||
|
||||
rgbdImagePub_.publish(msg);
|
||||
}
|
||||
|
||||
if( rgbStamp != image->header.stamp.toSec() ||
|
||||
depthStamp != depth->header.stamp.toSec())
|
||||
{
|
||||
NODELET_ERROR("Input stamps changed between the beginning and the end of the callback! Make "
|
||||
"sure the node publishing the topics doesn't override the same data after publishing them. A "
|
||||
"solution is to use this node within another nodelet manager. Stamps: "
|
||||
"rgb=%f->%f depth=%f->%f",
|
||||
rgbStamp, image->header.stamp.toSec(),
|
||||
depthStamp, depth->header.stamp.toSec());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
double depthScale_;
|
||||
int decimation_;
|
||||
double compressedRate_;
|
||||
boost::thread * warningThread_;
|
||||
bool callbackCalled_;
|
||||
|
||||
ros::Time lastCompressedPublished_;
|
||||
|
||||
ros::Publisher rgbdImagePub_;
|
||||
ros::Publisher rgbdImageCompressedPub_;
|
||||
|
||||
image_transport::SubscriberFilter imageSub_;
|
||||
image_transport::SubscriberFilter imageDepthSub_;
|
||||
message_filters::Subscriber<sensor_msgs::CameraInfo> cameraInfoSub_;
|
||||
|
||||
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo> MyApproxSyncDepthPolicy;
|
||||
message_filters::Synchronizer<MyApproxSyncDepthPolicy> * approxSyncDepth_;
|
||||
|
||||
typedef message_filters::sync_policies::ExactTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo> MyExactSyncDepthPolicy;
|
||||
message_filters::Synchronizer<MyExactSyncDepthPolicy> * exactSyncDepth_;
|
||||
};
|
||||
|
||||
PLUGINLIB_EXPORT_CLASS(rtabmap_sync::RGBDSync, nodelet::Nodelet);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,340 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <pluginlib/class_list_macros.hpp>
|
||||
#include <nodelet/nodelet.h>
|
||||
|
||||
#include <message_filters/sync_policies/approximate_time.h>
|
||||
#include <message_filters/sync_policies/exact_time.h>
|
||||
#include <message_filters/subscriber.h>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "rtabmap_msgs/RGBDImages.h"
|
||||
#include "rtabmap_sync/CommonDataSubscriber.h"
|
||||
|
||||
namespace rtabmap_sync
|
||||
{
|
||||
|
||||
class RGBDXSync : public nodelet::Nodelet
|
||||
{
|
||||
public:
|
||||
RGBDXSync() :
|
||||
warningThread_(0),
|
||||
callbackCalled_(false),
|
||||
SYNC_INIT(rgbd2),
|
||||
SYNC_INIT(rgbd3),
|
||||
SYNC_INIT(rgbd4),
|
||||
SYNC_INIT(rgbd5),
|
||||
SYNC_INIT(rgbd6),
|
||||
SYNC_INIT(rgbd7),
|
||||
SYNC_INIT(rgbd8)
|
||||
{}
|
||||
|
||||
virtual ~RGBDXSync()
|
||||
{
|
||||
SYNC_DEL(rgbd2);
|
||||
|
||||
if(warningThread_)
|
||||
{
|
||||
callbackCalled_=true;
|
||||
warningThread_->join();
|
||||
delete warningThread_;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
virtual void onInit()
|
||||
{
|
||||
ros::NodeHandle & nh = getNodeHandle();
|
||||
ros::NodeHandle & pnh = getPrivateNodeHandle();
|
||||
|
||||
int queueSize = 10;
|
||||
bool approxSync = true;
|
||||
int rgbdCameras = 2;
|
||||
double approxSyncMaxInterval = 0.0;
|
||||
pnh.param("approx_sync", approxSync, approxSync);
|
||||
pnh.param("approx_sync_max_interval", approxSyncMaxInterval, approxSyncMaxInterval);
|
||||
pnh.param("queue_size", queueSize, queueSize);
|
||||
pnh.param("rgbd_cameras", rgbdCameras, rgbdCameras);
|
||||
|
||||
NODELET_INFO("%s: approx_sync = %s", getName().c_str(), approxSync?"true":"false");
|
||||
if(approxSync)
|
||||
NODELET_INFO("%s: approx_sync_max_interval = %f", getName().c_str(), approxSyncMaxInterval);
|
||||
NODELET_INFO("%s: queue_size = %d", getName().c_str(), queueSize);
|
||||
NODELET_INFO("%s: rgbd_cameras = %d", getName().c_str(), rgbdCameras);
|
||||
|
||||
rgbdImagesPub_ = nh.advertise<rtabmap_msgs::RGBDImages>("rgbd_images", 1);
|
||||
|
||||
ROS_ASSERT(rgbdCameras>=2 && rgbdCameras<=8);
|
||||
|
||||
rgbdSubs_.resize(rgbdCameras);
|
||||
for(int i=0; i<rgbdCameras; ++i)
|
||||
{
|
||||
rgbdSubs_[i] = new message_filters::Subscriber<rtabmap_msgs::RGBDImage>;
|
||||
rgbdSubs_[i]->subscribe(nh, uFormat("rgbd_image%d", i), queueSize);
|
||||
}
|
||||
|
||||
std::string name_ = this->getName();
|
||||
std::string subscribedTopicsMsg_;
|
||||
if(rgbdCameras==2)
|
||||
{
|
||||
SYNC_DECL2(RGBDXSync, rgbd2, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd2ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==3)
|
||||
{
|
||||
SYNC_DECL3(RGBDXSync, rgbd3, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd3ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==4)
|
||||
{
|
||||
SYNC_DECL4(RGBDXSync, rgbd4, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]), (*rgbdSubs_[3]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd4ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==5)
|
||||
{
|
||||
SYNC_DECL5(RGBDXSync, rgbd5, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]), (*rgbdSubs_[3]), (*rgbdSubs_[4]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd5ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==6)
|
||||
{
|
||||
SYNC_DECL6(RGBDXSync, rgbd6, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]), (*rgbdSubs_[3]), (*rgbdSubs_[4]), (*rgbdSubs_[5]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd6ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==7)
|
||||
{
|
||||
SYNC_DECL7(RGBDXSync, rgbd7, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]), (*rgbdSubs_[3]), (*rgbdSubs_[4]), (*rgbdSubs_[5]), (*rgbdSubs_[6]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd7ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
else if(rgbdCameras==8)
|
||||
{
|
||||
SYNC_DECL8(RGBDXSync, rgbd8, approxSync, queueSize, (*rgbdSubs_[0]), (*rgbdSubs_[1]), (*rgbdSubs_[2]), (*rgbdSubs_[3]), (*rgbdSubs_[4]), (*rgbdSubs_[5]), (*rgbdSubs_[6]), (*rgbdSubs_[7]));
|
||||
if(approxSync && approxSyncMaxInterval>0.0)
|
||||
{
|
||||
rgbd8ApproximateSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
}
|
||||
}
|
||||
|
||||
warningThread_ = new boost::thread(boost::bind(&RGBDXSync::warningLoop, this, subscribedTopicsMsg_, approxSync));
|
||||
NODELET_INFO("%s%s", subscribedTopicsMsg_.c_str(),
|
||||
approxSync&&approxSyncMaxInterval!=0.0?uFormat(" (approx sync max interval=%fs)", approxSyncMaxInterval).c_str():"");
|
||||
}
|
||||
|
||||
void warningLoop(const std::string & subscribedTopicsMsg, bool approxSync)
|
||||
{
|
||||
ros::Duration r(5.0);
|
||||
while(!callbackCalled_)
|
||||
{
|
||||
r.sleep();
|
||||
if(!callbackCalled_)
|
||||
{
|
||||
ROS_WARN("%s: Did not receive data since 5 seconds! Make sure the input topics are "
|
||||
"published (\"$ rostopic hz my_topic\") and the timestamps in their "
|
||||
"header are set. %s%s",
|
||||
getName().c_str(),
|
||||
approxSync?"":"Parameter \"approx_sync\" is false, which means that input "
|
||||
"topics should have all the exact timestamp for the callback to be called.",
|
||||
subscribedTopicsMsg.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DATA_SYNCS2(rgbd2, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS3(rgbd3, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS4(rgbd4, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS5(rgbd5, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS6(rgbd6, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS7(rgbd7, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
DATA_SYNCS8(rgbd8, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage, rtabmap_msgs::RGBDImage);
|
||||
|
||||
private:
|
||||
boost::thread * warningThread_;
|
||||
bool callbackCalled_;
|
||||
|
||||
ros::Publisher rgbdImagesPub_;
|
||||
|
||||
std::vector<message_filters::Subscriber<rtabmap_msgs::RGBDImage>*> rgbdSubs_;
|
||||
};
|
||||
|
||||
|
||||
void RGBDXSync::rgbd2Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(2);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd3Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(3);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd4Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image3)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(4);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
output.rgbd_images[3]=(*image3);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd5Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image3,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image4)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(5);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
output.rgbd_images[3]=(*image3);
|
||||
output.rgbd_images[4]=(*image4);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd6Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image3,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image4,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image5)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(6);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
output.rgbd_images[3]=(*image3);
|
||||
output.rgbd_images[4]=(*image4);
|
||||
output.rgbd_images[5]=(*image5);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd7Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image3,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image4,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image5,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image6)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(7);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
output.rgbd_images[3]=(*image3);
|
||||
output.rgbd_images[4]=(*image4);
|
||||
output.rgbd_images[5]=(*image5);
|
||||
output.rgbd_images[6]=(*image6);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
void RGBDXSync::rgbd8Callback(
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image0,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image1,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image2,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image3,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image4,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image5,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image6,
|
||||
const rtabmap_msgs::RGBDImageConstPtr& image7)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
rtabmap_msgs::RGBDImages output;
|
||||
output.header = image0->header;
|
||||
output.rgbd_images.resize(8);
|
||||
output.rgbd_images[0]=(*image0);
|
||||
output.rgbd_images[1]=(*image1);
|
||||
output.rgbd_images[2]=(*image2);
|
||||
output.rgbd_images[3]=(*image3);
|
||||
output.rgbd_images[4]=(*image4);
|
||||
output.rgbd_images[5]=(*image5);
|
||||
output.rgbd_images[6]=(*image6);
|
||||
output.rgbd_images[7]=(*image7);
|
||||
rgbdImagesPub_.publish(output);
|
||||
}
|
||||
|
||||
PLUGINLIB_EXPORT_CLASS(rtabmap_sync::RGBDXSync, nodelet::Nodelet);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the Universite de Sherbrooke nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <ros/ros.h>
|
||||
#include <pluginlib/class_list_macros.hpp>
|
||||
#include <nodelet/nodelet.h>
|
||||
|
||||
#include <sensor_msgs/Image.h>
|
||||
#include <sensor_msgs/CompressedImage.h>
|
||||
#include <sensor_msgs/image_encodings.h>
|
||||
#include <sensor_msgs/CameraInfo.h>
|
||||
|
||||
#include <image_transport/image_transport.h>
|
||||
#include <image_transport/subscriber_filter.h>
|
||||
|
||||
#include <message_filters/sync_policies/approximate_time.h>
|
||||
#include <message_filters/sync_policies/exact_time.h>
|
||||
#include <message_filters/subscriber.h>
|
||||
|
||||
#include <cv_bridge/cv_bridge.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
#include "rtabmap_msgs/RGBDImage.h"
|
||||
|
||||
#include "rtabmap/core/Compression.h"
|
||||
#include "rtabmap/utilite/UConversion.h"
|
||||
|
||||
namespace rtabmap_sync
|
||||
{
|
||||
|
||||
class StereoSync : public nodelet::Nodelet
|
||||
{
|
||||
public:
|
||||
StereoSync() :
|
||||
compressedRate_(0),
|
||||
warningThread_(0),
|
||||
callbackCalled_(false),
|
||||
approxSync_(0),
|
||||
exactSync_(0)
|
||||
{}
|
||||
|
||||
virtual ~StereoSync()
|
||||
{
|
||||
if(approxSync_)
|
||||
delete approxSync_;
|
||||
if(exactSync_)
|
||||
delete exactSync_;
|
||||
|
||||
if(warningThread_)
|
||||
{
|
||||
callbackCalled_=true;
|
||||
warningThread_->join();
|
||||
delete warningThread_;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void onInit()
|
||||
{
|
||||
ros::NodeHandle & nh = getNodeHandle();
|
||||
ros::NodeHandle & pnh = getPrivateNodeHandle();
|
||||
|
||||
int queueSize = 10;
|
||||
bool approxSync = false;
|
||||
double approxSyncMaxInterval = 0.0;
|
||||
pnh.param("approx_sync", approxSync, approxSync);
|
||||
if(approxSync)
|
||||
pnh.param("approx_sync_max_interval", approxSyncMaxInterval, approxSyncMaxInterval);
|
||||
pnh.param("queue_size", queueSize, queueSize);
|
||||
pnh.param("compressed_rate", compressedRate_, compressedRate_);
|
||||
|
||||
NODELET_INFO("%s: approx_sync = %s", getName().c_str(), approxSync?"true":"false");
|
||||
NODELET_INFO("%s: approx_sync_max_interval = %f", getName().c_str(), approxSyncMaxInterval);
|
||||
NODELET_INFO("%s: queue_size = %d", getName().c_str(), queueSize);
|
||||
NODELET_INFO("%s: compressed_rate = %f", getName().c_str(), compressedRate_);
|
||||
|
||||
rgbdImagePub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image", 1);
|
||||
rgbdImageCompressedPub_ = nh.advertise<rtabmap_msgs::RGBDImage>("rgbd_image/compressed", 1);
|
||||
|
||||
if(approxSync)
|
||||
{
|
||||
approxSync_ = new message_filters::Synchronizer<MyApproxSyncPolicy>(MyApproxSyncPolicy(queueSize), imageLeftSub_, imageRightSub_, cameraInfoLeftSub_, cameraInfoRightSub_);
|
||||
if(approxSyncMaxInterval>0.0)
|
||||
approxSync_->setMaxIntervalDuration(ros::Duration(approxSyncMaxInterval));
|
||||
approxSync_->registerCallback(boost::bind(&StereoSync::callback, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
|
||||
}
|
||||
else
|
||||
{
|
||||
exactSync_ = new message_filters::Synchronizer<MyExactSyncPolicy>(MyExactSyncPolicy(queueSize), imageLeftSub_, imageRightSub_, cameraInfoLeftSub_, cameraInfoRightSub_);
|
||||
exactSync_->registerCallback(boost::bind(&StereoSync::callback, this, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_3, boost::placeholders::_4));
|
||||
}
|
||||
|
||||
ros::NodeHandle left_nh(nh, "left");
|
||||
ros::NodeHandle right_nh(nh, "right");
|
||||
ros::NodeHandle left_pnh(pnh, "left");
|
||||
ros::NodeHandle right_pnh(pnh, "right");
|
||||
image_transport::ImageTransport rgb_it(left_nh);
|
||||
image_transport::ImageTransport depth_it(right_nh);
|
||||
image_transport::TransportHints hintsRgb("raw", ros::TransportHints(), left_pnh);
|
||||
image_transport::TransportHints hintsDepth("raw", ros::TransportHints(), right_pnh);
|
||||
|
||||
imageLeftSub_.subscribe(rgb_it, left_nh.resolveName("image_rect"), 1, hintsRgb);
|
||||
imageRightSub_.subscribe(depth_it, right_nh.resolveName("image_rect"), 1, hintsDepth);
|
||||
cameraInfoLeftSub_.subscribe(left_nh, "camera_info", 1);
|
||||
cameraInfoRightSub_.subscribe(right_nh, "camera_info", 1);
|
||||
|
||||
std::string subscribedTopicsMsg = uFormat("\n%s subscribed to (%s sync%s):\n %s \\\n %s \\\n %s \\\n %s",
|
||||
getName().c_str(),
|
||||
approxSync?"approx":"exact",
|
||||
approxSync&&approxSyncMaxInterval!=0.0?uFormat(", max interval=%fs", approxSyncMaxInterval).c_str():"",
|
||||
imageLeftSub_.getTopic().c_str(),
|
||||
imageRightSub_.getTopic().c_str(),
|
||||
cameraInfoLeftSub_.getTopic().c_str(),
|
||||
cameraInfoRightSub_.getTopic().c_str());
|
||||
|
||||
warningThread_ = new boost::thread(boost::bind(&StereoSync::warningLoop, this, subscribedTopicsMsg, approxSync));
|
||||
NODELET_INFO("%s", subscribedTopicsMsg.c_str());
|
||||
}
|
||||
|
||||
void warningLoop(const std::string & subscribedTopicsMsg, bool approxSync)
|
||||
{
|
||||
ros::Duration r(5.0);
|
||||
while(!callbackCalled_)
|
||||
{
|
||||
r.sleep();
|
||||
if(!callbackCalled_)
|
||||
{
|
||||
ROS_WARN("%s: Did not receive data since 5 seconds! Make sure the input topics are "
|
||||
"published (\"$ rostopic hz my_topic\") and the timestamps in their "
|
||||
"header are set. %s%s",
|
||||
getName().c_str(),
|
||||
approxSync?"":"Parameter \"approx_sync\" is false, which means that input "
|
||||
"topics should have all the exact timestamp for the callback to be called.",
|
||||
subscribedTopicsMsg.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void callback(
|
||||
const sensor_msgs::ImageConstPtr& imageLeft,
|
||||
const sensor_msgs::ImageConstPtr& imageRight,
|
||||
const sensor_msgs::CameraInfoConstPtr& cameraInfoLeft,
|
||||
const sensor_msgs::CameraInfoConstPtr& cameraInfoRight)
|
||||
{
|
||||
callbackCalled_ = true;
|
||||
if(rgbdImagePub_.getNumSubscribers() || rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
double leftStamp = imageLeft->header.stamp.toSec();
|
||||
double rightStamp = imageRight->header.stamp.toSec();
|
||||
double leftInfoStamp = cameraInfoLeft->header.stamp.toSec();
|
||||
double rightInfoStamp = cameraInfoRight->header.stamp.toSec();
|
||||
|
||||
double stampDiff = fabs(leftStamp - rightStamp);
|
||||
if(stampDiff > 0.010)
|
||||
{
|
||||
NODELET_WARN("The time difference between left and right frames is "
|
||||
"high (diff=%fs, left=%fs, right=%fs). If your left and right cameras are hardware "
|
||||
"synchronized, use approx_sync:=false. Otherwise, you may want "
|
||||
"to set approx_sync_max_interval lower than 0.01s to reject spurious bad synchronizations.",
|
||||
stampDiff,
|
||||
leftStamp,
|
||||
rightStamp);
|
||||
}
|
||||
|
||||
rtabmap_msgs::RGBDImage msg;
|
||||
msg.header.frame_id = cameraInfoLeft->header.frame_id;
|
||||
msg.header.stamp = imageLeft->header.stamp>imageRight->header.stamp?imageLeft->header.stamp:imageRight->header.stamp;
|
||||
msg.rgb_camera_info = *cameraInfoLeft;
|
||||
msg.depth_camera_info = *cameraInfoRight;
|
||||
|
||||
if(rgbdImageCompressedPub_.getNumSubscribers())
|
||||
{
|
||||
bool publishCompressed = true;
|
||||
if (compressedRate_ > 0.0)
|
||||
{
|
||||
if ( lastCompressedPublished_ + ros::Duration(1.0/compressedRate_) > ros::Time::now())
|
||||
{
|
||||
NODELET_DEBUG("throttle last update at %f skipping", lastCompressedPublished_.toSec());
|
||||
publishCompressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(publishCompressed)
|
||||
{
|
||||
lastCompressedPublished_ = ros::Time::now();
|
||||
|
||||
rtabmap_msgs::RGBDImage msgCompressed = msg;
|
||||
|
||||
cv_bridge::CvImageConstPtr imagePtr = cv_bridge::toCvShare(imageLeft);
|
||||
imagePtr->toCompressedImageMsg(msgCompressed.rgb_compressed, cv_bridge::JPG);
|
||||
|
||||
cv_bridge::CvImageConstPtr imageDepthPtr = cv_bridge::toCvShare(imageRight);
|
||||
imageDepthPtr->toCompressedImageMsg(msgCompressed.depth_compressed, cv_bridge::JPG);
|
||||
|
||||
rgbdImageCompressedPub_.publish(msgCompressed);
|
||||
}
|
||||
}
|
||||
|
||||
if(rgbdImagePub_.getNumSubscribers())
|
||||
{
|
||||
msg.rgb = *imageLeft;
|
||||
msg.depth = *imageRight;
|
||||
rgbdImagePub_.publish(msg);
|
||||
}
|
||||
|
||||
if( leftStamp != imageLeft->header.stamp.toSec() ||
|
||||
rightStamp != imageRight->header.stamp.toSec())
|
||||
{
|
||||
NODELET_ERROR("Input stamps changed between the beginning and the end of the callback! Make "
|
||||
"sure the node publishing the topics doesn't override the same data after publishing them. A "
|
||||
"solution is to use this node within another nodelet manager. Stamps: "
|
||||
"left%f->%f right=%f->%f",
|
||||
leftStamp, imageLeft->header.stamp.toSec(),
|
||||
rightStamp, imageRight->header.stamp.toSec());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
double compressedRate_;
|
||||
boost::thread * warningThread_;
|
||||
bool callbackCalled_;
|
||||
ros::Time lastCompressedPublished_;
|
||||
|
||||
ros::Publisher rgbdImagePub_;
|
||||
ros::Publisher rgbdImageCompressedPub_;
|
||||
|
||||
image_transport::SubscriberFilter imageLeftSub_;
|
||||
image_transport::SubscriberFilter imageRightSub_;
|
||||
message_filters::Subscriber<sensor_msgs::CameraInfo> cameraInfoLeftSub_;
|
||||
message_filters::Subscriber<sensor_msgs::CameraInfo> cameraInfoRightSub_;
|
||||
|
||||
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo> MyApproxSyncPolicy;
|
||||
message_filters::Synchronizer<MyApproxSyncPolicy> * approxSync_;
|
||||
|
||||
typedef message_filters::sync_policies::ExactTime<sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo, sensor_msgs::CameraInfo> MyExactSyncPolicy;
|
||||
message_filters::Synchronizer<MyExactSyncPolicy> * exactSync_;
|
||||
};
|
||||
|
||||
PLUGINLIB_EXPORT_CLASS(rtabmap_sync::StereoSync, nodelet::Nodelet);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user