[I will repurpose this thread as 'BMP180 module development']
@dhylands: I'm quite sure it's from the rigth location. Reading 0xD0 also gives me the correct chip-id of 0x55 (altough it was printed out as b'U' - which took me a while to figure out^^)
I now have a (rough) implementation of a class for the BMP180. The values are off, but they react correctly, as in if I put my finger on the sensor the temperature rises and if I blow on it the pressure does to. I guess it's just some errors in the equations, there are quite some and long also to calculate the true values from the raw ones. But that's just bugs to find.
But it leads me to another question:
Right now I have implemented everything in a class.
That means when I use it as a module, it looks like this:
Code: Select all
import bmp180
sensor = bmp180.bmp180() # but this seems ugly ...
print(sensor.get_altitude())
... but the sensor = bmp180.bmp180() part seems rather ugly ... .
Is there a better way to do this?
I would like it to look something like that:
Code: Select all
import bmp180
print(bmp180.get_altitude())
... without having to create an Obkect from the class, but rather have already an object imported.
Here is the whole source of the module. Don't go hunting for bugs, but comments on the code are appreciated! I will also have to rethink which variables need to be self. and which not.
Code: Select all
import struct
import pyb
class bmp180():
def __init__(self):
self.oss = 3
self.bmp = pyb.I2C(2, pyb.I2C.MASTER)
self.bmp_addr = self.bmp.scan()[0]
self.chip_id = self.bmp.mem_read(2, self.bmp_addr, 0xAA)
self.AC1 = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xAA))[0]
self.AC2 = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xAC))[0]
self.AC3 = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xAE))[0]
self.AC4 = struct.unpack('>H', self.bmp.mem_read(2, self.bmp_addr, 0xB0))[0]
self.AC5 = struct.unpack('>H', self.bmp.mem_read(2, self.bmp_addr, 0xB2))[0]
self.AC6 = struct.unpack('>H', self.bmp.mem_read(2, self.bmp_addr, 0xB4))[0]
self.B1 = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xB6))[0]
self.B2 = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xB8))[0]
self.MB = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xBA))[0]
self.MC = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xBC))[0]
self.MD = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xBE))[0]
def read_uncomp_temp(self):
self.bmp.mem_write(0x2E, self.bmp_addr, 0xF4)
pyb.delay(5)
self.UT = struct.unpack('>h', self.bmp.mem_read(2, self.bmp_addr, 0xF6))[0]
return self.UT
def read_uncomp_pressure(self, oss):
self.bmp.mem_write((0x34+(self.oss<<6)), self.bmp_addr, 0xF4)
pyb.delay((self.oss+1)*7)
self.MSB = struct.unpack('<h', self.bmp.mem_read(1, self.bmp_addr, 0xF6))[0]
self.LSB = struct.unpack('<h', self.bmp.mem_read(1, self.bmp_addr, 0xF7))[0]
self.XLSB = struct.unpack('<h', self.bmp.mem_read(1, self.bmp_addr, 0xF8))[0]
self.UP = ((self.MSB<<16)+(self.LSB<<8)+self.XLSB)>>(8-self.oss)
return self.UP
def calc_temp(self, UT):
self.X1 = (self.UT-self.AC6)*self.AC5/2**15
self.X2 = self.MC*2**11/(self.X1+self.MD)
self.B5 = self.X1+self.X2
self.T = (self.B5+8)/2**4
return self.T, self.B5
def calc_pressure(self, UP, oss):
self.B5 = self.calc_temp(self.read_uncomp_temp())[1]
self.B6 = self.B5-4000
self.X1 = (self.B2*(self.B6*self.B6/2**12))/2**11
self.X2 = self.AC2*self.B6/2**11
self.X3 = self.X1+self.X2
self.B3 = ((int((self.AC1*4+self.X3))<<self.oss)+2)/4
self.X1 = self.AC3*self.B6/2**13
self.X2 = (self.B1*(self.B6*self.B6/2**12))/2**16
self.X3 = ((self.X1+self.X2)+2)/2**2
self.B4 = self.AC4*(self.X3+32768)/2**15
self.B7 = (self.UP-self.B3)*(5000>>self.oss)
if self.B7 < 0x80000000: self.p = (self.B7*2)/self.B4
else: self.p = (self.B7/self.B4)*2
self.X1 = (self.p/2**8)**2
self.X1 = (self.X1*3038)/2**16
self.X2 = (-7357*self.p)/2**16
self.p = self.p+(self.X1+self.X2+3791)/2**4
return self.p
def get_temp(self):
return self.calc_temp(self.read_uncomp_temp())[0]
def get_pressure(self, oss):
return self.calc_pressure(self.read_uncomp_pressure(self.oss), self.oss)
def get_temp_and_pressure(self, oss):
return self.get_temp(), self.get_pressure(self.oss)
def get_altitude(self):
self.h = 44330*(1-(self.get_pressure(3)/1013.25)**(1/5.255))
return self.h
edit: also: is class.__dict__ not implemented yet (I'm still on the version the board shipped with, need to update that)?