servo_serial_com.py
1.34 KB
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
# -*- coding: utf-8 -*-
"""
author Benoit Dubois
brief Subclass of SerialCom: add specific decoding message capability
for servo application.
"""
import serial_com
MNEMO_LIST = ["kp", "ki", "sp", "osp", "imax", "omax", "oscale", "fs"]
class ServoSerialCom(serial_com.SerialCom):
@staticmethod
def decode_message(msg):
"""Message can be of type request or command.
Request message are made of a mnemonique followed by '?'.
Command message are made of 3 parts:
- a mnemonique,
- the controller number,
- the value to be applied.
Each parts are separated with a space.
"""
if msg[:-1] == '?': # Request
return
else:
cmd = msg.split()
if len(cmd) != 3:
print("Decode error: bad command format")
return
mnemo = str(cmd[0])
if mnemo not in MNEMO_LIST:
print("Decode error: bad mnemonique")
return
num = cmd[1]
if num != '0' and num != '1':
print("Decode error: bad controller number")
return
try:
val = int(cmd[2])
except ValueError:
print("Decode error: bad command value")
return
return (mnemo, num, val)