宇树机器人对接Python-语音识别(ASR)和文字转语音(TTS)

前言

SDK地址

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

上位机连接

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

TTS

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

工单

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

语音唤醒(这个要用手机APP设置WIFI连接,直接使用的,不是开发使用)

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

注意

SDK只支持Ubuntu,版本20.04。

机器人中的PC python版本是3.8.10,所以我们开发过程中最好也用3.8.10。

开发的时候我们要切换到按键模式

模式切换 L1 + L2

  • 唤醒模式
  • 按键模式
  • 关闭交互

在按键模式下使用 L2 + Select 进行语音输入。

目前测试的只有按键模式下才能正常接受到语音消息。

ASR

示例

https://github.com/unitreerobotics/unitree_sdk2/blob/main/example/g1/audio/g1_audio_client_example.cpp

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import time
import sys
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactoryInitialize
from unitree_sdk2py.g1.audio.g1_audio_client import AudioClient
from unitree_sdk2py.idl.std_msgs.msg.dds_ import String_

def AudioHandler(msg: String_):
print(msg.data)
if audio_client and "欢迎" in msg.data:
audio_client.TtsMaker("领导你真帅!",0)
elif audio_client and "你好" in msg.data:
audio_client.TtsMaker("你也好呀!",0)
elif audio_client and "星期" in msg.data:
audio_client.TtsMaker("今天星期四!",0)
elif audio_client and "床前明月光" in msg.data:
audio_client.TtsMaker("疑是地上霜,举头望明月,低头思故乡!",0)

if __name__ == "__main__":
ChannelFactoryInitialize(0, "enp60s0")
global audio_client
audio_client = AudioClient()
audio_client.SetTimeout(10.0)
audio_client.Init()

audio_msg_subscriber = ChannelSubscriber("rt/audio_msg", String_)
audio_msg_subscriber.Init(AudioHandler, 10)
while True:
time.sleep(1)

切换模式

1
2
{"play_state":1}
{"play_state":0}

受到语音消息

1
{"index":34,"timestamp":1752742440086,"type":0,"text":"你好。","angle":0,"speaker_id":0,"emotion":"<|NEUTRAL|>","confidence":0.500000,"language":"<|zh|>","is_final":false}

TTS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import time
import sys
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactoryInitialize
from unitree_sdk2py.g1.audio.g1_audio_client import AudioClient

if __name__ == "__main__":
ChannelFactoryInitialize(0, "enp60s0")

audio_client = AudioClient()
audio_client.SetTimeout(10.0)
audio_client.Init()
# 获取音量
ret = audio_client.GetVolume()
print("debug GetVolume: ",ret)
# 设置音量
audio_client.SetVolume(80)
time.sleep(3)
audio_client.TtsMaker("你好!",0)