在 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
    
原文地址:https://www.cnblogs.com/xGonZh10n/p/12895061.html