1+ import time
2+ import sys
3+
4+ from unitree_sdk2py .core .channel import ChannelPublisher , ChannelFactoryInitialize
5+ from unitree_sdk2py .core .channel import ChannelSubscriber , ChannelFactoryInitialize
6+ from unitree_sdk2py .idl .default import unitree_go_msg_dds__LowCmd_
7+ from unitree_sdk2py .idl .default import unitree_go_msg_dds__LowState_
8+ from unitree_sdk2py .idl .unitree_go .msg .dds_ import LowCmd_
9+ from unitree_sdk2py .idl .unitree_go .msg .dds_ import LowState_
10+ from unitree_sdk2py .utils .thread import RecurrentThread
11+ import unitree_legged_const as b2
12+ from unitree_sdk2py .comm .motion_switcher .motion_switcher_client import MotionSwitcherClient
13+ from unitree_sdk2py .b2 .sport .sport_client import SportClient
14+
15+ from unitree_sdk2py .utils .crc import CRC
16+
17+ class Custom :
18+ def __init__ (self ):
19+ self .Kp = 1000.0
20+ self .Kd = 10.0
21+ self .time_consume = 0
22+ self .rate_count = 0
23+ self .sin_count = 0
24+ self .motiontime = 0
25+ self .dt = 0.002
26+
27+ self .low_cmd = unitree_go_msg_dds__LowCmd_ ()
28+ self .low_state = None
29+
30+ self .targetPos_1 = [0.0 , 1.36 , - 2.65 , 0.0 , 1.36 , - 2.65 ,
31+ - 0.2 , 1.36 , - 2.65 , 0.2 , 1.36 , - 2.65 ]
32+
33+ self .targetPos_2 = [0.0 , 0.67 , - 1.3 , 0.0 , 0.67 , - 1.3 ,
34+ 0.0 , 0.67 , - 1.3 , 0.0 , 0.67 , - 1.3 ]
35+
36+ self .targetPos_3 = [- 0.5 , 1.36 , - 2.65 , 0.5 , 1.36 , - 2.65 ,
37+ - 0.5 , 1.36 , - 2.65 , 0.5 , 1.36 , - 2.65 ]
38+
39+ self .startPos = [0.0 ] * 12
40+ self .duration_1 = 500
41+ self .duration_2 = 900
42+ self .duration_3 = 1000
43+ self .duration_4 = 900
44+ self .percent_1 = 0
45+ self .percent_2 = 0
46+ self .percent_3 = 0
47+ self .percent_4 = 0
48+
49+ self .firstRun = True
50+ self .done = False
51+
52+ # thread handling
53+ self .lowCmdWriteThreadPtr = None
54+
55+ self .crc = CRC ()
56+
57+ def Init (self ):
58+ self .InitLowCmd ()
59+
60+ # create publisher #
61+ self .lowcmd_publisher = ChannelPublisher ("rt/lowcmd" , LowCmd_ )
62+ self .lowcmd_publisher .Init ()
63+
64+ # create subscriber #
65+ self .lowstate_subscriber = ChannelSubscriber ("rt/lowstate" , LowState_ )
66+ self .lowstate_subscriber .Init (self .LowStateMessageHandler , 10 )
67+
68+ self .sc = SportClient ()
69+ self .sc .SetTimeout (5.0 )
70+ self .sc .Init ()
71+
72+ self .msc = MotionSwitcherClient ()
73+ self .msc .SetTimeout (5.0 )
74+ self .msc .Init ()
75+
76+ status , result = self .msc .CheckMode ()
77+ while result ['name' ]:
78+ self .sc .StandDown ()
79+ self .msc .ReleaseMode ()
80+ status , result = self .msc .CheckMode ()
81+ time .sleep (1 )
82+
83+
84+ def Start (self ):
85+ self .lowCmdWriteThreadPtr = RecurrentThread (
86+ interval = 0.002 , target = self .LowCmdWrite , name = "writebasiccmd"
87+ )
88+ self .lowCmdWriteThreadPtr .Start ()
89+
90+ def InitLowCmd (self ):
91+ self .low_cmd .head [0 ] = 0xFE
92+ self .low_cmd .head [1 ] = 0xEF
93+ self .low_cmd .level_flag = 0xFF
94+ self .low_cmd .gpio = 0
95+ for i in range (20 ):
96+ self .low_cmd .motor_cmd [i ].mode = 0x01
97+ self .low_cmd .motor_cmd [i ].q = b2 .PosStopF
98+ self .low_cmd .motor_cmd [i ].kp = 0
99+ self .low_cmd .motor_cmd [i ].dq = b2 .VelStopF
100+ self .low_cmd .motor_cmd [i ].kd = 0
101+ self .low_cmd .motor_cmd [i ].tau = 0
102+
103+ def LowStateMessageHandler (self , msg : LowState_ ):
104+ self .low_state = msg
105+
106+ def LowCmdWrite (self ):
107+
108+ if self .firstRun :
109+ for i in range (12 ):
110+ self .startPos [i ] = self .low_state .motor_state [i ].q
111+ self .firstRun = False
112+
113+ self .percent_1 += 1.0 / self .duration_1
114+ self .percent_1 = min (self .percent_1 , 1 )
115+ if self .percent_1 < 1 :
116+ for i in range (12 ):
117+ self .low_cmd .motor_cmd [i ].q = (1 - self .percent_1 ) * self .startPos [i ] + self .percent_1 * self .targetPos_1 [i ]
118+ self .low_cmd .motor_cmd [i ].dq = 0
119+ self .low_cmd .motor_cmd [i ].kp = self .Kp
120+ self .low_cmd .motor_cmd [i ].kd = self .Kd
121+ self .low_cmd .motor_cmd [i ].tau = 0
122+
123+ if (self .percent_1 == 1 ) and (self .percent_2 <= 1 ):
124+ self .percent_2 += 1.0 / self .duration_2
125+ self .percent_2 = min (self .percent_2 , 1 )
126+ for i in range (12 ):
127+ self .low_cmd .motor_cmd [i ].q = (1 - self .percent_2 ) * self .targetPos_1 [i ] + self .percent_2 * self .targetPos_2 [i ]
128+ self .low_cmd .motor_cmd [i ].dq = 0
129+ self .low_cmd .motor_cmd [i ].kp = self .Kp
130+ self .low_cmd .motor_cmd [i ].kd = self .Kd
131+ self .low_cmd .motor_cmd [i ].tau = 0
132+
133+ if (self .percent_1 == 1 ) and (self .percent_2 == 1 ) and (self .percent_3 < 1 ):
134+ self .percent_3 += 1.0 / self .duration_3
135+ self .percent_3 = min (self .percent_3 , 1 )
136+ for i in range (12 ):
137+ self .low_cmd .motor_cmd [i ].q = self .targetPos_2 [i ]
138+ self .low_cmd .motor_cmd [i ].dq = 0
139+ self .low_cmd .motor_cmd [i ].kp = self .Kp
140+ self .low_cmd .motor_cmd [i ].kd = self .Kd
141+ self .low_cmd .motor_cmd [i ].tau = 0
142+
143+ if (self .percent_1 == 1 ) and (self .percent_2 == 1 ) and (self .percent_3 == 1 ) and (self .percent_4 <= 1 ):
144+ self .percent_4 += 1.0 / self .duration_4
145+ self .percent_4 = min (self .percent_4 , 1 )
146+ for i in range (12 ):
147+ self .low_cmd .motor_cmd [i ].q = (1 - self .percent_4 ) * self .targetPos_2 [i ] + self .percent_4 * self .targetPos_3 [i ]
148+ self .low_cmd .motor_cmd [i ].dq = 0
149+ self .low_cmd .motor_cmd [i ].kp = self .Kp
150+ self .low_cmd .motor_cmd [i ].kd = self .Kd
151+ self .low_cmd .motor_cmd [i ].tau = 0
152+
153+ self .low_cmd .crc = self .crc .Crc (self .low_cmd )
154+ self .lowcmd_publisher .Write (self .low_cmd )
155+
156+ if __name__ == '__main__' :
157+
158+ print ("WARNING: Please ensure there are no obstacles around the robot while running this example." )
159+ input ("Press Enter to continue..." )
160+
161+ if len (sys .argv )> 1 :
162+ ChannelFactoryInitialize (0 , sys .argv [1 ])
163+ else :
164+ ChannelFactoryInitialize (0 )
165+
166+ custom = Custom ()
167+ custom .Init ()
168+ custom .Start ()
169+
170+ while True :
171+ if custom .percent_4 == 1.0 :
172+ time .sleep (1 )
173+ print ("Done!" )
174+ sys .exit (- 1 )
175+ time .sleep (1 )
0 commit comments