|
| 1 | +# Software License Agreement (BSD License) |
| 2 | +# |
| 3 | +# Copyright (c) 2023, PickNik Inc. |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# * Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# * Redistributions in binary form must reproduce the above |
| 13 | +# copyright notice, this list of conditions and the following |
| 14 | +# disclaimer in the documentation and/or other materials provided |
| 15 | +# with the distribution. |
| 16 | +# * Neither the name of the copyright holder nor the names of its |
| 17 | +# contributors may be used to endorse or promote products derived |
| 18 | +# from this software without specific prior written permission. |
| 19 | +# |
| 20 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | +# POSSIBILITY OF SUCH DAMAGE. |
| 32 | + |
| 33 | +import time |
| 34 | +from threading import Thread |
| 35 | + |
| 36 | +import rclpy |
| 37 | +from rclpy.action import ActionClient |
| 38 | +from rclpy.callback_groups import ReentrantCallbackGroup |
| 39 | +from rclpy.expand_topic_name import expand_topic_name |
| 40 | +from rosbridge_library.internal.message_conversion import ( |
| 41 | + extract_values, |
| 42 | + populate_instance, |
| 43 | +) |
| 44 | +from rosbridge_library.internal.ros_loader import ( |
| 45 | + get_action_class, |
| 46 | + get_action_goal_instance, |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +class InvalidActionException(Exception): |
| 51 | + def __init__(self, action_name): |
| 52 | + Exception.__init__(self, f"Action {action_name} does not exist") |
| 53 | + |
| 54 | + |
| 55 | +class ActionClientHandler(Thread): |
| 56 | + def __init__(self, action, action_type, args, success_callback, error_callback, node_handle): |
| 57 | + """ |
| 58 | + Create a client handler for the specified action. |
| 59 | + Use start() to start in a separate thread or run() to run in this thread. |
| 60 | +
|
| 61 | + Keyword arguments: |
| 62 | + action -- the name of the action to execute. |
| 63 | + args -- arguments to pass to the action. Can be an |
| 64 | + ordered list, or a dict of name-value pairs. Anything else will be |
| 65 | + treated as though no arguments were provided (which is still valid for |
| 66 | + some kinds of actions) |
| 67 | + success_callback -- a callback to call with the JSON result of the |
| 68 | + service call |
| 69 | + error_callback -- a callback to call if an error occurs. The |
| 70 | + callback will be passed the exception that caused the failure |
| 71 | + node_handle -- a ROS 2 node handle to call services. |
| 72 | + """ |
| 73 | + Thread.__init__(self) |
| 74 | + self.daemon = True |
| 75 | + self.action = action |
| 76 | + self.action_type = action_type |
| 77 | + self.args = args |
| 78 | + self.success = success_callback |
| 79 | + self.error = error_callback |
| 80 | + self.node_handle = node_handle |
| 81 | + |
| 82 | + def run(self): |
| 83 | + try: |
| 84 | + # Call the service and pass the result to the success handler |
| 85 | + self.success(send_goal(self.node_handle, self.action, self.action_type, args=self.args)) |
| 86 | + except Exception as e: |
| 87 | + # On error, just pass the exception to the error handler |
| 88 | + self.error(e) |
| 89 | + |
| 90 | + |
| 91 | +def args_to_action_goal_instance(action, inst, args): |
| 92 | + """ " |
| 93 | + Populate an action goal instance with the provided args |
| 94 | +
|
| 95 | + args can be a dictionary of values, or a list, or None |
| 96 | +
|
| 97 | + Propagates any exceptions that may be raised. |
| 98 | + """ |
| 99 | + msg = {} |
| 100 | + if isinstance(args, list): |
| 101 | + msg = dict(zip(inst.get_fields_and_field_types().keys(), args)) |
| 102 | + elif isinstance(args, dict): |
| 103 | + msg = args |
| 104 | + |
| 105 | + # Populate the provided instance, propagating any exceptions |
| 106 | + populate_instance(msg, inst) |
| 107 | + |
| 108 | + |
| 109 | +def send_goal(node_handle, action, action_type, args=None, sleep_time=0.001): |
| 110 | + # Given the action nam and type, fetch a request instance |
| 111 | + action_name = expand_topic_name(action, node_handle.get_name(), node_handle.get_namespace()) |
| 112 | + action_class = get_action_class(action_type) |
| 113 | + inst = get_action_goal_instance(action_type) |
| 114 | + |
| 115 | + # Populate the instance with the provided args |
| 116 | + args_to_action_goal_instance(action_name, inst, args) |
| 117 | + |
| 118 | + client = ActionClient(node_handle, action_class, action_name) |
| 119 | + send_goal_future = client.send_goal_async(inst) |
| 120 | + while rclpy.ok() and not send_goal_future.done(): |
| 121 | + time.sleep(sleep_time) |
| 122 | + goal_handle = send_goal_future.result() |
| 123 | + |
| 124 | + if not goal_handle.accepted: |
| 125 | + raise Exception("Action goal was rejected") # TODO: Catch better |
| 126 | + |
| 127 | + result = goal_handle.get_result() |
| 128 | + client.destroy() |
| 129 | + |
| 130 | + if result is not None: |
| 131 | + # Turn the response into JSON and pass to the callback |
| 132 | + json_response = extract_values(result) |
| 133 | + else: |
| 134 | + raise Exception(result) |
| 135 | + |
| 136 | + return json_response |
0 commit comments