ROS MelodicでTurtlebot3をGazeboで動かしてついでにSLAMする


まえがき

Gazebo上に置いたTurtlebot3でgmappingをやるための手順のメモ.

Turtlebot3とは?→ ROSが使える差動二輪ロボットプラットフォーム.
gmappingとは?→ 格子地図を生成するためのSLAMパッケージ.

gmappingは超簡単に言うと,tfでセンサの座標を見ながら/scanをsubscribeして/mapを作ってくれるすごいやつです.
Turtlebot3は自身のオドメトリから計算されるtfを更新しながら,搭載しているレーザセンサ(Laser Distance Sensor LDS-01)の値を/scanでpublishしています.
なので,Turtlebot3のgazeboシミュレータとgmappingのノードを動かせば地図が生成される.はず.

環境

私の実行環境は以下のとおりです.

  • CPU: Intel core i7 3770
  • GPU: Geforce RTX2070
  • OS: Ubuntu 18.04 LTS
  • ROS: Melodic

手順

前提

ROS Melodicのインストールが済んでいること.インストール方法は色々と情報が転がってると思うので割愛.
GazeboとRVizも使うので,ROSインストール時に一緒に入ると思うけど一応動作を確認しておいてください.

$ gazebo
$ rviz

依存パッケージをインストール

多い

sudo apt-get install ros-melodic-joy ros-melodic-teleop-twist-joy ros-melodic-teleop-twist-keyboard ros-melodic-laser-proc ros-melodic-rgbd-launch ros-melodic-depthimage-to-laserscan ros-melodic-rosserial-arduino ros-melodic-rosserial-python ros-melodic-rosserial-server ros-melodic-rosserial-client ros-melodic-rosserial-msgs ros-melodic-amcl ros-melodic-map-server ros-melodic-move-base ros-melodic-urdf ros-melodic-xacro ros-melodic-compressed-image-transport ros-melodic-rqt-image-view ros-melodic-navigation

本当はgmappingもaptで入ってくれると嬉しいんだけど,残念ながらまだMelodic版はaptで入ってくれない(2019.5.9現在)ので,これは後々手動で追加します.

Turtlebot3の環境構築

いつも通りワークスペースを作ります.ワークスペース名は任意.

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
$ cd ~/catkin_ws

Turtlebot3のROSパッケージをcloneします.

$ cd ~/catkin_ws/src
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_gazebo_plugin.git

ビルド.

$ cd ~/catkin_ws
$ catkin_make
$ source ~/catkin_ws/devel/setup.bash

RVizで動作確認

環境変数でTurtlebot3のモデルを指定する必要があるので以下を実行してください.

$ export TURTLEBOT3_MODEL=burger

モデルにはburgerの他にwafflewaffle_piがあります.好きなやつを指定してください.bashrcとかに書いちゃってもいいと思う.

まずRVizで動作を確認します.

$ roslaunch turtlebot3_fake turtlebot3_fake.launch

RVizが起動して,画面内にさっき指定したモデルのTurtlebot3がいればOK.(画像はwaffleです)

もしTurtlebot3のモデルが真っ白になってる場合は,他に起動しているroscoreを落としてもう一回RVizを立ち上げるとちゃんと表示されると思います.
/cmd_velでTwistを投げると動きます.rqtから手動で送ることもできるけど,キーボードで動かしたい場合はターミナルをもう一つ開いて,

$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

でキーボードコントローラを実行できます.WASDXキーでRViz上のTurtlebot3が動きます.


rqt_graphを表示した図.キーボードコントローラから/cmd_velトピックがTurtlebot3に流れていることがわかりますね.

Gazeboで動作確認

RVizでTurtlebot3が動くことを確認したらGazeboで動かします.
RVizを閉じて以下のコマンドを実行してください.

$ roslaunch turtlebot3_gazebo turtlebot3_world.launch

このとき,

terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid

とか出てGazeboが起動しない場合は

$ export LC_ALL=C; unset LANGUAGE

とかやると動くと思います.

Gazeboが起動するとカメ🐢のようなワールドにTurtlebot3が配置されています.

先ほどRViz内のTurtlebot3を動かしたときと同様に,キーボードコントローラを用いて動かすことができます.

gmappingの環境構築

gmappingを用いたSLAMをするための準備をします.まず起動しているGazeboとキーボードコントローラを終了してください.

gmapping関連のパッケージをcloneします.

$ cd ~/catkin_ws/src
$ git clone https://github.com/ros-perception/slam_gmapping.git
$ git clone https://github.com/ros-perception/openslam_gmapping.git
$ git clone https://github.com/ros-planning/navigation_msgs.git
$ git clone https://github.com/ros-planning/navigation.git
$ git clone https://github.com/ros/geometry2.git

再度ビルド.

$ cd ~/catkin_ws
$ catkin_make
$ source ~/catkin_ws/devel/setup.bash

gmappingのビルドにそれなりに時間がかかるのでお茶🍵でも飲んで待ちましょう.

gmappingの動作確認

ビルドが終了したら先程と同様にGazeboとteleopコントローラを立ち上げます.

$ roslaunch turtlebot3_gazebo turtlebot3_world.launch
$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

次にgmappingのノードを立ち上げます.Turtlebot3のパッケージにlaunchファイルが用意されているので,そちらを用います.

$ roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=gmapping

RVizが立ち上がり,Turtlebot3本体とセンサの取得データ,および生成されたmapデータが表示されていればOKです.

コントローラで動かして空間内を移動させていくと,地図が逐次更新されていくのがわかると思います.

rqt_graphで表示したノード間の関係です.

gazebo(Turtlebot3)がtfを更新しながら/scanをPublishし,gmappingノードがそれをSubscribeして/mapを出力しているのがわかります.

参考文献