diff --git a/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp b/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp index acec739df0..cc433e3986 100644 --- a/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp +++ b/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp @@ -215,7 +215,19 @@ bool NavEKF2_core::resetHeightDatum(void) stateStruct.position.z = 0.0f; // adjust the height of the EKF origin so that the origin plus baro height before and after the reset is the same if (validOrigin) { - ekfGpsRefHgt += (double)oldHgt; + const AP_GPS &gps = AP::gps(); + if (gps.status() >= AP_GPS::GPS_OK_FIX_3D) { + // if we have a good GPS lock then reset to the GPS + // altitude. This ensures the reported AMSL alt from + // getLLH() is equal to GPS altitude, while also ensuring + // that the relative alt is zero + ekfGpsRefHgt = gps.location().alt*0.01; + } else { + // if we don't have GPS lock then we shouldn't be doing a + // resetHeightDatum, but if we do then the best option is + // to maintain the old error + ekfGpsRefHgt += oldHgt; + } } // adjust the terrain state terrainState += oldHgt;