Files
rtabmap_ros/rtabmap_util/src/nodelets/lidar_deskewing.cpp
T

107 lines
4.3 KiB
C++
Raw Normal View History

2023-02-22 23:11:49 -08:00
#include <rtabmap_util/lidar_deskewing.hpp>
2022-11-27 16:21:59 -08:00
#include <laser_geometry/laser_geometry.hpp>
2023-02-22 23:11:49 -08:00
#include <rtabmap_conversions/MsgConversion.h>
2023-02-22 23:11:49 -08:00
namespace rtabmap_util
{
2022-11-27 16:21:59 -08:00
LidarDeskewing::LidarDeskewing(const rclcpp::NodeOptions & options) :
2023-02-22 23:11:49 -08:00
Node("lidar_deskewing", options),
2022-11-27 16:21:59 -08:00
waitForTransformDuration_(0.01),
slerp_(false)
{
2022-11-27 16:21:59 -08:00
tfBuffer_ = std::make_shared<tf2_ros::Buffer>(this->get_clock());
//auto timer_interface = std::make_shared<tf2_ros::CreateTimerROS>(
// this->get_node_base_interface(),
// this->get_node_timers_interface());
//tfBuffer_->setCreateTimerInterface(timer_interface);
tfListener_ = std::make_shared<tf2_ros::TransformListener>(*tfBuffer_);
int queueSize = 5;
int qos = 0;
queueSize = this->declare_parameter("queue_size", queueSize);
qos = this->declare_parameter("qos", qos);
fixedFrameId_ = this->declare_parameter("fixed_frame_id", fixedFrameId_);
waitForTransformDuration_ = this->declare_parameter("wait_for_transform", waitForTransformDuration_);
slerp_ = this->declare_parameter("slerp", slerp_);
RCLCPP_INFO(this->get_logger(), " fixed_frame_id: %s", fixedFrameId_.c_str());
RCLCPP_INFO(this->get_logger(), " wait_for_transform: %fs", waitForTransformDuration_);
RCLCPP_INFO(this->get_logger(), " slerp: %s", slerp_?"true":"false");
if(fixedFrameId_.empty())
{
2022-11-27 16:21:59 -08:00
RCLCPP_FATAL(this->get_logger(), "fixed_frame_id parameter cannot be empty!");
}
2022-11-27 16:21:59 -08:00
subScan_ = create_subscription<sensor_msgs::msg::LaserScan>("input_scan", rclcpp::QoS(queueSize).reliability((rmw_qos_reliability_policy_t)qos), std::bind(&LidarDeskewing::callbackScan, this, std::placeholders::_1));
subCloud_ = create_subscription<sensor_msgs::msg::PointCloud2>("input_cloud", rclcpp::QoS(queueSize).reliability((rmw_qos_reliability_policy_t)qos), std::bind(&LidarDeskewing::callbackCloud, this, std::placeholders::_1));
pubScan_ = create_publisher<sensor_msgs::msg::PointCloud2>(std::string(subScan_->get_topic_name()) + "/deskewed", rclcpp::QoS(1).reliability((rmw_qos_reliability_policy_t)qos));
pubCloud_ = create_publisher<sensor_msgs::msg::PointCloud2>(std::string(subCloud_->get_topic_name()) + "/deskewed", rclcpp::QoS(1).reliability((rmw_qos_reliability_policy_t)qos));
}
LidarDeskewing::~LidarDeskewing()
{
}
void LidarDeskewing::callbackScan(const sensor_msgs::msg::LaserScan::ConstSharedPtr msg)
{
// make sure the frame of the laser is updated during the whole scan time
2024-05-27 12:37:39 -07:00
rtabmap::Transform tmpT = rtabmap_conversions::getMovingTransform(
msg->header.frame_id,
fixedFrameId_,
msg->header.stamp,
rclcpp::Time(msg->header.stamp.sec, msg->header.stamp.nanosec) + rclcpp::Duration::from_seconds(msg->ranges.size()*msg->time_increment),
*tfBuffer_,
waitForTransformDuration_);
if(tmpT.isNull())
{
return;
}
2022-11-27 16:21:59 -08:00
sensor_msgs::msg::PointCloud2 scanOut;
laser_geometry::LaserProjection projection;
projection.transformLaserScanToPointCloud(fixedFrameId_, *msg, scanOut, *tfBuffer_);
2023-02-22 23:11:49 -08:00
rtabmap::Transform t = rtabmap_conversions::getTransform(msg->header.frame_id, scanOut.header.frame_id, msg->header.stamp, *tfBuffer_, waitForTransformDuration_);
2022-11-27 16:21:59 -08:00
if(t.isNull())
{
2022-11-27 16:21:59 -08:00
RCLCPP_ERROR(this->get_logger(), "Cannot transform back projected scan from \"%s\" frame to \"%s\" frame at time %fs.",
2023-02-22 23:11:49 -08:00
scanOut.header.frame_id.c_str(), msg->header.frame_id.c_str(), rtabmap_conversions::timestampFromROS(msg->header.stamp));
2022-11-27 16:21:59 -08:00
return;
}
2022-11-27 16:21:59 -08:00
sensor_msgs::msg::PointCloud2 scanOutDeskewed;
2023-02-22 23:11:49 -08:00
rtabmap_conversions::transformPointCloud(t.toEigen4f(), scanOut, scanOutDeskewed);
2022-11-27 16:21:59 -08:00
pubScan_->publish(scanOutDeskewed);
}
2022-11-27 16:21:59 -08:00
void LidarDeskewing::callbackCloud(const sensor_msgs::msg::PointCloud2::ConstSharedPtr msg)
{
sensor_msgs::msg::PointCloud2 msgDeskewed;
2023-02-22 23:11:49 -08:00
if(rtabmap_conversions::deskew(*msg, msgDeskewed, fixedFrameId_, *tfBuffer_, waitForTransformDuration_, slerp_))
{
2022-11-27 16:21:59 -08:00
pubCloud_->publish(msgDeskewed);
}
2022-11-27 16:21:59 -08:00
else
{
2022-11-27 16:21:59 -08:00
// Just republish the msg to not breakdown downstream
// A warning should be already shown (see deskew() source code)
RCLCPP_WARN(this->get_logger(), "deskewing failed! returning possible skewed cloud!");
pubCloud_->publish(*msg);
}
2022-11-27 16:21:59 -08:00
}
}
2023-02-22 23:11:49 -08:00
#include "rclcpp_components/register_node_macro.hpp"
2023-02-22 23:11:49 -08:00
// 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.
RCLCPP_COMPONENTS_REGISTER_NODE(rtabmap_util::LidarDeskewing)