-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathOdometryServer.cpp
More file actions
257 lines (228 loc) · 12.4 KB
/
OdometryServer.cpp
File metadata and controls
257 lines (228 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// MIT License
//
// Copyright (c) 2022 Ignacio Vizzo, Tiziano Guadagnino, Benedikt Mersch, Cyrill
// Stachniss.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <Eigen/Core>
#include <memory>
#include <sophus/se3.hpp>
#include <utility>
#include <vector>
// KISS-ICP-ROS
#include "OdometryServer.hpp"
#include "Utils.hpp"
// KISS-ICP
#include "kiss_icp/pipeline/KissICP.hpp"
// ROS 2 headers
#include <tf2_ros/static_transform_broadcaster.h>
#include <tf2_ros/transform_broadcaster.h>
#include <geometry_msgs/msg/pose_stamped.hpp>
#include <geometry_msgs/msg/transform_stamped.hpp>
#include <nav_msgs/msg/odometry.hpp>
#include <rclcpp/qos.hpp>
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <std_msgs/msg/string.hpp>
#include <std_srvs/srv/empty.hpp>
#include <tf2_ros/static_transform_broadcaster.hpp>
#include <tf2_ros/transform_broadcaster.hpp>
namespace {
Sophus::SE3d LookupTransform(const std::string &target_frame,
const std::string &source_frame,
const std::unique_ptr<tf2_ros::Buffer> &tf2_buffer) {
std::string err_msg;
if (tf2_buffer->canTransform(target_frame, source_frame, tf2::TimePointZero, &err_msg)) {
try {
auto tf = tf2_buffer->lookupTransform(target_frame, source_frame, tf2::TimePointZero);
return tf2::transformToSophus(tf);
} catch (tf2::TransformException &ex) {
RCLCPP_WARN(rclcpp::get_logger("LookupTransform"), "%s", ex.what());
}
}
RCLCPP_WARN(rclcpp::get_logger("LookupTransform"), "Failed to find tf. Reason=%s",
err_msg.c_str());
// default construction is the identity
return Sophus::SE3d();
}
} // namespace
namespace kiss_icp_ros {
using utils::EigenToPointCloud2;
using utils::GetTimestamps;
using utils::PointCloud2ToEigen;
OdometryServer::OdometryServer(const rclcpp::NodeOptions &options)
: rclcpp::Node("kiss_icp_node", options) {
kiss_icp::pipeline::KISSConfig config;
initializeParameters(config);
// Construct the main KISS-ICP odometry node
kiss_icp_ = std::make_unique<kiss_icp::pipeline::KissICP>(config);
// Initialize subscribers
pointcloud_sub_ = create_subscription<sensor_msgs::msg::PointCloud2>(
"pointcloud_topic", rclcpp::SensorDataQoS(),
std::bind(&OdometryServer::RegisterFrame, this, std::placeholders::_1));
// Initialize publishers
rclcpp::QoS qos((rclcpp::SystemDefaultsQoS().keep_last(1).durability_volatile()));
odom_publisher_ = create_publisher<nav_msgs::msg::Odometry>("kiss/odometry", qos);
pose_publisher_ =
create_publisher<geometry_msgs::msg::PoseWithCovarianceStamped>("kiss/pose", qos);
if (publish_debug_clouds_) {
frame_publisher_ = create_publisher<sensor_msgs::msg::PointCloud2>("kiss/frame", qos);
kpoints_publisher_ = create_publisher<sensor_msgs::msg::PointCloud2>("kiss/keypoints", qos);
map_publisher_ = create_publisher<sensor_msgs::msg::PointCloud2>("kiss/local_map", qos);
}
// Initialize the transform broadcaster
tf_broadcaster_ = std::make_unique<tf2_ros::TransformBroadcaster>(*this);
tf2_buffer_ = std::make_unique<tf2_ros::Buffer>(this->get_clock());
tf2_buffer_->setUsingDedicatedThread(true);
tf2_listener_ = std::make_unique<tf2_ros::TransformListener>(*tf2_buffer_);
// Initialize service servers
reset_service_ = create_service<std_srvs::srv::Empty>(
"kiss/reset", std::bind(&OdometryServer::ResetService, this, std::placeholders::_1,
std::placeholders::_2));
RCLCPP_INFO(this->get_logger(), "KISS-ICP ROS 2 odometry node initialized");
}
void OdometryServer::initializeParameters(kiss_icp::pipeline::KISSConfig &config) {
RCLCPP_INFO(this->get_logger(), "Initializing parameters");
base_frame_ = declare_parameter<std::string>("base_frame", base_frame_);
RCLCPP_INFO(this->get_logger(), "\tBase frame: %s", base_frame_.c_str());
lidar_odom_frame_ = declare_parameter<std::string>("lidar_odom_frame", lidar_odom_frame_);
RCLCPP_INFO(this->get_logger(), "\tLiDAR odometry frame: %s", lidar_odom_frame_.c_str());
publish_odom_tf_ = declare_parameter<bool>("publish_odom_tf", publish_odom_tf_);
RCLCPP_INFO(this->get_logger(), "\tPublish odometry transform: %d", publish_odom_tf_);
invert_odom_tf_ = declare_parameter<bool>("invert_odom_tf", invert_odom_tf_);
RCLCPP_INFO(this->get_logger(), "\tInvert odometry transform: %d", invert_odom_tf_);
publish_debug_clouds_ = declare_parameter<bool>("publish_debug_clouds", publish_debug_clouds_);
RCLCPP_INFO(this->get_logger(), "\tPublish debug clouds: %d", publish_debug_clouds_);
position_covariance_ = declare_parameter<double>("position_covariance", 0.1);
RCLCPP_INFO(this->get_logger(), "\tPosition covariance: %.2f", position_covariance_);
orientation_covariance_ = declare_parameter<double>("orientation_covariance", 0.1);
RCLCPP_INFO(this->get_logger(), "\tOrientation covariance: %.2f", orientation_covariance_);
config.max_range = declare_parameter<double>("data.max_range", config.max_range);
RCLCPP_INFO(this->get_logger(), "\tMax range: %.2f", config.max_range);
config.min_range = declare_parameter<double>("data.min_range", config.min_range);
RCLCPP_INFO(this->get_logger(), "\tMin range: %.2f", config.min_range);
config.deskew = declare_parameter<bool>("data.deskew", config.deskew);
RCLCPP_INFO(this->get_logger(), "\tDeskew: %d", config.deskew);
config.voxel_size = declare_parameter<double>("mapping.voxel_size", config.max_range / 100.0);
RCLCPP_INFO(this->get_logger(), "\tVoxel size: %.2f", config.voxel_size);
config.max_points_per_voxel =
declare_parameter<int>("mapping.max_points_per_voxel", config.max_points_per_voxel);
RCLCPP_INFO(this->get_logger(), "\tMax points per voxel: %d", config.max_points_per_voxel);
config.initial_threshold =
declare_parameter<double>("adaptive_threshold.initial_threshold", config.initial_threshold);
RCLCPP_INFO(this->get_logger(), "\tInitial threshold: %.2f", config.initial_threshold);
config.min_motion_th =
declare_parameter<double>("adaptive_threshold.min_motion_th", config.min_motion_th);
RCLCPP_INFO(this->get_logger(), "\tMin motion threshold: %.2f", config.min_motion_th);
config.max_num_iterations =
declare_parameter<int>("registration.max_num_iterations", config.max_num_iterations);
RCLCPP_INFO(this->get_logger(), "\tMax number of iterations: %d", config.max_num_iterations);
config.convergence_criterion = declare_parameter<double>("registration.convergence_criterion",
config.convergence_criterion);
RCLCPP_INFO(this->get_logger(), "\tConvergence criterion: %.2f", config.convergence_criterion);
config.max_num_threads =
declare_parameter<int>("registration.max_num_threads", config.max_num_threads);
RCLCPP_INFO(this->get_logger(), "\tMax number of threads: %d", config.max_num_threads);
if (config.max_range < config.min_range) {
RCLCPP_WARN(get_logger(),
"[WARNING] max_range is smaller than min_range, setting min_range to 0.0");
config.min_range = 0.0;
}
}
void OdometryServer::RegisterFrame(const sensor_msgs::msg::PointCloud2::ConstSharedPtr &msg) {
const auto cloud_frame_id = msg->header.frame_id;
const auto points = PointCloud2ToEigen(msg);
const auto timestamps = GetTimestamps(msg);
// Register frame, main entry point to KISS-ICP pipeline
const auto &[frame, keypoints] = kiss_icp_->RegisterFrame(points, timestamps);
// Extract the last KISS-ICP pose, ego-centric to the LiDAR
const Sophus::SE3d kiss_pose = kiss_icp_->pose();
// Spit the current estimated pose to ROS msgs handling the desired target frame
PublishOdometry(kiss_pose, msg->header);
// Publishing these clouds is a bit costly, so do it only if we are debugging
if (publish_debug_clouds_) {
PublishClouds(frame, keypoints, msg->header);
}
}
void OdometryServer::PublishOdometry(const Sophus::SE3d &kiss_pose,
const std_msgs::msg::Header &header) {
// If necessary, transform the ego-centric pose to the specified base_link/base_footprint frame
const auto cloud_frame_id = header.frame_id;
const auto egocentric_estimation = (base_frame_.empty() || base_frame_ == cloud_frame_id);
const auto moving_frame = egocentric_estimation ? cloud_frame_id : base_frame_;
const auto pose = [&]() -> Sophus::SE3d {
if (egocentric_estimation) return kiss_pose;
const Sophus::SE3d cloud2base = LookupTransform(base_frame_, cloud_frame_id, tf2_buffer_);
return cloud2base * kiss_pose * cloud2base.inverse();
}();
// Broadcast the tf ---
if (publish_odom_tf_) {
geometry_msgs::msg::TransformStamped transform_msg;
transform_msg.header.stamp = header.stamp;
if (invert_odom_tf_) {
transform_msg.header.frame_id = moving_frame;
transform_msg.child_frame_id = lidar_odom_frame_;
transform_msg.transform = tf2::sophusToTransform(pose.inverse());
} else {
transform_msg.header.frame_id = lidar_odom_frame_;
transform_msg.child_frame_id = moving_frame;
transform_msg.transform = tf2::sophusToTransform(pose);
}
tf_broadcaster_->sendTransform(transform_msg);
}
// publish odometry msg
nav_msgs::msg::Odometry odom_msg;
odom_msg.header.stamp = header.stamp;
odom_msg.header.frame_id = lidar_odom_frame_;
odom_msg.child_frame_id = moving_frame;
odom_msg.pose.pose = tf2::sophusToPose(pose);
odom_msg.pose.covariance.fill(0.0);
odom_msg.pose.covariance[0] = position_covariance_;
odom_msg.pose.covariance[7] = position_covariance_;
odom_msg.pose.covariance[14] = position_covariance_;
odom_msg.pose.covariance[21] = orientation_covariance_;
odom_msg.pose.covariance[28] = orientation_covariance_;
odom_msg.pose.covariance[35] = orientation_covariance_;
odom_publisher_->publish(std::move(odom_msg));
geometry_msgs::msg::PoseWithCovarianceStamped pose_msg;
pose_msg.header = odom_msg.header;
pose_msg.pose = odom_msg.pose;
pose_publisher_->publish(pose_msg);
}
void OdometryServer::PublishClouds(const std::vector<Eigen::Vector3d> &frame,
const std::vector<Eigen::Vector3d> &keypoints,
const std_msgs::msg::Header &header) {
const auto kiss_map = kiss_icp_->LocalMap();
frame_publisher_->publish(std::move(EigenToPointCloud2(frame, header)));
kpoints_publisher_->publish(std::move(EigenToPointCloud2(keypoints, header)));
auto local_map_header = header;
local_map_header.frame_id = lidar_odom_frame_;
map_publisher_->publish(std::move(EigenToPointCloud2(kiss_map, local_map_header)));
}
void OdometryServer::ResetService(
[[maybe_unused]] const std::shared_ptr<std_srvs::srv::Empty::Request> request,
[[maybe_unused]] std::shared_ptr<std_srvs::srv::Empty::Response> response) {
RCLCPP_INFO(this->get_logger(), "Resetting KISS-ICP map and odometry");
// Reset the KISS-ICP pipeline
kiss_icp_->Reset();
RCLCPP_INFO(this->get_logger(), "KISS-ICP reset completed successfully");
}
} // namespace kiss_icp_ros
#include "rclcpp_components/register_node_macro.hpp"
RCLCPP_COMPONENTS_REGISTER_NODE(kiss_icp_ros::OdometryServer)