Skip to content

Commit 6cf6a18

Browse files
author
yangning wu
committed
add MotorCmds and MotorState idl & add joystick tool & fix typos
1 parent ee32f90 commit 6cf6a18

5 files changed

Lines changed: 301 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Generated by Eclipse Cyclone DDS idlc Python Backend
3+
Cyclone DDS IDL version: v0.11.0
4+
Module: unitree_go.msg.dds_
5+
IDL file: MotorCmds_.idl
6+
7+
"""
8+
9+
from enum import auto
10+
from typing import TYPE_CHECKING, Optional
11+
from dataclasses import dataclass, field
12+
13+
import cyclonedds.idl as idl
14+
import cyclonedds.idl.annotations as annotate
15+
import cyclonedds.idl.types as types
16+
17+
@dataclass
18+
@annotate.final
19+
@annotate.autoid("sequential")
20+
class MotorCmds_(idl.IdlStruct, typename="unitree_go.msg.dds_.MotorCmds_"):
21+
cmds: types.sequence['unitree_dds_wrapper.idl.unitree_go.msg.dds_.MotorCmd_'] = field(default_factory=lambda: [])
22+
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Generated by Eclipse Cyclone DDS idlc Python Backend
3+
Cyclone DDS IDL version: v0.11.0
4+
Module: unitree_go.msg.dds_
5+
IDL file: MotorStates_.idl
6+
7+
"""
8+
9+
from enum import auto
10+
from typing import TYPE_CHECKING, Optional
11+
from dataclasses import dataclass, field
12+
13+
import cyclonedds.idl as idl
14+
import cyclonedds.idl.annotations as annotate
15+
import cyclonedds.idl.types as types
16+
17+
@dataclass
18+
@annotate.final
19+
@annotate.autoid("sequential")
20+
class MotorStates_(idl.IdlStruct, typename="unitree_go.msg.dds_.MotorStates_"):
21+
states: types.sequence['unitree_dds_wrapper.idl.unitree_go.msg.dds_.MotorState_'] = field(default_factory=lambda: [])
22+
23+

unitree_sdk2py/idl/unitree_go/msg/dds_/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from ._LowCmd_ import LowCmd_
1818
from ._LowState_ import LowState_
1919
from ._MotorCmd_ import MotorCmd_
20+
from ._MotorCmds_ import MotorCmds_
2021
from ._MotorState_ import MotorState_
22+
from ._MotorStates_ import MotorStates_
2123
from ._Req_ import Req_
2224
from ._Res_ import Res_
2325
from ._SportModeState_ import SportModeState_
@@ -26,4 +28,4 @@
2628
from ._UwbState_ import UwbState_
2729
from ._UwbSwitch_ import UwbSwitch_
2830
from ._WirelessController_ import WirelessController_
29-
__all__ = ["AudioData_", "BmsCmd_", "BmsState_", "Error_", "Go2FrontVideoData_", "HeightMap_", "IMUState_", "InterfaceConfig_", "LidarState_", "LowCmd_", "LowState_", "MotorCmd_", "MotorState_", "Req_", "Res_", "SportModeState_", "TimeSpec_", "PathPoint_", "UwbState_", "UwbSwitch_", "WirelessController_", ]
31+
__all__ = ["AudioData_", "BmsCmd_", "BmsState_", "Error_", "Go2FrontVideoData_", "HeightMap_", "IMUState_", "InterfaceConfig_", "LidarState_", "LowCmd_", "LowState_", "MotorCmd_", "MotorCmds_", "MotorState_", "MotorStates_", "Req_", "Res_", "SportModeState_", "TimeSpec_", "PathPoint_", "UwbState_", "UwbSwitch_", "WirelessController_", ]

unitree_sdk2py/utils/crc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self):
3131
if platform.machine()=="x86_64":
3232
self.crc_lib = ctypes.CDLL(script_dir + '/lib/crc_amd64.so')
3333
elif platform.machine()=="aarch64":
34-
self.crc_lib = ctypes.CDLL(script_dir + '/lib/crc_arm64.so')
34+
self.crc_lib = ctypes.CDLL(script_dir + '/lib/crc_aarch64.so')
3535

3636
self.crc_lib.crc32_core.argtypes = (ctypes.POINTER(ctypes.c_uint32), ctypes.c_uint32)
3737
self.crc_lib.crc32_core.restype = ctypes.c_uint32

unitree_sdk2py/utils/joystick.py

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
import math
2+
import struct
3+
import os
4+
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" # Disable pygame welcome message
5+
import pygame
6+
import time
7+
8+
class Button:
9+
def __init__(self) -> None:
10+
self.pressed = False
11+
self.on_pressed = False
12+
self.on_released = False
13+
self.data = 0
14+
self.click_count = 0 # 记录连续点击次数
15+
self.last_pressed_time = 0 # 上次按下时间
16+
17+
def __call__(self, data) -> None:
18+
current_time = time.perf_counter()
19+
# print('before',self.data)
20+
21+
self.pressed = (data != 0)
22+
self.on_pressed = self.pressed and self.data == 0
23+
self.on_released = not self.pressed and self.data != 0
24+
25+
# print('after',self.data)
26+
# 处理连续点击
27+
if self.on_pressed:
28+
# print('on_pressed')
29+
# print('on_pressed current_time',current_time)
30+
# print('on_pressed last_pressed_time',self.last_pressed_time)
31+
# print('on_pressed diff',current_time-self.last_pressed_time)
32+
33+
if current_time - self.last_pressed_time <= 0.3: # 0.1 秒以内的连续点击
34+
self.click_count += 1
35+
# print(self.click_count)
36+
else:
37+
self.click_count = 0 # 超过时间间隔,重置计数器
38+
self.last_pressed_time = current_time
39+
self.data = data
40+
41+
def reset_click_count(self):
42+
"""手动重置连续点击计数器"""
43+
self.click_count = 0
44+
45+
class Axis:
46+
def __init__(self) -> None:
47+
self.data = 0.0
48+
self.pressed = False
49+
self.on_pressed = False
50+
self.on_released = False
51+
52+
self.smooth = 0.03
53+
self.deadzone = 0.01
54+
self.threshold = 0.5
55+
56+
def __call__(self, data) -> None:
57+
data_deadzone = 0.0 if math.fabs(data) < self.deadzone else data
58+
new_data = self.data * (1 - self.smooth) + data_deadzone * self.smooth
59+
self.pressed = math.fabs(new_data) > self.threshold
60+
self.on_pressed = self.pressed and math.fabs(self.data) < self.threshold
61+
self.on_released = not self.pressed and math.fabs(self.data) > self.threshold
62+
self.data = new_data
63+
64+
65+
class Joystick:
66+
def __init__(self) -> None:
67+
# Buttons
68+
self.back = Button()
69+
self.start = Button()
70+
# self.LS = Button()
71+
# self.RS = Button()
72+
self.LB = Button()
73+
self.RB = Button()
74+
self.LT = Button()
75+
self.RT = Button()
76+
self.A = Button()
77+
self.B = Button()
78+
self.X = Button()
79+
self.Y = Button()
80+
self.up = Button()
81+
self.down = Button()
82+
self.left = Button()
83+
self.right = Button()
84+
self.F1 = Button()
85+
self.F2 = Button()
86+
87+
# Axes
88+
# self.LT = Axis()
89+
# self.RT = Axis()
90+
self.lx = Axis()
91+
self.ly = Axis()
92+
self.rx = Axis()
93+
self.ry = Axis()
94+
95+
self.last_active_time = time.perf_counter() # 最后一次活动时间
96+
self.inactive_timeout = 0.5 # 超时时间(单位:秒)
97+
def update(self):
98+
"""
99+
Update the current handle key based on the original data
100+
Used to update flag bits such as on_pressed
101+
102+
Examples:
103+
>>> new_A_data = 1
104+
>>> self.A( new_A_data )
105+
"""
106+
pass
107+
108+
def extract(self, wireless_remote):
109+
"""
110+
Extract data from unitree_joystick
111+
wireless_remote: uint8_t[40]
112+
"""
113+
# Buttons
114+
button1 = [int(data) for data in f'{wireless_remote[2]:08b}']
115+
button2 = [int(data) for data in f'{wireless_remote[3]:08b}']
116+
self.LT(button1[2])
117+
self.RT(button1[3])
118+
self.back(button1[4])
119+
self.start(button1[5])
120+
self.LB(button1[6])
121+
self.RB(button1[7])
122+
self.left(button2[0])
123+
self.down(button2[1])
124+
self.right(button2[2])
125+
self.up(button2[3])
126+
self.Y(button2[4])
127+
self.X(button2[5])
128+
self.B(button2[6])
129+
self.A(button2[7])
130+
# Axes
131+
self.lx( struct.unpack('f', bytes(wireless_remote[4:8]))[0] )
132+
self.rx( struct.unpack('f', bytes(wireless_remote[8:12]))[0] )
133+
self.ry( struct.unpack('f', bytes(wireless_remote[12:16]))[0] )
134+
self.ly( struct.unpack('f', bytes(wireless_remote[20:24]))[0] )
135+
136+
137+
# 检查是否有按键按下
138+
if any([
139+
self.LT.pressed, self.RT.pressed, self.back.pressed, self.start.pressed,
140+
self.LB.pressed, self.RB.pressed, self.left.pressed, self.down.pressed,
141+
self.right.pressed, self.up.pressed, self.Y.pressed, self.X.pressed,
142+
self.B.pressed, self.A.pressed
143+
]):
144+
self.last_active_time = time.perf_counter() # 更新最后一次活动时间
145+
elif time.perf_counter() - self.last_active_time > self.inactive_timeout:
146+
# 超过设定的超时时间未按下任何键,重置所有按键的点击计数
147+
self.reset_all_click_counts()
148+
self.last_active_time = time.perf_counter() # 重置最后活动时间
149+
150+
def reset_all_click_counts(self):
151+
"""重置所有按键的连续点击计数器"""
152+
for button in [
153+
self.LT, self.RT, self.back, self.start, self.LB, self.RB,
154+
self.left, self.down, self.right, self.up, self.Y, self.X, self.B, self.A
155+
]:
156+
button.reset_click_count()
157+
158+
def combine(self):
159+
"""
160+
Merge data from Joystick to wireless_remote
161+
"""
162+
# prepare an empty list
163+
wireless_remote = [0 for _ in range(40)]
164+
165+
# Buttons
166+
wireless_remote[2] = int(''.join([f'{key}' for key in [
167+
0, 0, round(self.LT.data), round(self.RT.data),
168+
self.back.data, self.start.data, self.LB.data, self.RB.data,
169+
]]), 2)
170+
wireless_remote[3] = int(''.join([f'{key}' for key in [
171+
self.left.data, self.down.data, self.right.data,
172+
self.up.data, self.Y.data, self.X.data, self.B.data, self.A.data,
173+
]]), 2)
174+
175+
# Axes
176+
sticks = [self.lx.data, self.rx.data, self.ry.data, self.ly.data]
177+
packs = list(map(lambda x: struct.pack('f', x), sticks))
178+
wireless_remote[4:8] = packs[0]
179+
wireless_remote[8:12] = packs[1]
180+
wireless_remote[12:16] = packs[2]
181+
wireless_remote[20:24] = packs[3]
182+
return wireless_remote
183+
184+
class PyGameJoystick(Joystick):
185+
def __init__(self) -> None:
186+
super().__init__()
187+
188+
pygame.init()
189+
pygame.joystick.init()
190+
if pygame.joystick.get_count() <= 0:
191+
raise Exception("No joystick found!")
192+
193+
self._joystick = pygame.joystick.Joystick(0)
194+
self._joystick.init()
195+
196+
def print(self):
197+
print("\naxes: ")
198+
for i in range(self._joystick.get_numaxes()):
199+
print(self._joystick.get_axis(i), end=" ")
200+
print("\nbuttons: ")
201+
for i in range(self._joystick.get_numbuttons()):
202+
print(self._joystick.get_button(i), end=" ")
203+
print("\nhats: ")
204+
for i in range(self._joystick.get_numhats()):
205+
print(self._joystick.get_hat(i), end=" ")
206+
print("\nballs: ")
207+
for i in range(self._joystick.get_numballs()):
208+
print(self._joystick.get_ball(i), end=" ")
209+
print("\n")
210+
211+
class LogicJoystick(PyGameJoystick):
212+
""" Logic F710 """
213+
def __init__(self) -> None:
214+
super().__init__()
215+
216+
def update(self):
217+
pygame.event.pump()
218+
219+
self.back(self._joystick.get_button(6))
220+
self.start(self._joystick.get_button(7))
221+
self.LS(self._joystick.get_button(9))
222+
self.RS(self._joystick.get_button(10))
223+
self.LB(self._joystick.get_button(4))
224+
self.RB(self._joystick.get_button(5))
225+
self.A(self._joystick.get_button(0))
226+
self.B(self._joystick.get_button(1))
227+
self.X(self._joystick.get_button(2))
228+
self.Y(self._joystick.get_button(3))
229+
230+
self.LT((self._joystick.get_axis(2) + 1)/2)
231+
self.RT((self._joystick.get_axis(5) + 1)/2)
232+
self.rx(self._joystick.get_axis(3))
233+
self.ry(-self._joystick.get_axis(4))
234+
235+
236+
# Logitech controller has 2 modes
237+
# mode 1: light down
238+
self.up(1 if self._joystick.get_hat(0)[1] > 0.5 else 0)
239+
self.down(1 if self._joystick.get_hat(0)[1] < -0.5 else 0)
240+
self.left(1 if self._joystick.get_hat(0)[0] < -0.5 else 0)
241+
self.right(1 if self._joystick.get_hat(0)[0] > 0.5 else 0)
242+
self.lx(self._joystick.get_axis(0))
243+
self.ly(-self._joystick.get_axis(1))
244+
# mode 2: light up
245+
# self.up(1 if self._joystick.get_axis(1) < -0.5 else 0)
246+
# self.down(1 if self._joystick.get_axis(0) > 0.5 else 0)
247+
# self.left(1 if self._joystick.get_axis(0) < -0.5 else 0)
248+
# self.right(1 if self._joystick.get_axis(0) > 0.5 else 0)
249+
# self.lx(self._joystick.get_hat(0)[1])
250+
# self.ly(self._joystick.get_hat(0)[1])
251+

0 commit comments

Comments
 (0)