Skip to content

Commit

Permalink
Add API to gracefully cancel a controller
Browse files Browse the repository at this point in the history
Signed-off-by: Ramon Wijnands <[email protected]>
  • Loading branch information
Rayman committed Feb 22, 2024
1 parent 55c952d commit 52327c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions nav2_controller/src/controller_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,25 @@ void ControllerServer::computeControl()

last_valid_cmd_time_ = now();
rclcpp::WallRate loop_rate(controller_frequency_);
bool cancelling = false;
while (rclcpp::ok()) {
if (action_server_ == nullptr || !action_server_->is_server_active()) {
RCLCPP_DEBUG(get_logger(), "Action server unavailable or inactive. Stopping.");
return;
}

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 {
if (!cancelling) {
RCLCPP_INFO(get_logger(), "Goal was canceled. Trying to stop the robot.");
cancelling = true;
}
}
}

// Don't compute a trajectory until costmap is valid (after clear costmap)
Expand Down
9 changes: 9 additions & 0 deletions nav2_core/include/nav2_core/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ 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)
Expand Down

0 comments on commit 52327c8

Please sign in to comment.