def run(self): def PositionChangeHandler(self, positionChange, timeChange, indexTriggered): global prevTime currentTime = time.perf_counter() ## print(str((currentTime-prevTime)*1000)+" "+str(positionChange/timeChange/360/4*1000))## if ("y" in fileSave) or ("Y" in fileSave): ## f.write(str(positionChange/timeChange/360/4*1000)+"\r\n") f.write(str((currentTime - prevTime) * 1000) + "\r\n") avVel = positionChange prevTime = currentTime def EncoderAttached(e): try: attached = e print("\nEncoder Attached") print("\n") except PhidgetException as e: print("Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) def EncoderDetached(e): detached = e try: print("\nEncoder Detached") except PhidgetException as e: print("Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) def ErrorEvent(e, eCode, description): print("Error %i : %s" % (eCode, description)) en = Encoder() while not stopped: if not en.getAttached(): try: en.setOnAttachHandler(EncoderAttached) en.setOnDetachHandler(EncoderDetached) en.setOnErrorHandler(ErrorEvent) en.setOnPositionChangeHandler(PositionChangeHandler) print("\nWaiting for encoder to attach") en.openWaitForAttachment(5000) if (not en.getEnabled()): en.setEnabled(1) except PhidgetException as e: print("Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit\n") readin = sys.stdin.read(1) exit(1) en.setDataInterval(encoderDataRate) en.close()
def main(): #Create your Phidget channels encoder0 = Encoder() #Set addressing parameters to specify which channel to open (if any) #Assign any event handlers you need before calling open so that no events are missed. encoder0.setOnPositionChangeHandler(onPositionChange) #Open your Phidgets and wait for attachment encoder0.openWaitForAttachment(5000) #Do stuff with your Phidgets here or in your event handlers try: input("Press Enter to Stop\n") except (Exception, KeyboardInterrupt): pass #Close your Phidgets once the program is done. encoder0.close()
def main(): # Make sure current path is this file path abspath = os.path.abspath(__file__) dname = os.path.dirname(abspath) os.chdir(dname) ############ #import config file config = ConfigParser.ConfigParser() print("opening configuration file : config.cfg") config.read('config.cfg') ############ #connect to mqtt broker client = MQTT_client.createClient("Encoder", config) ############ #connection to Phidget encoder and wait for measures #publish the datas on config/MQTT/topic try: Log.enable(LogLevel.PHIDGET_LOG_INFO, "phidgetlog.log") #Create your Phidget channels encoder0 = Encoder() #Set addressing parameters to specify encoder0.client = client encoder0.clientTopic = config.get('MQTT', 'topic_publish') encoder0.printLog = config.getboolean('encoder', 'printLog') encoder0.chooseDataInterval = config.getint('encoder', 'dataInterval') #Assign any event handlers you need before calling open so that no events are missed. encoder0.setOnPositionChangeHandler(handler.onPositionChange) encoder0.setOnAttachHandler(handler.onAttach) encoder0.setOnDetachHandler(handler.onDetach) #Open your Phidgets and wait for attachment encoder0.openWaitForAttachment(5000) #Do stuff with your Phidgets here or in your event handlers. #Change the data interval from the encoder based on config datas # encoder0.setDataInterval(config.getint('encoder','dataInterval')) #Interupt script by pressing Enter try: input("Press Enter to Stop\n") except (Exception, KeyboardInterrupt): pass #Close your Phidgets once the program is done. encoder0.close() except PhidgetException as ex: #We will catch Phidget Exceptions here, and print the error informaiton. traceback.print_exc() print("") print("PhidgetException " + str(ex.code) + " (" + ex.description + "): " + ex.details) finally: encoder0.close()
#print("Position: %f\n" % motorControl.getMotorCount()) print("Position Changed: %7d %7.3lf %d\n" % (positionChange, timeChange, indexTriggered)) try: motorControl.setOnAttachHandler(ObjectAttached) motorControl.setOnDetachHandler(ObjectDetached) motorControl.setOnErrorHandler(ErrorEvent) motorControl.setOnVelocityUpdateHandler(VelocityUpdateHandler) enc.setOnAttachHandler(ObjectAttached) enc.setOnDetachHandler(ObjectDetached) enc.setOnErrorHandler(ErrorEvent) enc.setOnPositionChangeHandler(PositionChangeHandler) print("Waiting for the Phidget DCMotor Object to be attached...") motorControl.openWaitForAttachment(5000) enc.openWaitForAttachment(5000) except PhidgetException as e: motorControl.setTargetVelocity(0) print("Phidget Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) if(not enc.getEnabled()): enc.setEnabled(1)