URL: https://linuxfr.org/forums/programmation-python/posts/extraction-de-donnees-a-partir-du-materiel-phidget Title: Extraction de données à partir du matériel Phidget Authors: Patxi Date: 2017年03月14日T18:52:53+01:00 License: CC By-SA Tags: données, phidget, bridge, décodage et python Score: -1 Bonjour, dans le cadre de projet de fin d'année de prépas scientifique, j'ai créé une plateforme de force composé de 4 capteurs de force relié ensemble par un pont *`**PHIDGETS**`* . Mon objectif est d'extraire les valeurs des capteurs afin d'en calculer le barycentre. Si certains connaissent, il s'agit de l'_Interface Phidget 1046_. Avec ceci était disponible un programme python (plus bas dans le post). _NB_: Il est nécessaire d'installer la bibliothèque Phidgets de Python pour en utiliser les fonctions. bibliothèque disponible ici:` http://www.phidgets.com/docs/Language_-_Python#Installing_the_Phidget_Python_Module` ***Comment extraire les valeurs de l'interface? Comment les transformer sous forme de liste indépendante?*** ```ruby #! /usr/bin/python """Copyright 2011 Phidgets Inc. This work is licensed under the Creative Commons Attribution 2.5 Canada License. To view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/ """ __author__="Adam Stelmack" __version__="2.1.8" __date__ ="14-Jan-2011 2:29:14 PM" #Basic imports import sys from time import sleep #Phidget specific imports from Phidgets.PhidgetException import PhidgetException from Phidgets.Devices.Bridge import Bridge, BridgeGain from Phidgets.Phidget import PhidgetLogLevel #Create an accelerometer object try: bridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Information Display Function def displayDeviceInfo(): print("|------------|----------------------------------|--------------|------------|") print("|- Attached -|- Type -|- Serial No. -|- Version -|") print("|------------|----------------------------------|--------------|------------|") print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (bridge.isAttached(), bridge.getDeviceName(), bridge.getSerialNum(), bridge.getDeviceVersion())) print("|------------|----------------------------------|--------------|------------|") print("Number of bridge inputs: %i" % (bridge.getInputCount())) print("Data Rate Max: %d" % (bridge.getDataRateMax())) print("Data Rate Min: %d" % (bridge.getDataRateMin())) print("Input Value Max: %d" % (bridge.getBridgeMax(0))) print("Input Value Min: %d" % (bridge.getBridgeMin(0))) #Event Handler Callback Functions def BridgeAttached(e): attached = e.device print("Bridge %i Attached!" % (attached.getSerialNum())) def BridgeDetached(e): detached = e.device print("Bridge %i Detached!" % (detached.getSerialNum())) def BridgeError(e): try: source = e.device print("Bridge %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def BridgeData(e): source = e.device print("Bridge %i: Input %i: %f" % (source.getSerialNum(), e.index, e.value)) #Main Program Code try: #logging example, uncomment to generate a log file #bridge.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") bridge.setOnAttachHandler(BridgeAttached) bridge.setOnDetachHandler(BridgeDetached) bridge.setOnErrorhandler(BridgeError) bridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: bridge.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: bridge.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo() try: print("Set data rate to 8ms ...") bridge.setDataRate(16) sleep(2) print("Set Gain to 8...") bridge.setGain(0, BridgeGain.PHIDGET_BRIDGE_GAIN_8) sleep(2) print("Enable the Bridge input for reading data...") bridge.setEnabled(0, True) sleep(2) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) print("Press Enter to quit....") chr = sys.stdin.read(1) print("Closing...") try: print("Disable the Bridge input for reading data...") bridge.setEnabled(0, False) sleep(2) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Done.") exit(0) ``` Lorsqu'on lance le programme (si les capteur sont branché et activé), la console renvoie: ```ruby Opening phidget object.... Waiting for attach.... Bridge 469514 Attached! Bridge 469514: Input 0: -0.019670 Bridge 469514: Input 1: -0.066880 Bridge 469514: Input 2: -0.054000 Bridge 469514: Input 3: 0.072000 |------------|----------------------------------|--------------|------------| |- Attached -|- Type -|- Serial No. -|- Version -| |------------|----------------------------------|--------------|------------| |- True -|- Phidget Bridge 4-input -|- 469514 -|- 102 -| |------------|----------------------------------|--------------|------------| Number of bridge inputs: 4 Data Rate Max: 8 Data Rate Min: 1000 Input Value Max: 124 Input Value Min: -124 Set data rate to 8ms ... Bridge 469514: Input 0: -0.019476 Bridge 469514: Input 1: -0.069260 Bridge 469514: Input 2: -0.052450 Bridge 469514: Input 3: 0.070930 Bridge 469514: Input 0: -0.019759 Bridge 469514: Input 1: -0.069860 Bridge 469514: Input 2: -0.053880 Bridge 469514: Input 3: 0.071050 Bridge 469514: Input 0: -0.019476 Bridge 469514: Input 1: -0.068310 ``` Il semblerait que se soit la fonction BridgeData qui permette l'*affichage* des données. Mais je ne vois pas comment rendre indépendant chaque valeur. :-/ De plus je n'ai jamais utilisé les fonctions du type try: , except as... Si vous êtes en capacité de m'aider vous me sauvez la vie- ****Merci****