宇树机器人-Ros2环境配置

前言

SDK地址

https://support.unitree.com/home/zh/G1_developer/sdk_overview

上位机连接

https://support.unitree.com/home/zh/G1_developer/quick_development

工单

https://serviceconsole.unitree.com/#/new-work-order?progress=1

DDS 是由对象管理组织(OMG)定义的一种用于实时系统的数据分发与集成的中间件标准。它基于发布 - 订阅模式,为分布式系统中的数据共享提供了一种高效、灵活的机制。DDS 可以自动处理数据的传输、序列化、分发和发现等任务,使得不同的应用程序或设备能够方便地进行数据交换。

ROS 2(Robot Operating System 2)是用于机器人的开源元操作系统,它提供了各类工具、库和约定,旨在简化跨机器人平台创建复杂和健壮的机器人行为的任务。

ROS 2 在设计时采用了模块化架构,其中通信层依赖于 DDS 作为底层的数据分发机制。DDS 是一种基于发布 - 订阅模式的实时数据通信标准,ROS 2 利用 DDS 的这一特性,实现了节点之间高效、灵活的数据交换。在 ROS 2 系统中,各个节点可以像在 DDS 中一样,作为发布者将数据发布到特定的主题(Topic)上,也可以作为订阅者订阅感兴趣的主题来接收数据。

测试过的系统和ROS2版本

系统 ROS2 版本
Ubuntu 20.04 foxy
Ubuntu 22.04 humble

我这里使用的Ubuntu 20.04,所以使用ROS2的版本是foxy

更换源

1
2
# 备份源列表
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

ubuntu 20.04 LTS (focal) 配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

ROS2安装

Set locale

Make sure you have a locale which supports UTF-8. If you are in a minimal environment (such as a docker container), the locale may be something minimal like POSIX. We test with the following settings. However, it should be fine if you’re using a different UTF-8 supported locale.

1
2
3
4
5
6
7
8
locale  # check for UTF-8

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
export LANG=en_US.UTF-8

locale # verify settings

Setup Sources

You will need to add the ROS 2 apt repository to your system.

First ensure that the Ubuntu Universe repository is enabled.

1
2
sudo apt install software-properties-common
sudo add-apt-repository universe

Now add the ROS 2 GPG key with apt.

1
2
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

默认源

1
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

国内源

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

Install ROS 2 packages

Update your apt repository caches after setting up the repositories.

1
sudo apt update

ROS 2 packages are built on frequently updated Ubuntu systems. It is always recommended that you ensure your system is up to date before installing new packages.

1
sudo apt upgrade -y

Desktop Install (Recommended): ROS, RViz, demos, tutorials.

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

ROS-Base Install (Bare Bones): Communication libraries, message packages, command line tools. No GUI tools.

1
sudo apt install -y ros-foxy-ros-base python3-argcomplete

Development tools: Compilers and other tools to build ROS packages

1
sudo apt install -y ros-dev-tools

如果安装失败可能是镜像源的问题

image-20250722110316534

Environment setup

Set up your environment by sourcing the following file.

1
2
3
# Replace ".bash" with your shell if you're not using bash
# Possible values are: setup.bash, setup.sh, setup.zsh
source /opt/ros/foxy/setup.bash

查看话题

1
ros2 topic list

安装unitree_sdk2

安装依赖

1
sudo apt-get install libeigen3-dev

编译SDK

1
2
3
4
5
6
7
8
git clone https://github.com/unitreerobotics/unitree_sdk2.git

cd unitree_sdk2

mkdir build
cd build
cmake ..
sudo make install

默认安装位置在

1
/usr/local/include/

安装unitree_ros2

克隆仓库

ctrl+alt+T 打开终端,克隆仓库:https://github.com/unitreerobotics/unitree_ros2

1
git clone https://github.com/unitreerobotics/unitree_ros2

其中

  • cyclonedds_ws 文件夹为编译和安装 Unitree 机器人 ROS2 msg 的工作空间,在子文件夹 cyclonedds_ws/unitree/unitree_gocyclonedds_ws/unitree/unitree_api中定义了 Unitree 状态获取和控制相关的 ROS2 msg。

安装 Unitree ROS2 功能包

安装依赖

1
2
sudo apt install -y ros-foxy-rmw-cyclonedds-cpp
sudo apt install -y ros-foxy-rosidl-generator-dds-idl

注意

为了方便接口的使用,推荐同时安装 unitree_sdk2

编译 cyclone-dds

由于 G1 使用的是 cyclonedds 0.10.2 版本,因此需要先更改 ROS2 的 DDS 实现。

编译 cyclonedds 前请确保在启动终端时没有 source ros2 相关的环境变量,否则会导致 cyclonedds 编译报错。

如果安装 ROS2 时在~/.bashrc中添加了 source /opt/ros/foxy/setup.bash ,没有则跳过删除环节。

如果有需要修改 ~/.bashrc 文件将其删除:

1
2
sudo apt install gedit
sudo gedit ~/.bashrc

在弹出的窗口中,注释掉 ROS2 相关的环境变量,例如:

1
# source /opt/ros/foxy/setup.bash

在终端中执行以下操作编译 cyclone-dds

1
2
3
4
5
6
cd ./unitree_ros2/cyclonedds_ws/src
#克隆cyclonedds仓库
git clone https://github.com/ros2/rmw_cyclonedds -b foxy
git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x
cd ..
colcon build --packages-select cyclonedds #编译cyclonedds

编译 unitree_go 和 unitree_api 功能包

编译好 cyclone-dds 后就需要 Ros2 相关的依赖来完成 G1 功能包的编译,因此编译前需要先 source ROS2 的环境变量。

1
2
source /opt/ros/foxy/setup.bash #source ROS2 环境变量
colcon build #编译工作空间下的所有功能包

测试

配置

打开 unitree_ros2_setup.sh 文件

1
sudo gedit ~/project/unitree_robot/unitree_ros2/unitree_ros2_setup.sh

bash 的内容如下:

1
2
3
4
5
6
7
8
#!/bin/bash
echo "Setup unitree ros2 environment"
source /opt/ros/foxy/setup.bash
source $HOME/project/unitree_robot/unitree_ros2/cyclonedds_ws/install/setup.bash
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export CYCLONEDDS_URI='<CycloneDDS><Domain><General><Interfaces>
<NetworkInterface name="wlo1" priority="default" multicast="default" />
</Interfaces></General></Domain></CycloneDDS>'

其中 “wlo1” 为 G1 所连接的网卡名称,根据实际情况修改为对应的网卡名称。

在终端中执行:

1
source ~/project/unitree_robot/unitree_ros2/unitree_ros2_setup.sh

即可完成 G1 开发环境的设置。
如果不希望每次打开新终端都执行一次 bash 脚本,也可将 unitree_ros2_setup.sh 中的内容写入到 ~/.bashrc中,但是当系统有多个 ROS 环境共存需要注意。

1
sudo gedit ~/.bashrc

添加

1
source ~/project/unitree_robot/unitree_ros2/unitree_ros2_setup.sh

这样启动终端的时候,Ros2的环境变量也会生效。

连接测试

完成上述配置后,建议重启一下电脑再进行测试。
确保机器人连接正确,打开终端输入

1
ros2 topic list

其他命令

1
2
3
4
5
6
7
8
# 列出所有话题
ros2 topic list

# 查看 /chatter 话题的信息
ros2 topic info /audio_msg

# 订阅 /chatter 话题
ros2 topic echo /audio_msg