class StrainSensor(Sensor): '''Class used to handle load cell type sensors. Extends Sensor class ''' def __init__(self, deviceSN, channelNo, dataInterval, refreshPeriod, sensorName=None): ''' Constructor for strain sensor phidgets. Takes standard Sensor arguments ''' self.channelNo = channelNo self.sensorUnits = "Kg" self.useCallibration = False self.gradient = 1 self.intercept = 0 Sensor.__init__(self, deviceSN, dataInterval, refreshPeriod, sensorName) def attachSensor(self): ''' Connects the strain sensor to the application ''' self.channel = VoltageRatioInput() self.channel.setDeviceSerialNumber(self.deviceSN) self.channel.setChannel(self.channelNo) self.channel.openWaitForAttachment(100) print("\n***** {} Sensor Attached *****".format(self.sensorName)) self.attached = True self.channel.setDataInterval(self.dataInterval) self.channel.setBridgeGain(0x8) def activateDataListener(self): ''' Sets up the event which triggers when the sensor updates its utput values ''' self.startTime = time.time() def onSensorValueChange(channelObject, voltageRatio): rawTime = time.time() deltaTime = rawTime - self.startTime if self.useCallibration: voltageRatio = voltageRatio * self.gradient + self.intercept self.dataQ.put([voltageRatio, deltaTime, rawTime]) self.channel.setOnVoltageRatioChangeHandler(onSensorValueChange) def setCallibration(self, gradient, intercept): ''' Used to give the sensor callibration values. ''' self.gradient = gradient self.intercept = intercept self.useCallibration = True
class VoltageRatioSensor(Sensor): '''Class used to handle any sensor which uses a ratiometric voltage input ''' def __init__(self, deviceSN, channelNo, dataInterval, refreshPeriod, sensorType, sensorName=None): ''' Connects the sensor to the application ''' self.channelNo = channelNo self.sensorType = sensorType self.sensorUnits = None Sensor.__init__(self, deviceSN, dataInterval, refreshPeriod, sensorName) def attachSensor(self): ''' Connects the sensor to the application ''' self.channel = VoltageRatioInput() self.channel.setDeviceSerialNumber(self.deviceSN) self.channel.setChannel(self.channelNo) self.channel.openWaitForAttachment(100) print("\n***** {} Sensor Attached *****".format(self.sensorName)) self.attached = True self.channel.setSensorType(self.sensorType) self.channel.setDataInterval(self.dataInterval) self.sensorUnits = self.channel.getSensorUnit().symbol def activateDataListener(self): ''' Sets up the event which triggers when the sensor updates its outputs ''' self.startTime = time.time() def onSensorValueChange(channelObject, sensorVlue, sensorUnit): rawTime = time.time() deltaTime = rawTime - self.startTime self.dataQ.put( [channelObject.getSensorValue(), deltaTime, rawTime]) self.channel.setOnSensorChangeHandler(onSensorValueChange)
class jigCurrent(object): '''current sensor''' def __init__(self, provider, channel): self.ai = VoltageRatioInput() self.ai.setDeviceSerialNumber(provider) self.ai.setChannel(channel) self.ai.setOnAttachHandler(self._attached) self.ai.openWaitForAttachment(1000) def _attached(self, event): self.ai.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1122_DC) self.ai.setDataInterval(100) self.ai.setVoltageRatioChangeTrigger(0) @property def current(self): return self.ai.getSensorValue() @property def unitInfo(self): return self.ai.getSensorUnit()
def main(): voltageRatioInput0 = VoltageRatioInput() voltageRatioInput0.setIsHubPortDevice(True) voltageRatioInput0.setHubPort(0) print(*[x for x in voltageRatioInput0.__dir__() if "DataInterval" in x], sep="\n") voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange) voltageRatioInput0.openWaitForAttachment(5000) print(voltageRatioInput0.getMinDataInterval(), voltageRatioInput0.getMaxDataInterval()) voltageRatioInput0.setDataInterval(100) voltageRatioInput0.setSensorValueChangeTrigger(0.01) try: input("Press Enter to Stop\n") except (Exception, KeyboardInterrupt): pass voltageRatioInput0.close()
def main(): port = "/dev/ttyACM0" try: print("Trying Arduino Set Up...") s1 = serial.Serial(port, 9600) if s1.name == port: current_time_struct = time.localtime() current_time = "{}:{}:{}".format(current_time_struct[3], current_time_struct[4], current_time_struct[5]) s1.flushInput() lines = [] time.sleep(1.5) try: with open("hx711_output.csv", newline='') as csvFile: print("Successfully found CSV file.\n") reader = csv.reader(csvFile) except: print("File does not exist, creating file.\n") with open("hx711_output.csv", 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(["Time", "Weight"]) with open("hx711_output.csv", 'a') as csvFile: print("Successfully opened CSV file for writing\n") writer = csv.writer(csvFile) while 1: current_time_struct = time.localtime() current_time = '{}:{}:{}'.format(current_time_struct[3], current_time_struct[4], current_time_struct[5]) if s1.in_waiting > 0: num_bytes = s1.read(1) print(num_bytes) weight = s1.read(num_bytes) print(weight) #writer.writerows([current_time, weight]) except: try: print("Arduino Set Up Failed, Trying Phidget...") ch = VoltageRatioInput() ch.setOnAttachHandler(onAttachHandler) ch.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler) ch.voltageRatio = 0.0 try: print("Waiting for Attachment") ch.openWaitForAttachment(10000) ch.setDataInterval(10) except PhidgetException as e: print("Attachment Timed Out") #time.sleep(10) #Checks to see if enter has been pressed readin = sys.stdin.readline() while readin != "\n": time.sleep(1) ch.close() return 0 except PhidgetException as e: sys.stderr.write("\nExiting with error(s)...") DisplayError(e) traceback.print_exc() print("Cleaning up...") ch.close() return 1 except RunTimeError as e: sys.stderr.write("Runtime Error: \n\t" + e) traceback.print_exc() return 1
def VoltageRatioChangeHandlerA(e, voltageRatio): global a a = voltageRatio # def VoltageRatioChangeHandlerB(e, voltageRatio): global b b = voltageRatio # start = time.time() # Bridge (DAQ1500) is enabled by default so no need to enable it. # Some functions are Boolean. 1 means enabled. Unit of time is ms. ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerA) ch.setHubPort(1) ch.setIsHubPortDevice(1) ch.openWaitForAttachment(5000) ch.setDataInterval(500) #minimum 1 # ch2.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerB) ch2.setHubPort(0) ch2.setChannel(0) ch2.openWaitForAttachment(5000) ch2.setDataInterval(500) # minimum 20 ################################ X=[] Y=[] ################################ while 1: F= b*S+CS # Force # "b" is signal from load cell D= a*L+CL #Displacement or position. "a" comes from lvdt # in this example, mechanical zero and stage zero coincide. Equation for D # may need to be modified.