Advertisement
fabiobiondi

bmp180.py

Oct 22nd, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. '''
  2. bmp180 is a micropython module for the Bosch BMP180 sensor. It measures
  3. temperature as well as pressure, with a high enough resolution to calculate
  4. altitude.
  5. Breakoutboard: http://www.adafruit.com/products/1603
  6. data-sheet: http://ae-bst.resource.bosch.com/media/products/dokumente/
  7. bmp180/BST-BMP180-DS000-09.pdf
  8.  
  9. The MIT License (MIT)
  10. Copyright (c) 2014 Sebastian Plamauer, oeplse@gmail.com
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. '''
  27.  
  28. from ustruct import unpack as unp
  29. from machine import I2C, Pin
  30. import math
  31. import time
  32.  
  33. # BMP180 class
  34. class BMP180():
  35. '''
  36. Module for the BMP180 pressure sensor.
  37. '''
  38.  
  39. _bmp_addr = 119 # adress of BMP180 is hardcoded on the sensor
  40.  
  41. # init
  42. def __init__(self, i2c_bus):
  43.  
  44. # create i2c obect
  45. _bmp_addr = self._bmp_addr
  46. self._bmp_i2c = i2c_bus
  47. self._bmp_i2c.start()
  48. self.chip_id = self._bmp_i2c.readfrom_mem(_bmp_addr, 0xD0, 2)
  49. # read calibration data from EEPROM
  50. self._AC1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAA, 2))[0]
  51. self._AC2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAC, 2))[0]
  52. self._AC3 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAE, 2))[0]
  53. self._AC4 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB0, 2))[0]
  54. self._AC5 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB2, 2))[0]
  55. self._AC6 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB4, 2))[0]
  56. self._B1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB6, 2))[0]
  57. self._B2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB8, 2))[0]
  58. self._MB = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBA, 2))[0]
  59. self._MC = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBC, 2))[0]
  60. self._MD = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBE, 2))[0]
  61.  
  62. # settings to be adjusted by user
  63. self.oversample_setting = 3
  64. self.baseline = 101325.0
  65.  
  66. # output raw
  67. self.UT_raw = None
  68. self.B5_raw = None
  69. self.MSB_raw = None
  70. self.LSB_raw = None
  71. self.XLSB_raw = None
  72. self.gauge = self.makegauge() # Generator instance
  73. for _ in range(128):
  74. next(self.gauge)
  75. time.sleep_ms(1)
  76.  
  77. def compvaldump(self):
  78. '''
  79. Returns a list of all compensation values
  80. '''
  81. return [self._AC1, self._AC2, self._AC3, self._AC4, self._AC5, self._AC6,
  82. self._B1, self._B2, self._MB, self._MC, self._MD, self.oversample_setting]
  83.  
  84. # gauge raw
  85. def makegauge(self):
  86. '''
  87. Generator refreshing the raw measurments.
  88. '''
  89. delays = (5, 8, 14, 25)
  90. while True:
  91. self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x2E]))
  92. t_start = time.ticks_ms()
  93. while (time.ticks_ms() - t_start) <= 5: # 5mS delay
  94. yield None
  95. try:
  96. self.UT_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 2)
  97. except:
  98. yield None
  99. self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x34+(self.oversample_setting << 6)]))
  100. t_pressure_ready = delays[self.oversample_setting]
  101. t_start = time.ticks_ms()
  102. while (time.ticks_ms() - t_start) <= t_pressure_ready:
  103. yield None
  104. try:
  105. self.MSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 1)
  106. self.LSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF7, 1)
  107. self.XLSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF8, 1)
  108. except:
  109. yield None
  110. yield True
  111.  
  112. def blocking_read(self):
  113. if next(self.gauge) is not None: # Discard old data
  114. pass
  115. while next(self.gauge) is None:
  116. pass
  117.  
  118. @property
  119. def oversample_sett(self):
  120. return self.oversample_setting
  121.  
  122. @oversample_sett.setter
  123. def oversample_sett(self, value):
  124. if value in range(4):
  125. self.oversample_setting = value
  126. else:
  127. print('oversample_sett can only be 0, 1, 2 or 3, using 3 instead')
  128. self.oversample_setting = 3
  129.  
  130. @property
  131. def temperature(self):
  132. '''
  133. Temperature in degree C.
  134. '''
  135. next(self.gauge)
  136. try:
  137. UT = unp('>H', self.UT_raw)[0]
  138. except:
  139. return 0.0
  140. X1 = (UT-self._AC6)*self._AC5/2**15
  141. X2 = self._MC*2**11/(X1+self._MD)
  142. self.B5_raw = X1+X2
  143. return (((X1+X2)+8)/2**4)/10
  144.  
  145. @property
  146. def pressure(self):
  147. '''
  148. Pressure in mbar.
  149. '''
  150. next(self.gauge)
  151. self.temperature # Populate self.B5_raw
  152. try:
  153. MSB = unp('B', self.MSB_raw)[0]
  154. LSB = unp('B', self.LSB_raw)[0]
  155. XLSB = unp('B', self.XLSB_raw)[0]
  156. except:
  157. return 0.0
  158. UP = ((MSB << 16)+(LSB << 8)+XLSB) >> (8-self.oversample_setting)
  159. B6 = self.B5_raw-4000
  160. X1 = (self._B2*(B6**2/2**12))/2**11
  161. X2 = self._AC2*B6/2**11
  162. X3 = X1+X2
  163. B3 = ((int((self._AC1*4+X3)) << self.oversample_setting)+2)/4
  164. X1 = self._AC3*B6/2**13
  165. X2 = (self._B1*(B6**2/2**12))/2**16
  166. X3 = ((X1+X2)+2)/2**2
  167. B4 = abs(self._AC4)*(X3+32768)/2**15
  168. B7 = (abs(UP)-B3) * (50000 >> self.oversample_setting)
  169. if B7 < 0x80000000:
  170. pressure = (B7*2)/B4
  171. else:
  172. pressure = (B7/B4)*2
  173. X1 = (pressure/2**8)**2
  174. X1 = (X1*3038)/2**16
  175. X2 = (-7357*pressure)/2**16
  176. return pressure+(X1+X2+3791)/2**4
  177.  
  178. @property
  179. def altitude(self):
  180. '''
  181. Altitude in m.
  182. '''
  183. try:
  184. p = -7990.0*math.log(self.pressure/self.baseline)
  185. except:
  186. p = 0.0
  187. return p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement