|
| 1 | +# Copyright 2018 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from rcl_interfaces.msg import ParameterType |
| 16 | +from rcl_interfaces.srv import GetParameters |
| 17 | +from rcl_interfaces.srv import ListParameters |
| 18 | + |
| 19 | +import rclpy |
| 20 | + |
| 21 | + |
| 22 | +# https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L174 |
| 23 | +def list_parameters(node, timeout_sec=10.0): |
| 24 | + # create client |
| 25 | + client = node.create_client( |
| 26 | + ListParameters, |
| 27 | + 'consai2r2_description/list_parameters') |
| 28 | + |
| 29 | + # call as soon as ready |
| 30 | + ready = client.wait_for_service(timeout_sec) |
| 31 | + if not ready: |
| 32 | + raise RuntimeError('Wait for service timed out') |
| 33 | + |
| 34 | + request = ListParameters.Request() |
| 35 | + future = client.call_async(request) |
| 36 | + rclpy.spin_until_future_complete(node, future) |
| 37 | + |
| 38 | + # handle response |
| 39 | + response = future.result() |
| 40 | + if response is None: |
| 41 | + raise RuntimeError("Failed to get the list of parameters'") |
| 42 | + |
| 43 | + return response.result.names |
| 44 | + |
| 45 | + |
| 46 | +# https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L122 |
| 47 | +def get_parameters(node, parameter_names, timeout_sec=10.0): |
| 48 | + # create client |
| 49 | + client = node.create_client( |
| 50 | + GetParameters, |
| 51 | + 'consai2r2_description/get_parameters') |
| 52 | + |
| 53 | + # call as soon as ready |
| 54 | + ready = client.wait_for_service(timeout_sec) |
| 55 | + if not ready: |
| 56 | + raise RuntimeError('Wait for service timed out') |
| 57 | + |
| 58 | + request = GetParameters.Request() |
| 59 | + request.names = parameter_names |
| 60 | + future = client.call_async(request) |
| 61 | + rclpy.spin_until_future_complete(node, future) |
| 62 | + |
| 63 | + # handle response |
| 64 | + response = future.result() |
| 65 | + if response is None: |
| 66 | + e = future.exception() |
| 67 | + raise RuntimeError("Failed to get parameters form node 'consai2r2_description'") |
| 68 | + |
| 69 | + return_values = {} |
| 70 | + |
| 71 | + # https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L54 |
| 72 | + for i, pvalue in enumerate(response.values): |
| 73 | + if pvalue.type == ParameterType.PARAMETER_BOOL: |
| 74 | + value = pvalue.bool_value |
| 75 | + elif pvalue.type == ParameterType.PARAMETER_INTEGER: |
| 76 | + value = pvalue.integer_value |
| 77 | + elif pvalue.type == ParameterType.PARAMETER_DOUBLE: |
| 78 | + value = pvalue.double_value |
| 79 | + elif pvalue.type == ParameterType.PARAMETER_STRING: |
| 80 | + value = pvalue.string_value |
| 81 | + elif pvalue.type == ParameterType.PARAMETER_BYTE_ARRAY: |
| 82 | + value = pvalue.byte_array_value |
| 83 | + elif pvalue.type == ParameterType.PARAMETER_BOOL_ARRAY: |
| 84 | + value = pvalue.bool_array_value |
| 85 | + elif pvalue.type == ParameterType.PARAMETER_INTEGER_ARRAY: |
| 86 | + value = pvalue.integer_array_value |
| 87 | + elif pvalue.type == ParameterType.PARAMETER_DOUBLE_ARRAY: |
| 88 | + value = pvalue.double_array_value |
| 89 | + elif pvalue.type == ParameterType.PARAMETER_STRING_ARRAY: |
| 90 | + value = pvalue.string_array_value |
| 91 | + elif pvalue.type == ParameterType.PARAMETER_NOT_SET: |
| 92 | + value = None |
| 93 | + else: |
| 94 | + raise RuntimeError("Unknown parameter type '{pvalue.type}'".format_map(locals())) |
| 95 | + return_values[parameter_names[i]] = value |
| 96 | + |
| 97 | + return return_values |
0 commit comments