rpirena/pressure.py
stephan 70d5c48a2c rpirena: python class for parsing pressure recoreds
git-svn-id: svn+ssh://asterix.ieap.uni-kiel.de/home/subversion/stephan/solo/eda/cospi/host@7089 bc5caf13-1734-44f8-af43-603852e9ee25
2018-11-09 14:52:35 +00:00

70 lines
1.8 KiB
Python

# encoding: UTF-8
import numpy
class BATE(object):
def __init__(self):
self.reset()
def parse(self, l):
"""P parse internal pressure sensor packet
see the MS5534C datasheet for the calibration algorithm
The arrays in the datasheet are indexed based 1.
"""
if l[0] != 'P':
return
sensor = int(l[1])
ll = [int(i,0) for i in l.split()[1:]]
for i in ll[1:]:
if i == 0xba7e:
raise self.NM64Error("BaTe magic token mismatch %s" % l)
Word = [0]+ll[0:4]
D = ll[3:6] # Word C, and D are indexed base 1
C = [0]*7
C[1] = Word[1] >> 1
C[2] = ((Word[3] & 0x3f) << 6) | (Word[4] & 0x3f)
C[3] = Word[4] >> 6
C[4] = Word[3] >> 6
C[5] = ((Word[1] & 1) << 10) | (Word[2] >> 6)
C[6] = Word[2] & 0x3f
UT1 = 8*C[5]+20224
dT = D[2] - UT1
TEMP = 200 + dT*(C[6]+50)/1024.
OFF = C[2]*4 + ((C[4]-512)*dT)/4096.
SENS = C[1] + (C[3]*dT)/1024. + 24576
X = (SENS * (D[1]-7168))/16384. - OFF
P = X*10/32. + 2500
P /= 10.0
TEMP /= 10.0
self.BaTe += (1, TEMP, P, TEMP**2, P**2)
return (sensor, P, TEMP)
def reset(self):
self.BaTe = numpy.zeros((5,))
def mean(self):
n = self.BaTe[0]
if n:
TEMP = self.BaTe[1]/n
P = self.BaTe[2]/n
if n>1:
sTEMP = math.sqrt((self.BaTe[3]/n - TEMP**2)/(n-1)/n)
sP = math.sqrt((self.BaTe[4]/n - P**2)/(n-1)/n)
return (P, TEMP, sP, sTEMP)
return (P, TEMP, 0, 0)
def __str__(self):
return "T=%.1f°C P=%.1fmbar" % self.mean()[:2]
def parse(l):
return BATE().parse(l)