Zenn
🤖

【Dockerを使用】Unitree 4D Lidar L1/ROS2HumbleをUbuntu24.04上で動作させる

に公開

はじめに

機械・制御系研究室での研究で、LiDARをRaspberryPi5上で動作させるにあたって試行錯誤した結果を共有します。

概要

Ubuntu24.04にROS2Humbleをaptによってインストールできないため、Dockerを用いてUnitree 4D Lidar L1 のデータをRviz2で可視化する手順をご紹介します。

手順

以下のディレクトリ構成に従ってファイルを配置し、後述のコマンド群を上から順番に実行してください。

ディレクトリ構成

/
├─ docker-compose.yml
├─ Dockerfile
└─ .env

ファイル

Dockerfile
FROM ubuntu:22.04

# Timezone, Language設定
RUN apt update \
  && apt install -y --no-install-recommends \
     locales \
     software-properties-common tzdata \
  && locale-gen en_US en_US.UTF-8 \
  && update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 \
  && add-apt-repository universe

ENV LANG en_US.UTF-8
ENV TZ=Asia/Tokyo

# Install ROS2
RUN apt update \
  && apt install -y --no-install-recommends \
     curl gnupg2 lsb-release python3-pip vim wget build-essential ca-certificates

RUN 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 $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null

RUN apt update \
  && apt upgrade \
  && DEBIAN_FRONTEND=noninteractive \
  && apt install -y --no-install-recommends \
     ros-humble-desktop \
  && rm -rf /var/lib/apt/lists/*

RUN bash /opt/ros/humble/setup.sh 

# Install Nvidia Container Toolkit
RUN distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
  && curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | apt-key add - \
  && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

RUN apt-get update \
  && apt-get install -y --no-install-recommends nvidia-container-toolkit

# Add user and group
ARG UID
ARG GID
ARG USER_NAME
ARG GROUP_NAME

RUN groupadd -g ${GID} ${GROUP_NAME}
RUN useradd -u ${UID} -g ${GID} -s /bin/bash -m ${USER_NAME}

# ユーザーをdialoutグループに追加
RUN usermod -aG dialout ${USER_NAME}

# Install sudo
RUN apt install -y sudo \
  && echo "${USER_NAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USER_NAME}

USER ${USER_NAME}

WORKDIR /app

CMD ["/bin/bash"]
docker-compose.yml
version: "3"
services:
  ros2:
    container_name: ros2
    image: ros2
    user: "0"
    build:
      context: .
      args:
        - USER_NAME=${USER_NAME}
        - GROUP_NAME=${GROUP_NAME}
        - UID=${UID}
        - GID=${GID}
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"  # Mount the serial device
    privileged: true
    environment:
      - DISPLAY=${DISPLAY}
    volumes:
       - /tmp/.X11-unix:/tmp/.X11-unix
       - $HOME/.Xauthority/:/root/.Xauthority
       - ${WORKSPACE_DIR}:/app
    tty: true
    user: "${UID}:${GID}"
.env
USER_NAME={任意の値}
GROUP_NAME={任意の値}
UID={任意の値}
GID={任意の値}
WORKSPACE_DIR={任意の値}

コマンド

dockerコンテナを作成・起動し, シェルを接続する

docker-compose build
docker-compose up
docker exec -it ros2 bash

LiDARのビルド

  • apt のアップデート
sudo apt update
  • Gitをインストール
sudo apt install git
  • unilidar_sdkのリポジトリをクローン
git clone https://github.com/unitreerobotics/unilidar_sdk
  • corconをインストール
sudo apt install python3-colcon-common-extensions
  • ビルド
source /opt/ros/humble/setup.sh
cd unilidar_sdk/unitree_lidar_ros2

colcon build --symlink-install
colcon build

実行

  • USBへのアクセス権限を追加
sudo chmod 666 /dev/ttyUSB0
  • LiDARを起動
cd unilidar_sdk/unitree_lidar_ros2
source install/setup.bash
ros2 launch unitree_lidar_ros2 launch.py
  • rviz2で可視化
source /opt/ros/humble/setup.sh
cd unilidar_sdk/unitree_lidar_ros2
rviz2 -d src/unitree_lidar_ros2/rviz/view.rviz 

参考文献

本記事は以下の情報を参考にして執筆いたしました。

茨城高専 学生テックブログ

Discussion

ログインするとコメントできます