'Goal failed to cancel' in ROS 2 Rolling
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(1)
goal_handle.succeed()
# Populate result message
result = Fibonacci.Result()
result.sequence = feedback_msg.sequence
self.get_logger().info("Returning result: {0}".format(result.sequence))
return result
def main(args=None):
rclpy.init(args=args)
minimal_action_server = MinimalActionServer()
# Use a MultiThreadedExecutor to enable processing goals concurrently
executor = MultiThreadedExecutor()
rclpy.spin(minimal_action_server, executor=executor)
minimal_action_server.destroy()
rclpy.shutdown()
if __name__ == "__main__":
main()
Execution:
After colcon build --symlink-install
and sourcing the packages i executed ros2 run examples_rclpy_minimal_action_server server
and ros2 run examples_rclpy_minimal_action_client client_cancel
Output on server side:
[INFO] [1665904936.230794265] [minimal_action_server]: Received goal request
[INFO] [1665904936.231638391] [minimal_action_server]: Executing goal...
[INFO] [1665904936.231872544] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1])
[INFO] [1665904937.234087895] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2])
[INFO] [1665904938.236406191] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3])
[INFO] [1665904939.238750288] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5])
[INFO] [1665904940.241102316] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5, 8])
[INFO] [1665904941.243432982] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13])
[INFO] [1665904942.245741341] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21])
[INFO] [1665904943.247994186] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
[INFO] [1665904944.250271245] [minimal_action_server]: Publishing feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
[INFO] [1665904945.253059441] [minimal_action_server]: Returning result: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
Output on client side:
[INFO] [1665904935.966929974] [minimal_action_client]: Waiting for action server...
[INFO] [1665904936.218430440] [minimal_action_client]: Sending goal request...
[INFO] [1665904936.231534465] [minimal_action_client]: Goal accepted :)
[INFO] [1665904936.232283331] [minimal_action_client]: Received feedback: array('i', [0, 1, 1])
[INFO] [1665904937.235916634] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2])
[INFO] [1665904938.232744034] [minimal_action_client]: Canceling goal
[INFO] [1665904938.237996185] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3])
[INFO] [1665904939.240413786] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5])
[INFO] [1665904940.242826158] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5, 8])
[INFO] [1665904941.245216231] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13])
[INFO] [1665904942.247427880] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21])
[INFO] [1665904943.249521452] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
[INFO] [1665904944.251843465] [minimal_action_client]: Received feedback: array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
[INFO] [1665904945.256133147] [minimal_action_client]: Goal failed to cancel
Asked by hane_ruttel on 2022-10-04 05:30:27 UTC
Answers
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
Asked by ravijoshi on 2022-10-17 05:02:06 UTC
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?
Asked by hane_ruttel on 2022-10-17 06:44:39 UTC
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?
Asked by ravijoshi on 2022-10-17 08:09:40 UTC
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?
Asked by hane_ruttel on 2022-10-17 08:48:29 UTC
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.
Asked by gvdhoorn on 2022-10-17 09:25:50 UTC
thank you, i will avoid this general assumptions. i am grateful for the help
Asked by hane_ruttel on 2022-10-18 03:13:45 UTC
Comments
anyone? i guess ros2 is not suited for python.
Asked by hane_ruttel on 2022-10-13 03:22:06 UTC
We need more information. Therefore can you please post the commands you have used and a minimal reproducible example?
Asked by ravijoshi on 2022-10-14 05:07:07 UTC
thanks for the response, i edited my first entry
Asked by hane_ruttel on 2022-10-16 02:34:24 UTC