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/rclcpp.hpp"
24 #include "nav2_ros_common/lifecycle_node.hpp"
25 #include "nav2_core/goal_checker.hpp"
26 #include "rcl_interfaces/msg/set_parameters_result.hpp"
27 
28 namespace nav2_controller
29 {
30 
41 {
42 public:
51 
58  void initialize(
59  const nav2::LifecycleNode::WeakPtr & parent,
60  const std::string & plugin_name,
61  const std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros) override;
62 
66  void reset() override;
67 
76  bool isGoalReached(
77  const geometry_msgs::msg::Pose & query_pose,
78  const geometry_msgs::msg::Pose & goal_pose,
79  const geometry_msgs::msg::Twist & velocity,
80  const nav_msgs::msg::Path & transformed_global_plan) override;
81 
90  bool isGoalXYReached(
91  const geometry_msgs::msg::Pose & query_pose,
92  const geometry_msgs::msg::Pose & goal_pose,
93  const geometry_msgs::msg::Twist & velocity,
94  const nav_msgs::msg::Path & transformed_global_plan) override;
95 
103  bool getTolerances(
104  geometry_msgs::msg::Pose & pose_tolerance,
105  geometry_msgs::msg::Twist & vel_tolerance,
106  double & path_length_tolerance) override;
107 
108 protected:
111  {
112  NONE,
113  FINE_TOLERANCE,
114  COARSE_TOLERANCE_FINISH_LINE,
115  COARSE_TOLERANCE_STOPPED_STAGNATION,
116  COARSE_TOLERANCE_DISTANCE_STAGNATION
117  };
118 
119  nav2::LifecycleNode::WeakPtr node_;
120  rclcpp::Logger logger_{rclcpp::get_logger("adaptive_tolerance_goal_checker")};
121 
122  // Fine (desired) tolerance
123  double fine_xy_goal_tolerance_;
124  double fine_xy_goal_tolerance_sq_;
125  // Coarse (fallback) tolerance
126  double coarse_xy_goal_tolerance_;
127  double coarse_xy_goal_tolerance_sq_;
128  // Hysteresis buffer used when stateful to reset the
129  // check_xy_ when the robot drifts outside the accepted region.
130  double xy_goal_tolerance_buffer_;
131  double fine_xy_goal_tolerance_reset_sq_, coarse_xy_goal_tolerance_reset_sq_;
132 
133  double yaw_goal_tolerance_;
134  double path_length_tolerance_;
135  bool stateful_;
136  bool symmetric_yaw_tolerance_;
137 
138  // Velocity thresholds for detecting a stopped/stalled robot
139  double trans_stopped_velocity_;
140  double rot_stopped_velocity_;
141 
142  // Number of consecutive stopped cycles before accepting at coarse tolerance
143  int required_stagnation_cycles_;
144 
145  // Stateful tracking
146  bool check_xy_;
147  bool in_tolerance_zone_;
148  int stopped_stagnation_count_;
149  int distance_stagnation_count_;
150  double best_distance_sq_;
151  double approach_dx_;
152  double approach_dy_;
153  XyAcceptanceReason xy_acceptance_reason_;
154 
155  // Dynamic parameters
156  std::mutex mutex_;
157  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
158  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
159  std::string plugin_name_;
160 
166  static std::string toString(XyAcceptanceReason reason);
167 
176  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
177  const std::vector<rclcpp::Parameter> & parameters);
178 
185  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
186 };
187 
188 } // namespace nav2_controller
189 
190 #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.