2021/08/11

ROS2とPX4の連携環境準備

px4dronecoderosros2

ROS2とPX4(gazebo simulation上)の連携を試してみました。

以下のような図の概要部分の理解を目的に実施しました。

architecture-min.png

OS version

Ubuntu20.04で試しました。(詳細は以下)

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Fast DDS Setup

eProsima Fast DDS requires the following packages to work.

Ubuntu 20.04

  • Fast DDS 2.0.0 (or later)
  • Fast-RTPS-Gen 1.0.4 (not later!)

Install Fast DDS

Prerequisite

Sdkman

# install sdkman
$ curl -s "https://get.sdkman.io" | bash
$ source "${HOME}/.sdkman/bin/sdkman-init.sh"

Java

$ sdk list java
$ sdk install java 8.0.302-open
$ sdk default java 8.0.302-open
$ java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)
$ javac -version
javac 1.8.0_302

Gradle

# install gradle with sdkman
$ sdk install gradle 7.1.1
$ gradle --version
Welcome to Gradle 7.1.1!

Others

$ sudo apt install libasio-dev libtinyxml2-dev
$ sudo snap install cmake --classic

Foonathan memory

$ git clone https://github.com/eProsima/foonathan_memory_vendor
$ cd foonathan_memory_vendor
$ git checkout v1.1.0
$ mkdir build && cd build
$ cmake ..
$ sudo cmake --build . --target install

Fast CDR

$ git clone https://github.com/eProsima/Fast-CDR.git
$ cd Fast-CDR
$ git checkout v1.0.21
$ mkdir build && cd build
$ cmake ..
$ sudo cmake --build . --target install

Install

$ git clone https://github.com/eProsima/Fast-DDS.git
$ cd Fast-DDS
$ git checkout v2.3.4
$ mkdir build && cd build
$ cmake -DTHIRDPARTY=ON -DSECURITY=ON ..
$ make -j$(nproc --all)
$ sudo make install

Install Fast-DDS-Gen

$ git clone https://github.com/eProsima/Fast-DDS-Gen
$ cd Fast-DDS-Gen
$ git checkout v1.0.4
# https://github.com/PX4/px4_ros_com/issues/67 v2.0.0以上を使うと、rtps apiじゃなく、dds apiに変更になってしまい、px4_ros_comで対応できないようなので、v1.0.4をインストールする
$ ./gradlew assemble
$ sudo ./gradlew install
$ which fastrtpsgen
/usr/local/bin/fastrtpsgen

References

Install ROS2

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

Other tools

# colcon
$ sudo apt install python3-colcon-common-extensions
$ which colcon
/usr/bin/colcon

# eigen3_cmake_module
$ sudo apt install ros-foxy-eigen3-cmake-module

# empy/pyros-genmsg/setuptools
$ sudo apt install python3-testresources
$ sudo pip3 install -U empy pyros-genmsg setuptools

References

Build ROS2 Workspace

$ mkdir -p ~/px4_ros_com_ros2/src
$ git clone https://github.com/PX4/px4_ros_com.git ~/px4_ros_com_ros2/src/px4_ros_com
$ git clone https://github.com/PX4/px4_msgs.git ~/px4_ros_com_ros2/src/px4_msgs

$ source /opt/ros/galactic/setup.bash
$ colcon build --symlink-install --event-handlers console_direct+
...(中略)
---
Finished <<< px4_ros_com [1min 27s]

Summary: 2 packages finished [1min 31s]
  1 package had stderr output: px4_ros_com

Setup PX4 Ubuntu development environment

# シリアル通信で干渉を起こさないために消去
$ apt show modemmanager
$ sudo apt-get remove modemmanager -y
$ getent group dialout
$ sudo usermod -a -G dialout $USER

# install utilities
$ sudo apt-get install \
  astyle \
  ccache \
  lcov \
  shellcheck

# install PX4 Python3 dependencies
$ git clone https://github.com/PX4/PX4-Autopilot.git
$ cd PX4-Autopilot
$ git checkout v1.12.1  # use version v1.12.1
$ python3 -m pip install --user -r ./Tools/setup/requirements.txt

# installing NuttX dependencies
$ sudo apt-get install \
  binutils-dev \
  gdb-multiarch \
  genromfs \
  gperf \
  kconfig-frontends \
  libelf-dev \
  libexpat-dev \
  libgmp-dev \
  libisl-dev \
  libmpc-dev \
  libmpfr-dev \
  libncurses5-dev \
  libncursesw5-dev \
  libtool \
  pkg-config \
  screen \
  texinfo \
  u-boot-tools \
  util-linux \
  vim-common

# install arm-none-eabi-gcc
NUTTX_GCC_VERSION="9-2020-q2-update"
NUTTX_GCC_VERSION_SHORT="9-2020q2"
$ wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/${NUTTX_GCC_VERSION_SHORT}/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-x86_64-linux.tar.bz2 -O /tmp/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-linux.tar.bz2 && \
  sudo tar -jxf /tmp/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-linux.tar.bz2 -C /opt/
$ echo "export PATH=/opt/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}/bin:\$PATH" >> $HOME/.bashrc


# install gazebo and some utilities
$ sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
$ wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
$ sudo apt-get update -y
$ sudo apt-get install \
  bc \
  ant \
  libvecmath-java \
  dmidecode \
  gazebo11 \
  gstreamer1.0-plugins-bad \
  gstreamer1.0-plugins-base \
  gstreamer1.0-plugins-good \
  gstreamer1.0-plugins-ugly \
  gstreamer1.0-libav \
  libeigen3-dev \
  libgazebo11-dev \
  libgstreamer-plugins-base1.0-dev \
  libimage-exiftool-perl \
  libopencv-dev \
  libxml2-utils \
  pkg-config \
  protobuf-compiler

# install and set java 11 as default
$ sdk install java 11.0.12-open

sanity check the installation

$ cd ~/src/PX4-Autopilot
$ make px4_sitl_rtps gazebo

ターミナルで別タブを開いて以下を実行

$ cd ~/px4_ros_com_ros2
$ source ./install/setup.bash
$ micrortps_agent -t UDP

さらにもう一つターミナルのタブを開いて、以下を実行

$ cd ~/px4_ros_com_ros2
$ source ./install/setup.bash
$ ros2 launch px4_ros_com sensor_combined_listener.launch.py

ものすごい量のデータを受信

[sensor_combined_listener-1] RECEIVED SENSOR COMBINED DATA
[sensor_combined_listener-1] =============================
[sensor_combined_listener-1] ts: 833045604
[sensor_combined_listener-1] gyro_rad[0]: 0.00186421
[sensor_combined_listener-1] gyro_rad[1]: -0.000798947
[sensor_combined_listener-1] gyro_rad[2]: 0.000798948
[sensor_combined_listener-1] gyro_integral_dt: 4000
[sensor_combined_listener-1] accelerometer_timestamp_relative: 0
[sensor_combined_listener-1] accelerometer_m_s2[0]: 0.0885855
[sensor_combined_listener-1] accelerometer_m_s2[1]: -0.15682
[sensor_combined_listener-1] accelerometer_m_s2[2]: -9.81982
[sensor_combined_listener-1] accelerometer_integral_dt: 4000
[sensor_combined_listener-1]

ここで定義されているROS2ノードも実行できるようになっており、以下のように実行&出力できました。

$ros2 run px4_ros_com sensor_combined_listener

RECEIVED SENSOR COMBINED DATA
=============================
ts: 2960104232
gyro_rad[0]: 0.000293919
gyro_rad[1]: 0.00187428
gyro_rad[2]: -0.00101651
gyro_integral_dt: 4000
accelerometer_timestamp_relative: 0
accelerometer_m_s2[0]: -0.0588733
accelerometer_m_s2[1]: -0.0428247
accelerometer_m_s2[2]: -9.78118
accelerometer_integral_dt: 4000

少しハマり箇所もありましたが、(versionによってはビルドが通らなかったり、必要な実行ファイルが作られなかったり)なんとかサンプル実装が試せる状態にできたと思います。

今後は実機なども購入しつつ、試せて行けたらなと思っています。

以上です。