🍓

ラズパイにROS2インストールする

2025/02/01に公開

最近ROS2の勉強を細々とやってます。ESP32とかPicoなどのマイコン上でセンサーやモーターをROS2経由で制御することを直近の目標としてます。まず今回はUbuntu22.04へROS2をインストールする手順をメモっておきました。

前提条件

  1. Ubuntu22.04
  2. ROS2 Humble

今回はRaspberry Pi4, Zero 2の2台にUbuntuをインストールした状態からスタートしましたが、基本PCでも同じです。

ROS2 Humbleインストール

パッケージインストール

sudo apt update && sudo apt upgrade -y
sudo apt install -y locales curl gnupg lsb-release build-essential
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

ロケール設定を確認して、UTF-8 に設定する。

vi ~/.bashrc
export LANG=en_US.UTF-8
source ~/.bashrc

ROSの公式リポジトリを追加し、GPGキーを取得します。

sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key | sudo apt-key add -
echo "deb http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list

ROS 2 インストール

パッケージはros-baseとros-desktopがあるが、いろいろなツール類をあとで追加インストールしないといけないかもしれないので、とりあえずDesktop版をインストールします。
ros-baseだけのインストールだと、ros2コマンドとか、サンプルがインストールされなかったので。

# ROS 2 Desktop版 (GUIあり)
sudo apt install -y ros-humble-desktop -y

ROS 2の環境変数設定シェルを起動時にロードされるようにします。

vi ~/.bashrc
source /opt/ros/humble/setup.bash

source ~/.bashrc

rosdep インストール

rosdepは依存モジュールなどを管理インストールしてくれるツールなのでいれておく。

> sudo apt install python3-rosdep python3-pip

humbleインストール後、1回だけsudo権限でinitを実行する。humbleインストール時に依存するモジュールはインストールされるので、実際には意味ないかもしれないが念のため。

以下はsudo権限で実行する。

> sudo rosdep init

その後、updateをsudo権限なしで実行する。(sudoで実行しない)

> rosdep update

reading in sources list data from /etc/ros/rosdep/sources.list.d
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
Query rosdistro index https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
Skip end-of-life distro "ardent"
Skip end-of-life distro "bouncy"
Skip end-of-life distro "crystal"
Skip end-of-life distro "dashing"
Skip end-of-life distro "eloquent"
Skip end-of-life distro "foxy"
Skip end-of-life distro "galactic"
Skip end-of-life distro "groovy"
Add distro "humble"
Skip end-of-life distro "hydro"
Skip end-of-life distro "indigo"
Skip end-of-life distro "iron"
Skip end-of-life distro "jade"
Add distro "jazzy"
Skip end-of-life distro "kinetic"
Skip end-of-life distro "lunar"
Skip end-of-life distro "melodic"
Add distro "noetic"
Add distro "rolling"
updated cache in /home/ubuntu/.ros/rosdep/sources.cache

colcon インストール

ROS2ワークスペースのビルドツールになる。今後必要なのでインストールする。

> sudo apt install python3-colcon-common-extensions -y

セットアップ後の確認

とりあえずサンプルが動作するのか、以下を実行する(Publish)

ros2 run demo_nodes_cpp talker

[INFO] [1738284137.570831359] [talker]: Publishing: 'Hello World: 1'
[INFO] [1738284138.570663107] [talker]: Publishing: 'Hello World: 2'
[INFO] [1738284139.570628801] [talker]: Publishing: 'Hello World: 3'

別ターミナルで確認(Subscribe)

ros2 run demo_nodes_py listener

[INFO] [1738284154.748136474] [listener]: I heard: [Hello World: 18]
[INFO] [1738284155.579773543] [listener]: I heard: [Hello World: 19]

Discussion