-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkellerLD.py
191 lines (148 loc) · 5.21 KB
/
kellerLD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import time
import smbus
import struct
import os
class KellerLD(object):
_SLAVE_ADDRESS = 0x40
_REQUEST_MEASUREMENT = 0xAC
_DEBUG = False
_P_MODES = (
"PA Mode, Vented Gauge", # Zero at atmospheric pressure
"PR Mode, Sealed Gauge", # Zero at 1.0 bar
"PAA Mode, Absolute Gauge" # Zero at vacuum
)
_P_MODE_OFFSETS = (1.01325, 1.0, 0.0)
def __init__(self, bus=1):
self._bus = None
try:
self._bus = smbus.SMBus(bus)
except:
print("Bus %d is not available.") % bus
print("Available busses are listed as /dev/i2c*")
if os.uname()[1] == 'raspberrypi':
print("Enable the i2c interface using raspi-config!")
def init(self):
if self._bus is None:
print("No bus!")
return False
# Read out pressure-mode to determine relevant offset
self._bus.write_byte(self._SLAVE_ADDRESS, 0x12)
time.sleep(0.001)
# read three bytes (status, P MSB, P LSB)
# status byte should be 0b01BMoEXX, where
# B=(0: conversion complete, 1:busy),
# Mo=(00:normal mode, 01:command mode, 1X: reserved)
# E=(0:checksum okay, 1:memory error)
# X=(don't care)
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 3)
scaling0 = data[1] << 8 | data[2]
self.debug(("0x12:", scaling0, data))
pModeID = scaling0 & 0b11
self.pMode = self._P_MODES[pModeID]
self.pModeOffset = self._P_MODE_OFFSETS[pModeID]
self.debug(("pMode", self.pMode, "pressure offset [bar]", self.pModeOffset))
self.year = scaling0 >> 11
self.month = (scaling0 & 0b0000011110000000) >> 7
self.day = (scaling0 & 0b0000000001111100) >> 2
self.debug(("calibration date", self.year, self.month, self.day))
# Read out minimum pressure reading
time.sleep(0.001)
self._bus.write_byte(self._SLAVE_ADDRESS, 0x13)
time.sleep(0.001)
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 3)
MSWord = data[1] << 8 | data[2]
self.debug(("0x13:", MSWord, data))
time.sleep(0.001)
self._bus.write_byte(self._SLAVE_ADDRESS, 0x14)
time.sleep(0.001)
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 3)
LSWord = data[1] << 8 | data[2]
self.debug(("0x14:", LSWord, data))
self.pMin = MSWord << 16 | LSWord
self.debug(("pMin", self.pMin))
# Read out maximum pressure reading
time.sleep(0.001)
self._bus.write_byte(self._SLAVE_ADDRESS, 0x15)
time.sleep(0.001)
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 3)
MSWord = data[1] << 8 | data[2]
self.debug(("0x15:", MSWord, data))
time.sleep(0.001)
self._bus.write_byte(self._SLAVE_ADDRESS, 0x16)
time.sleep(0.001)
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 3)
LSWord = data[1] << 8 | data[2]
self.debug(("0x16:", LSWord, data))
self.pMax = MSWord << 16 | LSWord
self.debug(("pMax", self.pMax))
# 'I' for 32bit unsigned int
self.pMin = struct.unpack('f', struct.pack('I', self.pMin))[0]
self.pMax = struct.unpack('f', struct.pack('I', self.pMax))[0]
self.debug(("pMin:", self.pMin, "pMax:", self.pMax))
return True
def read(self):
if self._bus is None:
print("No bus!")
return False
if self.pMin is None or self.pMax is None:
print("Init required!")
print("Call init() at least one time before attempting to read()")
return False
self._bus.write_byte(self._SLAVE_ADDRESS, self._REQUEST_MEASUREMENT)
time.sleep(0.01) #10 ms, plenty of time according to spec.
data = self._bus.read_i2c_block_data(self._SLAVE_ADDRESS, 0, 5)
statusByte = data[0]
pressureRaw = data[1] << 8 | data[2]
temperatureRaw = data[3] << 8 | data[4]
'''
# Always busy for some reason
busy = statusByte & 1 << 5
if busy:
print("Conversion is not complete.")
return
'''
if statusByte & 0b11 << 3 :
print("Invalid mode: %d, expected 0!") % ((statusByte & 0b11 << 3) >> 3)
return False
if statusByte & 1 << 2 :
print("Memory checksum error!")
return False
self._pressure = (pressureRaw - 16384) * (self.pMax - self.pMin) / 32768 + self.pMin + self.pModeOffset
self._temperature = ((temperatureRaw >> 4) - 24) * 0.05 - 50
self.debug(("data:", data))
self.debug(("pressureRaw:", pressureRaw, "pressure:", self._pressure))
self.debug(("temperatureRaw", temperatureRaw, "temperature:", self._temperature))
return True
def temperature(self):
if self._temperature is None:
print("Call read() first to get a measurement")
return
return self._temperature
def pressure(self):
if self._pressure is None:
print("Call read() first to get a measurement")
return
return self._pressure
def debug(self, msg):
if self._DEBUG:
print(msg)
def __str__(self):
return ("Keller LD I2C Pressure/Temperature Transmitter\n" +
"\ttype: {}\n".format(self.pMode) +
"\tcalibration date: {}-{}-{}\n".format(self.year, self.month, self.day) +
"\tpressure offset: {:.5f} bar\n".format(self.pModeOffset) +
"\tminimum pressure: {:.5f} bar\n".format(self.pMin) +
"\tmaximum pressure: {:.5f} bar".format(self.pMax))
if __name__ == '__main__':
sensor = KellerLD()
if not sensor.init():
print("Failed to initialize Keller LD sensor!")
exit(1)
print(sensor)
while True:
try:
sensor.read()
print("pressure: %7.4f bar\ttemperature: %0.2f C") % (sensor.pressure(), sensor.temperature())
time.sleep(0.001)
except Exception as e:
print(e)