Easy way to cancel a ROS2 action
For quick testing, what's the easiest way to cancel a ROS2 action? Preferably something that I can run external to my program, without having to hack its source code.
It looks like rclpy
does not have a way to cancel an action without having a goal handle. https://github.com/ros2/rclpy/blob/ma... So we can rule out Python.
I don't see a command line option.
The best I have so far is this C++ script. It goes with the examples here.
#include "example_interfaces/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
namespace {
using Fibonacci = example_interfaces::action::Fibonacci;
}
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
rclcpp::NodeOptions node_options;
auto node = rclcpp::Node::make_shared("action_server_cancellation", "", node_options);
rclcpp_action::Client<Fibonacci>::SharedPtr client_ptr = rclcpp_action::create_client<Fibonacci>(node, "fibonacci");
if (!client_ptr->wait_for_action_server(std::chrono::seconds(5))) {
RCLCPP_ERROR(node->get_logger(), "Action server not available after waiting");
rclcpp::shutdown();
}
client_ptr->async_cancel_all_goals();
RCLCPP_ERROR(node->get_logger(), "Cancellation was requested.");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
So .. what's your question?
vaguely hoping there is an easier way while realizing that the ROS2 default implementation is a memory violation ;)
But maybe this will be useful to other people