Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
position_goal_checker.cpp
1 // Copyright (c) 2025 Prabhav Saxena
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 #include <memory>
16 #include <string>
17 #include <limits>
18 #include "nav2_controller/plugins/position_goal_checker.hpp"
19 #include "pluginlib/class_list_macros.hpp"
20 #include "nav2_ros_common/node_utils.hpp"
21 #include "nav2_util/geometry_utils.hpp"
22 
23 using rcl_interfaces::msg::ParameterType;
24 using std::placeholders::_1;
25 
26 namespace nav2_controller
27 {
28 
30 : xy_goal_tolerance_(0.25),
31  xy_goal_tolerance_buffer_(0.0),
32  xy_goal_tolerance_sq_(0.0625),
33  xy_goal_tolerance_reset_sq_(0.0625),
34  path_length_tolerance_(1.0),
35  stateful_(true),
36  position_reached_(false)
37 {
38 }
39 
41 {
42  auto node = node_.lock();
43  if (post_set_params_handler_ && node) {
44  node->remove_post_set_parameters_callback(post_set_params_handler_.get());
45  }
46  post_set_params_handler_.reset();
47  if (on_set_params_handler_ && node) {
48  node->remove_on_set_parameters_callback(on_set_params_handler_.get());
49  }
50  on_set_params_handler_.reset();
51 }
52 
54  const nav2::LifecycleNode::WeakPtr & parent,
55  const std::string & plugin_name,
56  const std::shared_ptr<nav2_costmap_2d::Costmap2DROS>/*costmap_ros*/)
57 {
58  plugin_name_ = plugin_name;
59  node_ = parent;
60  auto node = node_.lock();
61  logger_ = node->get_logger();
62 
63  xy_goal_tolerance_ = node->declare_or_get_parameter(plugin_name + ".xy_goal_tolerance", 0.25);
64  xy_goal_tolerance_buffer_ = node->declare_or_get_parameter(
65  plugin_name + ".xy_goal_tolerance_buffer", 0.0);
66  path_length_tolerance_ = node->declare_or_get_parameter(
67  plugin_name + ".path_length_tolerance", 1.0);
68  stateful_ = node->declare_or_get_parameter(plugin_name + ".stateful", true);
69 
70  xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_;
71  xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) *
72  (xy_goal_tolerance_ + xy_goal_tolerance_buffer_);
73 
74  // Add callback for dynamic parameters
75  post_set_params_handler_ = node->add_post_set_parameters_callback(
76  std::bind(
78  this, std::placeholders::_1));
79  on_set_params_handler_ = node->add_on_set_parameters_callback(
80  std::bind(
82  this, std::placeholders::_1));
83 }
84 
86 {
87  position_reached_ = false;
88 }
89 
91  const geometry_msgs::msg::Pose & query_pose, const geometry_msgs::msg::Pose & goal_pose,
92  const geometry_msgs::msg::Twist & velocity, const nav_msgs::msg::Path & transformed_global_plan)
93 {
94  return isGoalXYReached(query_pose, goal_pose, velocity, transformed_global_plan);
95 }
96 
98  const geometry_msgs::msg::Pose & query_pose, const geometry_msgs::msg::Pose & goal_pose,
99  const geometry_msgs::msg::Twist &, const nav_msgs::msg::Path & transformed_global_plan)
100 {
101  std::lock_guard<std::mutex> lock_reinit(mutex_);
102  // If the local plan length is longer than the tolerance, we skip the check
103  if (nav2_util::geometry_utils::calculate_path_length(transformed_global_plan) >
104  path_length_tolerance_)
105  {
106  return false;
107  }
108 
109  double dx = query_pose.position.x - goal_pose.position.x;
110  double dy = query_pose.position.y - goal_pose.position.y;
111  double dist_sq = dx * dx + dy * dy;
112 
113  // If stateful and position was already reached, keep the state
114  // unless we leave the reset region.
115  if (stateful_ && position_reached_) {
116  if (xy_goal_tolerance_buffer_ > 0.0 && dist_sq > xy_goal_tolerance_reset_sq_) {
117  position_reached_ = false;
118  return false;
119  }
120  return true;
121  }
122 
123  // Check if position is within tolerance
124  bool position_reached = (dist_sq <= xy_goal_tolerance_sq_);
125 
126  // If stateful, remember that we reached the position
127  if (stateful_ && position_reached) {
128  position_reached_ = true;
129  }
130 
131  return position_reached;
132 }
133 
135  geometry_msgs::msg::Pose & pose_tolerance,
136  geometry_msgs::msg::Twist & vel_tolerance,
137  double & path_length_tolerance)
138 {
139  std::lock_guard<std::mutex> lock_reinit(mutex_);
140  double invalid_field = std::numeric_limits<double>::lowest();
141 
142  pose_tolerance.position.x = xy_goal_tolerance_;
143  pose_tolerance.position.y = xy_goal_tolerance_;
144  pose_tolerance.position.z = invalid_field;
145 
146  // Return zero orientation tolerance as we don't check it
147  pose_tolerance.orientation.x = 0.0;
148  pose_tolerance.orientation.y = 0.0;
149  pose_tolerance.orientation.z = 0.0;
150  pose_tolerance.orientation.w = 1.0;
151 
152  vel_tolerance.linear.x = invalid_field;
153  vel_tolerance.linear.y = invalid_field;
154  vel_tolerance.linear.z = invalid_field;
155 
156  vel_tolerance.angular.x = invalid_field;
157  vel_tolerance.angular.y = invalid_field;
158  vel_tolerance.angular.z = invalid_field;
159 
160  path_length_tolerance = path_length_tolerance_;
161 
162  return true;
163 }
164 
165 rcl_interfaces::msg::SetParametersResult
167  const std::vector<rclcpp::Parameter> & parameters)
168 {
169  rcl_interfaces::msg::SetParametersResult result;
170  result.successful = true;
171  for (const auto & parameter : parameters) {
172  const auto & param_type = parameter.get_type();
173  const auto & param_name = parameter.get_name();
174  if (param_name.find(plugin_name_ + ".") != 0) {
175  continue;
176  }
177  if (param_type == ParameterType::PARAMETER_DOUBLE) {
178  if (parameter.as_double() < 0.0) {
179  RCLCPP_WARN(
180  logger_, "The value of parameter '%s' is incorrectly set to %f, "
181  "it should be >=0. Ignoring parameter update.",
182  param_name.c_str(), parameter.as_double());
183  result.successful = false;
184  }
185  }
186  }
187  return result;
188 }
189 
190 void
192  const std::vector<rclcpp::Parameter> & parameters)
193 {
194  std::lock_guard<std::mutex> lock_reinit(mutex_);
195  rcl_interfaces::msg::SetParametersResult result;
196  for (const auto & parameter : parameters) {
197  const auto & param_type = parameter.get_type();
198  const auto & param_name = parameter.get_name();
199  if (param_name.find(plugin_name_ + ".") != 0) {
200  continue;
201  }
202 
203  if (param_type == ParameterType::PARAMETER_DOUBLE) {
204  if (param_name == plugin_name_ + ".xy_goal_tolerance") {
205  xy_goal_tolerance_ = parameter.as_double();
206  xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_;
207  xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) *
208  (xy_goal_tolerance_ + xy_goal_tolerance_buffer_);
209  } else if (param_name == plugin_name_ + ".xy_goal_tolerance_buffer") {
210  xy_goal_tolerance_buffer_ = parameter.as_double();
211  xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) *
212  (xy_goal_tolerance_ + xy_goal_tolerance_buffer_);
213  } else if (param_name == plugin_name_ + ".path_length_tolerance") {
214  path_length_tolerance_ = parameter.as_double();
215  }
216  } else if (param_type == ParameterType::PARAMETER_BOOL) {
217  if (param_name == plugin_name_ + ".stateful") {
218  stateful_ = parameter.as_bool();
219  }
220  }
221  }
222 }
223 
224 } // namespace nav2_controller
225 
Goal Checker plugin that only checks XY position, ignoring orientation.
void reset() override
Reset the goal checker state.
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.
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)
~PositionGoalChecker()
Destroy the Position Goal Checker object.
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.
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.
PositionGoalChecker()
Construct a new Position Goal Checker object.
Function-object for checking whether a goal has been reached.