From de09238f5518e337c609d8248d5375dcc53825c2 Mon Sep 17 00:00:00 2001 From: JB Date: Thu, 26 Nov 2026 15:19:46 -0500 Subject: [PATCH 1/3] fix(terrain): take the height reference where the aircraft arms Height above ground is the estimated altitude minus the terrain's rise above a reference height. The reference was the map height at the GPS origin, which is where the first fix came in and never moves, while the altitude zero is set where the aircraft arms (inav_reset_altitude). An aircraft carried from where it got its fix to the launch spot, or armed a second time somewhere else with EACH_ARM, had every terrain height off by the difference in ground height between the two places. The map is now read where the aircraft stands while it is disarmed, and kept with the altitude it has there. Arming keeps the last such point for the flight; an emergency re-arm in the air keeps the flight's point, as the position estimator keeps its altitude zero. terrain.c and terrain_nav.c share the one reference instead of each latching the origin's map height. SITL (3D cruise + TERRAIN AGL HOLD engaged at 50 m over flat ground, minimum 60 m, so the hold levels off at 70 m): - first fix on ground 30 m higher, then carried to the launch spot: levelled at 49.6 m and reported 79.6 m; now 70.0 m - EACH_ARM, second arm on ground 30 m higher: levelled at 100.0 m; now 69.9 m - FIRST_ARM second arm, and a re-arm in the air: unchanged Co-Authored-By: Claude Fable 5.2 --- src/main/terrain/terrain.c | 114 +++++++++++++++++++++++---------- src/main/terrain/terrain.h | 3 + src/main/terrain/terrain_nav.c | 59 ++++------------- src/main/terrain/terrain_nav.h | 3 - 4 files changed, 93 insertions(+), 86 deletions(-) diff --git a/src/main/terrain/terrain.c b/src/main/terrain/terrain.c index 4f25ed3fbf7..523030eaf74 100644 --- a/src/main/terrain/terrain.c +++ b/src/main/terrain/terrain.c @@ -73,15 +73,25 @@ static struct { .terrainAMSL = 0, }; -static struct { - bool homeAltitudeFound; - float homeAltitudeM; - gpsLocation_t homeLocation; -} terrainHomePos = { - .homeAltitudeFound = false, - .homeAltitudeM = 0.0f, - .homeLocation = {.lat = 0, .lon = 0}, -}; +/* + * The height reference: the map height that the navigation's altitude zero stands for. + * Height above ground = estimated altitude - (map height here - reference). + * + * The altitude zero is set where the aircraft arms (inav_reset_altitude), which need not be + * where the GPS origin was set by the first fix. So the map is read where the aircraft + * stands while it is disarmed, and kept with the altitude it had there: the reference is + * that map height minus that altitude. Arming keeps the last such point for the flight. + */ +typedef struct { + bool located; // a position and altitude have been recorded + gpsLocation_t location; + float altitudeCm; // estimated altitude at that position, local frame + bool heightFound; // the map has answered for that position + float heightM; +} terrainGroundPoint_t; + +static terrainGroundPoint_t groundDisarmed; // follows the aircraft while it is disarmed +static terrainGroundPoint_t groundFlight; // the point of the flight, taken at arming /** * @brief Get the height above mean sea level (AMSL) in meters for a given GPS location. @@ -136,11 +146,62 @@ void terrainInit(void) { } +// Arming hands the disarmed point over to the flight. An emergency re-arm in the air keeps +// the flight's point, as the estimator keeps its altitude zero +static terrainGroundPoint_t *terrainGroundPointInUse(void) +{ + static bool wasArmed = false; + + const bool armed = ARMING_FLAG(ARMED); + if (armed && !wasArmed && !(STATE(IN_FLIGHT_EMERG_REARM) && groundFlight.located)) { + groundFlight = groundDisarmed; + } + wasArmed = armed; + + return armed ? &groundFlight : &groundDisarmed; +} + +static void terrainGroundPointUpdate(void) +{ + terrainGroundPoint_t *point = terrainGroundPointInUse(); + + if (!ARMING_FLAG(ARMED) && posControl.flags.estPosStatus >= EST_USABLE) { + const fpVector3_t *pos = &navGetCurrentActualPositionAndVelocity()->pos; + gpsLocation_t here; + if (geoConvertLocalToGeodetic(&here, &posControl.gpsOrigin, pos)) { + point->located = true; + point->location = here; + point->altitudeCm = pos->z; + point->heightFound = false; + } + } + + // In flight only a point whose block was not loaded at arming is still asked for + if (point->located && !point->heightFound) { + const float heightM = getHeightAmslMeters(&point->location); + if (heightM != TERRAIN_STATUS_NO_AMSL_DATA) { + point->heightFound = true; + point->heightM = heightM; + } + } +} + +bool terrainGetReferenceHeightM(float *heightM) +{ + const terrainGroundPoint_t *point = terrainGroundPointInUse(); + if (!point->located || !point->heightFound) { + return false; + } + + *heightM = point->heightM - point->altitudeCm * 0.01f; + return true; +} + /** * @brief Update the terrain subsystem. * * This function checks the status of the SD card and attempts to load terrain data into the cache. - * It also tries to determine the home altitude based on the GPS origin if it hasn't been found yet. + * It also keeps the height reference: the map height where the aircraft stands before it arms. */ void terrainUpdateTask(timeUs_t currentTimeUs) { @@ -154,39 +215,22 @@ void terrainUpdateTask(timeUs_t currentTimeUs) return; } - if(ARMING_FLAG(ARMED) && terrainHomePos.homeAltitudeFound == false){ - //vehicle is armed and we don't have loaded values for home position, don't try load from SD card durting flight if we are not sure SD it's possible to load data - return; - } + terrainGroundPointUpdate(); - ///////////////////////////////////////////////////////////////////////////////////////////////////// - //try to find home altitude to home offset - if(terrainHomePos.homeAltitudeFound == false && posControl.gpsOrigin.valid && (posControl.gpsOrigin.lat != terrainHomePos.homeLocation.lat || posControl.gpsOrigin.lon != terrainHomePos.homeLocation.lon)) { - - gpsLocation_t homeLoc = { - .lat = posControl.gpsOrigin.lat, - .lon = posControl.gpsOrigin.lon, - }; - - float heightASLHome = getHeightAmslMeters(&homeLoc); - - if(heightASLHome != TERRAIN_STATUS_NO_AMSL_DATA){ - terrainHomePos.homeAltitudeFound = true; - terrainHomePos.homeAltitudeM = heightASLHome; - terrainHomePos.homeLocation.lat = posControl.gpsOrigin.lat; - terrainHomePos.homeLocation.lon = posControl.gpsOrigin.lon; - } + float referenceM; + if (ARMING_FLAG(ARMED) && !terrainGetReferenceHeightM(&referenceM)) { + // armed without a height reference: the block under the take-off point was not + // loaded. That point is still asked for above; nothing else is read from the card + return; } - ///////////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////////// - if(terrainHomePos.homeAltitudeFound) + if(terrainGetReferenceHeightM(&referenceM)) { float heightASL = getHeightAmslMeters(&gpsSol.llh); if(heightASL != TERRAIN_STATUS_NO_AMSL_DATA){ terrainHeight.terrainAMSL = (int32_t)(heightASL * 100.0f); - terrainHeight.terrainAGL = MAX(0, ((int32_t)getEstimatedActualPosition(Z) + (int32_t)(terrainHomePos.homeAltitudeM * 100.0f)) - terrainHeight.terrainAMSL); + terrainHeight.terrainAGL = MAX(0, ((int32_t)getEstimatedActualPosition(Z) + (int32_t)(referenceM * 100.0f)) - terrainHeight.terrainAMSL); terrainHeight.lastUpdate = millis(); } } diff --git a/src/main/terrain/terrain.h b/src/main/terrain/terrain.h index dc43a12f821..5cf9c1287af 100644 --- a/src/main/terrain/terrain.h +++ b/src/main/terrain/terrain.h @@ -166,4 +166,7 @@ int32_t terrainGetLastAMSL(void); // TERRAIN_STATUS_NO_AMSL_DATA when the block is not loaded yet (a read gets scheduled) float getHeightAmslMeters(const gpsLocation_t *loc); int32_t terrainGetLastDistanceCm(void); +// The map height (m) that the navigation's altitude zero stands for: the map where the +// aircraft stood when it armed, less its altitude there. False until the map has answered +bool terrainGetReferenceHeightM(float *heightM); diff --git a/src/main/terrain/terrain_nav.c b/src/main/terrain/terrain_nav.c index d382d6ac5d4..c23df758409 100644 --- a/src/main/terrain/terrain_nav.c +++ b/src/main/terrain/terrain_nav.c @@ -50,33 +50,6 @@ // at 30 m cover ~1.9 km ahead, well beyond a practical climb-planning horizon #define TERRAIN_NAV_LOOKAHEAD_MAX_SAMPLES 64 -// Home terrain height, latched once the GPS origin is known and re-latched if -// the origin moves - the same relative datum the data layer uses for its own -// AGL, so map-datum errors cancel (docs: C-2) -static bool homeHeightValid; -static float homeHeightM; -static int32_t homeHeightLat; -static int32_t homeHeightLon; - -static bool terrainNavUpdateHomeHeight(void) -{ - if (!posControl.gpsOrigin.valid) { - homeHeightValid = false; - return false; - } - if (homeHeightValid && homeHeightLat == posControl.gpsOrigin.lat && homeHeightLon == posControl.gpsOrigin.lon) { - return true; - } - float heightM; - homeHeightValid = terrainNavGetHomeTerrainHeight(&heightM); - if (homeHeightValid) { - homeHeightM = heightM; - homeHeightLat = posControl.gpsOrigin.lat; - homeHeightLon = posControl.gpsOrigin.lon; - } - return homeHeightValid; -} - // The aircraft's position as the estimator sees it (not the raw GPS fix), so // the layer follows whatever INAV trusts for position static bool terrainNavCurrentLocation(gpsLocation_t *llh) @@ -88,8 +61,10 @@ static bool terrainNavCurrentLocation(gpsLocation_t *llh) } // Healthy = terrain enabled, the SD reader alive, a usable position estimate -// and the home terrain height known. Per-sample data availability is answered -// by the height queries themselves (no data = block not loaded yet) +// and the height reference known (terrainGetReferenceHeightM: the same relative +// datum the data layer uses for its own AGL, so map-datum errors cancel). +// Per-sample data availability is answered by the height queries themselves +// (no data = block not loaded yet) bool terrainNavIsHealthy(void) { if (!terrainConfig()->terrainEnabled || isTerrainIoFailure()) { @@ -98,7 +73,8 @@ bool terrainNavIsHealthy(void) if (posControl.flags.estPosStatus < EST_USABLE) { return false; } - return terrainNavUpdateHomeHeight(); + float referenceM; + return terrainGetReferenceHeightM(&referenceM); } bool terrainNavGetAGLCm(int32_t *aglCm) @@ -109,13 +85,15 @@ bool terrainNavGetAGLCm(int32_t *aglCm) gpsLocation_t here; float heightHereM; - if (!terrainNavCurrentLocation(&here) || !terrainNavGetHeightAtLocation(&here, &heightHereM)) { + float referenceM; + if (!terrainNavCurrentLocation(&here) || !terrainNavGetHeightAtLocation(&here, &heightHereM) + || !terrainGetReferenceHeightM(&referenceM)) { return false; } - // AGL = estimated altitude above home minus the terrain rise above home + // AGL = estimated altitude minus the terrain's rise above the height reference const int32_t agl = (int32_t)navGetCurrentActualPositionAndVelocity()->pos.z - - (int32_t)((heightHereM - homeHeightM) * 100.0f); + - (int32_t)((heightHereM - referenceM) * 100.0f); *aglCm = MAX(0, agl); return true; } @@ -137,21 +115,6 @@ bool terrainNavGetHeightAtLocation(const gpsLocation_t *loc, float *heightM) return true; } -bool terrainNavGetHomeTerrainHeight(float *heightM) -{ - if (!posControl.gpsOrigin.valid) { - return false; - } - - const gpsLocation_t homeLoc = { - .lat = posControl.gpsOrigin.lat, - .lon = posControl.gpsOrigin.lon, - .alt = 0, - }; - - return terrainNavGetHeightAtLocation(&homeLoc, heightM); -} - bool terrainNavLookahead(float bearingDeg, float distanceM, float climbRatio, float escapeRatio, terrainNavLookaheadResult_t *result) { if (result == NULL || !terrainNavIsHealthy()) { diff --git a/src/main/terrain/terrain_nav.h b/src/main/terrain/terrain_nav.h index 603116e91d6..b133a98635e 100644 --- a/src/main/terrain/terrain_nav.h +++ b/src/main/terrain/terrain_nav.h @@ -55,9 +55,6 @@ bool terrainNavGetAGLCm(int32_t *aglCm); // miss the block is scheduled for loading and the call fails - retry later. bool terrainNavGetHeightAtLocation(const gpsLocation_t *loc, float *heightM); -// Terrain height (map datum, meters) at the GPS origin (home) -bool terrainNavGetHomeTerrainHeight(float *heightM); - // Walk the map from the current position along bearingDeg for distanceM, // accumulating achievable climb (climbRatio = m of climb per m travelled), // and report the worst height deficit found. A descending path (negative From d1258c87a98d66d20f85064feedfa0791d2fbc60 Mon Sep 17 00:00:00 2001 From: JB Date: Thu, 26 Nov 2026 15:32:08 -0500 Subject: [PATCH 2/3] fix(terrain): height reference no higher than the GPS altitude The height reference was the map height under the take-off spot alone, so an error of the map there shifted every terrain height of the flight. A map that reads x m too high at take-off made the aircraft fly x m lower than it believed, everywhere. The GPS altitude of the altitude zero (the GPS origin's altitude) is a second reading of the same height. The lower of the two is now used: a reference that is too high puts the aircraft closer to the ground than it believes, a reference that is too low only makes it fly higher. The GPS reading counts only with a 3D fix and an EPV under inav_max_eph_epv when the aircraft armed; otherwise the map alone is used, as before. What changes in flight: - map too high at take-off: was that much too low, now correct - map too low at take-off, or launched from above the mapped ground: flies that much higher, as before - GPS altitude reads high: nothing changes - GPS altitude reads low: flies that much higher SITL (3D cruise + TERRAIN AGL HOLD engaged at 50 m over flat ground, minimum 60 m, so the hold levels off at 70 m): - map 15 m too high round the take-off spot: levelled at 49.7 m and reported 65.7 m; now 70.0 m - GPS altitude 10 m low: 80.0 m (was 70.0 m) - map 15 m too low (85.0 m), GPS 10 m high (70.0 m), a second arm with FIRST_ARM and EACH_ARM, and a re-arm in the air: unchanged Co-Authored-By: Claude Fable 5.2 --- docs/Navigation Terrain Following.md | 2 ++ src/main/terrain/terrain.c | 31 ++++++++++++++++++++++++++-- src/main/terrain/terrain.h | 3 ++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/Navigation Terrain Following.md b/docs/Navigation Terrain Following.md index c35066b8dfe..521a20d8d16 100644 --- a/docs/Navigation Terrain Following.md +++ b/docs/Navigation Terrain Following.md @@ -75,6 +75,8 @@ The mode narrates itself on the OSD. In plain terms: **TERRAIN AHEAD!** talks ab | **TERRAIN LOST - ALT FROZEN** | Caution | The terrain data stopped coming while the mode was holding your height — the altitude target is **frozen at the last good value** (it never descends on dead data). | Never descend blind; navigate out on the frozen altitude (it resumes after a few seconds of good data). | | **TERRAIN LOOKAHEAD OFF** | Info | The forward scan is unavailable (no heading estimate, or ground speed below 3 m/s). | Only the reactive floor guards you now — do **not** expect the early “TERRAIN AHEAD!” warning while this shows. | +> **Where heights are measured from.** Height above ground is your estimated altitude minus how far the terrain under you rises above your take-off spot. The mode fixes that spot when you **arm**: it reads the map where the aircraft stands, and compares it with the GPS altitude there. It uses the **lower** of the two, because a take-off height that is too high would put you closer to the ground than the mode believes. A map error at your take-off spot therefore no longer carries into the whole flight when it reads high; when it reads low, or when you launch from above the mapped ground, the mode flies higher by that amount. + > **Seeing "TERRAIN! PULL UP!" often while riding low? That is the geometry of the floor, not a fault.** The floor (default 60 m) is already the lowest safe height, so the warning band sits only 5 m under it — below a minimum there is no room to spare. And the mode does not glue AGL to the ground 1:1: the climb is rate-limited, so on rising slopes the real height sags a little under the held value, which is exactly when the warning earns its keep. **If you want quiet — hold height in hand:** engage at 80 m and it holds 80; the warning still starts only below 55 m, so you fly with 25 m of calm. Engage below the floor and it simply climbs to ~10 m above it by itself (*TERRAIN AUTO CLIMB TO MIN*), no alarm while the climb is winning. > **About `nav_max_altitude`:** that ceiling is **barometric altitude above your Home point** — it is *not* terrain-AGL aware. So over rising ground the terrain can legitimately need more altitude than the ceiling allows; that is exactly when *TERRAIN VS MAX ALT* appears. diff --git a/src/main/terrain/terrain.c b/src/main/terrain/terrain.c index 523030eaf74..d7acae72675 100644 --- a/src/main/terrain/terrain.c +++ b/src/main/terrain/terrain.c @@ -81,6 +81,14 @@ static struct { * where the GPS origin was set by the first fix. So the map is read where the aircraft * stands while it is disarmed, and kept with the altitude it had there: the reference is * that map height minus that altitude. Arming keeps the last such point for the flight. + * + * The GPS altitude of the altitude zero (the GPS origin's altitude) is a second reading of + * the same height, on the same datum as the map. The two differ by the map's error at the + * take-off spot, the GPS altitude error and the height of the aircraft above the ground when + * it armed. The lower of the two is used: a reference that is too high puts the ground too + * low and the aircraft closer to it than it believes, a reference that is too low only + * makes it fly higher. So a map that is too high at the take-off spot no longer lowers the + * whole flight, and a GPS altitude that reads high changes nothing. */ typedef struct { bool located; // a position and altitude have been recorded @@ -88,6 +96,7 @@ typedef struct { float altitudeCm; // estimated altitude at that position, local frame bool heightFound; // the map has answered for that position float heightM; + bool gpsUsable; // the GPS altitude was good enough there to be the second reading } terrainGroundPoint_t; static terrainGroundPoint_t groundDisarmed; // follows the aircraft while it is disarmed @@ -173,6 +182,8 @@ static void terrainGroundPointUpdate(void) point->location = here; point->altitudeCm = pos->z; point->heightFound = false; + point->gpsUsable = STATE(GPS_FIX) && gpsSol.fixType == GPS_FIX_3D + && gpsSol.epv < positionEstimationConfig()->max_eph_epv; } } @@ -186,14 +197,30 @@ static void terrainGroundPointUpdate(void) } } -bool terrainGetReferenceHeightM(float *heightM) +// The two readings of the height reference: from the map, and from the GPS altitude +static bool terrainReferenceReadings(float *mapM, bool *gpsUsable, float *gpsM) { const terrainGroundPoint_t *point = terrainGroundPointInUse(); if (!point->located || !point->heightFound) { return false; } - *heightM = point->heightM - point->altitudeCm * 0.01f; + *mapM = point->heightM - point->altitudeCm * 0.01f; + *gpsUsable = point->gpsUsable && posControl.gpsOrigin.valid; + *gpsM = posControl.gpsOrigin.alt * 0.01f; + return true; +} + +bool terrainGetReferenceHeightM(float *heightM) +{ + float mapM; + float gpsM; + bool gpsUsable; + if (!terrainReferenceReadings(&mapM, &gpsUsable, &gpsM)) { + return false; + } + + *heightM = (gpsUsable && gpsM < mapM) ? gpsM : mapM; return true; } diff --git a/src/main/terrain/terrain.h b/src/main/terrain/terrain.h index 5cf9c1287af..4ce088c5e80 100644 --- a/src/main/terrain/terrain.h +++ b/src/main/terrain/terrain.h @@ -167,6 +167,7 @@ int32_t terrainGetLastAMSL(void); float getHeightAmslMeters(const gpsLocation_t *loc); int32_t terrainGetLastDistanceCm(void); // The map height (m) that the navigation's altitude zero stands for: the map where the -// aircraft stood when it armed, less its altitude there. False until the map has answered +// aircraft stood when it armed, less its altitude there, or the GPS altitude of the altitude +// zero when that is lower. False until the map has answered bool terrainGetReferenceHeightM(float *heightM); From dafb39b7f1c5595e943b65121be90f51b760d371 Mon Sep 17 00:00:00 2001 From: JB Date: Thu, 26 Nov 2026 17:26:48 -0500 Subject: [PATCH 3/3] feat(terrain): OSD message when the map and the GPS altitude disagree at take-off With the hold engaged, "TERRAIN: MAP/GPS ALT DIFFER" shows when the two readings of the take-off height are more than 5 m apart: the map is wrong at the take-off spot, the GPS altitude was off at arming, or the aircraft armed well above the mapped ground. The lower reading is in use either way, so the message tells the pilot why the heights may be off, and that the hold errs toward flying higher. Adds the message to the table in Navigation Terrain Following.md. Co-Authored-By: Claude Fable 5.2 --- docs/Navigation Terrain Following.md | 1 + src/main/io/osd.c | 6 ++++++ src/main/io/osd.h | 1 + src/main/terrain/terrain.c | 14 ++++++++++++++ src/main/terrain/terrain.h | 6 ++++++ 5 files changed, 28 insertions(+) diff --git a/docs/Navigation Terrain Following.md b/docs/Navigation Terrain Following.md index 521a20d8d16..56eb22331d8 100644 --- a/docs/Navigation Terrain Following.md +++ b/docs/Navigation Terrain Following.md @@ -74,6 +74,7 @@ The mode narrates itself on the OSD. In plain terms: **TERRAIN AHEAD!** talks ab | **TERRAIN NOT READY** | Info | Engagement refused — no usable terrain data yet. | Fly stock cruise; check tiles / SD card if it persists. | | **TERRAIN LOST - ALT FROZEN** | Caution | The terrain data stopped coming while the mode was holding your height — the altitude target is **frozen at the last good value** (it never descends on dead data). | Never descend blind; navigate out on the frozen altitude (it resumes after a few seconds of good data). | | **TERRAIN LOOKAHEAD OFF** | Info | The forward scan is unavailable (no heading estimate, or ground speed below 3 m/s). | Only the reactive floor guards you now — do **not** expect the early “TERRAIN AHEAD!” warning while this shows. | +| **TERRAIN: MAP/GPS ALT DIFFER** | Info | The terrain map and the GPS altitude put your take-off spot more than 5 m apart: the map is wrong there, the GPS altitude was off when you armed, or you armed well above the mapped ground. The mode is using the lower of the two, so it errs toward flying higher. | Expect heights above ground to be off by up to that difference. If it shows at one site every time, the map is wrong there. | > **Where heights are measured from.** Height above ground is your estimated altitude minus how far the terrain under you rises above your take-off spot. The mode fixes that spot when you **arm**: it reads the map where the aircraft stands, and compares it with the GPS altitude there. It uses the **lower** of the two, because a take-off height that is too high would put you closer to the ground than the mode believes. A map error at your take-off spot therefore no longer carries into the whole flight when it reads high; when it reads low, or when you launch from above the mapped ground, the mode flies higher by that amount. diff --git a/src/main/io/osd.c b/src/main/io/osd.c index 61816d4deaa..b8966679d63 100644 --- a/src/main/io/osd.c +++ b/src/main/io/osd.c @@ -6379,6 +6379,12 @@ textAttributes_t osdGetSystemMessage(char *buff, size_t buff_size, bool isCenter && terrainNavHoldGetWarning() != TERRAIN_NAV_HOLD_WARN_NONE) { ADD_MSG(OSD_MESSAGE_STR(OSD_MSG_TERRAIN_AUTO_CLIMB)); } + /* ADDS MAXIMUM OF 1 MESSAGE TO TOTAL: the map and the GPS altitude + * disagree about the take-off height, so the heights the hold works + * with may be off by that much (the lower reference is in use) */ + if (terrainNavHoldIsEngaged() && terrainReferenceReadingsDisagree()) { + ADD_MSG(OSD_MESSAGE_STR(OSD_MSG_TERRAIN_REFERENCE)); + } #endif if (STATE(AIRPLANE)) { /* ADDS MAXIMUM OF 3 MESSAGES TO TOTAL */ #ifdef USE_FW_AUTOLAND diff --git a/src/main/io/osd.h b/src/main/io/osd.h index 66155b25866..337fece6460 100644 --- a/src/main/io/osd.h +++ b/src/main/io/osd.h @@ -182,6 +182,7 @@ #define OSD_MSG_TERRAIN_NO_HEADING "TERRAIN LOOKAHEAD OFF" #define OSD_MSG_TERRAIN_AUTO_CLIMB "TERRAIN AUTO CLIMB TO MIN" #define OSD_MSG_TERRAIN_AHEAD "TERRAIN AHEAD!" +#define OSD_MSG_TERRAIN_REFERENCE "TERRAIN: MAP/GPS ALT DIFFER" #endif #ifdef USE_ADSB diff --git a/src/main/terrain/terrain.c b/src/main/terrain/terrain.c index d7acae72675..994a7b7498d 100644 --- a/src/main/terrain/terrain.c +++ b/src/main/terrain/terrain.c @@ -36,6 +36,8 @@ /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// +#include + #include "platform.h" #ifdef USE_TERRAIN @@ -224,6 +226,18 @@ bool terrainGetReferenceHeightM(float *heightM) return true; } +bool terrainReferenceReadingsDisagree(void) +{ + float mapM; + float gpsM; + bool gpsUsable; + if (!terrainReferenceReadings(&mapM, &gpsUsable, &gpsM) || !gpsUsable) { + return false; + } + + return fabsf(mapM - gpsM) > TERRAIN_REFERENCE_DISAGREE_M; +} + /** * @brief Update the terrain subsystem. * diff --git a/src/main/terrain/terrain.h b/src/main/terrain/terrain.h index 4ce088c5e80..e7907e7ed65 100644 --- a/src/main/terrain/terrain.h +++ b/src/main/terrain/terrain.h @@ -31,6 +31,8 @@ #define TERRAIN_TASK_RATE_HZ 5 #define TERRAIN_NO_DATA_DELAY_MS 1000 +// The map and the GPS altitude are said to disagree about the take-off height beyond this +#define TERRAIN_REFERENCE_DISAGREE_M 5.0f #define TERRAIN_TER_FILE_FORMAT_VERSION 50 #define TERRAIN_SPACING 30 @@ -170,4 +172,8 @@ int32_t terrainGetLastDistanceCm(void); // aircraft stood when it armed, less its altitude there, or the GPS altitude of the altitude // zero when that is lower. False until the map has answered bool terrainGetReferenceHeightM(float *heightM); +// True when the map and the GPS altitude put the take-off spot more than +// TERRAIN_REFERENCE_DISAGREE_M apart: the map is wrong there, the GPS altitude is, or the +// aircraft armed well above the mapped ground. The lower one is in use either way +bool terrainReferenceReadingsDisagree(void);