0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WSL2にROS2を入れてgazeboでシミュレーションするまでの手順

Last updated at Posted at 2025-05-12

はじめに

PCに初めてWSLをインストールする場合を想定しています.
動作確認は下記環境で行っています.

・OS: Windows11 Pro (64bit)
・ROS: ROS2 humble (WSLのUbuntu22.04に)

1. WSLのインストール

Windows PowerSellを管理者権限で開き,下記コマンドを実行.
※Windows検索窓でPowerShellを検索するか、スタートメニューから、Windows PowerShellアイコンを探し出して右クリックから「管理者として実行」

wsl --install

インストール後,PCを再起動する(必要).

Ubuntuのダウンロードが始まったりして時間がかかる場合があるので待つ.

再度PowerSellを立ち上げ,バージョン確認

wsl --version

作業日:2025年5月12日 時点で下記のようになっていました.

2. Ubuntuのインストール

インストールできるLinuxディストリビューションの確認

wsl --list --online

その中にUbuntu-22.04があることを確認.
Ubuntu-22.04のインストール

wsl --install -d Ubuntu-22.04

インストールの最後にユーザー名とパスワードを聞かれるので設定.
パスワードは今後も頻繁に打ち込むことになるので,間違えずに素早く入力しやすいものを推奨します.

3. 規定のディストリビューションの変更

何か起動中の場合は「Ctrl + D」で切断して戻り,下記コマンドで規定のディストリビューションを確認する(* がついているもの)

wsl -l -v

Ubuntu-22.04に変更する場合

wsl --set-default Ubuntu-22.04

4. 起動とシャットダウン

起動

規定のディストリビューションになっていれば下記で起動できる

wsl

特定のディストリビューションを起動したい場合は、以下のコマンドを使用

wsl -d <ディストリビューション名>

※例えば、Ubuntu22.04を起動する場合

wsl -d Ubuntu-22.04

シャットダウン

Ubuntuのシャットダウン

sudo shutdown -h now

あるいはwsl上で

wsl --shutdown

とすると立ち上がっているものを全てOFFにできる.

5. ROS2のインストール

ROS2 humbleのインストール

UbuntuにROSをインストールします.Ubuntuを起動し,ターミナルで下記コマンドを実行していきます.
※Linux環境とWindows環境両方のショートカット操作が有効になるため注意

コピー
①マウス選択+エンター
②マウス選択+右クリック
③マウス選択+Ctrl+Shift+C
貼り付け
①右クリック
②Ctrl+Shift+V

sudo apt update && sudo apt install curl gnupg lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update
sudo apt install ros-humble-desktop

インストールが完了したら以下のコマンドを実行

echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc

その他モジュールのインストール

以下のコマンドを入力してその他のモジュールをインストール
※以降操作をまとめて記述していますが,一行ずつ行うことをお勧めします.
(一気にやるとエラーが出た場合にどこでエラーになったか分からなくなりやすい)
なお,何かワーニングやエラーが出た場合,もう一度同じコマンドを入れると成功する場合もあります.

sudo apt install ros-humble-gazebo-*
sudo apt install ros-humble-cartographer
sudo apt install ros-humble-cartographer-ros
sudo apt install ros-humble-navigation2
sudo apt install ros-humble-nav2-bringup

turtlebot3関係のインストール
以下のコマンドを実行

mkdir -p ~/turtlebot3_ws/src
cd ~/turtlebot3_ws/src/
git clone -b humble https://github.com/ROBOTIS-GIT/DynamixelSDK.git
git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git
git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3.git
sudo apt install python3-colcon-common-extensions
cd ~/turtlebot3_ws
colcon build --symlink-install
echo 'source ~/turtlebot3_ws/install/setup.bash' >> ~/.bashrc
source ~/.bashrc

※mkdir:フォルダの作成(ホームの下など分かりやすいところで実行することを推奨)

設定をbashrcに記述

echo 'export ROS_DOMAIN_ID=30 #TURTLEBOT3' >> ~/.bashrc
echo 'source /usr/share/gazebo/setup.sh' >> ~/.bashrc
source ~/.bashrc

turtlebotシミュレーション環境構築

以下のコマンドを実行する

cd ~/turtlebot3_ws/src/
git clone -b humble https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
cd ~/turtlebot3_ws && colcon build --symlink-install
source ~/.bashrc

以下を実行して,シミュレーション環境を起動する

export TURTLEBOT3_MODEL=burger
ros2 launch turtlebot3_gazebo empty_world.launch.py 

image.png
↑のような何もない空間にturtlebotが出現したら成功

以下のコマンドを実行して動かしてみよう
別のターミナル(別windowのWSL)で

export TURTLEBOT3_MODEL=burger
ros2 run turtlebot3_teleop teleop_keyboard

(追記)
ロボットのモデルを「waffle」にする場合はexportのところを

export TURTLEBOT3_MODEL=waffle

とします.

ターミナルを複数立ち上げるには

下図「+」を押してPowerShellのターミナルを増やして,それぞれで同じディストリビューションを立ち上げればよいようです.

以上で環境構築は完了です.

参考

6. SLAMのシミュレーション

最初にgazeboシミュレーターの中にturtlebot3が立ち上がってる状態にします.ワールドは「turtlebot3_world」にします.

export TURTLEBOT3_MODEL=burger
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py

次に別のターミナルで「cartographer」というLidarを使ったSLAMのプログラムを立ち上げます.

ros2 launch turtlebot3_cartographer cartographer.launch.py use_sim_time:=True

立ち上がったらキーボード操作(teleop_keybord)などを使って動かしてみましょう.

export TURTLEBOT3_MODEL=burger
ros2 run turtlebot3_teleop teleop_keyboard

gazeboの画面をうまく調整してロボットの動きを確認しながら,RVIZの画面で周辺地図の作成ができたら成功です。

地図の保存

RVIZで表示されている地図は下記で保存できます。

別のターミナルにて
ros2 run nav2_map_server map_saver_cli -f ~/map

※mapの部分は任意の名前でOK,「~/」を入れるとホームディレクトリ直下に保存される

7. ナビゲーションのシミュレーション

※SLAMで地図が保存された後を想定しています

ナビゲーションノードを立ち上げる

$ ros2 launch turtlebot3_navigation2 navigation2.launch.py use_sim_time:=True map:=$HOME/map.yaml

※map.yaml の部分はmapを保存した時の「自分でつけた名前」.yaml とする.

操作方法
1.「2D Pose Estimate」ボタンをクリック
2. 現在ロボットのいる位置をクリックして緑色の矢印をロボットが向いている方向に合わせる
3.「Navigation2 Goal」ボタンをクリック
4. ロボットを移動させたい位置をクリックして緑色の矢印を最終的にロボットに向かせたい方向に合わせる

ロボットが目的地に動いたら成功

備考

このナビゲーション,動かないことが結構あるようです.下記に気を付けるとうまく動く可能性が高まる気がします.

・現在のロボットのいる位置と方向はなるべく正確に合わせる(操作方法の2)
・操作方法の2と3の間に Teleop などでゆっくりロボットを動かしてみる
・操作方法の4の前に Teleop などロボットに動作指令を送るノードは全て落としておく
・目的地はあまり遠くに設定しない

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?