mirror of https://github.com/ros2/rclpy
Initial implementation
This commit is contained in:
parent
75d7bd2b1a
commit
c3115697a1
|
@ -0,0 +1,13 @@
|
|||
Any contribution that you make to this repository will
|
||||
be under the Apache 2 License, as dictated by that
|
||||
[license](http://www.apache.org/licenses/LICENSE-2.0.html):
|
||||
|
||||
~~~
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
~~~
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
|
@ -0,0 +1,2 @@
|
|||
# rclpy
|
||||
ROS Client Library for the Python language.
|
|
@ -0,0 +1,95 @@
|
|||
cmake_minimum_required(VERSION 2.8.3)
|
||||
|
||||
project(rclpy C)
|
||||
|
||||
find_package(ament_cmake REQUIRED)
|
||||
find_package(ament_cmake_python REQUIRED)
|
||||
find_package(rcl REQUIRED)
|
||||
find_package(rmw REQUIRED)
|
||||
find_package(rmw_implementation_cmake REQUIRED)
|
||||
|
||||
# NOTE(esteve): required for CMake-2.8 in Ubuntu 14.04
|
||||
set(Python_ADDITIONAL_VERSIONS 3.4)
|
||||
find_package(PythonInterp 3.4 REQUIRED)
|
||||
|
||||
find_package(python_cmake_module REQUIRED)
|
||||
find_package(PythonExtra MODULE)
|
||||
|
||||
if(NOT WIN32)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
|
||||
endif()
|
||||
|
||||
# enables using the Python package from the source space
|
||||
# This creates a folder in ${CMAKE_CURRENT_BINARY_DIR}/rclpy which has all the
|
||||
# as well as the compiled modules from the build space
|
||||
# necessary Python code and C Python extensions for running tests.
|
||||
configure_file("__init__.py.in" "rclpy/__init__.py" @ONLY)
|
||||
|
||||
ament_python_install_package(${PROJECT_NAME})
|
||||
|
||||
macro(set_properties _build_type)
|
||||
set_target_properties(${PROJECT_NAME}${target_suffix} PROPERTIES
|
||||
PREFIX ""
|
||||
LIBRARY_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
|
||||
RUNTIME_OUTPUT_DIRECTORY${_build_type} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}"
|
||||
OUTPUT_NAME "_${PROJECT_NAME}${target_suffix}${PythonExtra_EXTENSION_SUFFIX}"
|
||||
SUFFIX "${PythonExtra_EXTENSION_EXTENSION}")
|
||||
endmacro()
|
||||
|
||||
macro(target)
|
||||
if(NOT "${target_suffix} " STREQUAL " ")
|
||||
get_rcl_information("${rmw_implementation}" "rcl${target_suffix}")
|
||||
endif()
|
||||
if("${rmw_implementation} " STREQUAL "rmw_connext_dynamic_cpp ")
|
||||
message(STATUS "Skipping tests for '${rmw_implementation}'")
|
||||
return()
|
||||
endif()
|
||||
if("${rmw_implementation} " STREQUAL "rmw_fastrtps_cpp ")
|
||||
message(STATUS "Skipping tests for '${rmw_implementation}'")
|
||||
return()
|
||||
endif()
|
||||
|
||||
add_library(
|
||||
${PROJECT_NAME}${target_suffix}
|
||||
SHARED src/rclpy/_rclpy.c
|
||||
)
|
||||
target_compile_definitions(${PROJECT_NAME}${target_suffix}
|
||||
PRIVATE "RMW_IMPLEMENTATION_SUFFIX=${target_suffix}")
|
||||
|
||||
set_properties("")
|
||||
if(WIN32)
|
||||
set_properties("_DEBUG")
|
||||
set_properties("_MINSIZEREL")
|
||||
set_properties("_RELEASE")
|
||||
set_properties("_RELWITHDEBINFO")
|
||||
endif()
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}${target_suffix}
|
||||
${PythonExtra_LIBRARIES}
|
||||
)
|
||||
|
||||
ament_target_dependencies(${PROJECT_NAME}${target_suffix}
|
||||
"rcl${target_suffix}"
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}${target_suffix}
|
||||
PUBLIC
|
||||
${PythonExtra_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME}${target_suffix}
|
||||
DESTINATION "${PYTHON_INSTALL_DIR}/${PROJECT_NAME}")
|
||||
endmacro()
|
||||
|
||||
call_for_each_rmw_implementation(target)
|
||||
|
||||
if(AMENT_ENABLE_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
|
||||
ament_add_nose_test(rclpytests test
|
||||
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
|
||||
)
|
||||
endif()
|
||||
|
||||
ament_package()
|
|
@ -0,0 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# generated from rclpy/__init__.py.in
|
||||
# keep symbol table as clean as possible by deleting all unnecessary symbols
|
||||
|
||||
from os import path as os_path
|
||||
from pkgutil import extend_path
|
||||
from sys import path as sys_path
|
||||
|
||||
source_path = '@CMAKE_CURRENT_SOURCE_DIR@'
|
||||
sys_path.insert(0, source_path)
|
||||
del sys_path
|
||||
|
||||
__path__ = extend_path(__path__, __name__)
|
||||
del extend_path
|
||||
|
||||
__execfiles = []
|
||||
src_init_file = os_path.join(source_path, __name__ + '.py')
|
||||
if os_path.isfile(src_init_file):
|
||||
__execfiles.append(src_init_file)
|
||||
else:
|
||||
src_init_file = os_path.join(source_path, __name__, '__init__.py')
|
||||
if os_path.isfile(src_init_file):
|
||||
__execfiles.append(src_init_file)
|
||||
del os_path
|
||||
del src_init_file
|
||||
del source_path
|
||||
|
||||
for __execfile in __execfiles:
|
||||
with open(__execfile, 'r') as __fh:
|
||||
exec(__fh.read())
|
||||
del __fh
|
||||
del __execfile
|
||||
del __execfiles
|
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0"?>
|
||||
<package format="2">
|
||||
<name>rclpy</name>
|
||||
<version>0.0.0</version>
|
||||
<description>Package containing the Python client.</description>
|
||||
<maintainer email="esteve@osrfoundation.org">Esteve Fernandez</maintainer>
|
||||
<license>Apache License 2.0</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
<buildtool_depend>python_cmake_module</buildtool_depend>
|
||||
|
||||
<build_depend>rmw_implementation_cmake</build_depend>
|
||||
|
||||
<depend>rmw_implementation</depend>
|
||||
<depend>rcl</depend>
|
||||
|
||||
<!--
|
||||
<exec_depend>rosidl_default_runtime</exec_depend>
|
||||
|
||||
<depend>rmw_implementation</depend>
|
||||
-->
|
||||
|
||||
<test_depend>ament_cmake_nose</test_depend>
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
<test_depend>std_msgs</test_depend>
|
||||
<test_depend>rosidl_generator_py</test_depend>
|
||||
<test_depend>rosidl_typesupport_opensplice_c</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
|
@ -0,0 +1,51 @@
|
|||
# Copyright 2016 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.
|
||||
|
||||
import rclpy._rclpy__rmw_opensplice_cpp as _rclpy
|
||||
|
||||
from rclpy.node import Node
|
||||
|
||||
|
||||
def init(args):
|
||||
return _rclpy.rclpy_init(args)
|
||||
|
||||
|
||||
def create_node(node_name):
|
||||
node_handle = _rclpy.rclpy_create_node(node_name)
|
||||
return Node(node_handle)
|
||||
|
||||
|
||||
def spin(node):
|
||||
wait_set = _rclpy.rclpy_get_zero_initialized_wait_set()
|
||||
|
||||
_rclpy.rclpy_wait_set_init(wait_set, len(node.subscriptions), 0, 0)
|
||||
|
||||
while ok():
|
||||
_rclpy.rclpy_wait_set_clear_subscriptions(wait_set)
|
||||
for subscription in node.subscriptions:
|
||||
_rclpy.rclpy_wait_set_add_subscription(wait_set, subscription.subscription_handle)
|
||||
_rclpy.rclpy_wait(wait_set)
|
||||
|
||||
msg = _rclpy.rclpy_take(subscription.subscription_handle, subscription.msg_type)
|
||||
|
||||
if msg:
|
||||
subscription.callback(msg)
|
||||
|
||||
|
||||
def ok():
|
||||
return _rclpy.rclpy_ok()
|
||||
|
||||
|
||||
def shutdown():
|
||||
return _rclpy.rclpy_shutdown()
|
|
@ -0,0 +1,37 @@
|
|||
# Copyright 2016 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.
|
||||
|
||||
import rclpy
|
||||
|
||||
from rclpy.publisher import Publisher
|
||||
from rclpy.subscription import Subscription
|
||||
|
||||
|
||||
class Node:
|
||||
|
||||
def __init__(self, handle):
|
||||
self.handle = handle
|
||||
self.subscriptions = []
|
||||
|
||||
def create_publisher(self, msg_type, topic, qos_profile):
|
||||
publisher_handle = rclpy._rclpy.rclpy_create_publisher(self.handle, msg_type, topic)
|
||||
|
||||
return Publisher(publisher_handle, msg_type, topic, qos_profile)
|
||||
|
||||
def create_subscription(self, msg_type, topic, callback, qos_profile):
|
||||
subscription_handle = rclpy._rclpy.rclpy_create_subscription(self.handle, msg_type, topic)
|
||||
|
||||
subscription = Subscription(subscription_handle, msg_type, topic, callback, qos_profile)
|
||||
self.subscriptions.append(subscription)
|
||||
return subscription
|
|
@ -0,0 +1,26 @@
|
|||
# Copyright 2016 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.
|
||||
|
||||
import rclpy
|
||||
|
||||
|
||||
class Publisher:
|
||||
def __init__(self, publisher_handle, msg_type, topic, qos_profile):
|
||||
self.publisher_handle = publisher_handle
|
||||
self.msg_type = msg_type
|
||||
self.topic = topic
|
||||
self.qos_profile = qos_profile
|
||||
|
||||
def publish(self, msg):
|
||||
rclpy._rclpy.rclpy_publish(self.publisher_handle, msg)
|
|
@ -0,0 +1,19 @@
|
|||
# Copyright 2016 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.
|
||||
|
||||
|
||||
class QoSProfile:
|
||||
pass
|
||||
|
||||
qos_profile_default = QoSProfile()
|
|
@ -0,0 +1,23 @@
|
|||
# Copyright 2016 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.
|
||||
|
||||
|
||||
class Subscription:
|
||||
|
||||
def __init__(self, subscription_handle, msg_type, topic, callback, qos_profile):
|
||||
self.subscription_handle = subscription_handle
|
||||
self.msg_type = msg_type
|
||||
self.topic = topic
|
||||
self.callback = callback
|
||||
self.qos_profile = qos_profile
|
|
@ -0,0 +1,416 @@
|
|||
// Copyright 2016 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.
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
#include <rmw/rmw.h>
|
||||
#include <rcl/error_handling.h>
|
||||
#include <rcl/rcl.h>
|
||||
#include <rcl/node.h>
|
||||
#include <rosidl_generator_c/message_type_support_struct.h>
|
||||
|
||||
#ifndef RMW_IMPLEMENTATION_SUFFIX
|
||||
#error "RMW_IMPLEMENTATION_SUFFIX is required to be set for _rclpy.c"
|
||||
#endif
|
||||
|
||||
static PyObject *
|
||||
rclpy_init(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
|
||||
{
|
||||
// TODO(esteve): parse args
|
||||
rcl_ret_t ret = rcl_init(0, NULL, rcl_get_default_allocator());
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to init: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_create_node(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
const char * node_name;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &node_name)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_node_t * node = (rcl_node_t *)PyMem_Malloc(sizeof(rcl_node_t));
|
||||
node->impl = NULL;
|
||||
rcl_node_options_t default_options = rcl_node_get_default_options();
|
||||
rcl_ret_t ret = rcl_node_init(node, node_name, &default_options);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to create node: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
PyObject * pynode = PyCapsule_New(node, NULL, NULL);
|
||||
return pynode;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_create_publisher(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pynode;
|
||||
PyObject * pymsg_type;
|
||||
PyObject * pytopic;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OOO", &pynode, &pymsg_type, &pytopic)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
assert(PyUnicode_Check(pytopic));
|
||||
|
||||
char * topic = (char *)PyUnicode_1BYTE_DATA(pytopic);
|
||||
|
||||
rcl_node_t * node = (rcl_node_t *)PyCapsule_GetPointer(pynode, NULL);
|
||||
|
||||
PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
|
||||
|
||||
PyObject * pyts = PyObject_GetAttrString(pymetaclass, "_TYPE_SUPPORT");
|
||||
|
||||
rosidl_message_type_support_t * ts =
|
||||
(rosidl_message_type_support_t *)PyCapsule_GetPointer(pyts, NULL);
|
||||
|
||||
rcl_publisher_t * publisher = (rcl_publisher_t *)PyMem_Malloc(sizeof(rcl_publisher_t));
|
||||
publisher->impl = NULL;
|
||||
rcl_publisher_options_t publisher_ops = rcl_publisher_get_default_options();
|
||||
rcl_ret_t ret = rcl_publisher_init(publisher, node, ts, topic, &publisher_ops);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to create publisher: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
PyObject * pypublisher = PyCapsule_New(publisher, NULL, NULL);
|
||||
return pypublisher;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_publish(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pypublisher;
|
||||
PyObject * pymsg;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OO", &pypublisher, &pymsg)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_publisher_t * publisher = (rcl_publisher_t *)PyCapsule_GetPointer(pypublisher, NULL);
|
||||
|
||||
PyObject * pymsg_type = PyObject_GetAttrString(pymsg, "__class__");
|
||||
|
||||
PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
|
||||
|
||||
PyObject * pyconvert_from_py = PyObject_GetAttrString(pymetaclass, "_CONVERT_FROM_PY");
|
||||
|
||||
typedef void * (* convert_from_py_signature)(PyObject *);
|
||||
convert_from_py_signature convert_from_py =
|
||||
(convert_from_py_signature)PyCapsule_GetPointer(pyconvert_from_py, NULL);
|
||||
|
||||
void * raw_ros_message = convert_from_py(pymsg);
|
||||
|
||||
rcl_ret_t ret = rcl_publish(publisher, raw_ros_message);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to publish: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_create_subscription(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pynode;
|
||||
PyObject * pymsg_type;
|
||||
PyObject * pytopic;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OOO", &pynode, &pymsg_type, &pytopic)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
assert(PyUnicode_Check(pytopic));
|
||||
|
||||
char * topic = (char *)PyUnicode_1BYTE_DATA(pytopic);
|
||||
|
||||
rcl_node_t * node = (rcl_node_t *)PyCapsule_GetPointer(pynode, NULL);
|
||||
|
||||
PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
|
||||
|
||||
PyObject * pyts = PyObject_GetAttrString(pymetaclass, "_TYPE_SUPPORT");
|
||||
|
||||
rosidl_message_type_support_t * ts =
|
||||
(rosidl_message_type_support_t *)PyCapsule_GetPointer(pyts, NULL);
|
||||
|
||||
rcl_subscription_t * subscription =
|
||||
(rcl_subscription_t *)PyMem_Malloc(sizeof(rcl_subscription_t));
|
||||
subscription->impl = NULL;
|
||||
rcl_subscription_options_t subscription_ops = rcl_subscription_get_default_options();
|
||||
rcl_ret_t ret = rcl_subscription_init(subscription, node, ts, topic, &subscription_ops);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to create subscriptions: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
PyObject * pysubscription = PyCapsule_New(subscription, NULL, NULL);
|
||||
return pysubscription;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_get_implementation_identifier(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
|
||||
{
|
||||
const char * rmw_implementation_identifier = rmw_get_implementation_identifier();
|
||||
|
||||
PyObject * pyrmw_implementation_identifier = Py_BuildValue(
|
||||
"s", rmw_implementation_identifier);
|
||||
|
||||
return pyrmw_implementation_identifier;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_get_zero_initialized_wait_set(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
|
||||
{
|
||||
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyMem_Malloc(sizeof(rcl_wait_set_t));
|
||||
wait_set->subscriptions = NULL;
|
||||
wait_set->size_of_subscriptions = 0;
|
||||
wait_set->guard_conditions = NULL;
|
||||
wait_set->size_of_guard_conditions = 0;
|
||||
wait_set->timers = NULL;
|
||||
wait_set->size_of_timers = 0;
|
||||
wait_set->impl = NULL;
|
||||
PyObject * pywait_set = PyCapsule_New(wait_set, NULL, NULL);
|
||||
return pywait_set;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_wait_set_init(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pywait_set;
|
||||
unsigned PY_LONG_LONG number_of_subscriptions;
|
||||
unsigned PY_LONG_LONG number_of_guard_conditions;
|
||||
unsigned PY_LONG_LONG number_of_timers;
|
||||
|
||||
if (!PyArg_ParseTuple(
|
||||
args, "OKKK", &pywait_set, &number_of_subscriptions,
|
||||
&number_of_guard_conditions, &number_of_timers))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyCapsule_GetPointer(pywait_set, NULL);
|
||||
|
||||
rcl_ret_t ret = rcl_wait_set_init(
|
||||
wait_set, number_of_subscriptions, number_of_guard_conditions, number_of_timers,
|
||||
rcl_get_default_allocator());
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to initialize wait set: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_wait_set_clear_subscriptions(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pywait_set;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &pywait_set)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyCapsule_GetPointer(pywait_set, NULL);
|
||||
rcl_ret_t ret = rcl_wait_set_clear_subscriptions(wait_set);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to clear subscriptions from wait set: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_wait_set_add_subscription(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pywait_set;
|
||||
PyObject * pysubscription;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OO", &pywait_set, &pysubscription)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyCapsule_GetPointer(pywait_set, NULL);
|
||||
rcl_subscription_t * subscription =
|
||||
(rcl_subscription_t *)PyCapsule_GetPointer(pysubscription, NULL);
|
||||
rcl_ret_t ret = rcl_wait_set_add_subscription(wait_set, subscription);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to add subscription to wait set: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_wait(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pywait_set;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &pywait_set)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_wait_set_t * wait_set = (rcl_wait_set_t *)PyCapsule_GetPointer(pywait_set, NULL);
|
||||
rcl_ret_t ret = rcl_wait(wait_set, RCL_S_TO_NS(1));
|
||||
if (ret != RCL_RET_OK && ret != RCL_RET_TIMEOUT) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to wait on wait set: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_take(PyObject * Py_UNUSED(self), PyObject * args)
|
||||
{
|
||||
PyObject * pysubscription;
|
||||
PyObject * pymsg_type;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OO", &pysubscription, &pymsg_type)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rcl_subscription_t * subscription =
|
||||
(rcl_subscription_t *)PyCapsule_GetPointer(pysubscription, NULL);
|
||||
|
||||
PyObject * pymetaclass = PyObject_GetAttrString(pymsg_type, "__class__");
|
||||
|
||||
PyObject * pyconvert_from_py = PyObject_GetAttrString(pymetaclass, "_CONVERT_FROM_PY");
|
||||
|
||||
typedef void *(* convert_from_py_signature)(PyObject *);
|
||||
convert_from_py_signature convert_from_py =
|
||||
(convert_from_py_signature)PyCapsule_GetPointer(pyconvert_from_py, NULL);
|
||||
|
||||
PyObject * pymsg = PyObject_CallObject(pymsg_type, NULL);
|
||||
|
||||
void * taken_msg = convert_from_py(pymsg);
|
||||
|
||||
bool taken = false;
|
||||
|
||||
rcl_ret_t ret = rcl_take(subscription, taken_msg, &taken, NULL);
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to take from a subscription: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (taken) {
|
||||
PyObject * pyconvert_to_py = PyObject_GetAttrString(pymsg_type, "_CONVERT_TO_PY");
|
||||
|
||||
typedef PyObject *(* convert_to_py_signature)(void *);
|
||||
convert_to_py_signature convert_to_py =
|
||||
(convert_to_py_signature)PyCapsule_GetPointer(pyconvert_to_py, NULL);
|
||||
|
||||
PyObject * pytaken_msg = convert_to_py(taken_msg);
|
||||
|
||||
Py_INCREF(pytaken_msg);
|
||||
|
||||
return pytaken_msg;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_ok(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
|
||||
{
|
||||
bool ok = rcl_ok();
|
||||
if (ok) {
|
||||
Py_RETURN_TRUE;
|
||||
} else {
|
||||
Py_RETURN_FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
rclpy_shutdown(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args))
|
||||
{
|
||||
rcl_ret_t ret = rcl_shutdown();
|
||||
if (ret != RCL_RET_OK) {
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"Failed to shutdown: %s", rcl_get_error_string_safe());
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyMethodDef rclpy_methods[] = {
|
||||
{"rclpy_init", rclpy_init, METH_VARARGS,
|
||||
"Initialize RCL."},
|
||||
{"rclpy_create_node", rclpy_create_node, METH_VARARGS,
|
||||
"Create a Node."},
|
||||
{"rclpy_create_publisher", rclpy_create_publisher, METH_VARARGS,
|
||||
"Create a Publisher."},
|
||||
{"rclpy_publish", rclpy_publish, METH_VARARGS,
|
||||
"Publish a message."},
|
||||
{"rclpy_create_subscription", rclpy_create_subscription, METH_VARARGS,
|
||||
"Create a Subscription."},
|
||||
|
||||
{"rclpy_get_zero_initialized_wait_set", rclpy_get_zero_initialized_wait_set, METH_NOARGS,
|
||||
"rclpy_get_zero_initialized_wait_set."},
|
||||
|
||||
{"rclpy_wait_set_init", rclpy_wait_set_init, METH_VARARGS,
|
||||
"rclpy_wait_set_init."},
|
||||
|
||||
{"rclpy_wait_set_clear_subscriptions", rclpy_wait_set_clear_subscriptions, METH_VARARGS,
|
||||
"rclpy_wait_set_clear_subscriptions."},
|
||||
|
||||
{"rclpy_wait_set_add_subscription", rclpy_wait_set_add_subscription, METH_VARARGS,
|
||||
"rclpy_wait_set_add_subscription."},
|
||||
|
||||
{"rclpy_wait", rclpy_wait, METH_VARARGS,
|
||||
"rclpy_wait."},
|
||||
|
||||
{"rclpy_take", rclpy_take, METH_VARARGS,
|
||||
"rclpy_take."},
|
||||
|
||||
{"rclpy_ok", rclpy_ok, METH_NOARGS,
|
||||
"rclpy_ok."},
|
||||
|
||||
{"rclpy_shutdown", rclpy_shutdown, METH_NOARGS,
|
||||
"rclpy_shutdown."},
|
||||
|
||||
{"rclpy_get_implementation_identifier", rclpy_get_implementation_identifier,
|
||||
METH_NOARGS, "Retrieve the identifier for the active RMW implementation."},
|
||||
{NULL, NULL, 0, NULL} /* sentinel */
|
||||
};
|
||||
|
||||
static struct PyModuleDef _rclpymodule = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_rclpy",
|
||||
"_rclpy_doc",
|
||||
-1, /* -1 means that the module keeps state in global variables */
|
||||
rclpy_methods,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
#define MAKE_FN_NAME(x) PyInit__rclpy ## x
|
||||
#define FUNCTION_NAME(suffix) MAKE_FN_NAME(suffix)
|
||||
|
||||
PyMODINIT_FUNC FUNCTION_NAME(RMW_IMPLEMENTATION_SUFFIX)(void) {
|
||||
return PyModule_Create(&_rclpymodule);
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
# Copyright 2016 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 functools import partial
|
||||
from multiprocessing import Process
|
||||
from time import sleep
|
||||
|
||||
|
||||
def talker():
|
||||
import rclpy
|
||||
from rclpy.qos import qos_profile_default
|
||||
|
||||
from std_msgs.msg import String
|
||||
assert String.__class__._TYPE_SUPPORT is not None
|
||||
rclpy.init([])
|
||||
|
||||
node = rclpy.create_node('talker')
|
||||
|
||||
chatter_pub = node.create_publisher(String, 'chatter', qos_profile_default)
|
||||
|
||||
msg = String()
|
||||
|
||||
i = 1
|
||||
while True:
|
||||
msg.data = 'Hello World: {0}'.format(i)
|
||||
i += 1
|
||||
chatter_pub.publish(msg)
|
||||
sleep(1)
|
||||
|
||||
|
||||
def listener_callback(msg, talker_process, received_messages):
|
||||
import rclpy
|
||||
talker_process.terminate()
|
||||
received_messages.append(msg)
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
def test_rclpy_talker_listener():
|
||||
talker_process = Process(target=talker)
|
||||
talker_process.start()
|
||||
|
||||
import rclpy
|
||||
from rclpy.qos import qos_profile_default
|
||||
|
||||
rclpy.init([])
|
||||
|
||||
node = rclpy.create_node('listener')
|
||||
|
||||
received_messages = []
|
||||
|
||||
chatter_callback = partial(
|
||||
listener_callback, talker_process=talker_process, received_messages=received_messages)
|
||||
|
||||
from std_msgs.msg import String
|
||||
assert String.__class__._TYPE_SUPPORT is not None
|
||||
|
||||
node.create_subscription(String, 'chatter', chatter_callback, qos_profile_default)
|
||||
|
||||
rclpy.spin(node)
|
||||
|
||||
talker_process.join()
|
||||
|
||||
assert len(received_messages) > 0, "Should have received a message from talker"
|
Loading…
Reference in New Issue