39 #include "nav2_controller/plugins/simple_goal_checker.hpp"
40 #include "pluginlib/class_list_macros.hpp"
41 #include "angles/angles.h"
42 #include "nav2_util/node_utils.hpp"
43 #include "nav2_util/geometry_utils.hpp"
44 #pragma GCC diagnostic push
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #include "tf2/utils.h"
47 #pragma GCC diagnostic pop
49 using rcl_interfaces::msg::ParameterType;
50 using std::placeholders::_1;
52 namespace nav2_controller
55 SimpleGoalChecker::SimpleGoalChecker()
56 : xy_goal_tolerance_(0.25),
57 yaw_goal_tolerance_(0.25),
60 symmetric_yaw_tolerance_(false),
61 xy_goal_tolerance_sq_(0.0625)
65 void SimpleGoalChecker::initialize(
66 const rclcpp_lifecycle::LifecycleNode::WeakPtr & parent,
67 const std::string & plugin_name,
68 const std::shared_ptr<nav2_costmap_2d::Costmap2DROS>)
70 plugin_name_ = plugin_name;
71 auto node = parent.lock();
73 nav2_util::declare_parameter_if_not_declared(
75 plugin_name +
".xy_goal_tolerance", rclcpp::ParameterValue(0.25));
76 nav2_util::declare_parameter_if_not_declared(
78 plugin_name +
".yaw_goal_tolerance", rclcpp::ParameterValue(0.25));
79 nav2_util::declare_parameter_if_not_declared(
81 plugin_name +
".stateful", rclcpp::ParameterValue(
true));
82 nav2_util::declare_parameter_if_not_declared(
84 plugin_name +
".symmetric_yaw_tolerance", rclcpp::ParameterValue(
false));
86 node->get_parameter(plugin_name +
".xy_goal_tolerance", xy_goal_tolerance_);
87 node->get_parameter(plugin_name +
".yaw_goal_tolerance", yaw_goal_tolerance_);
88 node->get_parameter(plugin_name +
".stateful", stateful_);
89 node->get_parameter(plugin_name +
".symmetric_yaw_tolerance", symmetric_yaw_tolerance_);
91 xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_;
94 dyn_params_handler_ = node->add_on_set_parameters_callback(
95 std::bind(&SimpleGoalChecker::dynamicParametersCallback,
this, _1));
98 void SimpleGoalChecker::reset()
103 bool SimpleGoalChecker::isGoalReached(
104 const geometry_msgs::msg::Pose & query_pose,
const geometry_msgs::msg::Pose & goal_pose,
105 const geometry_msgs::msg::Twist &)
108 double dx = query_pose.position.x - goal_pose.position.x,
109 dy = query_pose.position.y - goal_pose.position.y;
110 if (dx * dx + dy * dy > xy_goal_tolerance_sq_) {
120 double query_yaw = tf2::getYaw(query_pose.orientation);
121 double goal_yaw = tf2::getYaw(goal_pose.orientation);
122 if (symmetric_yaw_tolerance_) {
124 double dyaw_forward = angles::shortest_angular_distance(query_yaw, goal_yaw);
125 double dyaw_backward = angles::shortest_angular_distance(
126 query_yaw, angles::normalize_angle(goal_yaw + M_PI));
128 bool forward_match = fabs(dyaw_forward) <= yaw_goal_tolerance_;
129 bool backward_match = fabs(dyaw_backward) <= yaw_goal_tolerance_;
131 return forward_match || backward_match;
133 double dyaw = angles::shortest_angular_distance(query_yaw, goal_yaw);
134 return fabs(dyaw) <= yaw_goal_tolerance_;
138 bool SimpleGoalChecker::getTolerances(
139 geometry_msgs::msg::Pose & pose_tolerance,
140 geometry_msgs::msg::Twist & vel_tolerance)
142 double invalid_field = std::numeric_limits<double>::lowest();
144 pose_tolerance.position.x = xy_goal_tolerance_;
145 pose_tolerance.position.y = xy_goal_tolerance_;
146 pose_tolerance.position.z = invalid_field;
147 pose_tolerance.orientation =
148 nav2_util::geometry_utils::orientationAroundZAxis(yaw_goal_tolerance_);
150 vel_tolerance.linear.x = invalid_field;
151 vel_tolerance.linear.y = invalid_field;
152 vel_tolerance.linear.z = invalid_field;
154 vel_tolerance.angular.x = invalid_field;
155 vel_tolerance.angular.y = invalid_field;
156 vel_tolerance.angular.z = invalid_field;
161 rcl_interfaces::msg::SetParametersResult
162 SimpleGoalChecker::dynamicParametersCallback(std::vector<rclcpp::Parameter> parameters)
164 rcl_interfaces::msg::SetParametersResult result;
165 for (
auto & parameter : parameters) {
166 const auto & type = parameter.get_type();
167 const auto & name = parameter.get_name();
169 if (type == ParameterType::PARAMETER_DOUBLE) {
170 if (name == plugin_name_ +
".xy_goal_tolerance") {
171 xy_goal_tolerance_ = parameter.as_double();
172 xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_;
173 }
else if (name == plugin_name_ +
".yaw_goal_tolerance") {
174 yaw_goal_tolerance_ = parameter.as_double();
176 }
else if (type == ParameterType::PARAMETER_BOOL) {
177 if (name == plugin_name_ +
".stateful") {
178 stateful_ = parameter.as_bool();
179 }
else if (name == plugin_name_ +
".symmetric_yaw_tolerance") {
180 symmetric_yaw_tolerance_ = parameter.as_bool();
184 result.successful =
true;
Goal Checker plugin that only checks the position difference.
Function-object for checking whether a goal has been reached.