LoginSignup
12
12

More than 5 years have passed since last update.

ROS2チュートリアル - パッケージ作成

Last updated at Posted at 2019-03-06

本記事ではROS2でパッケージを作成する方法をまとめています。
今回はHello worldの文字列を配信するpublisherノードを含むパッケージを作成します。
このパッケージを単体ビルドするところまでやります。
開発環境はUbuntu 18.04、ROS2のバージョンはCrystalです。

インストール

ROS2はソースからビルドします。Installationにしたがって環境設定等行っていることを前提とし、パッケージの作成を行います。
ROS2公式のドキュメントに従いインストールを進めていくとcolcon build --symlink installでstderrが発生する場合があります。作成するパッケージに依存しない場合は無視します。

パッケージの作成

まずワークスペースに移動し、ros2 pkg create [package_name]によってパッケージの雛形を作成します。

$ cd ~/ros2_ws/src
$ ros2 pkg create ros2_my_package

するとワークスペース内のsrc/に以下のようなディレクトリが作成されます。

src/
 |--ros2_my_package/
       |--include/
       |--src/
       |--CMakeList.txt
       |--package.xml

次にpublisherのソースファイルをmy_publisher.cppとしてros2/examples/rclcpp/minimal_publisher/member_function.cppをコピーします。

$ cd ~/ros2_ws/src/ros2_my_package/src/
$ gedit my_publisher.cpp  #ソースコードをコピー

ソースファイルを作成後、CMakeList.txtpackage.xmlを編集し、ノード名や依存パッケージ等の情報を追加します。

$ cd ../
$ gedit CMakeList.txt package.xml
CMakeList.txt
cmake_minimum_required(VERSION 3.5)
project(ros2_my_package)

# 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)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

# uncomment the following section in order to fill in
# further dependencies manually
# find _package(<dependency> REQUIRED)

add_executable(my_publisher src/my_publisher.cpp)
ament_target_dependencies(my_publisher rclcpp std_msgs)

install(TARGETS
  publisher_member_function
  DESTINATION lib/${PROJECT_NAME}
)

ament_package()
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
  <name>ros2_my_package</name>
  <version>0.6.2</version>
  <description>My Publisher Node</description>
  <maintainer email="your@email">yourname</maintainer>
  <license>Apache License 2.0</license>
  <author>yourname</author>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <build_depend>rclcpp</build_depend>
  <build_depend>std_msgs</build_depend>

  <exec_depend>rclcpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

パッケージのビルド

ファイルを編集したら、ビルドします。
--packages-selectオプションを使用することでパッケージ単体でのビルドが可能です。

$ cd ~/ros2_ws
$ colcon build --packages-select ros2_my_package
Starting >>> ros2_my_package
Finished <<< ros2_my_package [7.46s]

ビルドが完了したらパッケージの動作確認を行います。
ros2 run [パッケージ名] [ノード名]でパッケージの動作を確認します。
Hello worldが出力されたら成功です。

$ source install/setup.bash
$ ros2 run ros2_my_package my_publisher
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 0'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 1'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 2'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 3'

今回はexampleのpublisherをもとにパッケージを作成する手順をまとめました。
説明の誤り、意見等ありましたらコメントよろしくお願いします。

参考サイト

ROS2:https://index.ros.org/

12
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
12
12