|
| 1 | +// Copyright 2025 Ekumen, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef MH_AMCL__MH_AMCL_HPP_ |
| 16 | +#define MH_AMCL__MH_AMCL_HPP_ |
| 17 | + |
| 18 | +#include <list> |
| 19 | +#include <memory> |
| 20 | +#include <mutex> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <Eigen/Dense> |
| 24 | +#include <Eigen/LU> |
| 25 | + |
| 26 | +// The particle traits need to be included before the particle_cloud |
| 27 | +#include "beluga_demo_mh_amcl/mh_amcl_particle_traits.hpp" |
| 28 | +#include <beluga_ros/particle_cloud.hpp> |
| 29 | + |
| 30 | +#include <geometry_msgs/msg/pose_array.hpp> |
| 31 | +#include <geometry_msgs/msg/pose_with_covariance_stamped.hpp> |
| 32 | +#include <nav_msgs/msg/occupancy_grid.hpp> |
| 33 | +#include <rclcpp/rclcpp.hpp> |
| 34 | +#include <rclcpp_lifecycle/lifecycle_node.hpp> |
| 35 | +#include <sensor_msgs/msg/laser_scan.hpp> |
| 36 | +#include <tf2/LinearMath/Transform.h> |
| 37 | +#include <tf2/transform_datatypes.h> |
| 38 | +#include <tf2_ros/buffer.h> |
| 39 | +#include <tf2_ros/transform_broadcaster.h> |
| 40 | +#include <tf2_ros/transform_listener.h> |
| 41 | + |
| 42 | +#include "beluga_demo_mh_amcl/map_matcher.hpp" |
| 43 | +#include "beluga_demo_mh_amcl/particles_distribution.hpp" |
| 44 | + |
| 45 | +namespace mh_amcl { |
| 46 | + |
| 47 | +using CallbackReturnT = |
| 48 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn; |
| 49 | + |
| 50 | +/** |
| 51 | + * MH_AMCL_Node class |
| 52 | + * |
| 53 | + * Serves as the main lifecycle node, integrating all the components. |
| 54 | + * Instantiates a list of 'ParticlesDistribution', each element representing a |
| 55 | + * set of particles (hypothesis), and the map matcher. |
| 56 | + */ |
| 57 | +class MH_AMCL_Node : public rclcpp_lifecycle::LifecycleNode { |
| 58 | +public: |
| 59 | + explicit MH_AMCL_Node( |
| 60 | + const rclcpp::NodeOptions &options = rclcpp::NodeOptions()); |
| 61 | + |
| 62 | + // Initialization |
| 63 | + void init(); |
| 64 | + |
| 65 | + // Lifecycle node methods |
| 66 | + CallbackReturnT on_configure(const rclcpp_lifecycle::State &state) override; |
| 67 | + CallbackReturnT on_activate(const rclcpp_lifecycle::State &state) override; |
| 68 | + CallbackReturnT on_deactivate(const rclcpp_lifecycle::State &state) override; |
| 69 | + CallbackReturnT on_cleanup(const rclcpp_lifecycle::State &state) override; |
| 70 | + CallbackReturnT on_shutdown(const rclcpp_lifecycle::State &state) override; |
| 71 | + CallbackReturnT on_error(const rclcpp_lifecycle::State &state) override; |
| 72 | + |
| 73 | +protected: |
| 74 | + // Steps of the MH-AMCL algorithm |
| 75 | + void predict(); |
| 76 | + void correct(); |
| 77 | + void reseed(); |
| 78 | + void manage_hypotesis(); |
| 79 | + |
| 80 | + // Methods to publish the results of the algorithm (for localization and for |
| 81 | + // visualization) |
| 82 | + void publish_particles(); |
| 83 | + void publish_position(); |
| 84 | + |
| 85 | + // Auxiliar methods |
| 86 | + void get_distances(const geometry_msgs::msg::Pose &pose1, |
| 87 | + const geometry_msgs::msg::Pose &pose2, double &dist_xy, |
| 88 | + double &dist_theta); |
| 89 | + unsigned char get_cost(const geometry_msgs::msg::Pose &pose); |
| 90 | + geometry_msgs::msg::Pose toMsg(const tf2::Transform &tf); |
| 91 | + |
| 92 | +private: |
| 93 | + // Publishers and subscribers |
| 94 | + rclcpp::Subscription<nav_msgs::msg::OccupancyGrid>::SharedPtr map_sub_; |
| 95 | + rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr laser_sub_; |
| 96 | + rclcpp::Subscription<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr |
| 97 | + init_pose_sub_; |
| 98 | + rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr |
| 99 | + pose_pub_; |
| 100 | + rclcpp::Publisher<geometry_msgs::msg::PoseArray>::SharedPtr particles_pub_; |
| 101 | + |
| 102 | + // Timers |
| 103 | + rclcpp::TimerBase::SharedPtr predict_timer_; |
| 104 | + rclcpp::TimerBase::SharedPtr correct_timer_; |
| 105 | + rclcpp::TimerBase::SharedPtr reseed_timer_; |
| 106 | + rclcpp::TimerBase::SharedPtr hypotesys_timer_; |
| 107 | + rclcpp::TimerBase::SharedPtr publish_particles_timer_; |
| 108 | + rclcpp::TimerBase::SharedPtr publish_position_timer_; |
| 109 | + |
| 110 | + // Configurable params |
| 111 | + int max_hypotheses_; |
| 112 | + bool multihypothesis_; |
| 113 | + float min_candidate_weight_; |
| 114 | + double min_candidate_distance_; |
| 115 | + double min_candidate_angle_; |
| 116 | + float low_q_hypo_threshold_; |
| 117 | + float very_low_q_hypo_threshold_; |
| 118 | + double hypo_merge_distance_; |
| 119 | + double hypo_merge_angle_; |
| 120 | + float good_hypo_threshold_; |
| 121 | + float min_hypo_diff_winner_; |
| 122 | + |
| 123 | + // Time used as the timestamp for the published results |
| 124 | + rclcpp::Time last_time_; |
| 125 | + |
| 126 | + // Particles |
| 127 | + std::list<std::shared_ptr<ParticlesDistribution>> particles_population_; |
| 128 | + std::shared_ptr<ParticlesDistribution> current_hypothesis_; |
| 129 | + float current_hypothesis_q_; |
| 130 | + |
| 131 | + // Transformations |
| 132 | + tf2::BufferCore tf_buffer_; |
| 133 | + tf2_ros::TransformListener tf_listener_; |
| 134 | + std::shared_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_; |
| 135 | + tf2::Stamped<tf2::Transform> odom2prevbf_; |
| 136 | + bool valid_prev_odom2bf_{false}; |
| 137 | + |
| 138 | + // Map-matching |
| 139 | + std::shared_ptr<beluga_ros::OccupancyGrid> costmap_; |
| 140 | + sensor_msgs::msg::LaserScan::UniquePtr last_laser_; |
| 141 | + std::shared_ptr<mh_amcl::MapMatcher> map_matcher_; |
| 142 | + |
| 143 | + // Callbacks |
| 144 | + void map_callback(const nav_msgs::msg::OccupancyGrid::ConstSharedPtr &msg); |
| 145 | + void laser_callback(sensor_msgs::msg::LaserScan::UniquePtr lsr_msg); |
| 146 | + void initpose_callback( |
| 147 | + const geometry_msgs::msg::PoseWithCovarianceStamped::ConstSharedPtr |
| 148 | + &pose_msg); |
| 149 | +}; |
| 150 | + |
| 151 | +} // namespace mh_amcl |
| 152 | + |
| 153 | +#endif // MH_AMCL__MH_AMCL_HPP_ |
0 commit comments