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

'Goal failed to cancel' in ROS 2 Rolling

asked 2022-10-04 05:30:27 -0500

hane_ruttel gravatar image

updated 2022-10-17 05:09:54 -0500

ravijoshi gravatar image

After installing ROS 2 Rolling and playing with turtlesim, I downloaded further examples. I am having problems running the example client_cancel.py and server.py. It does not cancel the goal on the server successfully.

I followed the instructions and downloaded examples with git using the following command:

git clone https://github.com/ros2/examples src/examples -b rolling

Is there something further that needs to be done to make it work? I saw this post but didn't get anything out of it. Below are the files:

client_cancel.py:

# license removed for brevity

from example_interfaces.action import Fibonacci

import rclpy
from rclpy.action import ActionClient
from rclpy.node import Node


class MinimalActionClient(Node):
    def __init__(self):
        super().__init__("minimal_action_client")
        self._action_client = ActionClient(self, Fibonacci, "fibonacci")

    def cancel_done(self, future):
        cancel_response = future.result()
        if len(cancel_response.goals_canceling) > 0:
            self.get_logger().info("Goal successfully canceled")
        else:
            self.get_logger().info("Goal failed to cancel")
        rclpy.shutdown()

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

        self._goal_handle = goal_handle
        self.get_logger().info("Goal accepted :)")

        # Start a 2 second timer
        self._timer = self.create_timer(2.0, self.timer_callback)

    def feedback_callback(self, feedback):
        self.get_logger().info("Received feedback: {0}".format(feedback.feedback.sequence))

    def timer_callback(self):
        self.get_logger().info("Canceling goal")
        # Cancel the goal
        future = self._goal_handle.cancel_goal_async()
        future.add_done_callback(self.cancel_done)

        # Cancel the timer
        self._timer.cancel()

    def send_goal(self):
        self.get_logger().info("Waiting for action server...")
        self._action_client.wait_for_server()
        goal_msg = Fibonacci.Goal()
        goal_msg.order = 10
        self.get_logger().info("Sending goal request...")
        self._send_goal_future = self._action_client.send_goal_async(goal_msg, feedback_callback=self.feedback_callback)
        self._send_goal_future.add_done_callback(self.goal_response_callback)


def main(args=None):
    rclpy.init(args=args)

    action_client = MinimalActionClient()
    action_client.send_goal()
    rclpy.spin(action_client)


if __name__ == "__main__":
    main()

server.py:

# license removed for brevity

import time

from example_interfaces.action import Fibonacci

import rclpy
from rclpy.action import ActionServer, CancelResponse, GoalResponse
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.executors import MultiThreadedExecutor
from rclpy.node import Node


class MinimalActionServer(Node):
    def __init__(self):
        super().__init__("minimal_action_server")

        self._action_server = ActionServer(
            self,
            Fibonacci,
            "fibonacci",
            execute_callback=self.execute_callback,
            callback_group=ReentrantCallbackGroup(),
            goal_callback=self.goal_callback,
            cancel_callback=self.cancel_callback,
        )

    def destroy(self):
        self._action_server.destroy()
        super().destroy_node()

    def goal_callback(self, goal_request):
        """Accept or reject a client request to begin an action."""
        # This server allows multiple goals in parallel
        self.get_logger().info("Received goal request")
        return GoalResponse.ACCEPT

    def cancel_callback(self, goal_handle):
        """Accept or reject a client request to cancel an action."""
        self.get_logger().info("Received cancel request")
        return CancelResponse.ACCEPT

    async def execute_callback(self, goal_handle):
        """Execute a goal."""
        self.get_logger().info("Executing goal...")

        # Append the seeds for the Fibonacci sequence
        feedback_msg = Fibonacci.Feedback()
        feedback_msg.sequence = [0, 1]

        # Start executing the action
        for i in range(1, goal_handle.request.order):
            if goal_handle.is_cancel_requested:
                goal_handle.canceled()
                self.get_logger().info("Goal canceled")
                return Fibonacci.Result()

            # Update Fibonacci sequence
            feedback_msg.sequence.append(feedback_msg.sequence[i] + feedback_msg.sequence[i - 1])
            self.get_logger().info("Publishing feedback: {0}".format(feedback_msg.sequence))

            # Publish the feedback
            goal_handle.publish_feedback(feedback_msg)

            # Sleep for demonstration purposes
            time.sleep ...
(more)
edit retag flag offensive close merge delete

Comments

anyone? i guess ros2 is not suited for python.

hane_ruttel gravatar image hane_ruttel  ( 2022-10-13 03:22:06 -0500 )edit

We need more information. Therefore can you please post the commands you have used and a minimal reproducible example?

ravijoshi gravatar image ravijoshi  ( 2022-10-14 05:07:07 -0500 )edit

thanks for the response, i edited my first entry

hane_ruttel gravatar image hane_ruttel  ( 2022-10-16 02:34:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-17 05:02:06 -0500

ravijoshi gravatar image

I tested your code on ROS Galactic and found it working. Therefore, I suggest using Galactic or Humble if you want to stay updated. However, please note that I have not tested your code in Humble.

Below are the results reported in the terminal:

  • Server:

    $ ros2 run examples_rclpy_minimal_service service
    [INFO] [1666000409.550238114] [minimal_action_server]: Received goal request
    [INFO] [1666000409.551262021] [minimal_action_server]: Executing goal...
    [INFO] [1666000409.551549517] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1])
    [INFO] [1666000410.552772896] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2])
    [INFO] [1666000411.561754895] [minimal_action_server]: Received cancel request
    [INFO] [1666000411.562464815] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3])
    [INFO] [1666000412.566019658] [minimal_action_server]: Goal canceled
    
  • Client:

    $ ros2 run examples_rclpy_minimal_client client
    [INFO] [1666000409.539484246] [minimal_action_client]: Waiting for action server...
    [INFO] [1666000409.539845804] [minimal_action_client]: Sending goal request...
    [INFO] [1666000409.550877363] [minimal_action_client]: Goal accepted :)
    [INFO] [1666000409.551996243] [minimal_action_client]: Received feedback: array('i', [0, 1, 1])
    [INFO] [1666000410.553445095] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2])
    [INFO] [1666000411.552878882] [minimal_action_client]: Canceling goal
    [INFO] [1666000411.563110901] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3])
    [INFO] [1666000411.563414815] [minimal_action_client]: Goal successfully canceled
    
edit flag offensive delete link more

Comments

It works in Humble as well, thank you for your answer.

By any chance, do you know what changed?

I found the function to cancel the server exists in earlier ros2 versions. What can users do who can't use humble/galactic? Is updating the only way?

hane_ruttel gravatar image hane_ruttel  ( 2022-10-17 06:44:39 -0500 )edit

By any chance, do you know what changed?

It may take a while because first, I have to confirm your claim by installing Rolling. Right now, I don't know the answer to your question.

.... who can't use humble/galactic?

Sorry, can you please elaborate more? What made you pick Rolling instead of Humble or Galactic?

ravijoshi gravatar image ravijoshi  ( 2022-10-17 08:09:40 -0500 )edit

My post is about 'rolling', because i thought it has the latest bug fixes and features built in. I installed it only to test the functionality of new features. This was obviously a fallacy.

On my Project I have to use an older ros2 version because i am bound to ubuntu 18 (its eloquent). Now it seems i cant use the client cancel with python?

hane_ruttel gravatar image hane_ruttel  ( 2022-10-17 08:48:29 -0500 )edit
1

i guess ros2 is not suited for python. [..] This was obviously a fallacy. [..] Now it seems i cant use the client cancel with python?

Could I ask you to please stop jumping to conclusions and act a little more in good faith?

You've run into a snag with one particular piece of functionality, on your specific machine, with a specific version of ROS 2 (or, more precisely: with a specific version of a specific package, so a tiny part of ROS 2). That doesn't translate to "ROS 2 can't do X" or "this is all wrong, I can't do X".

Let's just keep it to the facts and try to diagnose what's going on, which I believe @ravijoshi is doing a pretty good job of.

gvdhoorn gravatar image gvdhoorn  ( 2022-10-17 09:25:50 -0500 )edit

thank you, i will avoid this general assumptions. i am grateful for the help

hane_ruttel gravatar image hane_ruttel  ( 2022-10-18 03:13:45 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-10-04 05:30:27 -0500

Seen: 82 times

Last updated: Oct 17 '22