気圧センサー BMP-085 クラス

前回、i2cデバイス用のクラスを自作してみたので、早速気圧センサーで使ってみることにした。

# coding: utf-8
from i2c import I2C

# ===============================================
# =   BMP085 Pressure/Temperrature Sensors      =
# ===============================================
class setup(I2C):

    # EEPROM data
    AC1 = 0
    AC2 = 0
    AC3 = 0
    AC4 = 0
    AC5 = 0
    AC6 = 0
    B1  = 0
    B2  = 0
    MB  = 0
    MC  = 0
    MD  = 0

    # Control Register address and wait
    rv = [[0x34,4.5],[0x74,7.5],[0xB4,13.5],[0xF4, 25.5]]

    # Constructor
    def __init__(self, bus):
        I2C.__init__(self, bus, 0x77)

        # Read EEPROM data
        self.AC1 = self.read_signed_short(0xAA, 0xAB)
        self.AC2 = self.read_signed_short(0xAC, 0xAD)
        self.AC3 = self.read_signed_short(0xAE, 0xAF)
        self.AC4 = self.read_unsigned_short(0xB0, 0xB1)
        self.AC5 = self.read_unsigned_short(0xB2, 0xB3)
        self.AC6 = self.read_unsigned_short(0xB4, 0xB5)
        self.B1  = self.read_signed_short(0xB6, 0xB7)
        self.B2  = self.read_signed_short(0xB8, 0xB9)
        self.MB  = self.read_signed_short(0xBA, 0xBB)
        self.MC  = self.read_signed_short(0xBC, 0xBD)
        self.MD  = self.read_signed_short(0xBE, 0xBF)

    def read(self, use_list = False, OSS = 1):
        if OSS not in [0, 1, 2, 3]:
            raise Exception("OSS is between 0 and 3.")
        if use_list not in [True, False]:
            raise Exception("use_list is True, False")

        # Read UT
        self.write_byte(0xF4, 0x2E)
        self.sleep_ms(4.5)
        UT = self.read_unsigned_short(0xF6, 0xF7)

        # Get UP
        self.write_byte(0xF4, self.rv[OSS][0])
        self.sleep_ms(self.rv[OSS][1])
        msb  = self.read_byte(0xF6)
        lsb  = self.read_byte(0xF7)
        xlsb = self.read_byte(0xF8)
        UP = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - OSS)

        # Calculate True Temperature
        X1 = ((UT - self.AC6) * self.AC5) >> 15
        X2 = (self.MC << 11) / (X1 + self.MD)
        B5 = X1 + X2
        T = int(round(((B5 + 8) >> 4) / 10.0, 0))

        # Calculate True Pressure
        B6 = B5 - 4000
        X1 = (self.B2 * ((B6 * B6) >> 12)) >> 11
        X2 = (self.AC2 * B6) >> 11
        X3 = X1 + X2
        B3 = (((self.AC1 * 4 + X3) << OSS) + 2) / 4
        X1 = (self.AC3 * B6) >> 13
        X2 = (self.B1 * ((B6 * B6) >> 12)) >> 16
        X3 = ((X1 + X2) + 2) >> 4
        B4 = (self.AC4 * (X3 + 32768)) >> 15
        B7 = (UP*1L - B3) * (50000 >> OSS)
        if B7 < 0x80000000:
            P = (B7 * 2) / B4
        else:
            P = (B7 / B4) * 2
        X1 = (P >> 8) * (P >> 8)
        X1 = (X1 * 3038) >> 16
        X2 = (-7357 * P) >> 16
        P  = int(round((P + ((X1 + X2 + 3791) >> 4)) / 100.0, 0))

        if use_list:
            return [P, T]
        else:
            return P

このクラスを使ったプログラムと実行結果は以下の通りです。

# test2.py coding: utf-8
import bmp085

sensor = bmp085.setup(1)
b1 = sensor.read()
b2 = sensor.read(True)
print "b1 = ", b1
print "b2 = ", b2
$ python test2.py
b1 =  1027
b2 =  [1027, 22]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください