はじめに
とある研修の教材作りにROS2を使用してはどうか、たまたま周りにJavaできる人多いよねという事で、私が手始めにROS2Javaを触ってみることにしました。
(この記事は身内向けの記事ですが、他にROS2Javaを試してみたい方にも参考になるかもしれないので投稿しました。)
まずはROS2Javaのリポジトリを見て、自分のMacBook Airにインストールしてみようと思いましたがbrew/pipで入れる依存モジュールやROS/ROS2の依存モジュールのバージョンの組み合わせが悪いのかビルドがうまくいきませんでした。
今回はいろいろ試行錯誤してできたビルド手順をまとめておきたいと思います。
macのローカルではなくUbuntu18.04のDockerコンテナ内でのビルドになります。
環境
- OS : macOS 10.15.7
- CPU : 1.2 GHz クアッドコアIntel Core i7
- Docker : 20.10.0
ビルド手順
Docker Imageのダウンロード
これを研修で使うにあたり、参加者のそれぞれのPCのローカルでROS2Javaをビルドするものならば環境の違いでビルドが通らないことが往々にして発生しうるだろうということで、Dockerコンテナ内でビルド環境を作ってこれを配布した方がいいと思いました。
既にビルド環境が整っているUbuntuのImageを見つけたので、これを使用することにします。
docker pull theosakamg7/ros2_java_docker:ubuntu-ros2java
コンテナ起動
念のためコンテナ内のビルドに使用するディレクトリをローカルにマウントしておきます。
ローカルにマウント用のディレクトリを作っておきます。
mkdir volume
起動のための
docker-compose.yml
を用意します。
version: '3'
services:
ros2java_ubuntu:
image: theosakamg7/ros2_java_docker:ubuntu
command: bash -c "/bin/bash"
tty: true
stdin_open: true
volumes:
- ./volume:/home/travis
コンテナ内に入りましょう。
docker-compose up -d
docker-compose exec ros2java_ubuntu bash
ビルド作業
amentのビルド
amentとはcmakeベースのビルドツールです。
ROS2をビルドする際に使用します。
まずは作業ディレクトリを作っておきましょう。
cd /home/travis/
mkdir ament_ws/src
cd ament_ws
touch ament_java.repos
ament_java.reposに以下を記述します。
repositories:
ament/ament_cmake:
type: git
url: https://github.com/ament/ament_cmake.git
version: 0.5.1
ament/ament_index:
type: git
url: https://github.com/ament/ament_index.git
version: 0.5.1
ament/ament_lint:
type: git
url: https://github.com/ament/ament_lint.git
version: 0.5.2
ament/ament_package:
type: git
url: https://github.com/ament/ament_package.git
version: 0.5.2
ament/ament_tools:
type: git
url: https://github.com/esteve/ament_tools.git
version: win32-platform
ament/googletest:
type: git
url: https://github.com/ament/googletest.git
version: 4b6e624e78ba3d43c1602ffc80478ee7253e0b04
ament/osrf_pycommon:
type: git
url: https://github.com/osrf/osrf_pycommon.git
version: 0.1.5
ament/uncrustify:
type: git
url: https://github.com/ament/uncrustify.git
version: 0.66.1
ament_java/ament_java:
type: git
url: https://github.com/esteve/ament_java.git
version: master
vcsを用いてソースコード一式をダウンロードし、amentをビルドします。
なお、vcsはDocker Imageにプリインストール済みなのでいきなり使えます。
vcs import src < ament_java.repos
src/ament/ament_tools/scripts/ament.py build --symlink-install --isolated
ROS2Javaのビルド
cd /home/travis
mkdir ros2java_ws/src
cd ros2java_ws
touch ros2_java_desktop.repos
公式リポジトリのREADME通りではビルドエラー吐きまくるので、試行錯誤しました。。。
2021/03/13現在以下の依存モジュールのバージョンでビルドできました。
ros2_java_desktop.repos
に以下を記述します。
repositories:
eProsima/Fast-CDR:
type: git
url: https://github.com/eProsima/Fast-CDR.git
version: v1.0.7
eProsima/Fast-RTPS:
type: git
url: https://github.com/eProsima/Fast-RTPS.git
version: 7a0b0fe7ca8d2c4ea41e36744c6024c263a6505a
ros/class_loader:
type: git
url: https://github.com/ros/class_loader.git
version: 1.1.0
ros/console_bridge:
type: git
url: https://github.com/ros/console_bridge.git
version: ad25f7307da76be2857545e7e5c2a20727eee542
ros2/console_bridge_vendor:
type: git
url: https://github.com/ros2/console_bridge_vendor.git
version: 1.3.0
ros2/ament_cmake_ros:
type: git
url: https://github.com/ros2/ament_cmake_ros.git
version: 0.5.0
ros2/common_interfaces:
type: git
url: https://github.com/ros2/common_interfaces.git
version: 0.5.0
ros2/example_interfaces:
type: git
url: https://github.com/ros2/example_interfaces.git
version: 0.5.0
ros2/launch:
type: git
url: https://github.com/ros2/launch.git
version: 0.5.2
ros2/libyaml_vendor:
type: git
url: https://github.com/ros2/libyaml_vendor.git
version: 1.0.0
ros2/poco_vendor:
type: git
url: https://github.com/ros2/poco_vendor.git
version: 1.1.1
ros2/tinydir_vendor:
type: git
url: https://github.com/ros2/tinydir_vendor.git
version: v1.0.0
ros2/rcl:
type: git
url: https://github.com/ros2/rcl.git
version: 0.5.1
ros2/rcl_interfaces:
type: git
url: https://github.com/ros2/rcl_interfaces.git
version: 0.5.0
ros2/rclpy:
type: git
url: https://github.com/ros2/rclpy.git
version: 0.5.3
ros2/rcutils:
type: git
url: https://github.com/ros2/rcutils.git
version: 0.5.1
ros2/rmw:
type: git
url: https://github.com/ros2/rmw.git
version: 0.5.0
ros2/rmw_fastrtps:
type: git
url: https://github.com/ros2/rmw_fastrtps.git
version: 0.5.1
ros2/rmw_implementation:
type: git
url: https://github.com/ros2/rmw_implementation.git
version: 0.5.1
ros2/rmw_opensplice:
type: git
url: https://github.com/ros2/rmw_opensplice.git
version: 0.5.2
ros2/ros2cli:
type: git
url: https://github.com/ros2/ros2cli.git
version: 0.5.3
ros2/rosidl:
type: git
url: https://github.com/ros2/rosidl.git
version: 0.5.1
ros2/rosidl_typesupport_opensplice:
type: git
url: https://github.com/ros2/rosidl_typesupport_opensplice.git
version: 0.5.0
ros2/rosidl_typesupport:
type: git
url: https://github.com/ros2/rosidl_typesupport
version: 0.5.0
ros2/rosidl_dds:
type: git
url: https://github.com/ros2/rosidl_dds.git
version: 0.5.0
ros2/rosidl_defaults:
type: git
url: https://github.com/ros2/rosidl_defaults.git
version: 0.5.0
ros2/rosidl_python:
type: git
url: https://github.com/ros2/rosidl_python.git
version: 0.5.2
ros2_java/ros2_java:
type: git
url: https://github.com/esteve/ros2_java.git
version: 62918477f4fb8810d4df0ac9c9e67b885b635d01
ros2_java/ros2_java_examples:
type: git
url: https://github.com/esteve/ros2_java_examples.git
version: 4305e58493bfa37484457b3fe12525cbc0f87319
ros2/unique_identifier_msgs:
type: git
url: https://github.com/ros2/unique_identifier_msgs.git
version: 2.2.0
ros2/test_interface_files:
type: git
url: https://github.com/ros2/test_interface_files.git
version: 124823291a0a7d35e983b2e00abc8803ec4cdbb2
ROS2Javaをビルドします。
まだまだ付け焼き刃的な対応が入っているので、追々根本的な調査と修正を行ってから追記する予定です。
vcs import src < ros2_java_desktop.repos
. ../ament_ws/install_isolated/local_setup.sh
# 途中Javaソースコードのコンパイルエラーが起きるので、一旦回避するため変えておく
sed -ie 's/client\.waitForService()/true/' ./src/ros2_java/ros2_java_examples/rcljava_examples/src/main/java/org/ros2/rcljava/examples/client/AddTwoIntsClient.java
# test_msgsだけビルドエラーが消えなかったので一旦スキップ
ament build --symlink-install --isolated --skip-packages test_msgs
. ./install_isolated/local_setup.sh
これでビルドが完了すればros2コマンドが使用できるはずです。