/* 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 "rtabmap_ros/OdometryROS.h" #include "pluginlib/class_list_macros.h" #include "nodelet/nodelet.h" #include #include #include #include #include #include #include #include #include #include "rtabmap_ros/MsgConversion.h" #include #include using namespace rtabmap; namespace rtabmap_ros { class StereoOdometry : public rtabmap_ros::OdometryROS { public: StereoOdometry() : rtabmap_ros::OdometryROS(true), approxSync_(0), exactSync_(0), queueSize_(5) { } virtual ~StereoOdometry() { if(approxSync_) { delete approxSync_; } if(exactSync_) { delete exactSync_; } } private: virtual void onOdomInit() { ros::NodeHandle & nh = getNodeHandle(); ros::NodeHandle & pnh = getPrivateNodeHandle(); bool approxSync = false; pnh.param("approx_sync", approxSync, approxSync); pnh.param("queue_size", queueSize_, queueSize_); NODELET_INFO("Approximate time sync = %s", approxSync?"true":"false"); 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 left_it(left_nh); image_transport::ImageTransport right_it(right_nh); image_transport::TransportHints hintsLeft("raw", ros::TransportHints(), left_pnh); image_transport::TransportHints hintsRight("raw", ros::TransportHints(), right_pnh); imageRectLeft_.subscribe(left_it, left_nh.resolveName("image_rect"), 1, hintsLeft); imageRectRight_.subscribe(right_it, right_nh.resolveName("image_rect"), 1, hintsRight); cameraInfoLeft_.subscribe(left_nh, "camera_info", 1); cameraInfoRight_.subscribe(right_nh, "camera_info", 1); if(approxSync) { approxSync_ = new message_filters::Synchronizer(MyApproxSyncPolicy(queueSize_), imageRectLeft_, imageRectRight_, cameraInfoLeft_, cameraInfoRight_); approxSync_->registerCallback(boost::bind(&StereoOdometry::callback, this, _1, _2, _3, _4)); } else { exactSync_ = new message_filters::Synchronizer(MyExactSyncPolicy(queueSize_), imageRectLeft_, imageRectRight_, cameraInfoLeft_, cameraInfoRight_); exactSync_->registerCallback(boost::bind(&StereoOdometry::callback, this, _1, _2, _3, _4)); } NODELET_INFO("\n%s subscribed to (%s sync):\n %s,\n %s,\n %s,\n %s", ros::this_node::getName().c_str(), approxSync?"approx":"exact", imageRectLeft_.getTopic().c_str(), imageRectRight_.getTopic().c_str(), cameraInfoLeft_.getTopic().c_str(), cameraInfoRight_.getTopic().c_str()); } void callback( const sensor_msgs::ImageConstPtr& imageRectLeft, const sensor_msgs::ImageConstPtr& imageRectRight, const sensor_msgs::CameraInfoConstPtr& cameraInfoLeft, const sensor_msgs::CameraInfoConstPtr& cameraInfoRight) { if(!this->isPaused()) { if(!(imageRectLeft->encoding.compare(sensor_msgs::image_encodings::MONO8) ==0 || imageRectLeft->encoding.compare(sensor_msgs::image_encodings::MONO16) ==0 || imageRectLeft->encoding.compare(sensor_msgs::image_encodings::BGR8) == 0 || imageRectLeft->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0) || !(imageRectRight->encoding.compare(sensor_msgs::image_encodings::MONO8) ==0 || imageRectRight->encoding.compare(sensor_msgs::image_encodings::MONO16) ==0 || imageRectRight->encoding.compare(sensor_msgs::image_encodings::BGR8) == 0 || imageRectRight->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0)) { NODELET_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8 (mono8 recommended)"); return; } ros::Time stamp = imageRectLeft->header.stamp>imageRectRight->header.stamp?imageRectLeft->header.stamp:imageRectRight->header.stamp; Transform localTransform = getTransform(this->frameId(), imageRectLeft->header.frame_id, stamp); if(localTransform.isNull()) { return; } ros::WallTime time = ros::WallTime::now(); int quality = -1; if(imageRectLeft->data.size() && imageRectRight->data.size()) { rtabmap::StereoCameraModel stereoModel = rtabmap_ros::stereoCameraModelFromROS(*cameraInfoLeft, *cameraInfoRight, localTransform); if(stereoModel.baseline() <= 0) { NODELET_FATAL("The stereo baseline (%f) should be positive (baseline=-Tx/fx). We assume a horizontal left/right stereo " "setup where the Tx (or P(0,3)) is negative in the right camera info msg.", stereoModel.baseline()); return; } if(stereoModel.baseline() > 10.0) { static bool shown = false; if(!shown) { NODELET_WARN("Detected baseline (%f m) is quite large! Is your " "right camera_info P(0,3) correctly set? Note that " "baseline=-P(0,3)/P(0,0). This warning is printed only once.", stereoModel.baseline()); shown = true; } } cv_bridge::CvImagePtr ptrImageLeft = cv_bridge::toCvCopy(imageRectLeft, "mono8"); cv_bridge::CvImagePtr ptrImageRight = cv_bridge::toCvCopy(imageRectRight, "mono8"); UTimer stepTimer; // UDEBUG("localTransform = %s", localTransform.prettyPrint().c_str()); rtabmap::SensorData data( ptrImageLeft->image, ptrImageRight->image, stereoModel, 0, rtabmap_ros::timestampFromROS(stamp)); this->processData(data, stamp); } else { NODELET_WARN("Odom: input images empty?!?"); } } } protected: virtual void flushCallbacks() { //flush callbacks if(approxSync_) { delete approxSync_; approxSync_ = new message_filters::Synchronizer(MyApproxSyncPolicy(queueSize_), imageRectLeft_, imageRectRight_, cameraInfoLeft_, cameraInfoRight_); approxSync_->registerCallback(boost::bind(&StereoOdometry::callback, this, _1, _2, _3, _4)); } if(exactSync_) { delete exactSync_; exactSync_ = new message_filters::Synchronizer(MyExactSyncPolicy(queueSize_), imageRectLeft_, imageRectRight_, cameraInfoLeft_, cameraInfoRight_); exactSync_->registerCallback(boost::bind(&StereoOdometry::callback, this, _1, _2, _3, _4)); } } private: image_transport::SubscriberFilter imageRectLeft_; image_transport::SubscriberFilter imageRectRight_; message_filters::Subscriber cameraInfoLeft_; message_filters::Subscriber cameraInfoRight_; typedef message_filters::sync_policies::ApproximateTime MyApproxSyncPolicy; message_filters::Synchronizer * approxSync_; typedef message_filters::sync_policies::ExactTime MyExactSyncPolicy; message_filters::Synchronizer * exactSync_; int queueSize_; }; PLUGINLIB_EXPORT_CLASS(rtabmap_ros::StereoOdometry, nodelet::Nodelet); }