From 2ecc91e47bb09485289531144ab9b038f4cfc432 Mon Sep 17 00:00:00 2001 From: Ramon Wijnands Date: Fri, 29 Mar 2024 19:39:24 +0100 Subject: [PATCH] Add API to gracefully cancel a controller (#4136) * Add API to gracefully cancel a controller Signed-off-by: Ramon Wijnands * Add `cancel_deceleration` to RegulatedPurePursuitController Signed-off-by: Ramon Wijnands * Update nav2_regulated_pure_pursuit_controller/src/parameter_handler.cpp Co-authored-by: Steve Macenski Signed-off-by: Ramon Wijnands --------- Signed-off-by: Ramon Wijnands Signed-off-by: Ramon Wijnands Co-authored-by: Steve Macenski --- nav2_controller/src/controller_server.cpp | 13 ++++++--- nav2_core/include/nav2_core/controller.hpp | 10 +++++++ .../parameter_handler.hpp | 1 + .../regulated_pure_pursuit_controller.hpp | 6 ++++ .../src/parameter_handler.cpp | 5 ++++ .../src/regulated_pure_pursuit_controller.cpp | 29 +++++++++++++++++++ 6 files changed, 60 insertions(+), 4 deletions(-) diff --git a/nav2_controller/src/controller_server.cpp b/nav2_controller/src/controller_server.cpp index 17c3b510dc..16c3db43a8 100644 --- a/nav2_controller/src/controller_server.cpp +++ b/nav2_controller/src/controller_server.cpp @@ -460,10 +460,15 @@ void ControllerServer::computeControl() } if (action_server_->is_cancel_requested()) { - RCLCPP_INFO(get_logger(), "Goal was canceled. Stopping the robot."); - action_server_->terminate_all(); - publishZeroVelocity(); - return; + if (controllers_[current_controller_]->cancel()) { + RCLCPP_INFO(get_logger(), "Cancellation was successful. Stopping the robot."); + action_server_->terminate_all(); + publishZeroVelocity(); + return; + } else { + RCLCPP_INFO_THROTTLE( + get_logger(), *get_clock(), 1000, "Waiting for the controller to finish cancellation"); + } } // Don't compute a trajectory until costmap is valid (after clear costmap) diff --git a/nav2_core/include/nav2_core/controller.hpp b/nav2_core/include/nav2_core/controller.hpp index dab79176e9..b01381a041 100644 --- a/nav2_core/include/nav2_core/controller.hpp +++ b/nav2_core/include/nav2_core/controller.hpp @@ -116,6 +116,16 @@ class Controller const geometry_msgs::msg::Twist & velocity, nav2_core::GoalChecker * goal_checker) = 0; + /** + * @brief Cancel the current control action + * @return True if the cancellation was successful. If false is returned, computeVelocityCommands + * will be called until cancel returns true. + */ + virtual bool cancel() + { + return true; + } + /** * @brief Limits the maximum linear speed of the robot. * @param speed_limit expressed in absolute value (in m/s) diff --git a/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/parameter_handler.hpp b/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/parameter_handler.hpp index d6ca585a49..d2258ceb80 100644 --- a/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/parameter_handler.hpp +++ b/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/parameter_handler.hpp @@ -53,6 +53,7 @@ struct Parameters double curvature_lookahead_dist; bool use_rotate_to_heading; double max_angular_accel; + double cancel_deceleration; double rotate_to_heading_min_angle; bool allow_reversing; double max_robot_pose_search_dist; diff --git a/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/regulated_pure_pursuit_controller.hpp b/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/regulated_pure_pursuit_controller.hpp index 417dc1c811..8176eb0649 100644 --- a/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/regulated_pure_pursuit_controller.hpp +++ b/nav2_regulated_pure_pursuit_controller/include/nav2_regulated_pure_pursuit_controller/regulated_pure_pursuit_controller.hpp @@ -96,6 +96,8 @@ class RegulatedPurePursuitController : public nav2_core::Controller const geometry_msgs::msg::Twist & velocity, nav2_core::GoalChecker * /*goal_checker*/) override; + bool cancel() override; + /** * @brief nav2_core setPlan - Sets the global plan * @param path The global plan @@ -111,6 +113,8 @@ class RegulatedPurePursuitController : public nav2_core::Controller */ void setSpeedLimit(const double & speed_limit, const bool & percentage) override; + void reset() override; + protected: /** * @brief Get lookahead distance @@ -212,6 +216,8 @@ class RegulatedPurePursuitController : public nav2_core::Controller Parameters * params_; double goal_dist_tol_; double control_duration_; + bool cancelling_ = false; + bool finished_cancelling_ = false; std::shared_ptr> global_path_pub_; std::shared_ptr> diff --git a/nav2_regulated_pure_pursuit_controller/src/parameter_handler.cpp b/nav2_regulated_pure_pursuit_controller/src/parameter_handler.cpp index dea2bc59a4..a47af13ef0 100644 --- a/nav2_regulated_pure_pursuit_controller/src/parameter_handler.cpp +++ b/nav2_regulated_pure_pursuit_controller/src/parameter_handler.cpp @@ -85,6 +85,8 @@ ParameterHandler::ParameterHandler( node, plugin_name_ + ".rotate_to_heading_min_angle", rclcpp::ParameterValue(0.785)); declare_parameter_if_not_declared( node, plugin_name_ + ".max_angular_accel", rclcpp::ParameterValue(3.2)); + declare_parameter_if_not_declared( + node, plugin_name_ + ".cancel_deceleration", rclcpp::ParameterValue(3.2)); declare_parameter_if_not_declared( node, plugin_name_ + ".allow_reversing", rclcpp::ParameterValue(false)); declare_parameter_if_not_declared( @@ -151,6 +153,7 @@ ParameterHandler::ParameterHandler( node->get_parameter( plugin_name_ + ".rotate_to_heading_min_angle", params_.rotate_to_heading_min_angle); node->get_parameter(plugin_name_ + ".max_angular_accel", params_.max_angular_accel); + node->get_parameter(plugin_name_ + ".cancel_deceleration", params_.cancel_deceleration); node->get_parameter(plugin_name_ + ".allow_reversing", params_.allow_reversing); node->get_parameter( plugin_name_ + ".max_robot_pose_search_dist", @@ -237,6 +240,8 @@ ParameterHandler::dynamicParametersCallback( params_.regulated_linear_scaling_min_speed = parameter.as_double(); } else if (name == plugin_name_ + ".max_angular_accel") { params_.max_angular_accel = parameter.as_double(); + } else if (name == plugin_name_ + ".cancel_deceleration") { + params_.cancel_deceleration = parameter.as_double(); } else if (name == plugin_name_ + ".rotate_to_heading_min_angle") { params_.rotate_to_heading_min_angle = parameter.as_double(); } diff --git a/nav2_regulated_pure_pursuit_controller/src/regulated_pure_pursuit_controller.cpp b/nav2_regulated_pure_pursuit_controller/src/regulated_pure_pursuit_controller.cpp index 692f21e82b..c4b3f500ca 100644 --- a/nav2_regulated_pure_pursuit_controller/src/regulated_pure_pursuit_controller.cpp +++ b/nav2_regulated_pure_pursuit_controller/src/regulated_pure_pursuit_controller.cpp @@ -238,6 +238,23 @@ geometry_msgs::msg::TwistStamped RegulatedPurePursuitController::computeVelocity collision_checker_->costAtPose(pose.pose.position.x, pose.pose.position.y), transformed_plan, linear_vel, x_vel_sign); + if (cancelling_) { + const double & dt = control_duration_; + linear_vel = speed.linear.x - x_vel_sign * dt * params_->cancel_deceleration; + + if (x_vel_sign > 0) { + if (linear_vel <= 0) { + linear_vel = 0; + finished_cancelling_ = true; + } + } else { + if (linear_vel >= 0) { + linear_vel = 0; + finished_cancelling_ = true; + } + } + } + // Apply curvature to angular velocity after constraining linear velocity angular_vel = linear_vel * regulation_curvature; } @@ -258,6 +275,12 @@ geometry_msgs::msg::TwistStamped RegulatedPurePursuitController::computeVelocity return cmd_vel; } +bool RegulatedPurePursuitController::cancel() +{ + cancelling_ = true; + return finished_cancelling_; +} + bool RegulatedPurePursuitController::shouldRotateToPath( const geometry_msgs::msg::PoseStamped & carrot_pose, double & angle_to_path, double & x_vel_sign) @@ -445,6 +468,12 @@ void RegulatedPurePursuitController::setSpeedLimit( } } +void RegulatedPurePursuitController::reset() +{ + cancelling_ = false; + finished_cancelling_ = false; +} + double RegulatedPurePursuitController::findVelocitySignChange( const nav_msgs::msg::Path & transformed_plan) {