在 Ubuntu 20.04 上構建 ROS 2 Foxy Fitzroy


系統設置

設置語言環境

確保系統設置支持 UTF-8 語言環境。如果您使用的是類似 Docker 容器的最小環境,則系統的語言環境可能被設置為 POSIX。

以下是設置語言環境的示例。但是,如果您使用的是其它支持 UTF-8 的語言環境,那應該也沒問題。

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

添加 ROS 2 apt 倉庫

您需要在系統中添加 ROS 2 apt 倉庫。為此,請首先使用 apt 授權 GPG 密鑰,具體如下:

sudo apt update && sudo apt install curl gnupg2 lsb-release
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

然后將該倉庫添加到您的源列表中:

sudo sh -c 'echo "deb http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'

安裝開發工具和 ROS 工具

sudo apt update && sudo apt install -y \
  build-essential \
  cmake \
  git \
  libbullet-dev \
  python3-colcon-common-extensions \
  python3-flake8 \
  python3-pip \
  python3-pytest-cov \
  python3-rosdep \
  python3-setuptools \
  python3-vcstool \
  wget
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
sudo apt install --no-install-recommends -y \
  libasio-dev \
  libtinyxml2-dev
sudo apt install --no-install-recommends -y \
  libcunit1-dev

獲取 ROS 2 代碼

創建一個工作空間並克隆所有存儲庫:

mkdir -p ~/ros2_foxy/src
cd ~/ros2_foxy
wget https://raw.githubusercontent.com/ros2/ros2/master/ros2.repos
vcs import src < ros2.repos

使用 rosdep 安裝依賴

sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro foxy -y --skip-keys "console_bridge fastcdr fastrtps rti-connext-dds-5.3.1 urdfdom_headers"

在工作空間中構建代碼

本教程中可以找到有關使用 ROS 工作空間的更多信息。

cd ~/ros2_foxy/
colcon build --symlink-install

注意:如果您在編譯示例時遇到麻煩,並且這妨礙了您成功完成構建,則可以使用AMENT_IGNORE 來忽略子樹或從工作空間中刪除該文件夾。例如:您希望避免安裝大型 OpenCV 庫。那么,只需在 cam2image demo 目錄中創建一個名為 AMENT_IGNORE 的空文件,即可將其排除在構建過程之外。

另外,如果您已經安裝了 ROS 2,請確保在全新環境中運行 build 命令。你可能需要在 .bashrc 文件中查看是否存在 source /opt/ros/${ROS_DISTRO}/setup.bash

colcon build --symlink-install --merge-install

然后從 install 文件夾中 source local_setup.*

環境設置

Source 安裝腳本

通過 source 以下文件來設置環境。

. ~/ros2_foxy/install/setup.bash

安裝 argcomplete(可選)

ROS 2 命令行工具使用 argcomplete 來實現自動補全。因此,如果要自動補全,則必須安裝 argcomplete。

sudo apt install python3-argcomplete

示例

在一個終端中,source 安裝腳本,然后運行 C++ talker

. ~/ros2_foxy/install/local_setup.bash
ros2 run demo_nodes_cpp talker

在另一個終端中,source 安裝腳本,然后運行 Python listener

. ~/ros2_foxy/install/local_setup.bash
ros2 run demo_nodes_py listener

卸載

  1. 如果您按照上面的指示使用 colcon 安裝了工作空間,則 "卸載" 可能只是打開一個新的終端而無需 source 工作空間的 setup 文件。這樣,您的環境將表現為系統上沒有安裝 Foxy。

  2. 如果您還試圖釋放空間,則可以使用以下方法刪除整個工作空間目錄:

    rm -rf ~/ros2_foxy
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM