def AttachServo(databasepath, serialNumber): def onAttachHandler(event): logString = "Servo Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Servo Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Servo Error " + str(event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Servo Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Servo Server Disconnect " + str(event.device.getSerialNum()) #print(logString) def positionChangeHandler(event): logString = "Servo Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO SERVO_POSITIONCHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.position)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = Servo() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnPositionChangeHandler(positionChangeHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
class PhidgetMovement: def __init__(self,serial): self.servo = Servo() self.servo.openPhidget(serial) print "connecting to servo: " + str(serial) self.servo.waitForAttach(10000) def setValue(self,value): self.servo.setPosition(0,value)
#Main Program Code try: servo.setOnAttachHandler(ServoAttached) servo.setOnDetachHandler(ServoDetached) servo.setOnErrorhandler(ServoError) servo.setOnPositionChangeHandler(ServoPositionChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: servo.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: servo.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: servo.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
#logging example, uncomment to generate a log file #servo.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") servo.setOnAttachHandler(ServoAttached) servo.setOnDetachHandler(ServoDetached) servo.setOnErrorhandler(ServoError) servo.setOnPositionChangeHandler(ServoPositionChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: servo.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: servo.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: servo.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
def AttachServo(databasepath, serialNumber): def onAttachHandler(event): logString = "Servo Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Servo Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Servo Error " + str( event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Servo Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Servo Server Disconnect " + str( event.device.getSerialNum()) #print(logString) def positionChangeHandler(event): logString = "Servo Changed " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO SERVO_POSITIONCHANGE VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.position)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = Servo() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnPositionChangeHandler(positionChangeHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)