Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
adaptive_tolerance_goal_checker.hpp
1 // Copyright (c) 2026, David Grbac
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 NAV2_CONTROLLER__PLUGINS__ADAPTIVE_TOLERANCE_GOAL_CHECKER_HPP_
16 #define NAV2_CONTROLLER__PLUGINS__ADAPTIVE_TOLERANCE_GOAL_CHECKER_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <limits>
22 
23 #include "rclcpp/logger.hpp"
24 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
25 #include "rclcpp/parameter.hpp"
26 #include "nav2_ros_common/lifecycle_node.hpp"
27 #include "nav2_core/goal_checker.hpp"
28 #include "rcl_interfaces/msg/set_parameters_result.hpp"
29 
30 namespace nav2_controller
31 {
32 
43 {
44 public:
53 
60  void initialize(
61  const nav2::LifecycleNode::WeakPtr & parent,
62  const std::string & plugin_name,
63  const std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros) override;
64 
68  void reset() override;
69 
78  bool isGoalReached(
79  const geometry_msgs::msg::Pose & query_pose,
80  const geometry_msgs::msg::Pose & goal_pose,
81  const geometry_msgs::msg::Twist & velocity,
82  const nav_msgs::msg::Path & transformed_global_plan) override;
83 
92  bool isGoalXYReached(
93  const geometry_msgs::msg::Pose & query_pose,
94  const geometry_msgs::msg::Pose & goal_pose,
95  const geometry_msgs::msg::Twist & velocity,
96  const nav_msgs::msg::Path & transformed_global_plan) override;
97 
105  bool getTolerances(
106  geometry_msgs::msg::Pose & pose_tolerance,
107  geometry_msgs::msg::Twist & vel_tolerance,
108  double & path_length_tolerance) override;
109 
110 protected:
113  {
114  NONE,
115  FINE_TOLERANCE,
116  COARSE_TOLERANCE_FINISH_LINE,
117  COARSE_TOLERANCE_STOPPED_STAGNATION,
118  COARSE_TOLERANCE_DISTANCE_STAGNATION
119  };
120 
121  nav2::LifecycleNode::WeakPtr node_;
122  rclcpp::Logger logger_{rclcpp::get_logger("adaptive_tolerance_goal_checker")};
123 
124  // Fine (desired) tolerance
125  double fine_xy_goal_tolerance_;
126  double fine_xy_goal_tolerance_sq_;
127  // Coarse (fallback) tolerance
128  double coarse_xy_goal_tolerance_;
129  double coarse_xy_goal_tolerance_sq_;
130  // Hysteresis buffer used when stateful to reset the
131  // check_xy_ when the robot drifts outside the accepted region.
132  double xy_goal_tolerance_buffer_;
133  double fine_xy_goal_tolerance_reset_sq_, coarse_xy_goal_tolerance_reset_sq_;
134 
135  double yaw_goal_tolerance_;
136  double path_length_tolerance_;
137  bool stateful_;
138  bool symmetric_yaw_tolerance_;
139 
140  // Velocity thresholds for detecting a stopped/stalled robot
141  double trans_stopped_velocity_;
142  double rot_stopped_velocity_;
143 
144  // Number of consecutive stopped cycles before accepting at coarse tolerance
145  int required_stagnation_cycles_;
146 
147  // Stateful tracking
148  bool check_xy_;
149  bool in_tolerance_zone_;
150  int stopped_stagnation_count_;
151  int distance_stagnation_count_;
152  double best_distance_sq_;
153  double approach_dx_;
154  double approach_dy_;
155  XyAcceptanceReason xy_acceptance_reason_;
156 
157  // Dynamic parameters
158  std::mutex mutex_;
159  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
160  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
161  std::string plugin_name_;
162 
168  static std::string toString(XyAcceptanceReason reason);
169 
178  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
179  const std::vector<rclcpp::Parameter> & parameters);
180 
187  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
188 };
189 
190 } // namespace nav2_controller
191 
192 #endif // NAV2_CONTROLLER__PLUGINS__ADAPTIVE_TOLERANCE_GOAL_CHECKER_HPP_
Goal Checker plugin with two tolerance tiers: a tight desired tolerance and a looser coarse tolerance...
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
bool isGoalXYReached(const geometry_msgs::msg::Pose &query_pose, const geometry_msgs::msg::Pose &goal_pose, const geometry_msgs::msg::Twist &velocity, const nav_msgs::msg::Path &transformed_global_plan) override
Check if XY goal position has been reached (without considering yaw)
XyAcceptanceReason
Reason that the XY component of the goal was accepted.
bool getTolerances(geometry_msgs::msg::Pose &pose_tolerance, geometry_msgs::msg::Twist &vel_tolerance, double &path_length_tolerance) override
Get the position and velocity tolerances.
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
void initialize(const nav2::LifecycleNode::WeakPtr &parent, const std::string &plugin_name, const std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros) override
Initialize the goal checker.
~AdaptiveToleranceGoalChecker()
Destroy the Progress Goal Checker object.
void reset() override
Reset the goal checker state.
AdaptiveToleranceGoalChecker()
Construct a new Progress Goal Checker object.
static std::string toString(XyAcceptanceReason reason)
Convert XY acceptance reason to string.
bool isGoalReached(const geometry_msgs::msg::Pose &query_pose, const geometry_msgs::msg::Pose &goal_pose, const geometry_msgs::msg::Twist &velocity, const nav_msgs::msg::Path &transformed_global_plan) override
Check if the goal is reached.
Function-object for checking whether a goal has been reached.