'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 ...
anyone? i guess ros2 is not suited for python.
We need more information. Therefore can you please post the commands you have used and a minimal reproducible example?
thanks for the response, i edited my first entry