一、前提介紹:
本文主要采用兩種方式在NVIDIA 下基于 Ubuntun20.04安裝 ros2-foxy。
使用環境:
NVIDIA 為 Jetson 系列下 Jetson Xavier NX;
Ubuntun版本:20.04
二、安裝方法:
1、使用腳本編譯方式:
使用執行下列腳本進行安裝:
#!/bin/bash
#
# Copyright (c) 2021 Jetsonhacks
# MIT License# Roughly follows the 'Install ROS From Source' procedures from:
# https://index.ros.org/doc/ros2/Installation/Foxy/Linux-Development-Setup/
# mostly from:
# Dockerfile.ros.foxy
# https://github.com/dusty-nv/jetson-containers
# ROS_PKG=ros_base
ROS_DISTRO=foxy
# Core ROS2 workspace - the "underlay"
ROS_BUILD_ROOT=/opt/ros/${ROS_DISTRO}-src
ROS_INSTALL_ROOT=/opt/ros/${ROS_DISTRO}locale # check for UTF-8sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8# Add the ROS 2 apt repository
sudo apt-get update
sudo apt-get install -y --no-install-recommends \curl \wget \ gnupg2 \lsb-release
sudo rm -rf /var/lib/apt/lists/*wget --no-check-certificate https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc
sudo apt-key add ros.asc
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'# install development packages
sudo apt-get update
sudo apt-get install -y --no-install-recommends \build-essential \cmake \git \libbullet-dev \libpython3-dev \python3-colcon-common-extensions \python3-flake8 \python3-pip \python3-pytest-cov \python3-rosdep \python3-setuptools \python3-vcstool \python3-rosinstall-generator \libasio-dev \libtinyxml2-dev \libcunit1-dev
sudo rm -rf /var/lib/apt/lists/*# install some pip packages needed for testing
python3 -m pip install -U \argcomplete \flake8-blind-except \flake8-builtins \flake8-class-newline \flake8-comprehensions \flake8-deprecated \flake8-docstrings \flake8-import-order \flake8-quotes \pytest-repeat \pytest-rerunfailures \pytest# compile yaml-cpp-0.6, which some ROS packages may use (but is not in the 18.04 apt repo)
git clone --branch yaml-cpp-0.6.0 https://github.com/jbeder/yaml-cpp yaml-cpp-0.6 && \cd yaml-cpp-0.6 && \mkdir build && \cd build && \cmake -DBUILD_SHARED_LIBS=ON .. && \make -j$(nproc) && \sudo cp libyaml-cpp.so.0.6.0 /usr/lib/aarch64-linux-gnu/ && \sudo ln -s /usr/lib/aarch64-linux-gnu/libyaml-cpp.so.0.6.0 /usr/lib/aarch64-linux-gnu/libyaml-cpp.so.0.6# https://answers.ros.org/question/325245/minimal-ros2-installation/?answer=325249#post-id-325249
sudo mkdir -p ${ROS_BUILD_ROOT}/src && \cd ${ROS_BUILD_ROOT}
sudo sh -c "rosinstall_generator --deps --rosdistro ${ROS_DISTRO} ${ROS_PKG} launch_xml launch_yaml example_interfaces > ros2.${ROS_DISTRO}.${ROS_PKG}.rosinstall && \
cat ros2.${ROS_DISTRO}.${ROS_PKG}.rosinstall && \vcs import src < ros2.${ROS_DISTRO}.${ROS_PKG}.rosinstall"# download unreleased packages
sudo sh -c "git clone --branch ros2 https://github.com/Kukanani/vision_msgs ${ROS_BUILD_ROOT}/src/vision_msgs && \git clone --branch ${ROS_DISTRO} https://github.com/ros2/demos demos && \cp -r demos/demo_nodes_cpp ${ROS_BUILD_ROOT}/src && \cp -r demos/demo_nodes_py ${ROS_BUILD_ROOT}/src && \rm -r -f demos"# install dependencies using rosdep
sudo apt-get updatecd ${ROS_BUILD_ROOT}
sudo rosdep init rosdep update && \rosdep install --from-paths src --ignore-src --rosdistro ${ROS_DISTRO} -y --skip-keys "console_bridge fastcdr fastrtps rti-connext-dds-5.3.1 urdfdom_headers qt_gui" && \sudo rm -rf /var/lib/apt/lists/*# build it!
sudo mkdir -p ${ROS_INSTALL_ROOT}
# sudo required to write build logs
sudo colcon build --merge-install --install-base ${ROS_INSTALL_ROOT}
# We do this twice to make sure everything gets built
# For some reason, this has been an issue
sudo colcon build --merge-install --install-base ${ROS_INSTALL_ROOT}# Using " expands environment variable immediately
echo "source $ROS_INSTALL_ROOT/setup.bash" >> ~/.bashrc
echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
echo "export _colcon_cd_root=~/ros2_install" >> ~/.bashrc
腳本安裝完畢后,默認會在在/opt/ros/目錄下,生成foxy和foxy-src文件夾,foxy為編譯后的程序;
使用source 將對應 foxy文件夾里面加載到環境變量中:
source /opt/ros/foxy/setup.bash
然后 運行 ros2 --help 如果不報錯就代表安裝成功。
2、使用docker安裝ros2-foxy
2.1 安裝docker命令
依次執行下列命令:
sudo apt-get update # 更新軟件列表
sudo apt-get install -y docker.io #安裝docker
systemctl start docker #配置開機啟動
systemctl enable docker
sudo docker --version #檢測是否安裝成功
2.2 下載啟動 ros鏡像
執行下列命令:
docker pull aigrobv/ros-foxy:ci@sha256:035df49e8f307946a10b73d307ac2c35ce0c08ea2da5a824dbdaa4952fcf06ed
#sudo docker pull osrf/ros:foxy-desktop //下載ROS鏡像,如果需要arm架構,可以修改對應鏡像
# https://hub.docker.com/layers/aigrobv/ros-foxy/ci/images/sha256-146d88b0137f260f442d1c0b77b749237b392358d44476074829ff880f705e6e
sudo docker images #查看鏡像信息
sudo docker run -it osrf/ros:foxy-desktop #啟動鏡像
ros2 -h #查看ROS2的幫助信息,如不報錯則代表安裝成功
三、總結:
本文在NVIDIA 使用了 Ubuntun20.04環境下 腳本編譯安裝 ros2-foxy 和 使用docker安裝 ros-foxy的兩種方式,相對來說,docker方式簡單一點,推薦docker方式安裝。