EasyNav Plugins
Loading...
Searching...
No Matches
AMCLLocalizer.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
2//
3// This file is part of the project Easy Navigation (EasyNav in short)
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
18
19#ifndef EASYNAV_SIMPLE_LOCALIZER__AMCLLOCALIZER_HPP_
20#define EASYNAV_SIMPLE_LOCALIZER__AMCLLOCALIZER_HPP_
21
22#include <vector>
23#include <random>
24#include <Eigen/Geometry>
25
26#include "geometry_msgs/msg/pose_array.hpp"
27#include "geometry_msgs/msg/pose_with_covariance_stamped.hpp"
28#include "nav_msgs/msg/odometry.hpp"
29
30#include "tf2/LinearMath/Transform.hpp"
31#include "tf2_ros/transform_broadcaster.hpp"
32
33#include "easynav_core/LocalizerMethodBase.hpp"
34
35namespace easynav
36{
37
39struct Particle
40{
41 tf2::Transform pose;
42 int hits;
43 int possible_hits;
44 double weight;
45};
46
48class AMCLLocalizer : public LocalizerMethodBase
49{
50public:
55
60
68 virtual void on_initialize() override;
69
77 void update_rt(NavState & nav_state) override;
78
86 void update(NavState & nav_state) override;
87
93 tf2::Transform getEstimatedPose() const;
94
100 nav_msgs::msg::Odometry get_pose();
101
102protected:
107
113 void publishTF(const tf2::Transform & map2bf);
114
119
125 void publishEstimatedPose(const tf2::Transform & est_pose);
126
132 void predict(NavState & nav_state);
133
139 void correct(NavState & nav_state);
140
144 void reseed();
145
147 std::unique_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_;
148
150 rclcpp::Publisher<geometry_msgs::msg::PoseArray>::SharedPtr particles_pub_;
151
153 rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr estimate_pub_;
154
156 rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub_;
157
163 void odom_callback(nav_msgs::msg::Odometry::UniquePtr msg);
164
166 std::vector<Particle> particles_;
167
169 std::default_random_engine rng_;
170
172 nav_msgs::msg::Odometry pose_;
173
175 double noise_translation_ {0.01};
176
178 double noise_rotation_ {0.01};
179
181 double noise_translation_to_rotation_ {0.01};
182
184 double min_noise_xy_ {0.05};
185
187 double min_noise_yaw_ {0.05};
188
190 tf2::Transform odom_{tf2::Transform::getIdentity()};
191
193 tf2::Transform last_odom_{tf2::Transform::getIdentity()};
194
196 bool initialized_odom_ = false;
197
199 double reseed_time_;
200
202 rclcpp::Time last_reseed_;
203
205 rclcpp::Time last_input_time_;
206};
207
208} // namespace easynav
209
210#endif // EASYNAV_PLANNER__SIMPLEMAPMANAGER_HPP_
A localization method implementing a simplified AMCL (Adaptive Monte Carlo Localization) approach.
Definition AMCLLocalizer.hpp:49
double min_noise_yaw_
Minimum yaw noise threshold.
Definition AMCLLocalizer.hpp:204
virtual void on_initialize() override
Initializes the localization method.
std::unique_ptr< tf2_ros::TransformBroadcaster > tf_broadcaster_
TF broadcaster to publish map to base_footprint transform.
Definition AMCLLocalizer.hpp:147
void update(NavState &nav_state) override
General update of the localization state.
double min_noise_xy_
Minimum translation noise threshold.
Definition AMCLLocalizer.hpp:201
nav_msgs::msg::Odometry pose_
Current estimated odometry-based pose.
Definition AMCLLocalizer.hpp:189
~AMCLLocalizer()
Destructor.
rclcpp::Subscription< nav_msgs::msg::Odometry >::SharedPtr odom_sub_
Subscriber for odometry messages.
Definition AMCLLocalizer.hpp:156
rclcpp::Publisher< geometry_msgs::msg::PoseArray >::SharedPtr particles_pub_
Publisher for visualization of the particle cloud.
Definition AMCLLocalizer.hpp:150
rclcpp::Time last_reseed_
Timestamp of the last reseed event.
Definition AMCLLocalizer.hpp:225
double reseed_time_
Time interval (in seconds) after which the particles should be reseeded.
Definition AMCLLocalizer.hpp:219
void update_rt(NavState &nav_state) override
Real-time update of the localization state.
void publishEstimatedPose(const tf2::Transform &est_pose)
Publishes the estimated pose with covariance.
rclcpp::Time last_input_time_
Timestamp of the last input message (odometry or initial pose).
Definition AMCLLocalizer.hpp:222
double noise_translation_to_rotation_
Coupling noise between translation and rotation.
Definition AMCLLocalizer.hpp:198
void publishTF(const tf2::Transform &map2bf)
Publishes a TF transform between map and base footprint.
void odom_callback(nav_msgs::msg::Odometry::UniquePtr msg)
Callback for receiving odometry updates.
tf2::Transform odom_
Last odometry transform received.
Definition AMCLLocalizer.hpp:210
tf2::Transform last_odom_
Previous odometry transform (used to compute deltas).
Definition AMCLLocalizer.hpp:213
void predict(NavState &nav_state)
Applies the motion model to update particle poses.
nav_msgs::msg::Odometry get_pose()
Gets the current estimated pose as an Odometry message.
double noise_rotation_
Rotational noise standard deviation.
Definition AMCLLocalizer.hpp:195
std::mt19937 rng_
Random number generator used for sampling noise.
Definition AMCLLocalizer.hpp:186
void publishParticles()
Publishes the current set of particles.
rclcpp::Publisher< geometry_msgs::msg::PoseWithCovarianceStamped >::SharedPtr estimate_pub_
Publisher for the estimated robot pose with covariance.
Definition AMCLLocalizer.hpp:153
tf2::Transform getEstimatedPose() const
Gets the current estimated pose as a transform.
double noise_translation_
Translational noise standard deviation.
Definition AMCLLocalizer.hpp:192
bool initialized_odom_
Flag indicating if the odometry has been initialized.
Definition AMCLLocalizer.hpp:216
void reseed()
Re-initializes the particle cloud if necessary.
void correct(NavState &nav_state)
Applies the sensor model to update particle weights.
AMCLLocalizer()
Default constructor.
std::vector< Particle > particles_
List of particles representing the belief distribution.
Definition AMCLLocalizer.hpp:183
void initializeParticles()
Initializes the set of particles.
Provides a mapping for often used cost values.
Definition cost_values.hpp:41
Structure representing a single particle in the AMCL algorithm.
Definition AMCLLocalizer.hpp:40
int hits
Number of sensor matches (hits) for this particle.
Definition AMCLLocalizer.hpp:42
double weight
Normalized importance weight of the particle.
Definition AMCLLocalizer.hpp:44
int possible_hits
Maximum number of possible hits.
Definition AMCLLocalizer.hpp:43
tf2::Transform pose
Estimated pose of the particle.
Definition AMCLLocalizer.hpp:41