-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMotorCommand.py
53 lines (38 loc) · 1.04 KB
/
MotorCommand.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
# -*- coding: utf-8 -*-
import struct
class MotorCommand(object):
_struct = struct.Struct('<BBhhhh')
def __init__(self):
self.id = 0
self.cmd = 0
self.pref = 0
self.P = 0
self.I = 0
self.D = 0
def serialize(self, buff):
#try
buff.write(MotorCommand._struct.pack(self.id, self.cmd, self.pref, self.P, self.I, self.D))
#except struct.error as se:
# raise SerializationError('Error in serialization %s' % (self.__str__))
def fromTuple(self, data):
self.id = data.id
self.cmd = data.cmd
self.pref = data.pref
self.P = data.P
self.I = data.I
self.D = data.D
def to_hex(data):
return ":".join("{:02x}".format(c) for c in data)
def test():
from io import BytesIO
buff = BytesIO()
CMD = MotorCommand()
CMD.id = ord('A')
CMD.cmd = 1
CMD.pref = 0
CMD.tref = 10
CMD.serialize(buff)
print(to_hex(buff.getvalue()))
print(buff.getvalue())
if __name__ == '__main__':
test()