ROS2の自動cmakeツールament_cmake_auto

はじめに

ament_cmake_autoは、package.xmlに記載してあるパッケージを探してくれる自動cmakeツールなので、CmakeLists.txtはシンプルに書けるようになる。

find_package

ament_cmake_autoは必要なので、これのみをfind_packageしておく。

find_package(ament_cmake_auto REQUIRED)

依存を見つける=ament_auto_find_build_dependencies

package.xmlに記載してあるdependenciesを、すべてfind_packageしてくれる。

ament_auto_find_build_dependencies()

ライブラリの生成=ament_auto_add_library

add_library、target_include_directories、target_link_libraries、ament_target_dependenciesをまとめてくれる。
マルチライブラリーのの場合、ライブラリーずつリストするのみで済む。

ament_auto_add_library(lite_serial src/lite_serial.cpp include/lite_serial.hpp)

実行ファイルの生成=ament_auto_add_executable

add_executable、target_include_directories、target_link_libraries、ament_target_dependenciesをまとめてくれる。
マルチノードの場合、ノードずつリストするのみで済む。

ament_auto_add_executable(haya_imu_node src/haya_imu_node.cpp)
ament_auto_add_executable(haya_topic_echo src/haya_topic_echo.cpp)
ament_auto_add_executable(haya_topic_hz src/haya_topic_hz.cpp)

msg/srvの生成=ament_auto_generate_code

メッセージも、サービスも以下1行で済む。

ament_auto_generate_code()

ライブラリーのビルド=ament_auto_package

1行のみでexport、install関係をまとめて仕上げてくれる。

ament_auto_package()

構文のチェック=ament_lint_auto

BUILD_TESTINGデフォルト=ON、colon build – -BUILD_TESTING=OFFに指定可能。

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

テストを行う=ament_auto_add_gtest

冒頭の使用例にテストは入っていないが、ament_auto_add_gtestでテストできるようにしている。

ament_auto_find_test_dependencies()
ament_auto_add_gtest()

ament_cmake_autoの使用例

筆者はament_cmake_autoを自社開発したROS2パッケージhaya_imu_ros2に取り組んだので、CmakeLists.txtは以下のとおりとなる。

# CmakeLists.txt of haya_imu_ros2
cmake_minimum_required(VERSION 3.8)
project(haya_imu_ros2)
find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()
ament_auto_add_library(lite_serial src/lite_serial.cpp include/lite_serial.hpp)
ament_auto_add_executable(haya_imu_node src/haya_imu_node.cpp)
ament_auto_add_executable(haya_topic_echo src/haya_topic_echo.cpp)
ament_auto_add_executable(haya_topic_hz src/haya_topic_hz.cpp)
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()
install(DIRECTORY launch config DESTINATION share/${PROJECT_NAME})
ament_auto_package()

リポジトリ

https://github.com/soarbear/haya_imu_ros2 に公開済み(BSD 3-Clause License)

参考資料

https://github.com/ament/ament_cmake
Ament-CMake-Documentation(humble)

ロボット・ドローン部品お探しなら
ROBOT翔・電子部品ストア