-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sportmode.py
144 lines (115 loc) · 3.87 KB
/
test_sportmode.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import time
import sys
from unitree_sdk2py.core.channel import ChannelSubscriber, ChannelFactoryInitialize
from unitree_sdk2py.idl.default import unitree_go_msg_dds__SportModeState_
from unitree_sdk2py.idl.unitree_go.msg.dds_ import SportModeState_
from unitree_sdk2py.go2.sport.sport_client import (
SportClient,
PathPoint,
SPORT_PATH_POINT_SIZE,
)
import math
class SportModeTest:
def __init__(self) -> None:
# Time count
self.t = 0
self.dt = 0.01
# Initial poition and yaw
self.px0 = 0
self.py0 = 0
self.yaw0 = 0
self.client = SportClient() # Create a sport client
self.client.SetTimeout(10.0)
self.client.Init()
def GetInitState(self, robot_state: SportModeState_):
self.px0 = robot_state.position[0]
self.py0 = robot_state.position[1]
self.yaw0 = robot_state.imu_state.rpy[2]
def StandUpDown(self):
self.client.StandDown()
print("Stand down !!!")
time.sleep(1)
self.client.StandUp()
print("Stand up !!!")
time.sleep(1)
self.client.StandDown()
print("Stand down !!!")
time.sleep(1)
self.client.Damp()
def VelocityMove(self):
elapsed_time = 1
for i in range(int(elapsed_time / self.dt)):
self.client.Move(0.3, 0, 0.0) # vx, vy vyaw
time.sleep(self.dt)
self.client.StopMove()
def BalanceAttitude(self):
self.client.Euler(0.1, 0.2, 0.3) # roll, pitch, yaw
self.client.BalanceStand()
def TrajectoryFollow(self):
time_seg = 0.2
time_temp = self.t - time_seg
path = []
for i in range(SPORT_PATH_POINT_SIZE):
time_temp += time_seg
px_local = 0.5 * math.sin(0.5 * time_temp)
py_local = 0
yaw_local = 0
vx_local = 0.25 * math.cos(0.5 * time_temp)
vy_local = 0
vyaw_local = 0
path_point_tmp = PathPoint(0, 0, 0, 0, 0, 0, 0)
path_point_tmp.timeFromStart = i * time_seg
path_point_tmp.x = (
px_local * math.cos(self.yaw0)
- py_local * math.sin(self.yaw0)
+ self.px0
)
path_point_tmp.y = (
px_local * math.sin(self.yaw0)
+ py_local * math.cos(self.yaw0)
+ self.py0
)
path_point_tmp.yaw = yaw_local + self.yaw0
path_point_tmp.vx = vx_local * math.cos(self.yaw0) - vy_local * math.sin(
self.yaw0
)
path_point_tmp.vy = vx_local * math.sin(self.yaw0) + vy_local * math.cos(
self.yaw0
)
path_point_tmp.vyaw = vyaw_local
path.append(path_point_tmp)
self.client.TrajectoryFollow(path)
def SpecialMotions(self):
self.client.RecoveryStand()
print("RecoveryStand !!!")
time.sleep(1)
self.client.Stretch()
print("Sit !!!")
time.sleep(1)
self.client.RecoveryStand()
print("RecoveryStand !!!")
time.sleep(1)
# Robot state
robot_state = unitree_go_msg_dds__SportModeState_()
def HighStateHandler(msg: SportModeState_):
global robot_state
robot_state = msg
if __name__ == "__main__":
if len(sys.argv)>1:
ChannelFactoryInitialize(0, sys.argv[1])
else:
ChannelFactoryInitialize(0)
sub = ChannelSubscriber("rt/sportmodestate", SportModeState_)
sub.Init(HighStateHandler, 10)
time.sleep(1)
test = SportModeTest()
test.GetInitState(robot_state)
print("Start test !!!")
while True:
test.t += test.dt
test.StandUpDown()
# test.VelocityMove()
# test.BalanceAttitude()
# test.TrajectoryFollow()
# test.SpecialMotions()
time.sleep(test.dt)