def attach_stepper(self, id): try: ch = Stepper() ch.setOnAttachHandler(self.StepperAttached) ch.setOnDetachHandler(self.StepperDetached) ch.setOnErrorHandler(self.ErrorEvent) ch.setOnPositionChangeHandler(self.PositionChangeHandler) ch.setDeviceSerialNumber(id) print(id,": Waiting for the Phidget Stepper Object to be attached...") ch.openWaitForAttachment(5000) except PhidgetException as e: print(id, ": Phidget Exception %i: %s" % (e.code, e.details)) exit(1) print("Engaging the motor %d" % id) ch.setEngaged(1) print("Created motor %d" %id) print("Acceleration Range [%d,%d] : %d" % (ch.getMinAcceleration(), ch.getMaxAcceleration(), ch.getAcceleration())) print("Velocity Range [%d,%d] : %d" % (ch.getMinVelocityLimit(), ch.getMaxVelocityLimit(), ch.getVelocityLimit())) #print("Position Range [%d,%d] : %d" % (ch.getMinPosition(), ch.getMaxPosition(), ch.getPosition())) print("CurrentLimit Range [%d,%d] : %d" % (ch.getMinCurrentLimit(), ch.getMaxCurrentLimit(), ch.getCurrentLimit())) ch.setCurrentLimit(3) return ch
class Positioner: def __init__(self, stepper_sn, debug=False): self.debug = debug try: self.channel = Stepper() except Exception as e: print('\x1b[1;31mPhidget::SN::{}\x1b[0m <> {}'.format( stepper_sn, e)) try: self.channel.setDeviceSerialNumber(stepper_sn) self.channel.setIsHubPortDevice(False) self.channel.setChannel(0) self.channel.setOnAttachHandler(PhidgetHandlers.on_attach_handler) self.channel.setOnDetachHandler(PhidgetHandlers.on_detach_handler) self.channel.setOnErrorHandler(PhidgetHandlers.on_error_handler) self.channel.setOnPositionChangeHandler( PhidgetHandlers.on_position_change_handler) self.channel.openWaitForAttachment(10000) except PhidgetException as e: print('\x1b[1;31mPhidget::SN::{}\x1b[0m <> {}'.format( stepper_sn, e)) def __del__(self): self.close() def _get_device_str(self): return str('\x1b[1;34mPhidget::SN::{}\x1b[0m'.format( self.channel.getDeviceSerialNumber())) def close(self): print('\x1b[1;34mPhidget::SN::{}\x1b[0m > Closing channel {}'.format( self.channel.getDeviceSerialNumber(), self.channel.getChannel())) self.channel.close() def set_acceleration(self, acc): self.channel.setAcceleration(acc) def set_target_absolute_position(self, position): # One full rotation is 2 mm movement along the rail shaft_conversion = 1 / 2 self.channel.setTargetPosition(position * shaft_conversion) def wait_to_settle(self): is_moving = self.channel.getIsMoving() while is_moving: time.sleep(0.01) is_moving = self.channel.getIsMoving() position = self.channel.getPosition() print( '\x1b[1;34mPhidget::SN::{}\x1b[0m < Position {:8.4f}\t<=>\t{:8.4f} mm' .format(self.channel.getDeviceSerialNumber(), position, position * 2)) return is_moving def set_velocity_limit(self, v_lim): self.channel.setVelocityLimit(v_lim) def print_movement_info(self): print('\x1b[1;34mPhidget::SN::{}\x1b[0m < minAcceleration {:8.4f}'. format(self.channel.getDeviceSerialNumber(), self.channel.getMinAcceleration())) print('\x1b[1;34mPhidget::SN::{}\x1b[0m < maxAcceleration {:8.4f}'. format(self.channel.getDeviceSerialNumber(), self.channel.getMaxAcceleration())) print('\x1b[1;34mPhidget::SN::{}\x1b[0m < Acceleration {:8.4f}'.format( self.channel.getDeviceSerialNumber(), self.channel.getAcceleration())) print( '\x1b[1;34mPhidget::SN::{}\x1b[0m < velocityLimit {:8.4f}'.format( self.channel.getDeviceSerialNumber(), self.channel.getVelocityLimit()))