Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
amcl_node.hpp
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 
21 #ifndef NAV2_AMCL__AMCL_NODE_HPP_
22 #define NAV2_AMCL__AMCL_NODE_HPP_
23 
24 #include <atomic>
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "geometry_msgs/msg/pose_stamped.hpp"
32 #include "message_filters/subscriber.h"
33 #include "nav2_util/lifecycle_node.hpp"
34 #include "nav2_amcl/motion_model/motion_model.hpp"
35 #include "nav2_amcl/sensors/laser/laser.hpp"
36 #include "nav2_msgs/msg/particle.hpp"
37 #include "nav2_msgs/msg/particle_cloud.hpp"
38 #include "nav2_msgs/srv/set_initial_pose.hpp"
39 #include "nav_msgs/srv/set_map.hpp"
40 #include "sensor_msgs/msg/laser_scan.hpp"
41 #include "std_srvs/srv/empty.hpp"
42 #include "tf2_ros/transform_broadcaster.h"
43 #include "tf2_ros/transform_listener.h"
44 #include "pluginlib/class_loader.hpp"
45 
46 #pragma GCC diagnostic push
47 #pragma GCC diagnostic ignored "-Wunused-parameter"
48 #pragma GCC diagnostic ignored "-Wreorder"
49 #include "tf2_ros/message_filter.h"
50 #pragma GCC diagnostic pop
51 
52 #define NEW_UNIFORM_SAMPLING 1
53 
54 namespace nav2_amcl
55 {
56 /*
57  * @class AmclNode
58  * @brief ROS wrapper for AMCL
59  */
61 {
62 public:
63  /*
64  * @brief AMCL constructor
65  * @param options Additional options to control creation of the node.
66  */
67  explicit AmclNode(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
68  /*
69  * @brief AMCL destructor
70  */
71  ~AmclNode();
72 
73 protected:
74  /*
75  * @brief Lifecycle configure
76  */
77  nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
78  /*
79  * @brief Lifecycle activate
80  */
81  nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
82  /*
83  * @brief Lifecycle deactivate
84  */
85  nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
86  /*
87  * @brief Lifecycle cleanup
88  */
89  nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
90  /*
91  * @brief Lifecycle shutdown
92  */
93  nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
94 
99  rcl_interfaces::msg::SetParametersResult
100  dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters);
101 
102  // Dynamic parameters handler
103  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_;
104 
105  // Since the sensor data from gazebo or the robot is not lifecycle enabled, we won't
106  // respond until we're in the active state
107  std::atomic<bool> active_{false};
108 
109  // Dedicated callback group and executor for services and subscriptions in AmclNode,
110  // in order to isolate TF timer used in message filter.
111  rclcpp::CallbackGroup::SharedPtr callback_group_;
112  rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
113  std::unique_ptr<nav2_util::NodeThread> executor_thread_;
114 
115  // Pose hypothesis
116  typedef struct
117  {
118  double weight; // Total weight (weights sum to 1)
119  pf_vector_t pf_pose_mean; // Mean of pose esimate
120  pf_matrix_t pf_pose_cov; // Covariance of pose estimate
121  } amcl_hyp_t;
122 
123  // Map-related
124  /*
125  * @brief Get new map from ROS topic to localize in
126  * @param msg Map message
127  */
128  void mapReceived(const nav_msgs::msg::OccupancyGrid::SharedPtr msg);
129  /*
130  * @brief Handle a new map message
131  * @param msg Map message
132  */
133  void handleMapMessage(const nav_msgs::msg::OccupancyGrid & msg);
134  /*
135  * @brief Creates lookup table of free cells in map
136  */
137  void createFreeSpaceVector();
138  /*
139  * @brief Frees allocated map related memory
140  */
141  void freeMapDependentMemory();
142  map_t * map_{nullptr};
143  /*
144  * @brief Convert an occupancy grid map to an AMCL map
145  * @param map_msg Map message
146  * @return pointer to map for AMCL to use
147  */
148  map_t * convertMap(const nav_msgs::msg::OccupancyGrid & map_msg);
149  bool first_map_only_{true};
150  std::atomic<bool> first_map_received_{false};
151  amcl_hyp_t * initial_pose_hyp_;
152  std::recursive_mutex mutex_;
153  rclcpp::Subscription<nav_msgs::msg::OccupancyGrid>::ConstSharedPtr map_sub_;
154 #if NEW_UNIFORM_SAMPLING
155  struct Point2D { int32_t x; int32_t y; };
156  static std::vector<Point2D> free_space_indices;
157 #endif
158 
159  // Transforms
160  /*
161  * @brief Initialize required ROS transformations
162  */
163  void initTransforms();
164  std::shared_ptr<tf2_ros::TransformBroadcaster> tf_broadcaster_;
165  std::shared_ptr<tf2_ros::TransformListener> tf_listener_;
166  std::shared_ptr<tf2_ros::Buffer> tf_buffer_;
167  bool sent_first_transform_{false};
168  bool latest_tf_valid_{false};
169  tf2::Transform latest_tf_;
170 
171  // Message filters
172  /*
173  * @brief Initialize incoming data message subscribers and filters
174  */
175  void initMessageFilters();
176  std::unique_ptr<message_filters::Subscriber<sensor_msgs::msg::LaserScan,
177  rclcpp_lifecycle::LifecycleNode>> laser_scan_sub_;
178  std::unique_ptr<tf2_ros::MessageFilter<sensor_msgs::msg::LaserScan>> laser_scan_filter_;
179  message_filters::Connection laser_scan_connection_;
180 
181  // Publishers and subscribers
182  /*
183  * @brief Initialize pub subs of AMCL
184  */
185  void initPubSub();
186  rclcpp::Subscription<geometry_msgs::msg::PoseWithCovarianceStamped>::ConstSharedPtr
187  initial_pose_sub_;
188  rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr
189  pose_pub_;
190  rclcpp_lifecycle::LifecyclePublisher<nav2_msgs::msg::ParticleCloud>::SharedPtr
191  particle_cloud_pub_;
192  /*
193  * @brief Handle with an initial pose estimate is received
194  */
195  void initialPoseReceived(geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr msg);
196  /*
197  * @brief Handle when a laser scan is received
198  */
199  void laserReceived(sensor_msgs::msg::LaserScan::ConstSharedPtr laser_scan);
200 
201  // Services and service callbacks
202  /*
203  * @brief Initialize state services
204  */
205  void initServices();
206  rclcpp::Service<std_srvs::srv::Empty>::SharedPtr global_loc_srv_;
207  /*
208  * @brief Service callback for a global relocalization request
209  */
210  void globalLocalizationCallback(
211  const std::shared_ptr<rmw_request_id_t> request_header,
212  const std::shared_ptr<std_srvs::srv::Empty::Request> request,
213  std::shared_ptr<std_srvs::srv::Empty::Response> response);
214 
215  // service server for providing an initial pose guess
216  rclcpp::Service<nav2_msgs::srv::SetInitialPose>::SharedPtr initial_guess_srv_;
217  /*
218  * @brief Service callback for an initial pose guess request
219  */
220  void initialPoseReceivedSrv(
221  const std::shared_ptr<rmw_request_id_t> request_header,
222  const std::shared_ptr<nav2_msgs::srv::SetInitialPose::Request> request,
223  std::shared_ptr<nav2_msgs::srv::SetInitialPose::Response> response);
224 
225  // Let amcl update samples without requiring motion
226  rclcpp::Service<std_srvs::srv::Empty>::SharedPtr nomotion_update_srv_;
227  /*
228  * @brief Request an AMCL update even though the robot hasn't moved
229  */
230  void nomotionUpdateCallback(
231  const std::shared_ptr<rmw_request_id_t> request_header,
232  const std::shared_ptr<std_srvs::srv::Empty::Request> request,
233  std::shared_ptr<std_srvs::srv::Empty::Response> response);
234 
235  // Nomotion update control. Used to temporarily let amcl update samples even when no motion occurs
236  std::atomic<bool> force_update_{false};
237 
238  // Odometry
239  /*
240  * @brief Initialize odometry
241  */
242  void initOdometry();
243  std::shared_ptr<nav2_amcl::MotionModel> motion_model_;
244  geometry_msgs::msg::PoseStamped latest_odom_pose_;
245  geometry_msgs::msg::PoseWithCovarianceStamped last_published_pose_;
246  double init_pose_[3]; // Initial robot pose
247  double init_cov_[3];
248  pluginlib::ClassLoader<nav2_amcl::MotionModel> plugin_loader_{"nav2_amcl",
249  "nav2_amcl::MotionModel"};
250  /*
251  * @brief Get robot pose in odom frame using TF
252  */
253  bool getOdomPose(
254  // Helper to get odometric pose from transform system
255  geometry_msgs::msg::PoseStamped & pose,
256  double & x, double & y, double & yaw,
257  const rclcpp::Time & sensor_timestamp, const std::string & frame_id);
258  std::atomic<bool> first_pose_sent_;
259 
260  // Particle filter
261  /*
262  * @brief Initialize particle filter
263  */
264  void initParticleFilter();
265  /*
266  * @brief Pose-generating function used to uniformly distribute particles over the map
267  */
268  static pf_vector_t uniformPoseGenerator(void * arg);
269  pf_t * pf_{nullptr};
270  bool pf_init_;
271  pf_vector_t pf_odom_pose_;
272  int resample_count_{0};
273 
274  // Laser scan related
275  /*
276  * @brief Initialize laser scan
277  */
278  void initLaserScan();
279  /*
280  * @brief Create a laser object
281  */
282  nav2_amcl::Laser * createLaserObject();
283  int scan_error_count_{0};
284  std::vector<nav2_amcl::Laser *> lasers_;
285  std::vector<bool> lasers_update_;
286  std::map<std::string, int> frame_to_laser_;
287  rclcpp::Time last_laser_received_ts_;
288 
289  /*
290  * @brief Check if sufficient time has elapsed to get an update
291  */
292  bool checkElapsedTime(std::chrono::seconds check_interval, rclcpp::Time last_time);
293  rclcpp::Time last_time_printed_msg_;
294  /*
295  * @brief Add a new laser scanner if a new one is received in the laser scallbacks
296  */
297  bool addNewScanner(
298  int & laser_index,
299  const sensor_msgs::msg::LaserScan::ConstSharedPtr & laser_scan,
300  const std::string & laser_scan_frame_id,
301  geometry_msgs::msg::PoseStamped & laser_pose);
302  /*
303  * @brief Whether the pf needs to be updated
304  */
305  bool shouldUpdateFilter(const pf_vector_t pose, pf_vector_t & delta);
306  /*
307  * @brief Update the PF
308  */
309  bool updateFilter(
310  const int & laser_index,
311  const sensor_msgs::msg::LaserScan::ConstSharedPtr & laser_scan,
312  const pf_vector_t & pose);
313  /*
314  * @brief Publish particle cloud
315  */
316  void publishParticleCloud(const pf_sample_set_t * set);
317  /*
318  * @brief Get the current state estimat hypothesis from the particle cloud
319  */
320  bool getMaxWeightHyp(
321  std::vector<amcl_hyp_t> & hyps, amcl_hyp_t & max_weight_hyps,
322  int & max_weight_hyp);
323  /*
324  * @brief Publish robot pose in map frame from AMCL
325  */
326  void publishAmclPose(
327  const sensor_msgs::msg::LaserScan::ConstSharedPtr & laser_scan,
328  const std::vector<amcl_hyp_t> & hyps, const int & max_weight_hyp);
329  /*
330  * @brief Determine TF transformation from map to odom
331  */
332  void calculateMaptoOdomTransform(
333  const sensor_msgs::msg::LaserScan::ConstSharedPtr & laser_scan,
334  const std::vector<amcl_hyp_t> & hyps,
335  const int & max_weight_hyp);
336  /*
337  * @brief Publish TF transformation from map to odom
338  */
339  void sendMapToOdomTransform(const tf2::TimePoint & transform_expiration);
340  /*
341  * @brief Handle a new pose estimate callback
342  */
343  void handleInitialPose(geometry_msgs::msg::PoseWithCovarianceStamped & msg);
344  bool init_pose_received_on_inactive{false};
345  bool initial_pose_is_known_{false};
346  bool set_initial_pose_{false};
347  bool always_reset_initial_pose_;
348  double initial_pose_x_;
349  double initial_pose_y_;
350  double initial_pose_z_;
351  double initial_pose_yaw_;
352 
353  /*
354  * @brief Get ROS parameters for node
355  */
356  void initParameters();
357  double alpha1_;
358  double alpha2_;
359  double alpha3_;
360  double alpha4_;
361  double alpha5_;
362  std::string base_frame_id_;
363  double beam_skip_distance_;
364  double beam_skip_error_threshold_;
365  double beam_skip_threshold_;
366  bool do_beamskip_;
367  std::string global_frame_id_;
368  double lambda_short_;
369  double laser_likelihood_max_dist_;
370  double laser_max_range_;
371  double laser_min_range_;
372  std::string sensor_model_type_;
373  int max_beams_;
374  int max_particles_;
375  int min_particles_;
376  std::string odom_frame_id_;
377  double pf_err_;
378  double pf_z_;
379  double alpha_fast_;
380  double alpha_slow_;
381  int resample_interval_;
382  std::string robot_model_type_;
383  tf2::Duration save_pose_period_;
384  double sigma_hit_;
385  bool tf_broadcast_;
386  tf2::Duration transform_tolerance_;
387  double a_thresh_;
388  double d_thresh_;
389  double z_hit_;
390  double z_max_;
391  double z_short_;
392  double z_rand_;
393  std::string scan_topic_{"scan"};
394  std::string map_topic_{"map"};
395  bool freespace_downsampling_ = false;
396 };
397 
398 } // namespace nav2_amcl
399 
400 #endif // NAV2_AMCL__AMCL_NODE_HPP_
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
Definition: amcl_node.cpp:1171
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
Definition: pf.hpp:112
Definition: map.hpp:62