|
39 | 39 |
|
40 | 40 | #include "MS5611.h" |
41 | 41 |
|
| 42 | +#define MINIMUM_PRESSURE 0.3734 |
| 43 | +#define MAXIMUM_ALTITUDE 84852.0 |
| 44 | +#define AIR_GAS_CONSTANT 287.053 |
| 45 | +#define GRAVITATIONAL_ACCELERATION -9.90665 |
| 46 | + |
| 47 | +static const float H[7] = {0, 11000, 20000, 32000, 47000, 51000, 71000}; // base heights of each atmospheric layer (m) |
| 48 | +static const float P[7] = {101335, 22632.04, 5474.88, 868.016, 111.09, 66.9385, 3.95639}; // base pressures (Pa) |
| 49 | +static const float T[7] = {288.15, 216.65, 216.65, 228.65, 270.65, 270.65, 214.65}; // base temperatures (K) |
| 50 | +static const float Lapse_Rate[7] = {-0.0065, 0, 0.001, 0.0028, 0, -0.0028, -0.002}; // base lapse rates (K/m) |
| 51 | +// https://www.sensorsone.com/icao-standard-atmosphere-altitude-pressure-calculator/ |
| 52 | + |
42 | 53 | #include <SPI.h> |
43 | 54 | SPISettings settingsA( |
44 | 55 | 1000000, MSBFIRST, |
@@ -228,4 +239,57 @@ uint32_t MS5611::readADC() { |
228 | 239 | return val; |
229 | 240 | } |
230 | 241 |
|
| 242 | +// (from MS5837) |
| 243 | +// https://www.mide.com/air-pressure-at-altitude-calculator |
| 244 | +// https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 (stale link). |
| 245 | +// https://en.wikipedia.org/wiki/Pressure_altitude |
| 246 | +float MS5611::getAltitude(float airPressure) |
| 247 | +{ |
| 248 | + // NOTE: _pressure is in Pascal (#44) and airPressure is in mBar. |
| 249 | + float ratio = _pressure * 0.01 / airPressure; |
| 250 | + return 44307.694 * (1 - pow(ratio, 0.190284)); |
| 251 | +} |
| 252 | + |
| 253 | + |
| 254 | +float MS5611::getAltitudeExtendedModel() // altitude in meters |
| 255 | +{ |
| 256 | + // New formula |
| 257 | + |
| 258 | + // Using the ICAO standard atmospheric model |
| 259 | + // valid for altitudes 0-84 km |
| 260 | + // assumes a piecewise model of the atmosphere, where each |
| 261 | + // layer has a constant temperature lapse rate |
| 262 | + // Use pressure to find layer, and then altitude |
| 263 | + // TODO: Adjustment from actual temperature vs. expected |
| 264 | + // Adjust layer base values |
| 265 | + |
| 266 | + https://www.sensorsone.com/icao-standard-atmosphere-altitude-pressure-calculator/ |
| 267 | + |
| 268 | + if(_pressure < MINIMUM_PRESSURE) |
| 269 | + { |
| 270 | + return MAXIMUM_ALTITUDE; |
| 271 | + } |
| 272 | + |
| 273 | + int b = 0; // which layer are we in |
| 274 | + while(b < 6) |
| 275 | + { |
| 276 | + if(_pressure >= P[b + 1]) |
| 277 | + { |
| 278 | + break; |
| 279 | + } |
| 280 | + ++b; |
| 281 | + } |
| 282 | + if (b == 1 || b == 4) // isothermal, tropopause/stratopause |
| 283 | + { |
| 284 | + return H[b] + log(_pressure / P[b]) * AIR_GAS_CONSTANT * T[b] / GRAVITATIONAL_ACCELERATION; |
| 285 | + } |
| 286 | + else // constant temperature gradient |
| 287 | + { |
| 288 | + return H[b] + (pow(_pressure / P[b], Lapse_Rate[b] * AIR_GAS_CONSTANT / GRAVITATIONAL_ACCELERATION) - 1) * T[b] / Lapse_Rate[b]; |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | + |
231 | 293 | // END OF FILE |
| 294 | + |
| 295 | + |
0 commit comments