@@ -119,13 +119,63 @@ def initialize(self):
119
119
120
120
@property
121
121
def _pressure_temperature (self ):
122
- press_sens = self ._calibration_constants [1 ]
123
- press_offset = self ._calibration_constants [2 ]
124
- press_sens_temp_coeff = self ._calibration_constants [3 ]
125
- press_offset_temp_coeff = self ._calibration_constants [4 ]
126
- ref_temp = self ._calibration_constants [5 ]
127
- temp_temp_coeff = self ._calibration_constants [6 ]
128
- #################
122
+ raw_temperature , raw_pressure = self ._read_temp_pressure ()
123
+
124
+ self ._scale_temp_pressure (raw_temperature , raw_pressure )
125
+
126
+ return (self ._temperature , self ._pressure )
127
+
128
+ def _scale_temp_pressure (self , raw_temperature , raw_pressure ):
129
+ # See figure 7 'PRESSURE COMPENSATION (SECOND ORDER OVER TEMPERATURE)'
130
+ # in the MS8607 datasheet
131
+ delta_temp = self ._dt (raw_temperature )
132
+
133
+ initial_temp = 2000 + (delta_temp * self ._calibration_constants [6 ] >> 23 )
134
+
135
+ temp2 , offset2 , sensitivity2 = self ._corrections (initial_temp , delta_temp )
136
+
137
+ self ._temperature = (initial_temp - temp2 ) / 100
138
+ offset = self ._pressure_offset (delta_temp ) - offset2
139
+
140
+ sensitivity = self ._pressure_scaling (delta_temp ) - sensitivity2
141
+
142
+ self ._pressure = ((((raw_pressure * sensitivity ) >> 21 ) - offset ) >> 15 ) / 100
143
+
144
+ @staticmethod
145
+ def _corrections (initial_temp , delta_temp ):
146
+ # # Second order temperature compensation
147
+ if initial_temp < 2000 :
148
+ delta_2k = initial_temp - 2000
149
+ temp_factor = delta_2k ** 2 >> 4
150
+ temp2 = (3 * delta_temp ** 2 ) >> 33
151
+ offset2 = 61 * temp_factor
152
+ sensitivity2 = 29 * temp_factor
153
+
154
+ if initial_temp < - 1500 :
155
+ delta_15k = initial_temp + 1500
156
+ temp_factor = delta_15k ** 2
157
+
158
+ offset2 += 17 * temp_factor
159
+ sensitivity2 += 9 * temp_factor
160
+ #
161
+ else :
162
+ temp2 = (5 * delta_temp ** 2 ) >> 38
163
+ offset2 = 0
164
+ sensitivity2 = 0
165
+ return temp2 , offset2 , sensitivity2
166
+
167
+ def _pressure_scaling (self , delta_temp ):
168
+ return (self ._calibration_constants [1 ] << 16 ) + (
169
+ (self ._calibration_constants [3 ] * delta_temp ) >> 7
170
+ )
171
+
172
+ def _pressure_offset (self , delta_temp ):
173
+ return ((self ._calibration_constants [2 ]) << 17 ) + (
174
+ (self ._calibration_constants [4 ] * delta_temp ) >> 6
175
+ )
176
+
177
+ def _read_temp_pressure (self ):
178
+
129
179
# First read temperature
130
180
cmd = self ._psensor_resolution_osr * 2
131
181
cmd |= _MS8607_PT_CMD_TEMP_START
@@ -144,7 +194,6 @@ def _pressure_temperature(self):
144
194
# temp is only 24 bits but unpack wants 4 bytes so add a forth byte
145
195
self ._buffer [0 ] = 0
146
196
raw_temperature = unpack_from (">I" , self ._buffer )[0 ]
147
- #################
148
197
149
198
# next read pressure
150
199
cmd = self ._psensor_resolution_osr * 2
@@ -160,52 +209,16 @@ def _pressure_temperature(self):
160
209
i2c .write_then_readinto (
161
210
self ._buffer , self ._buffer , out_start = 0 , out_end = 1 , in_start = 1 , in_end = 3
162
211
)
163
- # temp is only 24 bits but unpack wants 4 bytes so add a forth byte
212
+ # pressure is only 24 bits but unpack wants 4 bytes so add a forth byte
164
213
self ._buffer [0 ] = 0
165
214
166
215
raw_pressure = unpack_from (">I" , self ._buffer )[0 ]
167
- #################
168
-
169
- delta_temp = raw_temperature - (ref_temp << 8 )
216
+ return raw_temperature , raw_pressure
170
217
171
- # # Actual temperature = 2000 + delta_temp * TEMPSENS
172
- tmp_temp = 2000 + (delta_temp * temp_temp_coeff >> 23 )
218
+ def _dt (self , raw_temperature ):
173
219
174
- # # Second order temperature compensation
175
- if tmp_temp < 2000 :
176
- t_sq = (3 * (delta_temp * delta_temp )) >> 33
177
- offset_sq = 61 * (tmp_temp - 2000 ) * (tmp_temp - 2000 ) / 16
178
- sens2 = 29 * (tmp_temp - 2000 ) * (tmp_temp - 2000 ) / 16
179
-
180
- if tmp_temp < - 1500 :
181
- offset_sq += 17 * (tmp_temp + 1500 ) * (tmp_temp + 1500 )
182
- sens2 += 9 * (tmp_temp + 1500 ) * (tmp_temp + 1500 )
183
- #
184
- else :
185
- t_sq = (5 * (delta_temp * delta_temp )) >> 38
186
- offset_sq = 0
187
- sens2 = 0
188
- #
189
- # offset = OFF_T1 + TCO * delta_temp
190
- # this can be refactored into a coefficient applied to delta_temp
191
- offset = ((press_offset ) << 17 ) + ((press_offset_temp_coeff * delta_temp ) >> 6 )
192
- offset -= offset_sq
193
-
194
- # Sensitivity at actual temperature = SENS_T1 + TCS * delta_temp
195
- # this can be refactored into a coefficient applied to delta_temp
196
- sensitivity = (press_sens << 16 ) + ((press_sens_temp_coeff * delta_temp ) >> 7 )
197
- sensitivity -= sens2
198
-
199
- # Temperature compensated pressure = D1 * sensitivity - offset
200
- temp_compensated_pressure = (
201
- ((raw_pressure * sensitivity ) >> 21 ) - offset
202
- ) >> 15
203
-
204
- self ._temperature = (tmp_temp - t_sq ) / 100
205
- self ._pressure = temp_compensated_pressure / 100
206
-
207
- # return status
208
- return (self ._temperature , self ._pressure )
220
+ ref_temp = self ._calibration_constants [5 ]
221
+ return raw_temperature - (ref_temp << 8 )
209
222
210
223
@property
211
224
def temperature (self ):
0 commit comments