Skip to content

Commit

Permalink
Add action cancellation callback
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass committed Dec 13, 2023
1 parent 6332054 commit cc7bd99
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import rclpy
from rclpy.action import ActionServer
from rclpy.action.server import CancelResponse, ServerGoalHandle
from rclpy.callback_groups import ReentrantCallbackGroup
from rosbridge_library.capability import Capability
from rosbridge_library.internal import message_conversion
Expand Down Expand Up @@ -62,6 +63,7 @@ def __init__(
get_action_class(action_type),
action_name,
self.execute_callback,
cancel_callback=self.cancel_callback,
callback_group=ReentrantCallbackGroup(), # https://github.com/ros2/rclpy/issues/834#issuecomment-961331870
)

Expand All @@ -71,6 +73,7 @@ def next_id(self) -> int:
return id

async def execute_callback(self, goal: Any) -> Any:
"""Action server goal callback function."""
# generate a unique ID
goal_id = f"action_goal:{self.action_name}:{self.next_id()}"

Expand Down Expand Up @@ -103,6 +106,14 @@ def done_callback(fut: rclpy.task.Future()) -> None:
del self.goal_futures[goal_id]
del self.goal_handles[goal_id]

def cancel_callback(self, cancel_request: ServerGoalHandle) -> CancelResponse:
"""Action server cancel callback function."""
for goal_id, goal_handle in self.goal_handles.items():
if cancel_request.goal_id == goal_handle.goal_id:
self.protocol.log("warning", f"Canceling action {goal_id}")
self.goal_futures[goal_id].cancel()
return CancelResponse.ACCEPT

def handle_feedback(self, goal_id: str, feedback: Any) -> None:
"""
Called by the ActionFeedback capability to handle action feedback from the external client.
Expand Down Expand Up @@ -147,6 +158,10 @@ def graceful_shutdown(self) -> None:
future = self.goal_futures[future_id]
future.set_exception(RuntimeError(f"Action {self.action_name} was unadvertised"))

# Uncommenting this, you may get a segfault.
# See https://github.com/ros2/rclcpp/issues/2163#issuecomment-1850925883
# self.action_server.destroy()


class AdvertiseAction(Capability):
actions_glob = None
Expand Down

0 comments on commit cc7bd99

Please sign in to comment.