Skip to content

Commit 41209cd

Browse files
author
yangning wu
committed
add unitree_sdk2py
0 parents  commit 41209cd

142 files changed

Lines changed: 6074 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generated by MacOS
2+
.DS_Store
3+
4+
# Generated by Windows
5+
Thumbs.db
6+
7+
# Applications
8+
*.app
9+
*.exe
10+
*.war
11+
12+
# Large media files
13+
*.mp4
14+
*.tiff
15+
*.avi
16+
*.flv
17+
*.mov
18+
*.wmv
19+
*.jpg
20+
*.png
21+
22+
# VS Code
23+
.vscode
24+
25+
# other
26+
*.egg-info
27+
__pycache__
28+
29+
# IDEs
30+
.idea
31+
32+
# cache
33+
.pytest_cache
34+
35+
# JetBrains IDE
36+
.idea/

README zh.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# unitree_sdk2py
2+
unitree_sdk2 python 接口
3+
4+
# 安装
5+
## 依赖
6+
- python>=3.8
7+
- cyclonedds==0.10.2
8+
- numpy
9+
- opencv-python
10+
11+
## 安装 unitree_sdk2py
12+
在终端中执行:
13+
```bash
14+
cd ~
15+
sudo apt install python3-pip
16+
git clone https://github.com/unitreerobotics/unitree_sdk2py.git
17+
cd unitree_sdk2py
18+
pip3 install -e .
19+
```
20+
## FAQ
21+
##### 1. `pip3 install -e .` 遇到报错
22+
```bash
23+
Could not locate cyclonedds. Try to set CYCLONEDDS_HOME or CMAKE_PREFIX_PATH
24+
```
25+
该错误提示找不到 cyclonedds 路径。首先编译安装cyclonedds:
26+
```bash
27+
cd ~
28+
git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x
29+
cd cyclonedds && mkdir build install && cd build
30+
cmake .. -DCMAKE_INSTALL_PREFIX=../install
31+
cmake --build . --target install
32+
```
33+
进入 unitree_sdk2py 目录,设置 `CYCLONEDDS_HOME` 为刚刚编译好的 cyclonedds 所在路径,再安装unitree_sdk2py
34+
```bash
35+
cd ~/unitree_sdk2py
36+
export CYCLONEDDS_HOME="~/cyclonedds/install"
37+
pip3 install -e .
38+
```
39+
40+
详细见:
41+
https://pypi.org/project/cyclonedds/#installing-with-pre-built-binaries
42+
43+
# 使用
44+
python sdk2 接口与 unitree_skd2的接口保持一致,通过请求响应或订阅发布topic实现机器人的状态获取和控制。相应的例程位于`/example`目录下。在运行例程前,需要根据文档 https://support.unitree.com/home/zh/developer/Quick_start 配置好机器人的网络连接。
45+
## DDS通讯
46+
在终端中执行:
47+
```bash
48+
python3 ./example/helloworld/publisher.py
49+
```
50+
打开新的终端,执行:
51+
```bash
52+
python3 ./example/helloworld/subscriber.py
53+
```
54+
可以看到终端输出的数据信息。`publisher.py``subscriber.py` 传输的数据定义在 `user_data.py` 中,用户可以根据需要自行定义需要传输的数据结构。
55+
56+
## 高层状态和控制
57+
高层接口的数据结构和控制方式与unitree_sdk2一致。具体可见:https://support.unitree.com/home/zh/developer/sports_services
58+
### 高层状态
59+
终端中执行:
60+
```bash
61+
python3 ./example/high_level/read_highstate.py enp2s0
62+
```
63+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。
64+
### 高层控制
65+
终端中执行:
66+
```bash
67+
python3 ./example/high_level/sportmode_test.py enp2s0
68+
```
69+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。
70+
该例程提供了几种测试方法,可根据测试需要选择:
71+
```python
72+
test.StandUpDown() # 站立趴下
73+
# test.VelocityMove() # 速度控制
74+
# test.BalanceAttitude() # 姿态控制
75+
# test.TrajectoryFollow() # 轨迹跟踪
76+
# test.SpecialMotions() # 特殊动作
77+
78+
```
79+
## 底层状态和控制
80+
底层接口的数据结构和控制方式与unitree_sdk2一致。具体可见:https://support.unitree.com/home/zh/developer/Basic_services
81+
### 底层状态
82+
终端中执行:
83+
```bash
84+
python3 ./example/low_level/lowlevel_control.py enp2s0
85+
```
86+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。程序会输出右前腿hip关节的状态、IMU和电池电压信息。
87+
88+
### 底层电机控制
89+
首先使用 app 关闭高层运动服务(sport_mode),否则会导致指令冲突。
90+
终端中执行:
91+
```bash
92+
python3 ./example/low_level/lowlevel_control.py enp2s0
93+
```
94+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。左后腿 hip 关节会保持在0角度 (安全起见,这里设置 kp=10, kd=1),左后腿 calf 关节将持续输出 1Nm 的转矩。
95+
96+
## 遥控器状态获取
97+
终端中执行:
98+
```bash
99+
python3 ./example/wireless_controller/wireless_controller.py enp2s0
100+
```
101+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。
102+
终端将输出每一个按键的状态。对于遥控器按键的定义和数据结构可见: https://support.unitree.com/home/zh/developer/Get_remote_control_status
103+
104+
## 前置摄像头
105+
使用opencv获取前置摄像头(确保在有图形界面的系统下运行, 按 ESC 退出程序):
106+
```bash
107+
python3 ./example/front_camera/camera_opencv.py enp2s0
108+
```
109+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。
110+
111+
## 避障开关
112+
```bash
113+
python3 ./example/obstacles_avoid_switch/obstacles_avoid_switch.py enp2s0
114+
```
115+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。机器人将循环开启和关闭避障功能。关于避障服务,详细见 https://support.unitree.com/home/zh/developer/ObstaclesAvoidClient
116+
117+
## 灯光音量控制
118+
```bash
119+
python3 ./example/vui_client/vui_client_example.py enp2s0
120+
```
121+
其中 `enp2s0` 为机器人所连接的网卡名称,请根据实际情况修改。机器人将循环调节音量和灯光亮度。该接口详细见 https://support.unitree.com/home/zh/developer/VuiClient

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# unitree_sdk2py
2+
Python interface for unitree sdk2
3+
4+
# Installation
5+
## Dependencies
6+
- Python >= 3.8
7+
- cyclonedds == 0.10.2
8+
- numpy
9+
- opencv-python
10+
## Install unitree_sdk2py
11+
Execute the following commands in the terminal:
12+
```bash
13+
cd ~
14+
sudo apt install python3-pip
15+
git clone https://github.com/unitreerobotics/unitree_sdk2py.git
16+
cd unitree_sdk2py
17+
pip3 install -e .
18+
```
19+
## FAQ
20+
##### 1. Error when `pip3 install -e .`:
21+
```bash
22+
Could not locate cyclonedds. Try to set CYCLONEDDS_HOME or CMAKE_PREFIX_PATH
23+
```
24+
This error mentions that the cyclonedds path could not be found. First compile and install cyclonedds:
25+
26+
```bash
27+
cd ~
28+
git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x
29+
cd cyclonedds && mkdir build install && cd build
30+
cmake .. -DCMAKE_INSTALL_PREFIX=../install
31+
cmake --build . --target install
32+
```
33+
Enter the unitree_sdk2py directory, set `CYCLONEDDS_HOME` to the path of the cyclonedds you just compiled, and then install unitree_sdk2py.
34+
```bash
35+
cd ~/unitree_sdk2py
36+
export CYCLONEDDS_HOME="~/cyclonedds/install"
37+
pip3 install -e .
38+
```
39+
For details, see: https://pypi.org/project/cyclonedds/#installing-with-pre-built-binaries
40+
41+
# Usage
42+
The Python sdk2 interface maintains consistency with the unitree_sdk2 interface, achieving robot status acquisition and control through request-response or topic subscription/publishing. Example programs are located in the `/example` directory. Before running the examples, configure the robot's network connection as per the instructions in the document at https://support.unitree.com/home/en/developer/Quick_start.
43+
## DDS Communication
44+
In the terminal, execute:
45+
```bash
46+
python3 ./example/helloworld/publisher.py
47+
```
48+
Open a new terminal and execute:
49+
```bash
50+
python3 ./example/helloworld/subscriber.py
51+
```
52+
You will see the data output in the terminal. The data structure transmitted between `publisher.py` and `subscriber.py` is defined in `user_data.py`, and users can define the required data structure as needed.
53+
## High-Level Status and Control
54+
The high-level interface maintains consistency with unitree_sdk2 in terms of data structure and control methods. For detailed information, refer to https://support.unitree.com/home/en/developer/sports_services.
55+
### High-Level Status
56+
Execute the following command in the terminal:
57+
```bash
58+
python3 ./example/high_level/read_highstate.py enp2s0
59+
```
60+
Replace `enp2s0` with the name of the network interface to which the robot is connected,.
61+
### High-Level Control
62+
Execute the following command in the terminal:
63+
```bash
64+
python3 ./example/high_level/sportmode_test.py enp2s0
65+
```
66+
Replace `enp2s0` with the name of the network interface to which the robot is connected. This example program provides several test methods, and you can choose the required tests as follows:
67+
```python
68+
test.StandUpDown() # Stand up and lie down
69+
# test.VelocityMove() # Velocity control
70+
# test.BalanceAttitude() # Attitude control
71+
# test.TrajectoryFollow() # Trajectory tracking
72+
# test.SpecialMotions() # Special motions
73+
```
74+
## Low-Level Status and Control
75+
The low-level interface maintains consistency with unitree_sdk2 in terms of data structure and control methods. For detailed information, refer to https://support.unitree.com/home/en/developer/Basic_services.
76+
### Low-Level Status
77+
Execute the following command in the terminal:
78+
```bash
79+
python3 ./example/low_level/lowlevel_control.py enp2s0
80+
```
81+
Replace `enp2s0` with the name of the network interface to which the robot is connected. The program will output the state of the right front leg hip joint, IMU, and battery voltage.
82+
### Low-Level Motor Control
83+
First, use the app to turn off the high-level motion service (sport_mode) to prevent conflicting instructions.
84+
Execute the following command in the terminal:
85+
```bash
86+
python3 ./example/low_level/lowlevel_control.py enp2s0
87+
```
88+
Replace `enp2s0` with the name of the network interface to which the robot is connected. The left hind leg hip joint will maintain a 0-degree position (for safety, set kp=10, kd=1), and the left hind leg calf joint will continuously output 1Nm of torque.
89+
## Wireless Controller Status
90+
Execute the following command in the terminal:
91+
```bash
92+
python3 ./example/wireless_controller/wireless_controller.py enp2s0
93+
```
94+
Replace `enp2s0` with the name of the network interface to which the robot is connected. The terminal will output the status of each key. For the definition and data structure of the remote control keys, refer to https://support.unitree.com/home/en/developer/Get_remote_control_status.
95+
## Front Camera
96+
Use OpenCV to obtain the front camera (ensure to run on a system with a graphical interface, and press ESC to exit the program):
97+
```bash
98+
python3 ./example/front_camera/camera_opencv.py enp2s0
99+
```
100+
Replace `enp2s0` with the name of the network interface to which the robot is connected.
101+
102+
## Obstacle Avoidance Switch
103+
```bash
104+
python3 ./example/obstacles_avoid_switch/obstacles_avoid_switch.py enp2s0
105+
```
106+
Replace `enp2s0` with the name of the network interface to which the robot is connected. The robot will cycle obstacle avoidance on and off. For details on the obstacle avoidance service, see https://support.unitree.com/home/en/developer/ObstaclesAvoidClient
107+
108+
## Light and volume control
109+
```bash
110+
python3 ./example/vui_client/vui_client_example.py enp2s0
111+
```
112+
Replace `enp2s0` with the name of the network interface to which the robot is connected.T he robot will cycle the volume and light brightness. The interface is detailed at https://support.unitree.com/home/en/developer/VuiClient
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from unitree_sdk2py.core.channel import ChannelFactortyInitialize
2+
from unitree_sdk2py.go2.video.video_client import VideoClient
3+
import cv2
4+
import numpy as np
5+
import sys
6+
7+
8+
if __name__ == "__main__":
9+
if len(sys.argv)>1:
10+
ChannelFactortyInitialize(0, sys.argv[1])
11+
else:
12+
ChannelFactortyInitialize(0)
13+
14+
client = VideoClient() # Create a video client
15+
client.SetTimeout(3.0)
16+
client.Init()
17+
18+
code, data = client.GetImageSample()
19+
20+
# Request normal when code==0
21+
while code == 0:
22+
# Get Image data from Go2 robot
23+
code, data = client.GetImageSample()
24+
25+
# Convert to numpy image
26+
image_data = np.frombuffer(bytes(data), dtype=np.uint8)
27+
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
28+
29+
# Display image
30+
cv2.imshow("front_camera", image)
31+
# Press ESC to stop
32+
if cv2.waitKey(20) == 27:
33+
break
34+
35+
if code != 0:
36+
print("Get image sample error. code:", code)
37+
else:
38+
# Capture an image
39+
cv2.imwrite("front_image.jpg", image)
40+
41+
cv2.destroyWindow("front_camera")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
import os
3+
import sys
4+
5+
from unitree_sdk2py.core.channel import ChannelFactortyInitialize
6+
from unitree_sdk2py.go2.video.video_client import VideoClient
7+
8+
if __name__ == "__main__":
9+
if len(sys.argv)>1:
10+
ChannelFactortyInitialize(0, sys.argv[1])
11+
else:
12+
ChannelFactortyInitialize(0)
13+
14+
client = VideoClient()
15+
client.SetTimeout(3.0)
16+
client.Init()
17+
18+
print("##################GetImageSample###################")
19+
code, data = client.GetImageSample()
20+
21+
if code != 0:
22+
print("get image sample error. code:", code)
23+
else:
24+
imageName = "./img.jpg"
25+
print("ImageName:", imageName)
26+
27+
with open(imageName, "+wb") as f:
28+
f.write(bytes(data))
29+
30+
time.sleep(1)

example/helloworld/publisher.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
3+
from unitree_sdk2py.core.channel import ChannelPublisher, ChannelFactortyInitialize
4+
from user_data import *
5+
6+
7+
if __name__ == "__main__":
8+
ChannelFactortyInitialize()
9+
10+
# Create a publisher to publish the data defined in UserData class
11+
pub = ChannelPublisher("topic", UserData)
12+
pub.Init()
13+
14+
for i in range(30):
15+
# Create a Userdata message
16+
msg = UserData(" ", 0)
17+
msg.string_data = "Hello world"
18+
msg.float_data = time.time()
19+
20+
# Publish message
21+
if pub.Write(msg, 0.5):
22+
print("Publish success. msg:", msg)
23+
else:
24+
print("Waitting for subscriber.")
25+
26+
time.sleep(1)
27+
28+
pub.Close()

example/helloworld/subscriber.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
3+
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactortyInitialize
4+
from user_data import *
5+
6+
7+
if __name__ == "__main__":
8+
ChannelFactortyInitialize()
9+
# Create a subscriber to subscribe the data defined in UserData class
10+
sub = ChannelSubscriber("topic", UserData)
11+
sub.Init()
12+
13+
while True:
14+
msg = sub.Read()
15+
if msg is not None:
16+
print("Subscribe success. msg:", msg)
17+
else:
18+
print("No data subscribed.")
19+
break
20+
sub.Close()

0 commit comments

Comments
 (0)