servo_serial_com.py 1.34 KB
# -*- 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)