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')
I would also like to know this. Did you find an answer? It seems straightforward in C++ but not in python
@dannee did you ever find a working python code to cancel actions? I'd like to give this a try.
@printf42 yes! see my answer to this question