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)
def main(): ir_reflective_sensor = VoltageRatioInput() ir_reflective_sensor.setChannel(5) ir_reflective_sensor.setOnSensorChangeHandler(on_ir_change) ir_reflective_sensor.openWaitForAttachment(5000) ir_reflective_sensor.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1103) sharp_distance_sensor_left = VoltageRatioInput() sharp_distance_sensor_left.setChannel(6) sharp_distance_sensor_left.setOnSensorChangeHandler(on_sharp_change) sharp_distance_sensor_left.openWaitForAttachment(5000) sharp_distance_sensor_left.setSensorType( VoltageRatioSensorType.SENSOR_TYPE_1101_SHARP_2Y0A21) sharp_distance_sensor_right = VoltageRatioInput() sharp_distance_sensor_right.setChannel(7) sharp_distance_sensor_right.setOnSensorChangeHandler(on_sharp_change) sharp_distance_sensor_right.openWaitForAttachment(5000) sharp_distance_sensor_right.setSensorType( VoltageRatioSensorType.SENSOR_TYPE_1101_SHARP_2Y0A21) while True: pass ir_reflective_sensor.close() sharp_distance_sensor_left.close() sharp_distance_sensor_right.close()
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(): #Create your Phidget channels voltageRatioInput0 = VoltageRatioInput() #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. voltageRatioInput0.setOnSensorChangeHandler(onSensorChange) #Open your Phidgets and wait for attachment voltageRatioInput0.openWaitForAttachment(5000) #Do stuff with your Phidgets here or in your event handlers. #Set the sensor type to match the analog sensor you are using after opening the Phidget voltageRatioInput0.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1111) try: input("Press Enter to Stop\n") except (Exception, KeyboardInterrupt): pass #Close your Phidgets once the program is done. voltageRatioInput0.close()