Skip to content

Commit f26d85b

Browse files
authored
feat: add ROS service to reset odometry (#479)
* feat: Implement a ROS service '/reset_odometry' that allows users to reset the internal state of the odometry and map. * This feature was requested in the closed issue #478.
1 parent 8a5597b commit f26d85b

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

cpp/kiss_icp/pipeline/KissICP.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,16 @@ KissICP::Vector3dVectorTuple KissICP::Voxelize(const std::vector<Eigen::Vector3d
7373
const auto source = kiss_icp::VoxelDownsample(frame_downsample, voxel_size * 1.5);
7474
return {source, frame_downsample};
7575
}
76+
void KissICP::Reset() {
77+
last_pose_ = Sophus::SE3d();
78+
last_delta_ = Sophus::SE3d();
79+
80+
// Clear the local map
81+
local_map_.Clear();
82+
83+
// Reset adaptive threshold (it will start fresh)
84+
adaptive_threshold_ =
85+
AdaptiveThreshold(config_.initial_threshold, config_.min_motion_th, config_.max_range);
86+
}
7687

7788
} // namespace kiss_icp::pipeline

cpp/kiss_icp/pipeline/KissICP.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class KissICP {
8282

8383
const Sophus::SE3d &delta() const { return last_delta_; }
8484
Sophus::SE3d &delta() { return last_delta_; }
85+
void Reset();
8586

8687
private:
8788
Sophus::SE3d last_pose_;

ros/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ find_package(rcutils REQUIRED)
4747
find_package(sensor_msgs REQUIRED)
4848
find_package(std_msgs REQUIRED)
4949
find_package(tf2_ros REQUIRED)
50+
find_package(std_srvs REQUIRED)
5051

5152
# ROS 2 node
5253
add_library(odometry_component SHARED src/OdometryServer.cpp)
@@ -62,6 +63,7 @@ ament_target_dependencies(
6263
rcutils
6364
sensor_msgs
6465
std_msgs
66+
std_srvs
6567
tf2_ros)
6668

6769
rclcpp_components_register_node(odometry_component PLUGIN "kiss_icp_ros::OdometryServer" EXECUTABLE kiss_icp_node)

ros/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<depend>sensor_msgs</depend>
4343
<depend>std_msgs</depend>
4444
<depend>tf2_ros</depend>
45+
<depend>std_srvs</depend>
4546

4647
<depend>eigen</depend>
4748
<depend>sophus</depend>

ros/src/OdometryServer.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include <rclcpp/rclcpp.hpp>
4545
#include <sensor_msgs/msg/point_cloud2.hpp>
4646
#include <std_msgs/msg/string.hpp>
47-
47+
#include <std_srvs/srv/empty.hpp>
4848
namespace {
4949
Sophus::SE3d LookupTransform(const std::string &target_frame,
5050
const std::string &source_frame,
@@ -98,6 +98,10 @@ OdometryServer::OdometryServer(const rclcpp::NodeOptions &options)
9898
tf2_buffer_ = std::make_unique<tf2_ros::Buffer>(this->get_clock());
9999
tf2_buffer_->setUsingDedicatedThread(true);
100100
tf2_listener_ = std::make_unique<tf2_ros::TransformListener>(*tf2_buffer_);
101+
// Initialize service servers
102+
reset_service_ = create_service<std_srvs::srv::Empty>(
103+
"kiss/reset", std::bind(&OdometryServer::ResetService, this, std::placeholders::_1,
104+
std::placeholders::_2));
101105

102106
RCLCPP_INFO(this->get_logger(), "KISS-ICP ROS 2 odometry node initialized");
103107
}
@@ -223,11 +227,20 @@ void OdometryServer::PublishClouds(const std::vector<Eigen::Vector3d> &frame,
223227

224228
frame_publisher_->publish(std::move(EigenToPointCloud2(frame, header)));
225229
kpoints_publisher_->publish(std::move(EigenToPointCloud2(keypoints, header)));
226-
227230
auto local_map_header = header;
228231
local_map_header.frame_id = lidar_odom_frame_;
229232
map_publisher_->publish(std::move(EigenToPointCloud2(kiss_map, local_map_header)));
230233
}
234+
void OdometryServer::ResetService(
235+
[[maybe_unused]] const std::shared_ptr<std_srvs::srv::Empty::Request> request,
236+
[[maybe_unused]] std::shared_ptr<std_srvs::srv::Empty::Response> response) {
237+
RCLCPP_INFO(this->get_logger(), "Resetting KISS-ICP map and odometry");
238+
239+
// Reset the KISS-ICP pipeline
240+
kiss_icp_->Reset();
241+
242+
RCLCPP_INFO(this->get_logger(), "KISS-ICP reset completed successfully");
243+
}
231244
} // namespace kiss_icp_ros
232245

233246
#include "rclcpp_components/register_node_macro.hpp"

ros/src/OdometryServer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <rclcpp/rclcpp.hpp>
3535
#include <sensor_msgs/msg/point_cloud2.hpp>
3636
#include <std_msgs/msg/header.hpp>
37+
#include <std_srvs/srv/empty.hpp>
3738
#include <string>
3839

3940
namespace kiss_icp_ros {
@@ -59,6 +60,8 @@ class OdometryServer : public rclcpp::Node {
5960
void PublishClouds(const std::vector<Eigen::Vector3d> &frame,
6061
const std::vector<Eigen::Vector3d> &keypoints,
6162
const std_msgs::msg::Header &header);
63+
void ResetService(const std::shared_ptr<std_srvs::srv::Empty::Request> request,
64+
std::shared_ptr<std_srvs::srv::Empty::Response> response);
6265

6366
private:
6467
/// Tools for broadcasting TFs.
@@ -78,6 +81,9 @@ class OdometryServer : public rclcpp::Node {
7881
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr kpoints_publisher_;
7982
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr map_publisher_;
8083

84+
/// Service servers.
85+
rclcpp::Service<std_srvs::srv::Empty>::SharedPtr reset_service_;
86+
8187
/// KISS-ICP
8288
std::unique_ptr<kiss_icp::pipeline::KissICP> kiss_icp_;
8389

0 commit comments

Comments
 (0)