# unitree_sdk2_python：连接 G1 / Go2 的 DDS 网络、读取状态并发送高层运动指令

> 原文链接：https://robotics.hellobike.com/tutorials/unitree-sdk2-python-g1-highlevel
> 频道：tutorials | 更新日期：2026-10-22 | 标签：unitree_sdk2_python, DDS, High-Level Control, G1, Go2, CycloneDDS
> 核心摘要：本教程整理 unitree_sdk2_python 的安装、DDS 通信示例、高层状态读取与高层控制示例，按文档命令在终端运行，并说明网络接口参数与常见 cyclonedds 安装问题。

# unitree_sdk2_python：连接 G1 / Go2 的 DDS 网络、读取状态并发送高层运动指令

&gt; 本文依据以下官方文档整理：[raw.githubusercontent.com/unitreerobotics/unitree_sdk2_pytho](https://raw.githubusercontent.com/unitreerobotics/unitree_sdk2_python/master/README.md)。命令与版本以文档当前版本为准。

## 前置条件

- Python &gt;= 3.8
- cyclonedds == 0.10.2
- numpy
- opencv-python
- 运行示例前已配置机器人的网络连接
- 知道机器人连接到的网络接口名称，例如 enp2s0

## 1. 安装 unitree_sdk2_python

先在终端安装 pip，拉取 unitree_sdk2_python 源码，并以可编辑方式安装。以下命令需逐行执行，安装完成后即可运行 /example 目录中的示例程序。
```bash
cd ~
sudo apt install python3-pip
git clone https://github.com/unitreerobotics/unitree_sdk2_python.git
cd unitree_sdk2_python
pip3 install -e .
```

## 2. 处理 cyclonedds 路径错误

如果执行 pip3 install -e . 时出现 Could not locate cyclonedds，说明未找到 cyclonedds 路径。先编译并安装 cyclonedds：
```bash
cd ~
git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x 
cd cyclonedds && mkdir build install && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=../install
cmake --build . --target install
```

## 3. 设置 CYCLONEDDS_HOME 后重新安装

进入 unitree_sdk2_python 目录，将 CYCLONEDDS_HOME 设置为刚编译的 cyclonedds 路径，然后再次安装 unitree_sdk2_python。
```bash
cd ~/unitree_sdk2_python
export CYCLONEDDS_HOME=~/cyclonedds/install
pip3 install -e .
```

## 4. 运行 DDS 发布与订阅示例

Python sdk2 通过 request-response 或 topic subscription/publishing 获取状态与控制。先启动 publisher，再打开新终端启动 subscriber，终端会看到数据输出。
```bash
python3 ./example/helloworld/publisher.py
```
```bash
python3 ./example/helloworld/subscriber.py
```

## 5. 读取高层状态

高层接口在数据结构和控制方法上与 unitree_sdk2 保持一致。读取高层状态时，将 enp2s0 替换为机器人连接到的网络接口名称。
```bash
python3 ./example/high_level/read_highstate.py enp2s0
```

## 6. 发送高层运动控制指令

运行高层控制示例，同样将 enp2s0 替换为机器人连接到的网络接口名称。该示例提供多种测试方法，可按需要选择。
```bash
python3 ./example/high_level/sportmode_test.py enp2s0
```
```python
test.StandUpDown() # Stand up and lie down
## test.VelocityMove() # Velocity control
## test.BalanceAttitude() # Attitude control
## test.TrajectoryFollow() # Trajectory tracking
## test.SpecialMotions() # Special motions
```

## 验证是否跑通

DDS 通信示例中，subscriber.py 终端能看到数据输出；read_highstate.py 能读取高层状态；sportmode_test.py 按所选测试方法执行，例如 StandUpDown 对应 stand up and lie down。

## 文档里点名的注意事项

- pip3 install -e . 报 Could not locate cyclonedds 时，需要先编译安装 cyclonedds，并设置 CYCLONEDDS_HOME。
- read_highstate.py、sportmode_test.py 等命令中的 enp2s0 需要替换为机器人连接到的网络接口名称。
- 进行 Low-Level Motor Control 前，需先用 app 关闭 high-level motion service (sport_mode)，以防止 conflicting instructions。
- Front Camera 示例需在有 graphical interface 的系统上运行，并按 ESC 退出程序。

## 相关 SDK 条目

- [Unitree SDK2 Python](/sdks/unitree-sdk2-python)

## 原始参考来源 (Sources)

- [raw.githubusercontent.com/unitreerobotics/unitree_sdk2_python/master/README.md](https://raw.githubusercontent.com/unitreerobotics/unitree_sdk2_python/master/README.md)