Skip to content

Commit ecd13f0

Browse files
committed
Only use LERP baro altitude on esp82xx
1 parent 69e362a commit ecd13f0

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/lib/Baro/baro_base.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ uint8_t BaroI2CBase::m_address = 0;
99
/**
1010
* @brief: Return altitude in cm from pressure in deci-Pascals
1111
**/
12-
// int32_t BaroBase::pressureToAltitude(uint32_t pressuredPa)
13-
// {
14-
// const float seaLeveldPa = 1013250; // 1013.25hPa
15-
// return 4433000 * (1.0 - pow(pressuredPa / seaLeveldPa, 0.1903));
16-
// }
17-
18-
int32_t BaroBase::pressureToAltitude(uint32_t pressurePa)
12+
int32_t BaroBase::pressureToAltitude(uint32_t pressuredPa)
1913
{
14+
#if defined(PLATFORM_ESP32)
15+
const float seaLeveldPa = 1013250; // 1013.25hPa
16+
return 4433000 * (1.0 - pow(pressuredPa / seaLeveldPa, 0.1903));
17+
#else // ESP8266
2018
const size_t LUT_CNT = 6;
2119
const int32_t pressureTable[LUT_CNT] = { 1013250, 898750, 794950, 701080, 616400, 540200 };
2220
const int32_t altitudeTable[LUT_CNT] = { 0, 100000, 200000, 300000, 400000, 500000 };
2321

2422
unsigned i = 0;
25-
while (i < LUT_CNT-2 && (int32_t)pressurePa < pressureTable[i + 1])
23+
while (i < LUT_CNT-2 && (int32_t)pressuredPa < pressureTable[i + 1])
2624
i++;
2725

2826
// Linear interpolation, note that >5000m the error grows exponentially
@@ -31,7 +29,8 @@ int32_t BaroBase::pressureToAltitude(uint32_t pressurePa)
3129
int32_t a0 = altitudeTable[i];
3230
int32_t a1 = altitudeTable[i + 1];
3331

34-
return map(pressurePa, p0, p1, a0, a1);
32+
return map(pressuredPa, p0, p1, a0, a1);
33+
#endif
3534
}
3635

3736
void BaroI2CBase::readRegister(uint8_t reg, uint8_t *data, size_t size)

src/lib/Baro/baro_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BaroBase
2828

2929
// Base functions
3030
bool isInitialized() const { return m_initialized; }
31-
int32_t pressureToAltitude(uint32_t pressurePa);
31+
int32_t pressureToAltitude(uint32_t pressuredPa);
3232
// Properties
3333
int32_t getAltitudeHome() const { return m_altitudeHome; }
3434
void setAltitudeHome(int32_t altitudeHome) { m_altitudeHome = altitudeHome; }

0 commit comments

Comments
 (0)