diff --git a/libraries/AP_NavEKF2/AP_NavEKF2_Measurements.cpp b/libraries/AP_NavEKF2/AP_NavEKF2_Measurements.cpp index 7cb84f4021..966caa30dd 100644 --- a/libraries/AP_NavEKF2/AP_NavEKF2_Measurements.cpp +++ b/libraries/AP_NavEKF2/AP_NavEKF2_Measurements.cpp @@ -580,7 +580,11 @@ void NavEKF2_core::readGpsData() // convert GPS measurements to local NED and save to buffer to be fused later if we have a valid origin if (validOrigin) { gpsDataNew.pos = EKF_origin.get_distance_NE(gpsloc); - gpsDataNew.hgt = (float)((double)0.01 * (double)gpsloc.alt - ekfGpsRefHgt); + if ((frontend->_originHgtMode & (1<<2)) == 0) { + gpsDataNew.hgt = (float)((double)0.01 * (double)gpsloc.alt - ekfGpsRefHgt); + } else { + gpsDataNew.hgt = 0.01 * (gpsloc.alt - EKF_origin.alt); + } storedGPS.push(gpsDataNew); // declare GPS available for use gpsNotAvailable = false; diff --git a/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp b/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp index b256809bdb..6666733e20 100644 --- a/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp +++ b/libraries/AP_NavEKF2/AP_NavEKF2_PosVelFusion.cpp @@ -222,14 +222,15 @@ bool NavEKF2_core::resetHeightDatum(void) // 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 += (int32_t)(100.0f * oldHgt); + EKF_origin.alt += (int32_t)(100.0f * oldHgt); } else { // 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 = AP::gps().location().alt*0.01; + EKF_origin.alt = AP::gps().location().alt; } + ekfGpsRefHgt = (double)0.01 * (double)EKF_origin.alt; } // set the terrain state to zero (on ground). The adjustment for