Here is an example of the ORP class I created to allow the ORP module to be used. In the example I have hard coded the calibration data, but you could also pass it in. You can re-calibrate by instantiating a new object and calling readRaw() to get the unconverted value and then hard coding the value, or override the calibration datain the instances. In the example I had calibrated at 0mv (1480) and 400mv (367).
The class only depends on the Adafruit I2C library for communication.
Code: Select all
#!/usr/bin/python
from Adafruit_I2C import Adafruit_I2C
class RA_ORP(object):
def __init__(self, address = 0x4C, busnum = 1, calibration = ( 1480, 367, 0, 400 )):
self.i2c = Adafruit_I2C(address=address)
self.address = address
self.calibration = calibration
self.queue = []
self.lastRaw = 0
self.overCnt = 0
self.maxChange = 300
self.maxOverCnt = 20
self.queueLength = 20
self.debug = 0
def readRaw(self):
return self.i2c.reverseByteOrder(self.i2c.readU16(0x00))
def readSingle(self):
return self.convert(self.readRaw())
def read(self):
value = self.readRaw()
if self.debug: print "Raw value read:",value
if self.lastRaw == 0 or abs(value - self.lastRaw) < self.maxChange:
self.queue.append(value)
self.lastRaw = value
self.overCnt = 0
else:
self.overCnt += 1
if self.overCnt > self.maxOverCnt:
self.overCnt = 0
self.queue.append(value)
self.lastRaw = value
if len(self.queue) > self.queueLength:
self.queue.pop(0)
return self.convert(self.mean())
def convert(self,x):
return (x - self.calibration[0]) * (self.calibration[3] - self.calibration[2]) // (self.calibration[1] - self.calibration[0]) + self.calibration[2]
def mean(self):
n = len(self.queue)
if n < 1:
raise ValueError('mean requires at least one data point')
return sum(self.queue)/n