こんにちは、KN_Appleです。
今回はROS2でPythonのパッケージを作ってみようと思います。
環境は以下の通り。
- Ubuntu 18.04
- ROS dashing
準備
まずはROS1での開発と同様にワークスペースを作成して、移動。
$ mkdir -p ~/ros2_ws/src
$ cd ~/ros2_ws/src/
パッケージ作成
次にパッケージ作成。ROS1ではcatkin_create_pkg
コマンドを用いてパッケージを作成していました。ROS2にも同様のコマンドがあるので、それを使います。
$ ros2 pkg create ros2_demo_py
コマンドの書式はros2 pkg create
の後ろにパッケージ名をつける。(今回はros2_demo_pyとしました。)
これでパッケージのひな型が作成されます。
次にPython用の設定ファイルを追加します。
ROS1ではPythonでコードを書くときは特に設定ファイルは書きませんでしたが、ROS2からは必要になります。
$ touch ros2_demo_py/setup.py
$ touch ros2_demo_py/setup.cfg
ファイルの中身はここからコピーしたものを少し変更して使います。
変更後のファイルは以下の通り。authorやmaintainer、Licenseなどは各自自分のものに書き換えましょう。
from setuptools import setup
package_name = 'ros2_demo_py'
setup(
name=package_name,
version='0.6.1',
packages=[],
py_modules=[
'demo'
],
install_requires=['setuptools'],
zip_safe=True,
author='yourname',
author_email='email@hoge',
maintainer='yourname',
maintainer_email='email@hoge',
keywords=['ROS'],
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Topic :: Software Development',
],
description='Examples of minimal publishers using rclpy.',
license='Apache License, Version 2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'demo = demo:main',
],
},
)
[develop]
script-dir=$base/lib/ros2_demo_py
[install]
install-scripts=$base/lib/ros2_demo_py
package.xmlのパッケージ名も変更します。
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>ros2_demo_py</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="nakano16180@gmail.com">kaito</maintainer>
<license>TODO: License declaration</license>
<exec_depend>rclpy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- These test dependencies are optional
Their purpose is to make sure that the code passes the linters -->
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
最後にソースファイルを追加します。
ソースファイルの名前をdemo.pyとし、上のリンク先のexamples_rclpy_minimal_publisher/publisher_member_function.pyをそのままコピーして貼り付け。
Buildと実行
ビルドのコマンドは以下の通り。
$ cd ~/ros2_ws/ && colcon build --symlink-install
ビルドが成功したら、以下のコマンドでpackageができてることを確認
$ source install/setup.bash
$ source install/local_setup.bash
$ ros2 pkg list | grep ros2
実行は以下のコマンド
ros2 run ros2_demo_py demo
こちらの書式はros2 run パッケージ名 ノード名
となっています。
これで自作パッケージができました。
次回はSubscriberを作成します。