|
| 1 | +# Copyright 2024, FZI Forschungszentrum Informatik |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# * Neither the name of the {copyright_holder} nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from |
| 15 | +# this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | +import logging |
| 29 | +import time |
| 30 | + |
| 31 | +import rclpy |
| 32 | +from rclpy.action import ActionClient |
| 33 | + |
| 34 | +from controller_manager_msgs.srv import ListControllers |
| 35 | + |
| 36 | +TIMEOUT_WAIT_SERVICE = 10 |
| 37 | +TIMEOUT_WAIT_SERVICE_INITIAL = 120 |
| 38 | +TIMEOUT_WAIT_ACTION = 20 |
| 39 | + |
| 40 | + |
| 41 | +def _wait_for_service(node, srv_name, srv_type, timeout): |
| 42 | + client = node.create_client(srv_type, srv_name) |
| 43 | + |
| 44 | + logging.info("Waiting for service '%s' with timeout %fs...", srv_name, timeout) |
| 45 | + if client.wait_for_service(timeout) is False: |
| 46 | + raise Exception(f"Could not reach service '{srv_name}' within timeout of {timeout}") |
| 47 | + logging.info(" Successfully connected to service '%s'", srv_name) |
| 48 | + |
| 49 | + return client |
| 50 | + |
| 51 | + |
| 52 | +def _wait_for_action(node, action_name, action_type, timeout): |
| 53 | + client = ActionClient(node, action_type, action_name) |
| 54 | + |
| 55 | + logging.info("Waiting for action server '%s' with timeout %fs...", action_name, timeout) |
| 56 | + if client.wait_for_server(timeout) is False: |
| 57 | + raise Exception( |
| 58 | + f"Could not reach action server '{action_name}' within timeout of {timeout}" |
| 59 | + ) |
| 60 | + |
| 61 | + logging.info(" Successfully connected to action server '%s'", action_name) |
| 62 | + return client |
| 63 | + |
| 64 | + |
| 65 | +def wait_for_controller( |
| 66 | + node, controller_name, timeout, cm_list_service="/controller_manager/list_controllers" |
| 67 | +): |
| 68 | + logging.info("Waiting for controller '%s' with timeout %fs...", controller_name, timeout) |
| 69 | + client = _wait_for_service(node, cm_list_service, ListControllers, TIMEOUT_WAIT_SERVICE) |
| 70 | + controller_active = False |
| 71 | + start_time = node.get_clock().now() |
| 72 | + while not controller_active and ( |
| 73 | + node.get_clock().now() - start_time < rclpy.time.Duration(seconds=timeout) |
| 74 | + ): |
| 75 | + result = _call_service(node, client, ListControllers.Request()) |
| 76 | + for controller in result.controller: |
| 77 | + if controller.name == controller_name: |
| 78 | + controller_active = controller.state == "active" |
| 79 | + if controller_active: |
| 80 | + logging.info("Controller '%s' is active.", controller_name) |
| 81 | + return True |
| 82 | + time.sleep(1) |
| 83 | + raise Exception( |
| 84 | + f"Could not find active controller '{controller_name}' within timeout of {timeout}" |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def _call_service(node, client, request): |
| 89 | + logging.info("Calling service client '%s' with request '%s'", client.srv_name, request) |
| 90 | + future = client.call_async(request) |
| 91 | + |
| 92 | + rclpy.spin_until_future_complete(node, future) |
| 93 | + |
| 94 | + if future.result() is not None: |
| 95 | + logging.info(" Received result: %s", future.result()) |
| 96 | + return future.result() |
| 97 | + |
| 98 | + raise Exception(f"Error while calling service '{client.srv_name}': {future.exception()}") |
| 99 | + |
| 100 | + |
| 101 | +class _ServiceInterface: |
| 102 | + def __init__( |
| 103 | + self, node, initial_timeout=TIMEOUT_WAIT_SERVICE_INITIAL, timeout=TIMEOUT_WAIT_SERVICE |
| 104 | + ): |
| 105 | + self.__node = node |
| 106 | + |
| 107 | + self.__service_clients = { |
| 108 | + srv_name: ( |
| 109 | + _wait_for_service(self.__node, srv_name, srv_type, initial_timeout), |
| 110 | + srv_type, |
| 111 | + ) |
| 112 | + for srv_name, srv_type in self.__initial_services.items() |
| 113 | + } |
| 114 | + self.__service_clients.update( |
| 115 | + { |
| 116 | + srv_name: (_wait_for_service(self.__node, srv_name, srv_type, timeout), srv_type) |
| 117 | + for srv_name, srv_type in self.__services.items() |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + def __init_subclass__(mcs, namespace="", initial_services={}, services={}, **kwargs): |
| 122 | + super().__init_subclass__(**kwargs) |
| 123 | + |
| 124 | + mcs.__initial_services = { |
| 125 | + namespace + "/" + srv_name: srv_type for srv_name, srv_type in initial_services.items() |
| 126 | + } |
| 127 | + mcs.__services = { |
| 128 | + namespace + "/" + srv_name: srv_type for srv_name, srv_type in services.items() |
| 129 | + } |
| 130 | + |
| 131 | + for srv_name, srv_type in list(initial_services.items()) + list(services.items()): |
| 132 | + full_srv_name = namespace + "/" + srv_name |
| 133 | + |
| 134 | + setattr( |
| 135 | + mcs, |
| 136 | + srv_name, |
| 137 | + lambda s, full_srv_name=full_srv_name, *args, **kwargs: _call_service( |
| 138 | + s.__node, |
| 139 | + s.__service_clients[full_srv_name][0], |
| 140 | + s.__service_clients[full_srv_name][1].Request(*args, **kwargs), |
| 141 | + ), |
| 142 | + ) |
| 143 | + |
| 144 | + |
| 145 | +class ActionInterface: |
| 146 | + def __init__(self, node, action_name, action_type, timeout=TIMEOUT_WAIT_ACTION): |
| 147 | + self.__node = node |
| 148 | + |
| 149 | + self.__action_name = action_name |
| 150 | + self.__action_type = action_type |
| 151 | + self.__action_client = _wait_for_action(node, action_name, action_type, timeout) |
| 152 | + |
| 153 | + def send_goal(self, *args, **kwargs): |
| 154 | + goal = self.__action_type.Goal(*args, **kwargs) |
| 155 | + |
| 156 | + logging.info("Sending goal to action server '%s': %s", self.__action_name, goal) |
| 157 | + future = self.__action_client.send_goal_async(goal) |
| 158 | + |
| 159 | + rclpy.spin_until_future_complete(self.__node, future) |
| 160 | + |
| 161 | + if future.result() is not None: |
| 162 | + logging.info(" Received result: %s", future.result()) |
| 163 | + return future.result() |
| 164 | + pass |
| 165 | + |
| 166 | + def get_result(self, goal_handle, timeout): |
| 167 | + future_res = goal_handle.get_result_async() |
| 168 | + |
| 169 | + logging.info( |
| 170 | + "Waiting for action result from '%s' with timeout %fs", self.__action_name, timeout |
| 171 | + ) |
| 172 | + rclpy.spin_until_future_complete(self.__node, future_res, timeout_sec=timeout) |
| 173 | + |
| 174 | + if future_res.result() is not None: |
| 175 | + logging.info(" Received result: %s", future_res.result().result) |
| 176 | + return future_res.result().result |
| 177 | + else: |
| 178 | + raise Exception( |
| 179 | + f"Exception while calling action '{self.__action_name}': {future_res.exception()}" |
| 180 | + ) |
0 commit comments