Skip to content

Commit 3a49d40

Browse files
author
Murilo Marinho
committed
[actions] Describing the action client in more detail
1 parent b15ccd9 commit 3a49d40

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

docs/source/action_clients.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Action Clients
1010
TODO
1111

1212
#. Create the Node with an :code:`ActionClient`.
13+
#. Update the :file:`setup.py` so that :program:`ros2 run` finds the node (if needed).
1314

1415
:download:`move_straight_in_2d_action_client_node.py <../../ros2_tutorial_workspace/src/python_package_that_uses_the_actions/python_package_that_uses_the_actions/move_straight_in_2d_action_client_node.py>`
1516

ros2_tutorial_workspace/src/python_package_that_uses_the_actions/python_package_that_uses_the_actions/move_straight_in_2d_action_client_node.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"""
2424
import rclpy
2525
from rclpy.action import ActionClient
26+
from rclpy.action.client import ClientGoalHandle
2627
from rclpy.node import Node
28+
from rclpy.task import Future
2729

2830
from geometry_msgs.msg import Point
2931
from package_with_interfaces.action import MoveStraightIn2D
@@ -33,37 +35,42 @@ class MoveStraightIn2DActionClientNode(Node):
3335

3436
def __init__(self):
3537
super().__init__('move_straight_in_2d_action_client')
38+
3639
self.action_client = ActionClient(self, MoveStraightIn2D, '/move_straight_in_2d')
3740

38-
def send_goal(self, desired_position: Point):
41+
self.send_goal_future = None # This will be used in `send_goal`
42+
self.get_result_future = None # This will be used in 'goal_response_callback'
43+
44+
def send_goal_async(self, desired_position: Point) -> None:
3945
goal_msg = MoveStraightIn2D.Goal()
4046
goal_msg.desired_position = desired_position
4147

4248
while not self.action_client.wait_for_server(timeout_sec=1.0):
4349
self.get_logger().info(f'service {self.action_client} not available, waiting...')
4450

51+
self.get_logger().info(f'Sending goal: {desired_position}.')
52+
4553
self.send_goal_future = self.action_client.send_goal_async(goal_msg, feedback_callback=self.action_feedback_callback)
4654
self.send_goal_future.add_done_callback(self.goal_response_callback)
4755

48-
def goal_response_callback(self, future):
49-
goal_handle = future.result()
50-
self.get_logger().info(f'The class of the future.result() is {goal_handle.__class__}')
56+
def goal_response_callback(self, future: Future) -> None:
57+
goal: ClientGoalHandle = future.result()
5158

52-
if not goal_handle.accepted:
59+
if not goal.accepted:
5360
self.get_logger().info('Goal was rejected by the server.')
5461
return
5562
self.get_logger().info('Goal was accepted by the server.')
5663

57-
self.get_result_future = goal_handle.get_result_async()
64+
self.get_result_future = goal.get_result_async()
5865
self.get_result_future.add_done_callback(self.action_result_callback)
5966

60-
def action_result_callback(self, future):
61-
result = future.result().result
62-
self.get_logger().info(f'Result: {result.final_position}')
67+
def action_result_callback(self, future: Future) -> None:
68+
response: MoveStraightIn2D.Response = future.result()
69+
self.get_logger().info(f'Final position was: {response.result.final_position}.')
6370

64-
def action_feedback_callback(self, feedback_msg):
71+
def action_feedback_callback(self, feedback_msg: MoveStraightIn2D.Feedback) -> None:
6572
feedback = feedback_msg.feedback
66-
self.get_logger().info(f'Received feedback: {feedback.distance}')
73+
self.get_logger().info(f'Received feedback distance: {feedback.distance}.')
6774

6875

6976
def main(args=None):
@@ -80,7 +87,7 @@ def main(args=None):
8087
desired_position = Point()
8188
desired_position.x = 1.0
8289
desired_position.y = -1.0
83-
node.send_goal(desired_position)
90+
node.send_goal_async(desired_position)
8491

8592
rclpy.spin(node)
8693
except KeyboardInterrupt:

0 commit comments

Comments
 (0)