ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

ROS2 action goal canceling problem

asked 2020-09-16 00:39:59 -0600

10908234 gravatar image

I followed the example codes on Github, but when client send cancel goal request, the action server didn't receive the cancel request.

I didn't change the example codes, so I just put the example link below.

Server Code

Client Code

Am I the only person who fail to cancel goal?

edit retag flag offensive close merge delete



I would also like to know this. Did you find an answer? It seems straightforward in C++ but not in python

dannee gravatar image dannee  ( 2022-03-16 08:55:07 -0600 )edit

@dannee did you ever find a working python code to cancel actions? I'd like to give this a try.

printf42 gravatar image printf42  ( 2022-06-02 08:48:03 -0600 )edit

@printf42 yes! see my answer to this question

dannee gravatar image dannee  ( 2022-06-03 03:54:46 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2022-06-03 03:53:56 -0600

dannee gravatar image

You can use the following structure for an action server/client.

import rclpy
from rclpy.node import Node
from rclpy.action import ActionServer
from rclpy.action import CancelResponse
from rclpy.action import ActionClient
from action_tutorials_interfaces.action import Fibonacci

class MyActionServer(Node):
    def __init__(self):
        self._action_server = ActionServer(

    def execute_callback(self, goal_handle):
        return Fibonacci.Result()

    def cancel_callback(self, cancel_request):
        return CancelResponse.ACCEPT

class MyActionClient(Node):
    def __init__(self):
        self._action_client = ActionClient(self, Fibonacci, 'fibonacci')

    def send_goal(self):
        #Send an action goal
        goal_msg = Fibonacci.Goal()
        future = self._action_client.send_goal_async(goal_msg)

    def goal_response_callback(self, future):
        self.goal_handle = future.result()
        if not goal_handle.accepted:
            self.get_logger().info('Goal rejected :(')

        self.get_logger().info('Goal accepted :)')

        self._get_result_future = goal_handle.get_result_async()

    def cancel_goal(self):
        self.get_logger().info('Canceling goal')
        future = self.goal_handle.cancel_goal_async()

    def goal_canceled_callback(self, future):
        cancel_response = future.result()
        if len(cancel_response.goals_canceling) > 0:
            self.get_logger().info('Cancelling of goal complete')
            self.get_logger().warning('Goal failed to cancel')
edit flag offensive delete link more


Thanks for this example. Is there a way to support long-running actions? E.g., in there is a multi-second delay. Thx.

clyde gravatar image clyde  ( 2022-11-17 17:17:32 -0600 )edit

I don't see why it isn't already supported. Can you explain more?

dannee gravatar image dannee  ( 2022-11-18 11:08:38 -0600 )edit

I was looking for an example that had time.sleep() in it to simulate a long-running calculation. I actually found some great examples here: See my other question:

clyde gravatar image clyde  ( 2022-11-19 08:47:54 -0600 )edit

answered 2021-01-21 18:33:17 -0600

action canceling example:

edit flag offensive delete link more


he has clearly asked for ROS2 , your link points to ROS

Fetullah Atas gravatar image Fetullah Atas  ( 2021-02-04 08:41:24 -0600 )edit

It is a ROS2 example. You can see the branch is set to foxy. Next time if you would like to know if the package is ROS1 or ROS2, an easy and fast way to do that is see CMakeLists.txt and package.xml file and check on the building system (catkin is for ROS1, ament is for ROS2)

Jaehyun Shim gravatar image Jaehyun Shim  ( 2021-02-04 18:29:05 -0600 )edit

Is there any examples in python?

dannee gravatar image dannee  ( 2022-03-16 08:57:33 -0600 )edit

Question Tools



Asked: 2020-09-16 00:39:59 -0600

Seen: 2,878 times

Last updated: Jun 03 '22