Skip to content

Commit f17114c

Browse files
committed
update g1 audio api and g1_audio_client_example
1 parent 0f17096 commit f17114c

3 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import time
2+
import sys
3+
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactoryInitialize
4+
from unitree_sdk2py.g1.audio.g1_audio_client import AudioClient
5+
from unitree_sdk2py.g1.loco.g1_loco_client import LocoClient
6+
7+
if __name__ == "__main__":
8+
if len(sys.argv) < 2:
9+
print(f"Usage: python3 {sys.argv[0]} networkInterface")
10+
sys.exit(-1)
11+
12+
ChannelFactoryInitialize(0, sys.argv[1])
13+
14+
audio_client = AudioClient()
15+
audio_client.SetTimeout(10.0)
16+
audio_client.Init()
17+
18+
sport_client = LocoClient()
19+
sport_client.SetTimeout(10.0)
20+
sport_client.Init()
21+
22+
ret = audio_client.GetVolume()
23+
print("debug GetVolume: ",ret)
24+
25+
audio_client.SetVolume(85)
26+
27+
ret = audio_client.GetVolume()
28+
print("debug GetVolume: ",ret)
29+
30+
sport_client.WaveHand()
31+
32+
audio_client.TtsMaker("大家好!我是宇树科技人形机器人。语音开发测试例程运行成功! 很高兴认识你!",0)
33+
time.sleep(8)
34+
audio_client.TtsMaker("接下来测试灯带开发例程!",0)
35+
time.sleep(1)
36+
audio_client.LedControl(255,0,0)
37+
time.sleep(1)
38+
audio_client.LedControl(0,255,0)
39+
time.sleep(1)
40+
audio_client.LedControl(0,0,255)
41+
42+
time.sleep(3)
43+
audio_client.TtsMaker("测试完毕,谢谢大家!",0)
44+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
" service name
3+
"""
4+
AUDIO_SERVICE_NAME = "voice"
5+
6+
"""
7+
" service api version
8+
"""
9+
AUDIO_API_VERSION = "1.0.0.0"
10+
11+
"""
12+
" api id
13+
"""
14+
ROBOT_API_ID_AUDIO_TTS = 1001
15+
ROBOT_API_ID_AUDIO_ASR = 1002
16+
ROBOT_API_ID_AUDIO_START_PLAY = 1003
17+
ROBOT_API_ID_AUDIO_STOP_PLAY = 1004
18+
ROBOT_API_ID_AUDIO_GET_VOLUME = 1005
19+
ROBOT_API_ID_AUDIO_SET_VOLUME = 1006
20+
ROBOT_API_ID_AUDIO_SET_RGB_LED = 1010
21+
22+
"""
23+
" error code
24+
"""
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import json
2+
3+
from ...rpc.client import Client
4+
from .g1_audio_api import *
5+
6+
"""
7+
" class SportClient
8+
"""
9+
class AudioClient(Client):
10+
def __init__(self):
11+
super().__init__(AUDIO_SERVICE_NAME, False)
12+
self.tts_index = 0
13+
14+
def Init(self):
15+
# set api version
16+
self._SetApiVerson(AUDIO_API_VERSION)
17+
18+
# regist api
19+
self._RegistApi(ROBOT_API_ID_AUDIO_TTS, 0)
20+
self._RegistApi(ROBOT_API_ID_AUDIO_ASR, 0)
21+
self._RegistApi(ROBOT_API_ID_AUDIO_START_PLAY, 0)
22+
self._RegistApi(ROBOT_API_ID_AUDIO_STOP_PLAY, 0)
23+
self._RegistApi(ROBOT_API_ID_AUDIO_GET_VOLUME, 0)
24+
self._RegistApi(ROBOT_API_ID_AUDIO_SET_VOLUME, 0)
25+
self._RegistApi(ROBOT_API_ID_AUDIO_SET_RGB_LED, 0)
26+
27+
## API Call ##
28+
def TtsMaker(self, text: str, speaker_id: int):
29+
self.tts_index += self.tts_index
30+
p = {}
31+
p["index"] = self.tts_index
32+
p["text"] = text
33+
p["speaker_id"] = speaker_id
34+
parameter = json.dumps(p)
35+
code, data = self._Call(ROBOT_API_ID_AUDIO_TTS, parameter)
36+
return code
37+
38+
def GetVolume(self):
39+
p = {}
40+
parameter = json.dumps(p)
41+
code, data = self._Call(ROBOT_API_ID_AUDIO_GET_VOLUME, parameter)
42+
if code == 0:
43+
return code, json.loads(data)
44+
else:
45+
return code, None
46+
47+
def SetVolume(self, volume: int):
48+
p = {}
49+
p["volume"] = volume
50+
# p["name"] = 'volume'
51+
parameter = json.dumps(p)
52+
code, data = self._Call(ROBOT_API_ID_AUDIO_SET_VOLUME, parameter)
53+
return code
54+
55+
def LedControl(self, R: int, G: int, B: int):
56+
p = {}
57+
p["R"] = R
58+
p["G"] = G
59+
p["B"] = B
60+
parameter = json.dumps(p)
61+
code, data = self._Call(ROBOT_API_ID_AUDIO_SET_RGB_LED, parameter)
62+
return code

0 commit comments

Comments
 (0)