From 69a086665a5c09fb0cfacb1cc43e47ff551a3fa7 Mon Sep 17 00:00:00 2001 From: Terence Tong Date: Mon, 21 Feb 2022 14:26:41 +0800 Subject: [PATCH] Mon Feb 21 02:26:41 PM HKT 2022 --- rospackage/tutorial/msg/Num.msg | 2 + rospackage/tutorial/package.xml | 24 ++++++++++ rospackage/tutorial/resource/tutorial | 0 rospackage/tutorial/setup.cfg | 4 ++ rospackage/tutorial/setup.py | 29 ++++++++++++ rospackage/tutorial/srv/AddTwoInts.srv | 5 ++ rospackage/tutorial/test/test_copyright.py | 23 +++++++++ rospackage/tutorial/test/test_flake8.py | 25 ++++++++++ rospackage/tutorial/test/test_pep257.py | 23 +++++++++ rospackage/tutorial/tutorial/__init__.py | 0 rospackage/tutorial/tutorial/client.py | 47 +++++++++++++++++++ rospackage/tutorial/tutorial/publisher.py | 41 ++++++++++++++++ rospackage/tutorial/tutorial/service.py | 31 ++++++++++++ rospackage/tutorial/tutorial/subscriber.py | 38 +++++++++++++++ rospackage/tutorial_interfaces/CMakeLists.txt | 39 +++++++++++++++ rospackage/tutorial_interfaces/msg/Num.msg | 2 + rospackage/tutorial_interfaces/package.xml | 20 ++++++++ .../tutorial_interfaces/srv/AddTwoInts.srv | 6 +++ 18 files changed, 359 insertions(+) create mode 100644 rospackage/tutorial/msg/Num.msg create mode 100644 rospackage/tutorial/package.xml create mode 100644 rospackage/tutorial/resource/tutorial create mode 100644 rospackage/tutorial/setup.cfg create mode 100644 rospackage/tutorial/setup.py create mode 100644 rospackage/tutorial/srv/AddTwoInts.srv create mode 100644 rospackage/tutorial/test/test_copyright.py create mode 100644 rospackage/tutorial/test/test_flake8.py create mode 100644 rospackage/tutorial/test/test_pep257.py create mode 100644 rospackage/tutorial/tutorial/__init__.py create mode 100644 rospackage/tutorial/tutorial/client.py create mode 100644 rospackage/tutorial/tutorial/publisher.py create mode 100644 rospackage/tutorial/tutorial/service.py create mode 100644 rospackage/tutorial/tutorial/subscriber.py create mode 100644 rospackage/tutorial_interfaces/CMakeLists.txt create mode 100644 rospackage/tutorial_interfaces/msg/Num.msg create mode 100644 rospackage/tutorial_interfaces/package.xml create mode 100644 rospackage/tutorial_interfaces/srv/AddTwoInts.srv diff --git a/rospackage/tutorial/msg/Num.msg b/rospackage/tutorial/msg/Num.msg new file mode 100644 index 0000000..3198c87 --- /dev/null +++ b/rospackage/tutorial/msg/Num.msg @@ -0,0 +1,2 @@ +int64 num + diff --git a/rospackage/tutorial/package.xml b/rospackage/tutorial/package.xml new file mode 100644 index 0000000..fe37755 --- /dev/null +++ b/rospackage/tutorial/package.xml @@ -0,0 +1,24 @@ + + + + tutorial + 0.0.0 + TODO: Package description + tmtong + TODO: License declaration + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + tutorial_interfaces + rclpy + std_msgs + rosidl_default_generators + rosidl_default_runtime + rosidl_interface_packages + + + ament_python + + diff --git a/rospackage/tutorial/resource/tutorial b/rospackage/tutorial/resource/tutorial new file mode 100644 index 0000000..e69de29 diff --git a/rospackage/tutorial/setup.cfg b/rospackage/tutorial/setup.cfg new file mode 100644 index 0000000..eece7fc --- /dev/null +++ b/rospackage/tutorial/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script-dir=$base/lib/tutorial +[install] +install-scripts=$base/lib/tutorial diff --git a/rospackage/tutorial/setup.py b/rospackage/tutorial/setup.py new file mode 100644 index 0000000..f75cb8c --- /dev/null +++ b/rospackage/tutorial/setup.py @@ -0,0 +1,29 @@ +from setuptools import setup + +package_name = 'tutorial' + +setup( + name=package_name, + version='0.0.0', + packages=[package_name], + data_files=[ + ('share/ament_index/resource_index/packages', + ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), + ], + install_requires=['setuptools'], + zip_safe=True, + maintainer='tmtong', + maintainer_email='tmtong@gmail.com', + description='TODO: Package description', + license='TODO: License declaration', + tests_require=['pytest'], + entry_points={ + 'console_scripts': [ + 'publisher = tutorial.publisher:main', + 'subscriber = tutorial.subscriber:main', + 'service = tutorial.service:main', + 'client = tutorial.client:main', + ], + }, +) diff --git a/rospackage/tutorial/srv/AddTwoInts.srv b/rospackage/tutorial/srv/AddTwoInts.srv new file mode 100644 index 0000000..01286e2 --- /dev/null +++ b/rospackage/tutorial/srv/AddTwoInts.srv @@ -0,0 +1,5 @@ +int64 a +int64 b +--- +int64 sum + diff --git a/rospackage/tutorial/test/test_copyright.py b/rospackage/tutorial/test/test_copyright.py new file mode 100644 index 0000000..cc8ff03 --- /dev/null +++ b/rospackage/tutorial/test/test_copyright.py @@ -0,0 +1,23 @@ +# Copyright 2015 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 ament_copyright.main import main +import pytest + + +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/rospackage/tutorial/test/test_flake8.py b/rospackage/tutorial/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/rospackage/tutorial/test/test_flake8.py @@ -0,0 +1,25 @@ +# Copyright 2017 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 ament_flake8.main import main_with_errors +import pytest + + +@pytest.mark.flake8 +@pytest.mark.linter +def test_flake8(): + rc, errors = main_with_errors(argv=[]) + assert rc == 0, \ + 'Found %d code style errors / warnings:\n' % len(errors) + \ + '\n'.join(errors) diff --git a/rospackage/tutorial/test/test_pep257.py b/rospackage/tutorial/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/rospackage/tutorial/test/test_pep257.py @@ -0,0 +1,23 @@ +# Copyright 2015 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 ament_pep257.main import main +import pytest + + +@pytest.mark.linter +@pytest.mark.pep257 +def test_pep257(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found code style errors / warnings' diff --git a/rospackage/tutorial/tutorial/__init__.py b/rospackage/tutorial/tutorial/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rospackage/tutorial/tutorial/client.py b/rospackage/tutorial/tutorial/client.py new file mode 100644 index 0000000..b47d168 --- /dev/null +++ b/rospackage/tutorial/tutorial/client.py @@ -0,0 +1,47 @@ + +from tutorial_interfaces.srv import AddTwoInts +import rclpy +from rclpy.node import Node +import sys + +class MinimalClientAsync(Node): + + def __init__(self): + super().__init__('minimal_client_async') + self.cli = self.create_client(AddTwoInts, 'add_two_ints') + while not self.cli.wait_for_service(timeout_sec=1.0): + self.get_logger().info('service not available, waiting again...') + self.req = AddTwoInts.Request() + + def send_request(self): + self.req.a = int(sys.argv[1]) + self.req.b = int(sys.argv[2]) + self.future = self.cli.call_async(self.req) + + +def main(args=None): + rclpy.init(args=args) + + minimal_client = MinimalClientAsync() + minimal_client.send_request() + + while rclpy.ok(): + rclpy.spin_once(minimal_client) + if minimal_client.future.done(): + try: + response = minimal_client.future.result() + except Exception as e: + minimal_client.get_logger().info( + 'Service call failed %r' % (e,)) + else: + minimal_client.get_logger().info( + 'Result of add_two_ints: for %d + %d = %d' % + (minimal_client.req.a, minimal_client.req.b, response.sum)) + break + + minimal_client.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main() diff --git a/rospackage/tutorial/tutorial/publisher.py b/rospackage/tutorial/tutorial/publisher.py new file mode 100644 index 0000000..1600c36 --- /dev/null +++ b/rospackage/tutorial/tutorial/publisher.py @@ -0,0 +1,41 @@ +import rclpy +from rclpy.node import Node + +from std_msgs.msg import String + + +class MinimalPublisher(Node): + + def __init__(self): + super().__init__('minimal_publisher') + self.publisher_ = self.create_publisher(String, 'topic', 10) + timer_period = 0.5 # seconds + self.timer = self.create_timer(timer_period, self.timer_callback) + self.i = 0 + + def timer_callback(self): + msg = String() + msg.data = 'Hello World: %d' % self.i + self.publisher_.publish(msg) + self.get_logger().info('Publishing: "%s"' % msg.data) + self.i += 1 + + +def main(args=None): + rclpy.init(args=args) + + minimal_publisher = MinimalPublisher() + + rclpy.spin(minimal_publisher) + + # Destroy the node explicitly + # (optional - otherwise it will be done automatically + # when the garbage collector destroys the node object) + minimal_publisher.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main() + + diff --git a/rospackage/tutorial/tutorial/service.py b/rospackage/tutorial/tutorial/service.py new file mode 100644 index 0000000..9effb16 --- /dev/null +++ b/rospackage/tutorial/tutorial/service.py @@ -0,0 +1,31 @@ +from tutorial_interfaces.srv import AddTwoInts + +import rclpy +from rclpy.node import Node + + +class MinimalService(Node): + + def __init__(self): + super().__init__('minimal_service') + self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_two_ints_callback) + + def add_two_ints_callback(self, request, response): + response.sum = request.a + request.b + self.get_logger().info('Incoming request\na: %d b: %d' % (request.a, request.b)) + + return response + + +def main(args=None): + rclpy.init(args=args) + + minimal_service = MinimalService() + + rclpy.spin(minimal_service) + + rclpy.shutdown() + + +if __name__ == '__main__': + main() diff --git a/rospackage/tutorial/tutorial/subscriber.py b/rospackage/tutorial/tutorial/subscriber.py new file mode 100644 index 0000000..ca8f4aa --- /dev/null +++ b/rospackage/tutorial/tutorial/subscriber.py @@ -0,0 +1,38 @@ +import rclpy +from rclpy.node import Node + +from std_msgs.msg import String + + +class MinimalSubscriber(Node): + + def __init__(self): + super().__init__('minimal_subscriber') + self.subscription = self.create_subscription( + String, + 'topic', + self.listener_callback, + 10) + self.subscription # prevent unused variable warning + + def listener_callback(self, msg): + self.get_logger().info('I heard: "%s"' % msg.data) + + +def main(args=None): + rclpy.init(args=args) + + minimal_subscriber = MinimalSubscriber() + + rclpy.spin(minimal_subscriber) + + # Destroy the node explicitly + # (optional - otherwise it will be done automatically + # when the garbage collector destroys the node object) + minimal_subscriber.destroy_node() + rclpy.shutdown() + + +if __name__ == '__main__': + main() + diff --git a/rospackage/tutorial_interfaces/CMakeLists.txt b/rospackage/tutorial_interfaces/CMakeLists.txt new file mode 100644 index 0000000..a09072b --- /dev/null +++ b/rospackage/tutorial_interfaces/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.5) +project(tutorial_interfaces) + +# Default to C99 +if(NOT CMAKE_C_STANDARD) + set(CMAKE_C_STANDARD 99) +endif() + +# Default to C++14 +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 14) +endif() + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +# uncomment the following section in order to fill in +# further dependencies manually. +# find_package( REQUIRED) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + # the following line skips the linter which checks for copyrights + # uncomment the line when a copyright and license is not present in all source files + #set(ament_cmake_copyright_FOUND TRUE) + # the following line skips cpplint (only works in a git repo) + # uncomment the line when this package is not in a git repo + #set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() +endif() +find_package(rosidl_default_generators REQUIRED) +rosidl_generate_interfaces(${PROJECT_NAME} + "msg/Num.msg" + "srv/AddTwoInts.srv" +) +ament_package() diff --git a/rospackage/tutorial_interfaces/msg/Num.msg b/rospackage/tutorial_interfaces/msg/Num.msg new file mode 100644 index 0000000..3198c87 --- /dev/null +++ b/rospackage/tutorial_interfaces/msg/Num.msg @@ -0,0 +1,2 @@ +int64 num + diff --git a/rospackage/tutorial_interfaces/package.xml b/rospackage/tutorial_interfaces/package.xml new file mode 100644 index 0000000..c04e7fc --- /dev/null +++ b/rospackage/tutorial_interfaces/package.xml @@ -0,0 +1,20 @@ + + + + tutorial_interfaces + 0.0.0 + TODO: Package description + tmtong + TODO: License declaration + + ament_cmake + rosidl_default_generators + rosidl_default_runtime + rosidl_interface_packages + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/rospackage/tutorial_interfaces/srv/AddTwoInts.srv b/rospackage/tutorial_interfaces/srv/AddTwoInts.srv new file mode 100644 index 0000000..4533090 --- /dev/null +++ b/rospackage/tutorial_interfaces/srv/AddTwoInts.srv @@ -0,0 +1,6 @@ +int64 a +int64 b +int64 c +--- +int64 sum +