われがわログ

最適化アルゴリズムとかプログラミングについて書きたい

WSL2でのROS (Melodic)の初期設定

ROSを触る機会があったのでまとめておく。 ROSはMelodic、OS はUbuntu18.04、XサーバはVcXsrvを想定。

XサーバにX410を使いたいとき →  Using X410 with WSL2 - X410.dev

WSL2インストール

docs.microsoft.com

aptのサーバを日本にしておくとDLが早い。

sudo sed -i 's/\/\/archive.ubuntu.com/\/\/jp.archive.ubuntu.com/g' /etc/apt/sources.list
sudo sed -i 's/\/\/us.archive.ubuntu.com/\/\/jp.archive.ubuntu.com/g' /etc/apt/sources.list
sudo sed -i 's/\/\/fr.archive.ubuntu.com/\/\/jp.archive.ubuntu.com/g' /etc/apt/sources.list

ROS2インストール

まず、公式ページに従ってROS本体をインストールする

wiki.ros.org

コマンドだけ羅列すると以下の通り。

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install ros-melodic-desktop-full
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
sudo rosdep init
rosdep update

catkin toolsをインストールしておく。 catkin buildでビルドできるようになる。

sudo apt install python-catkin-tools

ワークスペースを作成する。

mkdir -p ~/catkin_ws/src

ビルドできるか確認

cd ~/catkin_ws
catkin build 

WSL2向けGUI設定

# Xサーバー通信用のアプリ群を入れる
sudo apt install x11-apps -y 
# 環境変数を設定する
## rvizを正常起動させるため、間接レンダリングしない
echo 'export LIBGL_ALWAYS_INDIRECT=0' >> ~/.bashrc 
## /etc/resolv.confから取得した、ホストPCのIPアドレスをDISPLAY変数に代入
echo 'export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '\''{print $2}'\''):0' >> ~/.bashrc
source ~/.bashrc 

VcxSrvは、Native openglのチェックを外し、Disable access controlにチェックを入れる。 さらに、以下のサイトを参考にWindowsディフェンダーの設定を変更する。 具体的には、VcxSrvのプライベートとパブリックの欄にチェックを入れればよい。

odaryo.hatenablog.com

Windows上でVcxSrvを起動したのち、Ubuntu上でxeyes と打ってGUIが表示されれば、GUI環境は正しく設定できている。

もしGazebo起動時に

[Err] [REST.cc:205] Error in REST request

のようなエラーが出力されていた場合は、~/.ignition/fuel/config.yaml中のURLをhttps://api.ignitionrobotics.org に修正すればよい。

また、同じくGazebo起動時に

ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
AL lib: (EE) ALCplaybackAlsa_open: Could not open playback device 'default': No such file or directory

とエラーが出る場合には、以下の内容を/etc/asound.confに書き込み、alsaが使うデフォルトのオーディオデバイスをnullに設定すればよい。

pcm.!default {
    type plug
    slave.pcm "null"
}

この設定では音は鳴らないが、ROSを使う上で特に問題はないだろう。

Ref

demura.net

qiita.com

www.qoosky.io

github.com

新しいパッケージを作るときに参考にするサイト

demura.net