Skip to content

Commit 463c1fc

Browse files
author
yangning wu
committed
Improve crc efficiency use ctypes
1 parent 58c3f62 commit 463c1fc

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

example/test_crc.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import time
2+
import sys
3+
4+
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
5+
from unitree_sdk2py.idl.default import unitree_go_msg_dds__LowCmd_
6+
from unitree_sdk2py.utils.crc import CRC
7+
8+
crc = CRC()
9+
10+
PosStopF = 2.146e9
11+
VelStopF = 16000.0
12+
13+
if __name__ == "__main__":
14+
ChannelFactoryInitialize(0)
15+
16+
cmd = unitree_go_msg_dds__LowCmd_()
17+
cmd.head[0] = 0xFE
18+
cmd.head[1] = 0xEF
19+
cmd.level_flag = 0xFF
20+
cmd.gpio = 0
21+
for i in range(20):
22+
cmd.motor_cmd[i].mode = 0x01
23+
cmd.motor_cmd[i].q = PosStopF
24+
cmd.motor_cmd[i].kp = 0
25+
cmd.motor_cmd[i].dq = VelStopF
26+
cmd.motor_cmd[i].kd = 0
27+
cmd.motor_cmd[i].tau = 0
28+
29+
cmd.motor_cmd[0].q = 0.0
30+
cmd.motor_cmd[0].kp = 0.0
31+
cmd.motor_cmd[0].dq = 0.0
32+
cmd.motor_cmd[0].kd = 0.0
33+
cmd.motor_cmd[0].tau = 1.0
34+
35+
cmd.motor_cmd[1].q = 0.0
36+
cmd.motor_cmd[1].kp = 10.0
37+
cmd.motor_cmd[1].dq = 0.0
38+
cmd.motor_cmd[1].kd = 1.0
39+
cmd.motor_cmd[1].tau = 0.0
40+
41+
now = time.perf_counter()
42+
43+
44+
cmd.crc = crc.Crc(cmd)
45+
46+
print("CRC:", cmd.crc, "Time cost:", (time.perf_counter() - now)*1000)

unitree_sdk2py/utils/crc.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
from ..idl.unitree_hg.msg.dds_ import LowCmd_ as HGLowCmd_
1010
from ..idl.unitree_hg.msg.dds_ import LowState_ as HGLowState_
11+
import ctypes
12+
import os
13+
import platform
1114

1215
class CRC(Singleton):
1316
def __init__(self):
@@ -21,6 +24,18 @@ def __init__(self):
2124
#size 2092
2225
self.__packFmtHGLowState = '<2I2B2xI' + '13fh2x' + 'B3x4f2hf7I' * 35 + '40B5I'
2326

27+
28+
script_dir = os.path.dirname(os.path.abspath(__file__))
29+
self.platform = platform.system()
30+
if self.platform == "Linux":
31+
if platform.machine()=="x86_64":
32+
self.crc_lib = ctypes.CDLL(script_dir + '/lib/crc_amd64.so')
33+
elif platform.machine()=="aarch64":
34+
self.crc_lib = ctypes.CDLL(script_dir + '/lib/crc_arm64.so')
35+
36+
self.crc_lib.crc32_core.argtypes = (ctypes.POINTER(ctypes.c_uint32), ctypes.c_uint32)
37+
self.crc_lib.crc32_core.restype = ctypes.c_uint32
38+
2439
def Crc(self, msg: idl.IdlStruct):
2540
if msg.__idl_typename__ == 'unitree_go.msg.dds_.LowCmd_':
2641
return self.__Crc32(self.__PackLowCmd(msg))
@@ -177,7 +192,7 @@ def __Trans(self, packData):
177192

178193
return calcData
179194

180-
def __Crc32(self, data):
195+
def _crc_py(self, data):
181196
bit = 0
182197
crc = 0xFFFFFFFF
183198
polynomial = 0x04c11db7
@@ -199,3 +214,15 @@ def __Crc32(self, data):
199214
bit >>= 1
200215

201216
return crc
217+
218+
def _crc_ctypes(self, data):
219+
uint32_array = (ctypes.c_uint32 * len(data))(*data)
220+
length = len(data)
221+
crc=self.crc_lib.crc32_core(uint32_array, length)
222+
return crc
223+
224+
def __Crc32(self, data):
225+
if self.platform == "Linux":
226+
return self._crc_ctypes(data)
227+
else:
228+
return self._crc_py(data)
7.74 KB
Binary file not shown.
14.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)