Skip to content

Commit 892693e

Browse files
committed
add G1ArmActionClient and corresponding examples
1 parent 986f39d commit 892693e

3 files changed

Lines changed: 216 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import time
2+
import sys
3+
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactoryInitialize
4+
from unitree_sdk2py.g1.arm.g1_arm_action_client import G1ArmActionClient
5+
from unitree_sdk2py.g1.arm.g1_arm_action_client import action_map
6+
from dataclasses import dataclass
7+
8+
@dataclass
9+
class TestOption:
10+
name: str
11+
id: int
12+
13+
option_list = [
14+
TestOption(name="release arm", id=0),
15+
TestOption(name="shake hand", id=1),
16+
TestOption(name="high five", id=2),
17+
TestOption(name="hug", id=3),
18+
TestOption(name="high wave", id=4),
19+
TestOption(name="clap", id=5),
20+
TestOption(name="face wave", id=6),
21+
TestOption(name="left kiss", id=7),
22+
TestOption(name="heart", id=8),
23+
TestOption(name="right heart", id=9),
24+
TestOption(name="hands up", id=10),
25+
TestOption(name="x-ray", id=11),
26+
TestOption(name="right hand up", id=12),
27+
TestOption(name="reject", id=13),
28+
TestOption(name="right kiss", id=14),
29+
TestOption(name="two-hand kiss", id=15),
30+
31+
]
32+
33+
class UserInterface:
34+
def __init__(self):
35+
self.test_option_ = None
36+
37+
def convert_to_int(self, input_str):
38+
try:
39+
return int(input_str)
40+
except ValueError:
41+
return None
42+
43+
def terminal_handle(self):
44+
input_str = input("Enter id or name: \n")
45+
46+
if input_str == "list":
47+
self.test_option_.name = None
48+
self.test_option_.id = None
49+
for option in option_list:
50+
print(f"{option.name}, id: {option.id}")
51+
return
52+
53+
for option in option_list:
54+
if input_str == option.name or self.convert_to_int(input_str) == option.id:
55+
self.test_option_.name = option.name
56+
self.test_option_.id = option.id
57+
print(f"Test: {self.test_option_.name}, test_id: {self.test_option_.id}")
58+
return
59+
60+
print("No matching test option found.")
61+
62+
63+
if __name__ == "__main__":
64+
if len(sys.argv) < 2:
65+
print(f"Usage: python3 {sys.argv[0]} networkInterface")
66+
sys.exit(-1)
67+
68+
print("WARNING: Please ensure there are no obstacles around the robot while running this example.")
69+
input("Press Enter to continue...")
70+
71+
ChannelFactoryInitialize(0, sys.argv[1])
72+
73+
test_option = TestOption(name=None, id=None)
74+
user_interface = UserInterface()
75+
user_interface.test_option_ = test_option
76+
77+
armAction_client = G1ArmActionClient()
78+
armAction_client.SetTimeout(10.0)
79+
armAction_client.Init()
80+
81+
# actionList = armAction_client.GetActionList()
82+
# print("actionList\n",actionList)
83+
84+
print("Input \"list\" to list all test option ...")
85+
while True:
86+
user_interface.terminal_handle()
87+
88+
print(f"Updated Test Option: Name = {test_option.name}, ID = {test_option.id}")
89+
90+
if test_option.id == 0:
91+
armAction_client.ExecuteAction(action_map.get("release arm"))
92+
elif test_option.id == 1:
93+
armAction_client.ExecuteAction(action_map.get("shake hand"))
94+
time.sleep(2)
95+
armAction_client.ExecuteAction(action_map.get("release arm"))
96+
elif test_option.id == 2:
97+
armAction_client.ExecuteAction(action_map.get("high five"))
98+
time.sleep(2)
99+
armAction_client.ExecuteAction(action_map.get("release arm"))
100+
elif test_option.id == 3:
101+
armAction_client.ExecuteAction(action_map.get("hug"))
102+
time.sleep(2)
103+
armAction_client.ExecuteAction(action_map.get("release arm"))
104+
elif test_option.id == 4:
105+
armAction_client.ExecuteAction(action_map.get("high wave"))
106+
elif test_option.id == 5:
107+
armAction_client.ExecuteAction(action_map.get("clap"))
108+
elif test_option.id == 6:
109+
armAction_client.ExecuteAction(action_map.get("face wave"))
110+
elif test_option.id == 7:
111+
armAction_client.ExecuteAction(action_map.get("left kiss"))
112+
elif test_option.id == 8:
113+
armAction_client.ExecuteAction(action_map.get("heart"))
114+
time.sleep(2)
115+
armAction_client.ExecuteAction(action_map.get("release arm"))
116+
elif test_option.id == 9:
117+
armAction_client.ExecuteAction(action_map.get("right heart"))
118+
time.sleep(2)
119+
armAction_client.ExecuteAction(action_map.get("release arm"))
120+
elif test_option.id == 10:
121+
armAction_client.ExecuteAction(action_map.get("hands up"))
122+
time.sleep(2)
123+
armAction_client.ExecuteAction(action_map.get("release arm"))
124+
elif test_option.id == 11:
125+
armAction_client.ExecuteAction(action_map.get("x-ray"))
126+
time.sleep(2)
127+
armAction_client.ExecuteAction(action_map.get("release arm"))
128+
elif test_option.id == 12:
129+
armAction_client.ExecuteAction(action_map.get("right hand up"))
130+
time.sleep(2)
131+
armAction_client.ExecuteAction(action_map.get("release arm"))
132+
elif test_option.id == 13:
133+
armAction_client.ExecuteAction(action_map.get("reject"))
134+
time.sleep(2)
135+
armAction_client.ExecuteAction(action_map.get("release arm"))
136+
elif test_option.id == 14:
137+
armAction_client.ExecuteAction(action_map.get("right kiss"))
138+
elif test_option.id == 15:
139+
armAction_client.ExecuteAction(action_map.get("two-hand kiss"))
140+
141+
time.sleep(1)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
" service name
3+
"""
4+
ARM_ACTION_SERVICE_NAME = "arm"
5+
6+
"""
7+
" service api version
8+
"""
9+
ARM_ACTION_API_VERSION = "1.0.0.14"
10+
11+
"""
12+
" api id
13+
"""
14+
ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION = 7106
15+
ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST = 7107
16+
17+
"""
18+
" error code
19+
"""
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
3+
from ...rpc.client import Client
4+
from .g1_arm_action_api import *
5+
6+
action_map = {
7+
"release arm": 99,
8+
"two-hand kiss": 11,
9+
"left kiss": 12,
10+
"right kiss": 13,
11+
"hands up": 15,
12+
"clap": 17,
13+
"high five": 18,
14+
"hug": 19,
15+
"heart": 20,
16+
"right heart": 21,
17+
"reject": 22,
18+
"right hand up": 23,
19+
"x-ray": 24,
20+
"face wave": 25,
21+
"high wave": 26,
22+
"shake hand": 27,
23+
}
24+
25+
26+
"""
27+
" class SportClient
28+
"""
29+
class G1ArmActionClient(Client):
30+
def __init__(self):
31+
super().__init__(ARM_ACTION_SERVICE_NAME, False)
32+
33+
def Init(self):
34+
# set api version
35+
self._SetApiVerson(ARM_ACTION_API_VERSION)
36+
37+
# regist api
38+
self._RegistApi(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION, 0)
39+
self._RegistApi(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST, 0)
40+
41+
## API Call ##
42+
def ExecuteAction(self, action_id: int):
43+
p = {}
44+
p["data"] = action_id
45+
parameter = json.dumps(p)
46+
code, data = self._Call(ROBOT_API_ID_ARM_ACTION_EXECUTE_ACTION, parameter)
47+
return code
48+
49+
def GetActionList(self):
50+
p = {}
51+
parameter = json.dumps(p)
52+
code, data = self._Call(ROBOT_API_ID_ARM_ACTION_GET_ACTION_LIST, parameter)
53+
if code == 0:
54+
return code, json.loads(data)
55+
else:
56+
return code, None

0 commit comments

Comments
 (0)