2018-05-02 12:29:45 -04:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
#include <rtabmap_sync/stereo_sync.hpp>
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2020-01-30 17:18:48 -05:00
|
|
|
#include <sensor_msgs/msg/compressed_image.hpp>
|
|
|
|
|
#include <sensor_msgs/image_encodings.hpp>
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2024-06-04 22:42:06 -07:00
|
|
|
#ifdef PRE_ROS_IRON
|
2018-05-02 12:29:45 -04:00
|
|
|
#include <cv_bridge/cv_bridge.h>
|
2024-06-04 22:42:06 -07:00
|
|
|
#else
|
|
|
|
|
#include <cv_bridge/cv_bridge.hpp>
|
|
|
|
|
#endif
|
2018-05-02 12:29:45 -04:00
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
|
|
|
|
|
|
#include "rtabmap/core/Compression.h"
|
|
|
|
|
#include "rtabmap/utilite/UConversion.h"
|
2023-02-22 23:11:49 -08:00
|
|
|
#include "rtabmap_conversions/MsgConversion.h"
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
namespace rtabmap_sync
|
2018-05-02 12:29:45 -04:00
|
|
|
{
|
|
|
|
|
|
2020-01-30 17:18:48 -05:00
|
|
|
StereoSync::StereoSync(const rclcpp::NodeOptions & options) :
|
|
|
|
|
Node("stereo_sync", options),
|
2019-03-04 19:22:25 -05:00
|
|
|
compressedRate_(0),
|
2018-05-02 12:29:45 -04:00
|
|
|
approxSync_(0),
|
|
|
|
|
exactSync_(0)
|
2020-01-30 17:18:48 -05:00
|
|
|
{
|
2024-09-04 00:46:44 -07:00
|
|
|
int topicQueueSize = 10;
|
2024-11-30 17:28:16 -08:00
|
|
|
int syncQueueSize = 10;
|
2020-01-30 17:18:48 -05:00
|
|
|
bool approxSync = false;
|
2022-10-01 18:09:19 -07:00
|
|
|
double approxSyncMaxInterval = 0.0;
|
2024-11-30 17:28:16 -08:00
|
|
|
int qos = RMW_QOS_POLICY_RELIABILITY_SYSTEM_DEFAULT;
|
2020-01-30 17:18:48 -05:00
|
|
|
approxSync = this->declare_parameter("approx_sync", approxSync);
|
2022-10-01 18:09:19 -07:00
|
|
|
approxSyncMaxInterval = this->declare_parameter("approx_sync_max_interval", approxSyncMaxInterval);
|
2024-06-12 22:51:30 -07:00
|
|
|
topicQueueSize = this->declare_parameter("topic_queue_size", topicQueueSize);
|
|
|
|
|
int queueSize = this->declare_parameter("queue_size", -1);
|
|
|
|
|
if(queueSize != -1)
|
2018-05-02 12:29:45 -04:00
|
|
|
{
|
2024-06-12 22:51:30 -07:00
|
|
|
syncQueueSize = queueSize;
|
|
|
|
|
RCLCPP_WARN(this->get_logger(), "Parameter \"queue_size\" has been renamed "
|
|
|
|
|
"to \"sync_queue_size\" and will be removed "
|
|
|
|
|
"in future versions! The value (%d) is copied to "
|
|
|
|
|
"\"sync_queue_size\".", syncQueueSize);
|
|
|
|
|
}
|
|
|
|
|
syncQueueSize = this->declare_parameter("sync_queue_size", syncQueueSize);
|
2021-11-08 18:36:19 -05:00
|
|
|
qos = this->declare_parameter("qos", qos);
|
|
|
|
|
int qosCamInfo = this->declare_parameter("qos_camera_info", qos);
|
2020-01-30 17:18:48 -05:00
|
|
|
compressedRate_ = this->declare_parameter("compressed_rate", compressedRate_);
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2020-01-30 17:18:48 -05:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: approx_sync = %s", get_name(), approxSync?"true":"false");
|
2022-10-01 18:09:19 -07:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: approx_sync_max_interval = %f", get_name(), approxSyncMaxInterval);
|
2024-06-12 22:51:30 -07:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: topic_queue_size = %d", get_name(), topicQueueSize);
|
|
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: sync_queue_size = %d", get_name(), syncQueueSize);
|
|
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: qos = %d", get_name(), qos);
|
2021-11-08 18:36:19 -05:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: qos_camera_info = %d", get_name(), qosCamInfo);
|
2020-01-30 17:18:48 -05:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s: compressed_rate = %f", get_name(), compressedRate_);
|
|
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
rgbdImagePub_ = create_publisher<rtabmap_msgs::msg::RGBDImage>("rgbd_image", rclcpp::QoS(1).reliability((rmw_qos_reliability_policy_t)qos));
|
|
|
|
|
rgbdImageCompressedPub_ = create_publisher<rtabmap_msgs::msg::RGBDImage>("rgbd_image/compressed", rclcpp::QoS(1).reliability((rmw_qos_reliability_policy_t)qos));
|
2020-01-30 17:18:48 -05:00
|
|
|
|
|
|
|
|
if(approxSync)
|
2018-05-02 12:29:45 -04:00
|
|
|
{
|
2024-06-12 22:51:30 -07:00
|
|
|
approxSync_ = new message_filters::Synchronizer<MyApproxSyncPolicy>(MyApproxSyncPolicy(syncQueueSize), imageLeftSub_, imageRightSub_, cameraInfoLeftSub_, cameraInfoRightSub_);
|
2022-10-01 18:09:19 -07:00
|
|
|
if(approxSyncMaxInterval>0.0)
|
|
|
|
|
approxSync_->setMaxIntervalDuration(rclcpp::Duration::from_seconds(approxSyncMaxInterval));
|
2020-01-30 17:18:48 -05:00
|
|
|
approxSync_->registerCallback(std::bind(&StereoSync::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-06-12 22:51:30 -07:00
|
|
|
exactSync_ = new message_filters::Synchronizer<MyExactSyncPolicy>(MyExactSyncPolicy(syncQueueSize), imageLeftSub_, imageRightSub_, cameraInfoLeftSub_, cameraInfoRightSub_);
|
2020-01-30 17:18:48 -05:00
|
|
|
exactSync_->registerCallback(std::bind(&StereoSync::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
2018-05-02 12:29:45 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-30 17:18:48 -05:00
|
|
|
image_transport::TransportHints hints(this);
|
2024-06-12 22:51:30 -07:00
|
|
|
imageLeftSub_.subscribe(this, "left/image_rect", hints.getTransport(), rclcpp::QoS(topicQueueSize).reliability((rmw_qos_reliability_policy_t)qos).get_rmw_qos_profile());
|
|
|
|
|
imageRightSub_.subscribe(this, "right/image_rect", hints.getTransport(), rclcpp::QoS(topicQueueSize).reliability((rmw_qos_reliability_policy_t)qos).get_rmw_qos_profile());
|
|
|
|
|
cameraInfoLeftSub_.subscribe(this, "left/camera_info"), rclcpp::QoS(topicQueueSize).reliability((rmw_qos_reliability_policy_t)qosCamInfo).get_rmw_qos_profile();
|
|
|
|
|
cameraInfoRightSub_.subscribe(this, "right/camera_info", rclcpp::QoS(topicQueueSize).reliability((rmw_qos_reliability_policy_t)qosCamInfo).get_rmw_qos_profile());
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2023-10-14 15:22:56 -07:00
|
|
|
std::string subscribedTopicsMsg = uFormat("\n%s subscribed to (%s sync%s):\n %s,\n %s,\n %s,\n %s",
|
2020-01-30 17:18:48 -05:00
|
|
|
get_name(),
|
|
|
|
|
approxSync?"approx":"exact",
|
2022-10-01 18:09:19 -07:00
|
|
|
approxSync&&approxSyncMaxInterval!=0.0?uFormat(", max interval=%fs", approxSyncMaxInterval).c_str():"",
|
2021-10-01 18:48:45 -04:00
|
|
|
imageLeftSub_.getSubscriber().getTopic().c_str(),
|
|
|
|
|
imageRightSub_.getSubscriber().getTopic().c_str(),
|
|
|
|
|
cameraInfoLeftSub_.getSubscriber()->get_topic_name(),
|
|
|
|
|
cameraInfoRightSub_.getSubscriber()->get_topic_name());
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2023-10-14 15:22:56 -07:00
|
|
|
RCLCPP_INFO(this->get_logger(), "%s", subscribedTopicsMsg.c_str());
|
2018-05-02 12:29:45 -04:00
|
|
|
|
2023-11-19 17:14:24 -08:00
|
|
|
syncDiagnostic_.reset(new SyncDiagnostic(this));
|
|
|
|
|
syncDiagnostic_->init(imageLeftSub_.getSubscriber().getTopic(),
|
2023-10-14 15:22:56 -07:00
|
|
|
uFormat("%s: Did not receive data since 5 seconds! Make sure the input topics are "
|
|
|
|
|
"published (\"$ rostopic hz my_topic\") and the timestamps in their "
|
2024-09-04 00:46:44 -07:00
|
|
|
"header are set. Ajusting topic_queue_size (%d) and sync_queue_size (%d) "
|
|
|
|
|
"can also help for better synchronization if framerates and/or delays are different.%s%s",
|
2023-10-14 15:22:56 -07:00
|
|
|
get_name(),
|
2024-09-04 00:46:44 -07:00
|
|
|
topicQueueSize,
|
|
|
|
|
syncQueueSize,
|
2023-10-14 15:22:56 -07:00
|
|
|
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()));
|
2018-05-02 12:29:45 -04:00
|
|
|
}
|
|
|
|
|
|
2020-01-30 17:18:48 -05:00
|
|
|
StereoSync::~StereoSync()
|
|
|
|
|
{
|
|
|
|
|
delete approxSync_;
|
|
|
|
|
delete exactSync_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StereoSync::callback(
|
|
|
|
|
const sensor_msgs::msg::Image::ConstSharedPtr imageLeft,
|
|
|
|
|
const sensor_msgs::msg::Image::ConstSharedPtr imageRight,
|
|
|
|
|
const sensor_msgs::msg::CameraInfo::ConstSharedPtr cameraInfoLeft,
|
|
|
|
|
const sensor_msgs::msg::CameraInfo::ConstSharedPtr cameraInfoRight)
|
|
|
|
|
{
|
2024-11-30 17:28:16 -08:00
|
|
|
syncDiagnostic_->tickInput(imageLeft->header.stamp);
|
2020-01-30 17:18:48 -05:00
|
|
|
if(rgbdImagePub_->get_subscription_count() || rgbdImageCompressedPub_->get_subscription_count())
|
|
|
|
|
{
|
2023-02-22 23:11:49 -08:00
|
|
|
double leftStamp = rtabmap_conversions::timestampFromROS(imageLeft->header.stamp);
|
|
|
|
|
double rightStamp = rtabmap_conversions::timestampFromROS(imageRight->header.stamp);
|
2020-01-30 17:18:48 -05:00
|
|
|
|
2022-10-01 18:09:19 -07:00
|
|
|
double stampDiff = fabs(leftStamp - rightStamp);
|
|
|
|
|
if(stampDiff > 0.010)
|
|
|
|
|
{
|
|
|
|
|
RCLCPP_WARN(this->get_logger(), "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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
rtabmap_msgs::msg::RGBDImage::UniquePtr msg(new rtabmap_msgs::msg::RGBDImage);
|
2020-01-30 17:18:48 -05:00
|
|
|
msg->header.frame_id = cameraInfoLeft->header.frame_id;
|
|
|
|
|
msg->header.stamp = leftStamp>rightStamp?imageLeft->header.stamp:imageRight->header.stamp;
|
|
|
|
|
msg->rgb_camera_info = *cameraInfoLeft;
|
|
|
|
|
msg->depth_camera_info = *cameraInfoRight;
|
|
|
|
|
if(rgbdImageCompressedPub_->get_subscription_count())
|
|
|
|
|
{
|
|
|
|
|
bool publishCompressed = true;
|
|
|
|
|
if (compressedRate_ > 0.0)
|
|
|
|
|
{
|
2022-07-25 22:41:10 -04:00
|
|
|
if ( lastCompressedPublished_ + rclcpp::Duration::from_seconds(1.0/compressedRate_) > now())
|
2020-01-30 17:18:48 -05:00
|
|
|
{
|
|
|
|
|
RCLCPP_DEBUG(this->get_logger(), "throttle last update at %f skipping", lastCompressedPublished_.seconds());
|
|
|
|
|
publishCompressed = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(publishCompressed)
|
|
|
|
|
{
|
|
|
|
|
lastCompressedPublished_ = now();
|
|
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
rtabmap_msgs::msg::RGBDImage::UniquePtr msgCompressed(new rtabmap_msgs::msg::RGBDImage);
|
2020-01-30 17:18:48 -05:00
|
|
|
*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(std::move(msgCompressed));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(rgbdImagePub_->get_subscription_count())
|
|
|
|
|
{
|
|
|
|
|
msg->rgb = *imageLeft;
|
|
|
|
|
msg->depth = *imageRight;
|
|
|
|
|
rgbdImagePub_->publish(std::move(msg));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 23:11:49 -08:00
|
|
|
if( leftStamp != rtabmap_conversions::timestampFromROS(imageLeft->header.stamp) ||
|
|
|
|
|
rightStamp != rtabmap_conversions::timestampFromROS(imageRight->header.stamp))
|
2020-01-30 17:18:48 -05:00
|
|
|
{
|
|
|
|
|
RCLCPP_ERROR(this->get_logger(), "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",
|
2023-02-22 23:11:49 -08:00
|
|
|
leftStamp, rtabmap_conversions::timestampFromROS(imageLeft->header.stamp),
|
|
|
|
|
rightStamp, rtabmap_conversions::timestampFromROS(imageRight->header.stamp));
|
2020-01-30 17:18:48 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-30 17:28:16 -08:00
|
|
|
syncDiagnostic_->tickOutput(imageLeft->header.stamp);
|
2020-01-30 17:18:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include "rclcpp_components/register_node_macro.hpp"
|
|
|
|
|
|
|
|
|
|
// Register the component with class_loader.
|
|
|
|
|
// This acts as a sort of entry point, allowing the component to be discoverable when its library
|
|
|
|
|
// is being loaded into a running process.
|
2023-02-22 23:11:49 -08:00
|
|
|
RCLCPP_COMPONENTS_REGISTER_NODE(rtabmap_sync::StereoSync)
|
2020-01-30 17:18:48 -05:00
|
|
|
|