def openBridge(serialNum): # Create a bridge object try: bridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: 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("Calibration Script.\nOpening phidget object....") try: bridge.openPhidget(serialNum) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: bridge.waitForAttach(20000) print("Set data rate to %i ms ..." % (rate)) bridge.setDataRate(rate) sleep(1) print("Set Gain to %s..." % str(gainTable[gain])) setGainAllChanels(bridge, gain) sleep(1) 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(bridge) return bridge
def openBridge(serialNum): #Create a bridge object try: bridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: 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("Calibration Script.\nOpening phidget object....") try: bridge.openPhidget(serialNum) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: bridge.waitForAttach(20000) print("Set data rate to %i ms ..." % (rate)) bridge.setDataRate(rate) sleep(1) print("Set Gain to %s..." % str(gainTable[gain])) setGainAllChanels(bridge,gain) sleep(1) 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(bridge) return bridge
class PhidgetBridge(): def __init__(self): self.bridge = Bridge() self.bridge.setOnAttachHandler(BridgeAttached) self.bridge.setOnDetachHandler(BridgeDetached) self.bridge.setOnErrorhandler(BridgeError) self.bridge.setOnBridgeDataHandler(BridgeData) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self,waitTimeMS): self.bridge.openPhidget() try: self.bridge.waitForAttach(waitTimeMS) time.sleep(0.2) self.bridge.setDataRate(8) time.sleep(0.2) self.bridge.setGain(0, BridgeGain.PHIDGET_BRIDGE_GAIN_8) time.sleep(0.2) self.bridge.setEnabled(0, True) time.sleep(0.2) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): global data_to_be_sent_bridge global times_to_be_sent_bridge #return the data and times and update the lists data_to_return = [data_to_be_sent_bridge,times_to_be_sent_bridge] data_to_be_sent_bridge = [] times_to_be_sent_bridge = [] return data_to_return def close(self): self.bridge.setEnabled(0,False) time.sleep(2) self.bridge.closePhidget()
class PhidgetBridge(): def __init__(self): self.bridge = Bridge() self.bridge.setOnAttachHandler(BridgeAttached) self.bridge.setOnDetachHandler(BridgeDetached) self.bridge.setOnErrorhandler(BridgeError) self.bridge.setOnBridgeDataHandler(BridgeData) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self, waitTimeMS): self.bridge.openPhidget() try: self.bridge.waitForAttach(waitTimeMS) time.sleep(0.2) self.bridge.setDataRate(8) time.sleep(0.2) self.bridge.setGain(0, BridgeGain.PHIDGET_BRIDGE_GAIN_8) time.sleep(0.2) self.bridge.setEnabled(0, True) time.sleep(0.2) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): global data_to_be_sent_bridge global times_to_be_sent_bridge #return the data and times and update the lists data_to_return = [data_to_be_sent_bridge, times_to_be_sent_bridge] data_to_be_sent_bridge = [] times_to_be_sent_bridge = [] return data_to_return def close(self): self.bridge.setEnabled(0, False) time.sleep(2) self.bridge.closePhidget()
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\n" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo() try: print("Set data rate to 40ms ...")
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 ...")
axis1.set_title("Force over Time") yellowLevel = 60 redLevel = 80 yellowLine = axis1.axhline(yellowLevel, color='y', linestyle='-') # sets yellow horizontal line redLine = axis1.axhline(redLevel, color='r', linestyle='-') # sets red horizontal line axis1.grid(color='gray', linestyle='-', linewidth=.25) # sets up the grid axis1.set_ylabel("Force (lbs)") axis1.set_xlabel("Time (s)") line1, = axis1.plot([], [], 'k', lw=3) # creates a line on the axis with no datapoints and width of 2 print("Opening phidget object....") bridge = Bridge() # Creates PhidgetBridge object bridge.openPhidget() # Opens Phidgetbridge bridge.waitForAttach(10000) # waits 10000 ms (10s) for bridge to connect # variable initializations xData = [] yData = [] startTime = time.time() maxForce = 0 imagePos = 0 pause = True force_text = axis1.text(0.5, 125, "0", fontsize=25) max_text = axis1.text(0.5, 110, "0", fontsize=25) time.sleep(1) displayDeviceInfo() forceSensorOffset = -bridge.getBridgeValue(1)
except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) try: tempBridge.openPhidget(serial) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) if options.verbose: print("Waiting for attach....") try: tempBridge.waitForAttach(20000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: tempBridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: lBridges.append(tempBridge) # if options.verbose: displayDeviceInfo(lBridges[len(lBridges)-1]) sleep(1) #Configure settings on each bridge
def __init__(self): # 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("|------------|----------------------------------|--------------|------------|") # sets data rate (number of ms between data collection) # must be a multiple of 8 ms (125 Hz to 1 Hz) bridge.setDataRate(24) time.sleep(.2) print("Data rate set to: ", bridge.getDataRate(), "ms ", 1 / (bridge.getDataRate() * 0.001), " Hz") # Gain Resoluion Range # 1 119 nV/V 1000 mV/V # 8 14.9 nV/V 125 mV/V # 16 7.45 nV/V 62.5 mV/V # 32 3.72 nV/V 31.25 mV/V # 64 1.86 nV/V 15.625 mV/V # 128 0.92 nV/V 7.8125 mV/V # Force sensor maxes out around 4 mV/V so set gain to 128 bridge.setGain(1, BridgeGain.PHIDGET_BRIDGE_GAIN_128) time.sleep(.2) print("Gain set to: ", bridge.getGain(1)) # Sensor hooked up to input 1 on the PhidgetBridge print("Enabling Bridge input 1 for reading data...") bridge.setEnabled(1, True) time.sleep(.2) # defines initial parameters def init(): ax.set_ylim(0, 150) ax.set_xlim(0, 10) del xdata[:] del ydata[:] line.set_data(xdata, ydata) force_text.set_text('') return line, force_text # update the data def run(maxForce): # calculates seconds since program started running t = time.time() - startTime # converts bridge voltage output to lbs y = (bridge.getBridgeValue(1) + 0.007) * 74.3 # lbs/mV # adds this data onto the xdata and y data xdata.append(t) ydata.append(y) if len(ydata) > 30: ydata[len(ydata)-1] = (np.mean(ydata[(len(ydata) - 5):])) # updates plot title with current force* in lbs plt.title("%.1f" % np.mean(ydata[(len(ydata) - 10):])) # "%.1f" %" -- formats following number as a float with 1 value after the decimal # np.mean() -- takes the mean # len(ydata) -- computes the length of ydata # ydata[(len(ydata)-10):] -- takes the last ten values of ydata (similar to MATLAB colon notation) # So really avergaing the last 10 values (0.24s) worth of data currentForce = np.mean(ydata[(len(ydata) - 10):]) force_text.set_text("Current Force: %.1f" % currentForce + " lbs") force_text.set_size(30) if currentForce > maxForce: maxForce = currentForce max_text.set_text("Max Force: %.1f" % np.max(ydata) + " lbs") max_text.set_size(30) a, b = force_text.get_position() c, d = max_text.get_position() # calculates current bounds on the x-axis xmin, xmax = ax.get_xlim() # if the data is within 2 second of the end of the graph if t >= xmax - 2: ax.set_xlim(t-8, t+2) # increment the x-axis force_text.set_position((t-7.5, b)) max_text.set_position((t-7.5, d)) ax.figure.canvas.draw() # redraw the graph line.set_data(xdata, ydata) # update x and y values return line print("Opening phidget object....") # Creates bridge object bridge = Bridge() # Opens phidget bridge.openPhidget() # waits 10000 ms (10s) for bridge to connect bridge.waitForAttach(10000) displayDeviceInfo() # sets up plot fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) ax.grid() xdata, ydata = [], [] startTime = time.time() time_template = 'time = %.1fs' force_text = plt.text(0.5, 125, "0") max_text = plt.text(0.5, 110, "0") # animates the graph ani = animation.FuncAnimation(fig, run, blit=False, interval=10, repeat=False, init_func=init) plt.show()