print("bas") elif event.key == pygame.K_UP: modepygame = 3 elif event.key == pygame.K_LEFT: modepygame = 1 elif event.key == pygame.K_RIGHT: modepygame = 2 elif event.key == pygame.K_z: modepygame = 0 print("ZERO") position = tm.encoder_estimates.position elif event.key == pygame.K_c: modepygame = 0 print("CALIBRATION") tm.set_state(state=0, mode=0) tm.calibrate() elif event.key == pygame.K_r: print("RESET") tm.reset() elif event.key == pygame.K_SPACE: print("EMERGENCY STOP") modepygame = 0 elif event.key == pygame.K_p: sign = 1 modepygame = 4 count = 0 elif event.key == pygame.K_a: step += 1 * deg step = minimum(maximum(step, 0), 50) print("step:{:.0f}".format(step)) elif event.key == pygame.K_q:
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
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)))