【ROS2】foxy环境配置

一、系统环境

ros2版本:Installation — ROS 2 Documentation: Foxy documentation

操作系统:ubuntu20.04  (确保系统是 Ubuntu 20.04(Focal Fossa))

lsb_release -a

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:        20.04
Codename:       focal

二、安装步骤

1、配置语言

locale
sudo 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
echo 'export LANG=en_US.UTF-8' >> ~/.bashrc
echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc
source ~/.bashrc  # 重新加载配置
cat /etc/default/locale
locale

2、在 Ubuntu 上配置 ROS 2 软件源

(1)启用 Ubuntu Universe 仓库

sudo apt install software-properties-common -y
sudo add-apt-repository universe

(2)添加 ROS 2 的 GPG 密钥

sudo apt update && sudo apt install curl -y
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

(3)添加 ROS 2 软件源

清华源:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu focal main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

官方源:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

3、安装 ROS 2 软件包

 (1)更新软件包列表

sudo apt update

输出中应显示 Hit:1 https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu focal InRelease

(2)升级系统(可选但推荐)

sudo apt upgrade -y

说明:
-y 参数表示自动确认升级操作。

(3)桌面版安装(推荐初学者)

sudo apt install ros-foxy-desktop python3-argcomplete -y

说明:
ros-foxy-desktop:包含 ROS 2 的桌面版功能。
python3-argcomplete:提供 Python 命令行自动补全功能。

(4)安装开发工具(可选)

如果你计划开发或编译 ROS 2 包,可以安装以下开发工具:

sudo apt install ros-dev-tools -y

说明:
ros-dev-tools:包含编译 ROS 2 包所需的工具(如 colcon、ament 等)。

4、配置环境变量

为了在终端中直接使用 ROS 2 命令,需要将 ROS 2 的环境变量添加到 ~/.bashrc:

echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
source ~/.bashrc

5、安装依赖管理工具(可选)

ROS 2 使用 rosdep 管理依赖关系。如果尚未安装,可以运行以下命令:

sudo apt install python3-rosdep -y
sudo rosdep init

说明:
rosdep init:初始化 rosdep 数据库。
rosdep update:更新 rosdep 的依赖信息。

(1)修改 rosdep 源文件

rosdep 的源文件通常位于 /etc/ros/rosdep/sources.list.d/20-default.list。需要将默认的 GitHub 源替换为清华镜像源。

备份原始文件(可选但推荐)

sudo cp /etc/ros/rosdep/sources.list.d/20-default.list /etc/ros/rosdep/sources.list.d/20-default.list.bak

(2)编辑 20-default.list 文件

sudo nano /etc/ros/rosdep/sources.list.d/20-default.list

(3)替换为清华镜像源

将文件中的 GitHub 源替换为清华镜像源。修改后的内容如下:

# 原始内容(GitHub 源):
# os-specific listings first
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml osx

# generic
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
yaml https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
gbpdistro https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml fuerte

# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
# 其他类似行...

# 替换为清华镜像源:
# os-specific listings first
yaml https://mirrors.tuna.tsinghua.edu.cn/rosdistro/rosdep/osx-homebrew.yaml osx

# generic
yaml https://mirrors.tuna.tsinghua.edu.cn/rosdistro/rosdep/base.yaml
yaml https://mirrors.tuna.tsinghua.edu.cn/rosdistro/rosdep/python.yaml
yaml https://mirrors.tuna.tsinghua.edu.cn/rosdistro/rosdep/ruby.yaml
gbpdistro https://mirrors.tuna.tsinghua.edu.cn/rosdistro/releases/fuerte.yaml fuerte

# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
# 其他类似行...

说明:
将 https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/... 替换为 https://mirrors.tuna.tsinghua.edu.cn/rosdistro/rosdep/...。
类似地,将 https://raw.githubusercontent.com/ros/rosdistro/master/releases/... 替换为 https://mirrors.tuna.tsinghua.edu.cn/rosdistro/releases/...。

(4)更新 rosdep 数据库

修改源文件后,运行 rosdep update 以更新本地数据库:

rosdep update

(5)验证是否生效

运行以下命令验证 rosdep 是否使用清华镜像源:

rosdep update --verbose

6、常见问题排查
(1)依赖问题:
如果安装时提示依赖问题,运行 sudo apt --fix-broken install。
(2)环境变量未生效:
确保 source /opt/ros/foxy/setup.bash 已添加到 ~/.bashrc 并执行 source ~/.bashrc。
(3)rosdep 初始化失败:
如果 rosdep init 失败,检查网络连接或尝试手动初始化。
(4)清华镜像源同步延迟:
如果某些包无法找到,可能是清华镜像源尚未同步最新版本,可以暂时切换回官方源。

三、使用配置

1、环境设置

检查环境变量:

printenv | grep -i ROS

应当输出:

ROS_VERSION=2
ROS_PYTHON_VERSION=3
ROS_DISTRO=foxy

2、配置工作空间

(1)创建工作空间

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws

(2)构建工作空间

colcon build --symlink-install

(3)source环境

source install/setup.bash
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc

(4)设置便捷切换包目录

echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
echo "export _colcon_cd_root=/opt/ros/foxy/" >> ~/.bashrc

(5)设置colcon命令自动补全

echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
source ~/.bashrc

四、使用

1、创建包

cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python --node-name webrtc_client upstream

2、创建launch

mkdir -p ~/ros2_ws/src/your_package/launch
touch ~/ros2_ws/src/your_package/launch/your_launch_file.launch.py

对于python包,修改setup.py文件:

from setuptools import setup
import os
from glob import glob

package_name = 'upstream'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        # 安装 package.xml
        ('share/ament_index/resource_index/packages', ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        
        # 安装 launch 文件
        (os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
        
        # 安装其他资源文件(如配置文件)
        (os.path.join('share', package_name, 'config'), glob('config/*.yaml')),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='kickpi',
    maintainer_email='[email protected]',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'webrtc_client = upstream.webrtc_client:main'
        ],
    },
)

3、编译

cd ~/your_ws
colcon build --packages-select your_package
source install/setup.bash

验证launch

ls ~/ros2_ws/install/your_package/share/your_package/launch

4、运行

ros2 launch your_package demo.launch.py

你可能感兴趣的:(机器人)