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

Commit

Permalink
Merge pull request #54 from spiralray/dev/distribute_params_py
Browse files Browse the repository at this point in the history
Imprement Python library for getting parameters of consai2r2_description
  • Loading branch information
ShotaAk authored Jan 7, 2020
2 parents b50bcf3 + 9c178b7 commit fb1edeb
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 4 deletions.
13 changes: 10 additions & 3 deletions consai2r2_description/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)

set(_PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE}")

if(WIN32 AND CMAKE_BUILD_TYPE STREQUAL "Debug")
set(PYTHON_EXECUTABLE "${PYTHON_EXECUTABLE_DEBUG}")
endif()

ament_python_install_package(${PROJECT_NAME})

include_directories(include ${CMAKE_CURRENT_BINARY_DIR})
add_executable(${PROJECT_NAME}_node
src/${PROJECT_NAME}_node.cpp
)
ament_target_dependencies(
${PROJECT_NAME}_node
"rclcpp"
"launch_ros"
)


Expand All @@ -39,8 +46,6 @@ install(DIRECTORY
DESTINATION share/${PROJECT_NAME}/
)



if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
Expand All @@ -52,4 +57,6 @@ if(BUILD_TESTING)
ament_lint_auto_find_test_dependencies()
endif()

set(PYTHON_EXECUTABLE "${_PYTHON_EXECUTABLE}")

ament_package()
30 changes: 30 additions & 0 deletions consai2r2_description/consai2r2_description/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2019 SSL-Roots
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import consai2r2_description.parameter


class consai2r2_parameters():

def __init__(self, node, timeout_sec=10.0):
param_names = consai2r2_description.parameter.list_parameters(node, timeout_sec)
params = consai2r2_description.parameter.get_parameters(node, param_names, timeout_sec)
for param_name, param_value in params.items():
setattr(self, param_name, param_value)
97 changes: 97 additions & 0 deletions consai2r2_description/consai2r2_description/parameter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rcl_interfaces.msg import ParameterType
from rcl_interfaces.srv import GetParameters
from rcl_interfaces.srv import ListParameters

import rclpy


# https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L174
def list_parameters(node, timeout_sec=10.0):
# create client
client = node.create_client(
ListParameters,
'consai2r2_description/list_parameters')

# call as soon as ready
ready = client.wait_for_service(timeout_sec)
if not ready:
raise RuntimeError('Wait for service timed out')

request = ListParameters.Request()
future = client.call_async(request)
rclpy.spin_until_future_complete(node, future)

# handle response
response = future.result()
if response is None:
raise RuntimeError("Failed to get the list of parameters'")

return response.result.names


# https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L122
def get_parameters(node, parameter_names, timeout_sec=10.0):
# create client
client = node.create_client(
GetParameters,
'consai2r2_description/get_parameters')

# call as soon as ready
ready = client.wait_for_service(timeout_sec)
if not ready:
raise RuntimeError('Wait for service timed out')

request = GetParameters.Request()
request.names = parameter_names
future = client.call_async(request)
rclpy.spin_until_future_complete(node, future)

# handle response
response = future.result()
if response is None:
e = future.exception()
raise RuntimeError("Failed to get parameters form node 'consai2r2_description'")

return_values = {}

# https://github.com/ros2/ros2cli/blob/780923c046f8e537e884d18bef33a2338f2d409c/ros2param/ros2param/api/__init__.py#L54
for i, pvalue in enumerate(response.values):
if pvalue.type == ParameterType.PARAMETER_BOOL:
value = pvalue.bool_value
elif pvalue.type == ParameterType.PARAMETER_INTEGER:
value = pvalue.integer_value
elif pvalue.type == ParameterType.PARAMETER_DOUBLE:
value = pvalue.double_value
elif pvalue.type == ParameterType.PARAMETER_STRING:
value = pvalue.string_value
elif pvalue.type == ParameterType.PARAMETER_BYTE_ARRAY:
value = pvalue.byte_array_value
elif pvalue.type == ParameterType.PARAMETER_BOOL_ARRAY:
value = pvalue.bool_array_value
elif pvalue.type == ParameterType.PARAMETER_INTEGER_ARRAY:
value = pvalue.integer_array_value
elif pvalue.type == ParameterType.PARAMETER_DOUBLE_ARRAY:
value = pvalue.double_array_value
elif pvalue.type == ParameterType.PARAMETER_STRING_ARRAY:
value = pvalue.string_array_value
elif pvalue.type == ParameterType.PARAMETER_NOT_SET:
value = None
else:
raise RuntimeError("Unknown parameter type '{pvalue.type}'".format_map(locals()))
return_values[parameter_names[i]] = value

return return_values
2 changes: 1 addition & 1 deletion consai2r2_description/launch/config.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory


def generate_launch_description():
Expand Down
1 change: 1 addition & 0 deletions consai2r2_description/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>python_cmake_module</buildtool_depend>

<depend>rclcpp</depend>
<depend>launch_ros</depend>
Expand Down

0 comments on commit fb1edeb

Please sign in to comment.