Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Commit fb1edeb

Browse files
authored
Merge pull request #54 from spiralray/dev/distribute_params_py
Imprement Python library for getting parameters of consai2r2_description
2 parents b50bcf3 + 9c178b7 commit fb1edeb

5 files changed

Lines changed: 139 additions & 4 deletions

File tree

consai2r2_description/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ endif()
1414
find_package(ament_cmake REQUIRED)
1515
find_package(rclcpp REQUIRED)
1616

17+
set(_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")
18+
19+
if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
20+
set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}")
21+
endif()
22+
23+
ament_python_install_package(${PROJECT_NAME})
24+
1725
include_directories(include ${CMAKE_CURRENT_BINARY_DIR})
1826
add_executable(${PROJECT_NAME}_node
1927
src/${PROJECT_NAME}_node.cpp
2028
)
2129
ament_target_dependencies(
2230
${PROJECT_NAME}_node
2331
"rclcpp"
24-
"launch_ros"
2532
)
2633

2734

@@ -39,8 +46,6 @@ install(DIRECTORY
3946
DESTINATION share/${PROJECT_NAME}/
4047
)
4148

42-
43-
4449
if(BUILD_TESTING)
4550
find_package(ament_lint_auto REQUIRED)
4651
# the following line skips the linter which checks for copyrights
@@ -52,4 +57,6 @@ if(BUILD_TESTING)
5257
ament_lint_auto_find_test_dependencies()
5358
endif()
5459

60+
set(PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}")
61+
5562
ament_package()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2019 SSL-Roots
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
import consai2r2_description.parameter
22+
23+
24+
class consai2r2_parameters():
25+
26+
def __init__(self, node, timeout_sec=10.0):
27+
param_names = consai2r2_description.parameter.list_parameters(node, timeout_sec)
28+
params = consai2r2_description.parameter.get_parameters(node, param_names, timeout_sec)
29+
for param_name, param_value in params.items():
30+
setattr(self, param_name, param_value)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

consai2r2_description/launch/config.launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import os
2222

23+
from ament_index_python.packages import get_package_share_directory
2324
from launch import LaunchDescription
2425
from launch_ros.actions import Node
25-
from ament_index_python.packages import get_package_share_directory
2626

2727

2828
def generate_launch_description():

consai2r2_description/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<license>MIT</license>
99

1010
<buildtool_depend>ament_cmake</buildtool_depend>
11+
<buildtool_depend>python_cmake_module</buildtool_depend>
1112

1213
<depend>rclcpp</depend>
1314
<depend>launch_ros</depend>

0 commit comments

Comments
 (0)