/* * Software License Agreement (BSD License) * * Copyright (c) 2017, Locus Robotics * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "nav2_controller/plugins/simple_goal_checker.hpp" #include "pluginlib/class_list_macros.hpp" #include "angles/angles.h" #include "nav2_ros_common/node_utils.hpp" #include "nav2_util/geometry_utils.hpp" #include "tf2/utils.hpp" using rcl_interfaces::msg::ParameterType; using std::placeholders::_1; namespace nav2_controller { SimpleGoalChecker::SimpleGoalChecker() : xy_goal_tolerance_(0.25), xy_goal_tolerance_buffer_(0.0), yaw_goal_tolerance_(0.25), path_length_tolerance_(1.0), stateful_(true), check_xy_(true), symmetric_yaw_tolerance_(false), xy_goal_tolerance_sq_(0.0625), xy_goal_tolerance_reset_sq_(0.0625) { } SimpleGoalChecker::~SimpleGoalChecker() { auto node = node_.lock(); if (post_set_params_handler_ && node) { node->remove_post_set_parameters_callback(post_set_params_handler_.get()); } post_set_params_handler_.reset(); if (on_set_params_handler_ && node) { node->remove_on_set_parameters_callback(on_set_params_handler_.get()); } on_set_params_handler_.reset(); } void SimpleGoalChecker::initialize( const nav2::LifecycleNode::WeakPtr & parent, const std::string & plugin_name, const std::shared_ptr/*costmap_ros*/) { plugin_name_ = plugin_name; node_ = parent; auto node = node_.lock(); logger_ = node->get_logger(); xy_goal_tolerance_ = node->declare_or_get_parameter(plugin_name + ".xy_goal_tolerance", 0.25); xy_goal_tolerance_buffer_ = node->declare_or_get_parameter( plugin_name + ".xy_goal_tolerance_buffer", 0.0); yaw_goal_tolerance_ = node->declare_or_get_parameter(plugin_name + ".yaw_goal_tolerance", 0.25); path_length_tolerance_ = node->declare_or_get_parameter( plugin_name + ".path_length_tolerance", 1.0); stateful_ = node->declare_or_get_parameter(plugin_name + ".stateful", true); symmetric_yaw_tolerance_ = node->declare_or_get_parameter( plugin_name + ".symmetric_yaw_tolerance", false); xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_; xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) * (xy_goal_tolerance_ + xy_goal_tolerance_buffer_); // Add callback for dynamic parameters post_set_params_handler_ = node->add_post_set_parameters_callback( std::bind( &SimpleGoalChecker::updateParametersCallback, this, std::placeholders::_1)); on_set_params_handler_ = node->add_on_set_parameters_callback( std::bind( &SimpleGoalChecker::validateParameterUpdatesCallback, this, std::placeholders::_1)); } void SimpleGoalChecker::reset() { check_xy_ = true; } bool SimpleGoalChecker::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) { if (!isGoalXYReached(query_pose, goal_pose, velocity, transformed_global_plan)) { return false; } std::lock_guard lock_reinit(mutex_); double query_yaw = tf2::getYaw(query_pose.orientation); double goal_yaw = tf2::getYaw(goal_pose.orientation); if (symmetric_yaw_tolerance_) { // For symmetric robots: accept either goal orientation or goal + 180° double dyaw_forward = angles::shortest_angular_distance(query_yaw, goal_yaw); double dyaw_backward = angles::shortest_angular_distance( query_yaw, angles::normalize_angle(goal_yaw + M_PI)); bool forward_match = fabs(dyaw_forward) <= yaw_goal_tolerance_; bool backward_match = fabs(dyaw_backward) <= yaw_goal_tolerance_; return forward_match || backward_match; } else { double dyaw = angles::shortest_angular_distance(query_yaw, goal_yaw); return fabs(dyaw) <= yaw_goal_tolerance_; } } bool SimpleGoalChecker::isGoalXYReached( const geometry_msgs::msg::Pose & query_pose, const geometry_msgs::msg::Pose & goal_pose, const geometry_msgs::msg::Twist &, const nav_msgs::msg::Path & transformed_global_plan) { std::lock_guard lock_reinit(mutex_); // If the local plan length is longer than the tolerance, we skip the check if (nav2_util::geometry_utils::calculate_path_length(transformed_global_plan) > path_length_tolerance_) { return false; } if (check_xy_) { double dx = query_pose.position.x - goal_pose.position.x, dy = query_pose.position.y - goal_pose.position.y; if (dx * dx + dy * dy > xy_goal_tolerance_sq_) { return false; } // We are within the window // If we are stateful, change the state. if (stateful_) { check_xy_ = false; } } else if (stateful_ && xy_goal_tolerance_buffer_ > 0.0) { // If we are stateful and have a buffer, // check if we have left the buffer region to reset the state double dx = query_pose.position.x - goal_pose.position.x, dy = query_pose.position.y - goal_pose.position.y; if (dx * dx + dy * dy > xy_goal_tolerance_reset_sq_) { check_xy_ = true; return false; } } return true; } bool SimpleGoalChecker::getTolerances( geometry_msgs::msg::Pose & pose_tolerance, geometry_msgs::msg::Twist & vel_tolerance, double & path_length_tolerance) { std::lock_guard lock_reinit(mutex_); double invalid_field = std::numeric_limits::lowest(); pose_tolerance.position.x = xy_goal_tolerance_; pose_tolerance.position.y = xy_goal_tolerance_; pose_tolerance.position.z = invalid_field; pose_tolerance.orientation = nav2_util::geometry_utils::orientationAroundZAxis(yaw_goal_tolerance_); vel_tolerance.linear.x = invalid_field; vel_tolerance.linear.y = invalid_field; vel_tolerance.linear.z = invalid_field; vel_tolerance.angular.x = invalid_field; vel_tolerance.angular.y = invalid_field; vel_tolerance.angular.z = invalid_field; path_length_tolerance = path_length_tolerance_; return true; } rcl_interfaces::msg::SetParametersResult SimpleGoalChecker::validateParameterUpdatesCallback( const std::vector & parameters) { rcl_interfaces::msg::SetParametersResult result; result.successful = true; for (const auto & parameter : parameters) { const auto & param_type = parameter.get_type(); const auto & param_name = parameter.get_name(); if (param_name.find(plugin_name_ + ".") != 0) { continue; } if (param_type == ParameterType::PARAMETER_DOUBLE) { if (parameter.as_double() < 0.0) { RCLCPP_WARN( logger_, "The value of parameter '%s' is incorrectly set to %f, " "it should be >=0. Ignoring parameter update.", param_name.c_str(), parameter.as_double()); result.successful = false; } } } return result; } void SimpleGoalChecker::updateParametersCallback( const std::vector & parameters) { std::lock_guard lock_reinit(mutex_); rcl_interfaces::msg::SetParametersResult result; for (const auto & parameter : parameters) { const auto & param_type = parameter.get_type(); const auto & param_name = parameter.get_name(); if (param_name.find(plugin_name_ + ".") != 0) { continue; } if (param_type == ParameterType::PARAMETER_DOUBLE) { if (param_name == plugin_name_ + ".xy_goal_tolerance") { xy_goal_tolerance_ = parameter.as_double(); xy_goal_tolerance_sq_ = xy_goal_tolerance_ * xy_goal_tolerance_; xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) * (xy_goal_tolerance_ + xy_goal_tolerance_buffer_); } else if (param_name == plugin_name_ + ".xy_goal_tolerance_buffer") { xy_goal_tolerance_buffer_ = parameter.as_double(); xy_goal_tolerance_reset_sq_ = (xy_goal_tolerance_ + xy_goal_tolerance_buffer_) * (xy_goal_tolerance_ + xy_goal_tolerance_buffer_); } else if (param_name == plugin_name_ + ".yaw_goal_tolerance") { yaw_goal_tolerance_ = parameter.as_double(); } else if (param_name == plugin_name_ + ".path_length_tolerance") { path_length_tolerance_ = parameter.as_double(); } } else if (param_type == ParameterType::PARAMETER_BOOL) { if (param_name == plugin_name_ + ".stateful") { stateful_ = parameter.as_bool(); } else if (param_name == plugin_name_ + ".symmetric_yaw_tolerance") { symmetric_yaw_tolerance_ = parameter.as_bool(); } } } } } // namespace nav2_controller PLUGINLIB_EXPORT_CLASS(nav2_controller::SimpleGoalChecker, nav2_core::GoalChecker)