tshell_blog

ソフトウェアと車輪がついた乗り物のはなし

Raspberry Pi 4 + Ubuntu 20.04 + ROS2 foxyでルンバを動かす

f:id:tshell:20210314211444g:plain

ラズパイでルンバを動かす話はググるとたくさん出てきますが,ラズパイ上でROS2ノードをビルドしようとすると途中で止まったりやたらと時間がかかったりとハマりどころが多かったので以下にまとめます。

手順

以下の手順で進めます。

  1. ソースのダウンロード
  2. qemu-user-staticでビルド
  3. Raspberry Pi 4に転送する
  4. 動作確認

ルンバ用ROS2ノードのダウンロード

以下のソースをcreate_ws/srcにダウンロードします。

$ mkdir -p create_ws/src
$ cd create_ws/src
$ git clone -b foxy https://github.com/AutonomyLab/create_robot.git
$ git clone https://github.com/AutonomyLab/libcreate.git

qemu-user-staticを使ったDockerコンテナでビルドする

tshell.hatenablog.com

前回の記事で作成したRaspberry PiUbuntu 20.04をx86 PC上で動かすコンテナを起動し,その中でビルドします。

まずは先ほどダウンロードしたソースがあるcreate_wsをマウントしてコンテナを起動します。

$ docker run -it -v $(pwd)/create_ws:/home/ubuntu/create_ws --name raspi raspi-ubuntu-ros2:foxy /bin/bash
# adduser ubuntu
# usermod -a -G sudo ubuntu
# chmod ubuntu:ubuntu /home/ubuntu
# su ubuntu
$ cd ~/create_ws

libcreateの依存パッケージをインストールします。

$ sudo apt update
$ sudo apt install build-essential cmake libboost-system-dev libboost-thread-dev

create_robotの依存パッケージをインストールします。

$ source /opt/ros/foxy/setup.bash
$ sudo apt install python3-rosdep
$ cd ~/create_ws
$ sudo rosdep init
$ rosdep update
$ rosdep install --from-paths src -i

ビルドします。

$ colcon build

なにかエラーが出ています。

Summary: 6 packages finished [8min 15s]
  1 package had stderr output: libcreate

libcreateのstderr.logを見てみるとtestのビルドに入ったメッセージが出ているだけで特にエラーではないようなのでこのまま続行します。

$ cat log/latest_build/libcreate/stderr.log
GTest installation found. Building tests.

Raspberry Pi 4へ転送

ビルドしたファイルをtarに固めてRaspberry Pi 4へ転送します。
Raspberry Pi 4には予め/home/ubuntu/create_wsディレクトリを作成しておきます。

$ cd ~/create_ws
$ tar cf ../create_ws.tar .
$ cd ..
$ scp create_ws.tar ubuntu@<Raspberry Pi 4のIPアドレス>:~/create_ws

Raspberry Pi 4にSSH接続し,先ほど転送したtarファイルを展開します。

$ ssh ubuntu@<Raspberry Pi 4のIPアドレス>
$ cd create_ws
$ tar xvf create_ws.tar

ros-foxy-xacroをインストール後,動作確認します。

$ sudo apt install ros-foxy-xacro
$ source instal/setup.bash
$ ros2 launch create_bringup create_2.launch

デフォルトでは/dev/ttyUSB0を使ってルンバと通信しようとします。USB - シリアル変換器を接続していないと以下のようなエラーが発生します。

[create_driver-1]   what():  open: No such file or directory

ルンバと接続するシリアルポートを変更するには create_ws/install/create_bringup/share/create_bringup/config/default.yamldev:行を変更します。

create_driver:
  ros__parameters:
    # The device path for the robot
    dev: "/dev/ttyAMA2"
...

Raspberry Pi 4で複数のUARTを起動する方法は以下の記事に書いています。

tshell.hatenablog.com

動作確認

ルンバと接続しない状態で起動してみます。

$ ros2 launch create_bringup create_2.launch
...
[create_driver-1] [create::Create] retrying to establish serial connection...
[create_driver-1] [create::Serial] serial error - Operation canceled
[create_driver-1] [create::Serial] failed to receive data from Create. Check if robot is powered!
[create_driver-1] [create::Create] retrying to establish serial connection...
[create_driver-1] [create::Serial] serial error - Operation canceled
...

今度はcreate_driverノードが起動してルンバと接続を試みています。

ルンバと接続

f:id:tshell:20210314211552j:plain

ルンバのシリアルポートとRaspberry Pi 4のUARTを直結してos2 launch create_bringup create_2.launchを実行すると以下のようなメッセージが表示され,ルンバと通信できていることが確認できます。
本当はルンバのシリアルポートとRaspberry Pi 4のUARTは信号レベルが違うのでレベル変換回路を入れる必要がありますが,直結でも動きます。

[create_driver-1] [INFO] [1615607466.306440053] [create_driver]: [CREATE] Connection established.
[create_driver-1] [INFO] [1615607466.306769267] [create_driver]: [CREATE] Battery level 90.95 %
[create_driver-1] [INFO] [1615607466.350583726] [create_driver]: [CREATE] Ready.

ここまで来ればcmd_velにPublishすることでルンバを動かすことができます。

ジョイスティック操作

XBOXのコントローラを使ってルンバを動かしてみます。
XBOXのコントローラをPCに接続したら以下のコマンドでteleop_twist_joyを起動します。

$ ros2 launch teleop_twist_joy teleop-launch.py joy_config:='xbox'

LRボタンを同時押ししながら左スティックを操作するとルンバが動きます。