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

ROS2 action goal canceling problem

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

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 https://github.com/ros2/examples/blob...

Client Code https://github.com/ros2/examples/blob...

Am I the only person who fail to cancel goal?

edit retag flag offensive close merge delete

Comments

1

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 -0500 )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 -0500 )edit
1

@printf42 yes! see my answer to this question

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

2 Answers

Sort by ยป oldest newest most voted
0

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

action canceling example: https://github.com/ros/ros_tutorials/...

edit flag offensive delete link more

Comments

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

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

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 -0500 )edit
4

Is there any examples in python?

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

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

dannee gravatar image

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

https://github.com/ros2/rclpy/blob/galactic/rclpy/rclpy/action/server.py#L185

https://github.com/ros2/rclpy/blob/galactic/rclpy/rclpy/action/client.py#L111

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):
        super().__init__("my_action_server")
        self._action_server = ActionServer(
            self,
            Fibonacci,
            'fibonacci',
            self.execute_callback,
            cancel_callback=self.cancel_callback,
        )

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

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


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

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

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

        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()
        future.add_done_callback(self.goal_canceled_callback)

    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')
        else:
            self.get_logger().warning('Goal failed to cancel')
edit flag offensive delete link more

Comments

Thanks for this example. Is there a way to support long-running actions? E.g., in https://github.com/ros2/demos/blob/hu... there is a multi-second delay. Thx.

clyde gravatar image clyde  ( 2022-11-17 17:17:32 -0500 )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 -0500 )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: https://github.com/ros2/examples/tree.... See my other question: https://answers.ros.org/question/4095...

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

Question Tools

2 followers

Stats

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

Seen: 3,294 times

Last updated: Jun 03 '22