Beispiel #1
0
class UserWrapper:
    '''
    This is a user-friendly wrapper to the Tinymover class, that
    allows more intuitive functions to be used instead of plain
    getters/setters.
    This class sacrifices Pythonic-ness (e.g. raising exceptions)
    for better user-friendliness (e.g. using simple print statements).
    There are also additional checks performed prior to changing
    states, with errors displayed if checks fail.
    '''
    def __init__(self, **kwargs):
        self.tinymovr = Tinymovr(**kwargs)

    def __getattr__(self, attr):
        return getattr(self.tinymovr, attr)

    def calibrate(self):
        '''
        Enter calibration state, perform motor and encoder calibration
        '''
        state = self.tinymovr.state
        if state.error != 0:
            print("Error flag present, cannot continue with calibration. \
Please reset Tinymovr.")
        elif state.state != 0:
            print("Tinymovr state is not idle, \
calibration needs to be started from idle state.")
        else:
            input("Ready to calibrate. Please remove any loads \
from the motor and hit Enter to continue")
            self.tinymovr.calibrate()

    def idle(self):
        '''
        Enter idle state
        '''
        self.tinymovr.set_state(0)

    def position_control(self):
        '''
        Enter closed loop control state and position control mode
        '''
        state = self.tinymovr.state
        if state.error != 0:
            print("Error flag present, cannot enable position control. \
Please reset Tinymovr.")
        elif state.state == 1:
            print("Tinymovr is currently calibrating, \
please do not interrupt.")
        else:
            self.tinymovr.position_control()

    def velocity_control(self):
        '''
        Enter closed loop control state and velocity control mode
        '''
        state = self.tinymovr.state
        if state.error != 0:
            print("Error flag present, cannot enable velocity control. \
Please reset Tinymovr.")
        elif state.state == 1:
            print("Tinymovr is currently calibrating, \
please do not interrupt.")
        else:
            self.tinymovr.velocity_control()

    def current_control(self):
        '''
        Enter closed loop control state and current control mode
        '''
        state = self.tinymovr.state
        if state.error != 0:
            print("Error flag present, cannot enable current control. \
Please reset Tinymovr.")
        elif state.state == 1:
            print("Tinymovr is currently calibrating, \
please do not interrupt.")
        else:
            self.tinymovr.current_control()

    @property
    def error(self):
        '''
        Report controller error in human-readable form
        '''
        state = self.tinymovr.state
        error_id = ErrorIDs(state.error)
        print(error_descriptions[error_id] + " (error code: " + str(error_id) +
              ")")

    def __dir__(self):
        tm_keys = self.tinymovr.__dir__()
        self_keys = object.__dir__(self)
        self_keys.remove("tinymovr")
        return tm_keys + self_keys
Beispiel #2
0
class UserWrapper:
    """
    This is a user-friendly wrapper to the Tinymover class, that
    allows more intuitive functions to be used instead of plain
    getters/setters.
    This class sacrifices Pythonic-ness (e.g. raising exceptions)
    for better user-friendliness (e.g. using simple print statements).
    There are also additional checks performed prior to changing
    states, with errors displayed if checks fail.
    """
    def __init__(self, *args, **kwargs):
        self.tinymovr = Tinymovr(*args, **kwargs)

    def __getattr__(self, attr):
        return getattr(self.tinymovr, attr)

    def calibrate(self):
        """
        Enter calibration state, perform motor and encoder calibration
        """
        state = self.tinymovr.state
        if state.errors:
            print(
                "Error flag present, cannot continue with calibration. Please reset Tinymovr."
            )
        elif state.state != ControlStates.Idle:
            print("Tinymovr not in idle state, calibration cannot start.")
        else:
            input(
                "Ready to calibrate. Please remove any loads from the motor and hit Enter to continue."
            )
            self.tinymovr.calibrate()

    def idle(self):
        """
        Enter idle state
        """
        if self.tinymovr.state.state == ControlStates.Calibration:
            print(msg_calibrating)
        else:
            self.tinymovr.idle()

    def position_control(self):
        """
        Enter closed loop control state and position control mode
        """
        state = self.tinymovr.state
        if state.errors:
            print(
                "Error flag present, cannot enable position control. Please reset Tinymovr."
            )
        elif state.state == ControlStates.Calibration:
            print(msg_calibrating)
        else:
            self.tinymovr.position_control()

    def velocity_control(self):
        """
        Enter closed loop control state and velocity control mode
        """
        state = self.tinymovr.state
        if state.errors:
            print(
                "Error flag present, cannot enable velocity control. Please reset Tinymovr."
            )
        elif state.state == ControlStates.Calibration:
            print(msg_calibrating)
        else:
            self.tinymovr.velocity_control()

    def current_control(self):
        """
        Enter closed loop control state and current control mode
        """
        state = self.tinymovr.state
        if state.errors:
            print(
                "Error flag present, cannot enable current control. Please reset Tinymovr."
            )
        elif state.state == ControlStates.Calibration:
            print(msg_calibrating)
        else:
            self.tinymovr.current_control()

    def __dir__(self):
        tm_attrs = self.tinymovr.__dir__()
        self_attrs = [k for k in object.__dir__(self) if not k.startswith("_")]
        self_attrs.remove("tinymovr")
        return sorted(list(set(tm_attrs + self_attrs)))