|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import time |
| 4 | +from Adafruit_I2C import Adafruit_I2C |
| 5 | + |
| 6 | +# =========================================================================== |
| 7 | +# BMP085 Class |
| 8 | +# =========================================================================== |
| 9 | + |
| 10 | +class BMP085 : |
| 11 | + i2c = None |
| 12 | + |
| 13 | + # Operating Modes |
| 14 | + __BMP085_ULTRALOWPOWER = 0 |
| 15 | + __BMP085_STANDARD = 1 |
| 16 | + __BMP085_HIGHRES = 2 |
| 17 | + __BMP085_ULTRAHIGHRES = 3 |
| 18 | + |
| 19 | + # BMP085 Registers |
| 20 | + __BMP085_CAL_AC1 = 0xAA # R Calibration data (16 bits) |
| 21 | + __BMP085_CAL_AC2 = 0xAC # R Calibration data (16 bits) |
| 22 | + __BMP085_CAL_AC3 = 0xAE # R Calibration data (16 bits) |
| 23 | + __BMP085_CAL_AC4 = 0xB0 # R Calibration data (16 bits) |
| 24 | + __BMP085_CAL_AC5 = 0xB2 # R Calibration data (16 bits) |
| 25 | + __BMP085_CAL_AC6 = 0xB4 # R Calibration data (16 bits) |
| 26 | + __BMP085_CAL_B1 = 0xB6 # R Calibration data (16 bits) |
| 27 | + __BMP085_CAL_B2 = 0xB8 # R Calibration data (16 bits) |
| 28 | + __BMP085_CAL_MB = 0xBA # R Calibration data (16 bits) |
| 29 | + __BMP085_CAL_MC = 0xBC # R Calibration data (16 bits) |
| 30 | + __BMP085_CAL_MD = 0xBE # R Calibration data (16 bits) |
| 31 | + __BMP085_CONTROL = 0xF4 |
| 32 | + __BMP085_TEMPDATA = 0xF6 |
| 33 | + __BMP085_PRESSUREDATA = 0xF6 |
| 34 | + __BMP085_READTEMPCMD = 0x2E |
| 35 | + __BMP085_READPRESSURECMD = 0x34 |
| 36 | + |
| 37 | + # Private Fields |
| 38 | + _cal_AC1 = 0 |
| 39 | + _cal_AC2 = 0 |
| 40 | + _cal_AC3 = 0 |
| 41 | + _cal_AC4 = 0 |
| 42 | + _cal_AC5 = 0 |
| 43 | + _cal_AC6 = 0 |
| 44 | + _cal_B1 = 0 |
| 45 | + _cal_B2 = 0 |
| 46 | + _cal_MB = 0 |
| 47 | + _cal_MC = 0 |
| 48 | + _cal_MD = 0 |
| 49 | + |
| 50 | + # Constructor |
| 51 | + def __init__(self, address=0x77, mode=1, debug=False): |
| 52 | + self.i2c = Adafruit_I2C(address) |
| 53 | + |
| 54 | + self.address = address |
| 55 | + self.debug = debug |
| 56 | + # Make sure the specified mode is in the appropriate range |
| 57 | + if ((mode < 0) | (mode > 3)): |
| 58 | + if (self.debug): |
| 59 | + print "Invalid Mode: Using STANDARD by default" |
| 60 | + self.mode = self.__BMP085_STANDARD |
| 61 | + else: |
| 62 | + self.mode = mode |
| 63 | + # Read the calibration data |
| 64 | + self.readCalibrationData() |
| 65 | + |
| 66 | + def readCalibrationData(self): |
| 67 | + "Reads the calibration data from the IC" |
| 68 | + self._cal_AC1 = self.i2c.readS16(self.__BMP085_CAL_AC1) # INT16 |
| 69 | + self._cal_AC2 = self.i2c.readS16(self.__BMP085_CAL_AC2) # INT16 |
| 70 | + self._cal_AC3 = self.i2c.readS16(self.__BMP085_CAL_AC3) # INT16 |
| 71 | + self._cal_AC4 = self.i2c.readU16(self.__BMP085_CAL_AC4) # UINT16 |
| 72 | + self._cal_AC5 = self.i2c.readU16(self.__BMP085_CAL_AC5) # UINT16 |
| 73 | + self._cal_AC6 = self.i2c.readU16(self.__BMP085_CAL_AC6) # UINT16 |
| 74 | + self._cal_B1 = self.i2c.readS16(self.__BMP085_CAL_B1) # INT16 |
| 75 | + self._cal_B2 = self.i2c.readS16(self.__BMP085_CAL_B2) # INT16 |
| 76 | + self._cal_MB = self.i2c.readS16(self.__BMP085_CAL_MB) # INT16 |
| 77 | + self._cal_MC = self.i2c.readS16(self.__BMP085_CAL_MC) # INT16 |
| 78 | + self._cal_MD = self.i2c.readS16(self.__BMP085_CAL_MD) # INT16 |
| 79 | + if (self.debug): |
| 80 | + self.showCalibrationData() |
| 81 | + |
| 82 | + def showCalibrationData(self): |
| 83 | + "Displays the calibration values for debugging purposes" |
| 84 | + print "DBG: AC1 = %6d" % (self._cal_AC1) |
| 85 | + print "DBG: AC2 = %6d" % (self._cal_AC2) |
| 86 | + print "DBG: AC3 = %6d" % (self._cal_AC3) |
| 87 | + print "DBG: AC4 = %6d" % (self._cal_AC4) |
| 88 | + print "DBG: AC5 = %6d" % (self._cal_AC5) |
| 89 | + print "DBG: AC6 = %6d" % (self._cal_AC6) |
| 90 | + print "DBG: B1 = %6d" % (self._cal_B1) |
| 91 | + print "DBG: B2 = %6d" % (self._cal_B2) |
| 92 | + print "DBG: MB = %6d" % (self._cal_MB) |
| 93 | + print "DBG: MC = %6d" % (self._cal_MC) |
| 94 | + print "DBG: MD = %6d" % (self._cal_MD) |
| 95 | + |
| 96 | + def readRawTemp(self): |
| 97 | + "Reads the raw (uncompensated) temperature from the sensor" |
| 98 | + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READTEMPCMD) |
| 99 | + time.sleep(0.005) # Wait 5ms |
| 100 | + raw = self.i2c.readU16(self.__BMP085_TEMPDATA) |
| 101 | + if (self.debug): |
| 102 | + print "DBG: Raw Temp: 0x%04X (%d)" % (raw & 0xFFFF, raw) |
| 103 | + return raw |
| 104 | + |
| 105 | + def readRawPressure(self): |
| 106 | + "Reads the raw (uncompensated) pressure level from the sensor" |
| 107 | + self.i2c.write8(self.__BMP085_CONTROL, self.__BMP085_READPRESSURECMD + (self.mode << 6)) |
| 108 | + if (self.mode == self.__BMP085_ULTRALOWPOWER): |
| 109 | + time.sleep(0.005) |
| 110 | + elif (self.mode == self.__BMP085_HIGHRES): |
| 111 | + time.sleep(0.014) |
| 112 | + elif (self.mode == self.__BMP085_ULTRAHIGHRES): |
| 113 | + time.sleep(0.026) |
| 114 | + else: |
| 115 | + time.sleep(0.008) |
| 116 | + msb = self.i2c.readU8(self.__BMP085_PRESSUREDATA) |
| 117 | + lsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+1) |
| 118 | + xlsb = self.i2c.readU8(self.__BMP085_PRESSUREDATA+2) |
| 119 | + raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode) |
| 120 | + if (self.debug): |
| 121 | + print "DBG: Raw Pressure: 0x%04X (%d)" % (raw & 0xFFFF, raw) |
| 122 | + return raw |
| 123 | + |
| 124 | + def readTemperature(self): |
| 125 | + "Gets the compensated temperature in degrees celcius" |
| 126 | + UT = 0 |
| 127 | + X1 = 0 |
| 128 | + X2 = 0 |
| 129 | + B5 = 0 |
| 130 | + temp = 0.0 |
| 131 | + |
| 132 | + # Read raw temp before aligning it with the calibration values |
| 133 | + UT = self.readRawTemp() |
| 134 | + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 |
| 135 | + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) |
| 136 | + B5 = X1 + X2 |
| 137 | + temp = ((B5 + 8) >> 4) / 10.0 |
| 138 | + if (self.debug): |
| 139 | + print "DBG: Calibrated temperature = %f C" % temp |
| 140 | + return temp |
| 141 | + |
| 142 | + def readPressure(self): |
| 143 | + "Gets the compensated pressure in pascal" |
| 144 | + UT = 0 |
| 145 | + UP = 0 |
| 146 | + B3 = 0 |
| 147 | + B5 = 0 |
| 148 | + B6 = 0 |
| 149 | + X1 = 0 |
| 150 | + X2 = 0 |
| 151 | + X3 = 0 |
| 152 | + p = 0 |
| 153 | + B4 = 0 |
| 154 | + B7 = 0 |
| 155 | + |
| 156 | + UT = self.readRawTemp() |
| 157 | + UP = self.readRawPressure() |
| 158 | + |
| 159 | + # You can use the datasheet values to test the conversion results |
| 160 | + # dsValues = True |
| 161 | + dsValues = False |
| 162 | + |
| 163 | + if (dsValues): |
| 164 | + UT = 27898 |
| 165 | + UP = 23843 |
| 166 | + self._cal_AC6 = 23153 |
| 167 | + self._cal_AC5 = 32757 |
| 168 | + self._cal_MC = -8711 |
| 169 | + self._cal_MD = 2868 |
| 170 | + self._cal_B1 = 6190 |
| 171 | + self._cal_B2 = 4 |
| 172 | + self._cal_AC3 = -14383 |
| 173 | + self._cal_AC2 = -72 |
| 174 | + self._cal_AC1 = 408 |
| 175 | + self._cal_AC4 = 32741 |
| 176 | + self.mode = self.__BMP085_ULTRALOWPOWER |
| 177 | + if (self.debug): |
| 178 | + self.showCalibrationData() |
| 179 | + |
| 180 | + # True Temperature Calculations |
| 181 | + X1 = ((UT - self._cal_AC6) * self._cal_AC5) >> 15 |
| 182 | + X2 = (self._cal_MC << 11) / (X1 + self._cal_MD) |
| 183 | + B5 = X1 + X2 |
| 184 | + if (self.debug): |
| 185 | + print "DBG: X1 = %d" % (X1) |
| 186 | + print "DBG: X2 = %d" % (X2) |
| 187 | + print "DBG: B5 = %d" % (B5) |
| 188 | + print "DBG: True Temperature = %.2f C" % (((B5 + 8) >> 4) / 10.0) |
| 189 | + |
| 190 | + # Pressure Calculations |
| 191 | + B6 = B5 - 4000 |
| 192 | + X1 = (self._cal_B2 * (B6 * B6) >> 12) >> 11 |
| 193 | + X2 = (self._cal_AC2 * B6) >> 11 |
| 194 | + X3 = X1 + X2 |
| 195 | + B3 = (((self._cal_AC1 * 4 + X3) << self.mode) + 2) / 4 |
| 196 | + if (self.debug): |
| 197 | + print "DBG: B6 = %d" % (B6) |
| 198 | + print "DBG: X1 = %d" % (X1) |
| 199 | + print "DBG: X2 = %d" % (X2) |
| 200 | + print "DBG: B3 = %d" % (B3) |
| 201 | + |
| 202 | + X1 = (self._cal_AC3 * B6) >> 13 |
| 203 | + X2 = (self._cal_B1 * ((B6 * B6) >> 12)) >> 16 |
| 204 | + X3 = ((X1 + X2) + 2) >> 2 |
| 205 | + B4 = (self._cal_AC4 * (X3 + 32768)) >> 15 |
| 206 | + B7 = (UP - B3) * (50000 >> self.mode) |
| 207 | + if (self.debug): |
| 208 | + print "DBG: X1 = %d" % (X1) |
| 209 | + print "DBG: X2 = %d" % (X2) |
| 210 | + print "DBG: B4 = %d" % (B4) |
| 211 | + print "DBG: B7 = %d" % (B7) |
| 212 | + |
| 213 | + if (B7 < 0x80000000): |
| 214 | + p = (B7 * 2) / B4 |
| 215 | + else: |
| 216 | + p = (B7 / B4) * 2 |
| 217 | + |
| 218 | + X1 = (p >> 8) * (p >> 8) |
| 219 | + X1 = (X1 * 3038) >> 16 |
| 220 | + X2 = (-7375 * p) >> 16 |
| 221 | + if (self.debug): |
| 222 | + print "DBG: p = %d" % (p) |
| 223 | + print "DBG: X1 = %d" % (X1) |
| 224 | + print "DBG: X2 = %d" % (X2) |
| 225 | + |
| 226 | + p = p + ((X1 + X2 + 3791) >> 4) |
| 227 | + if (self.debug): |
| 228 | + print "DBG: Pressure = %d Pa" % (p) |
| 229 | + |
| 230 | + return p |
| 231 | + |
| 232 | + def readAltitude(self, seaLevelPressure=101325): |
| 233 | + "Calculates the altitude in meters" |
| 234 | + altitude = 0.0 |
| 235 | + pressure = float(self.readPressure()) |
| 236 | + altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903)) |
| 237 | + if (self.debug): |
| 238 | + print "DBG: Altitude = %d" % (altitude) |
| 239 | + return altitude |
| 240 | + |
| 241 | + return 0 |
0 commit comments