18
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ROS2でパッケージ作成(Publisher)

Last updated at Posted at 2019-02-09

こんにちは、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などは各自自分のものに書き換えましょう。

setup.py
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',
        ],
    },
)
setup.cfg
[develop]
script-dir=$base/lib/ros2_demo_py
[install]
install-scripts=$base/lib/ros2_demo_py

package.xmlのパッケージ名も変更します。

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を作成します。

参考

18
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?