2016-07-14 02:08:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
|
|
|
#include "AP_NavEKF3_core.h"
|
2020-11-05 19:30:16 -04:00
|
|
|
#include <AP_DAL/AP_DAL.h>
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
/* Monitor GPS data to see if quality is good enough to initialise the EKF
|
2019-02-22 19:35:24 -04:00
|
|
|
Monitor magnetometer innovations to see if the heading is good enough to use GPS
|
2016-07-14 02:08:43 -03:00
|
|
|
Return true if all criteria pass for 10 seconds
|
|
|
|
|
2020-08-11 02:03:49 -03:00
|
|
|
We also record the failure reason so that pre_arm_check()
|
2016-07-14 02:08:43 -03:00
|
|
|
can give a good report to the user on why arming is failing
|
|
|
|
*/
|
2019-07-02 00:42:52 -03:00
|
|
|
void NavEKF3_core::calcGpsGoodToAlign(void)
|
2016-07-14 02:08:43 -03:00
|
|
|
{
|
2023-09-26 22:04:35 -03:00
|
|
|
if (inFlight && !finalInflightYawInit && assume_zero_sideslip() && !use_compass()) {
|
2016-07-14 02:08:43 -03:00
|
|
|
// this is a special case where a plane has launched without magnetometer
|
|
|
|
// is now in the air and needs to align yaw to the GPS and start navigating as soon as possible
|
2019-07-02 00:42:52 -03:00
|
|
|
gpsGoodToAlign = true;
|
|
|
|
return;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// User defined multiplier to be applied to check thresholds
|
2021-05-04 08:12:23 -03:00
|
|
|
ftype checkScaler = 0.01f*(ftype)frontend->_gpsCheckScaler;
|
2016-07-14 02:08:43 -03:00
|
|
|
|
2019-07-02 00:42:52 -03:00
|
|
|
if (gpsGoodToAlign) {
|
|
|
|
/*
|
|
|
|
if we have already passed GPS alignment checks then raise
|
|
|
|
the check threshold so that we have some hysterisis and
|
|
|
|
don't continuously change from able to arm to not able to
|
|
|
|
arm
|
|
|
|
*/
|
|
|
|
checkScaler *= 1.3f;
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:08:43 -03:00
|
|
|
// If we have good magnetometer consistency and bad innovations for longer than 5 seconds then we reset heading and field states
|
|
|
|
// This enables us to handle large changes to the external magnetic field environment that occur before arming
|
|
|
|
if ((magTestRatio.x <= 1.0f && magTestRatio.y <= 1.0f && yawTestRatio <= 1.0f) || !consistentMagData) {
|
|
|
|
magYawResetTimer_ms = imuSampleTime_ms;
|
|
|
|
}
|
|
|
|
if ((imuSampleTime_ms - magYawResetTimer_ms > 5000) && !motorsArmed) {
|
|
|
|
// request reset of heading and magnetic field states
|
|
|
|
magYawResetRequest = true;
|
|
|
|
// reset timer to ensure that bad magnetometer data cannot cause a heading reset more often than every 5 seconds
|
|
|
|
magYawResetTimer_ms = imuSampleTime_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for significant change in GPS position if disarmed which indicates bad GPS
|
|
|
|
// This check can only be used when the vehicle is stationary
|
2020-11-07 02:24:13 -04:00
|
|
|
const auto &gps = dal.gps();
|
2017-12-01 21:02:55 -04:00
|
|
|
|
2023-02-02 18:58:39 -04:00
|
|
|
const Location &gpsloc = gps.location(preferred_gps); // Current location
|
2021-05-04 08:12:23 -03:00
|
|
|
const ftype posFiltTimeConst = 10.0; // time constant used to decay position drift
|
2019-02-22 19:35:24 -04:00
|
|
|
// calculate time lapsed since last update and limit to prevent numerical errors
|
2021-05-04 08:12:23 -03:00
|
|
|
ftype deltaTime = constrain_ftype(ftype(imuDataDelayed.time_ms - lastPreAlignGpsCheckTime_ms)*0.001f,0.01f,posFiltTimeConst);
|
2016-07-14 02:08:43 -03:00
|
|
|
lastPreAlignGpsCheckTime_ms = imuDataDelayed.time_ms;
|
|
|
|
// Sum distance moved
|
2019-04-08 10:16:20 -03:00
|
|
|
gpsDriftNE += gpsloc_prev.get_distance(gpsloc);
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsloc_prev = gpsloc;
|
|
|
|
// Decay distance moved exponentially to zero
|
|
|
|
gpsDriftNE *= (1.0f - deltaTime/posFiltTimeConst);
|
2019-02-22 19:35:24 -04:00
|
|
|
// Clamp the filter state to prevent excessive persistence of large transients
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsDriftNE = MIN(gpsDriftNE,10.0f);
|
|
|
|
// Fail if more than 3 metres drift after filtering whilst on-ground
|
|
|
|
// This corresponds to a maximum acceptable average drift rate of 0.3 m/s or single glitch event of 3m
|
|
|
|
bool gpsDriftFail = (gpsDriftNE > 3.0f*checkScaler) && onGround && (frontend->_gpsCheck & MASK_GPS_POS_DRIFT);
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (gpsDriftFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
|
|
|
"GPS drift %.1fm (needs %.1f)", (double)gpsDriftNE, (double)(3.0f*checkScaler));
|
|
|
|
gpsCheckStatus.bad_horiz_drift = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_horiz_drift = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the vertical GPS vertical velocity is reasonable after noise filtering
|
|
|
|
bool gpsVertVelFail;
|
2020-11-30 06:01:20 -04:00
|
|
|
if (gpsDataNew.have_vz && onGround) {
|
2016-07-14 02:08:43 -03:00
|
|
|
// check that the average vertical GPS velocity is close to zero
|
|
|
|
gpsVertVelFilt = 0.1f * gpsDataNew.vel.z + 0.9f * gpsVertVelFilt;
|
2021-05-04 08:12:23 -03:00
|
|
|
gpsVertVelFilt = constrain_ftype(gpsVertVelFilt,-10.0f,10.0f);
|
|
|
|
gpsVertVelFail = (fabsF(gpsVertVelFilt) > 0.3f*checkScaler) && (frontend->_gpsCheck & MASK_GPS_VERT_SPD);
|
2016-07-14 02:08:43 -03:00
|
|
|
} else {
|
|
|
|
gpsVertVelFail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (gpsVertVelFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
2021-05-04 08:12:23 -03:00
|
|
|
"GPS vertical speed %.2fm/s (needs %.2f)", (double)fabsF(gpsVertVelFilt), (double)(0.3f*checkScaler));
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsCheckStatus.bad_vert_vel = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_vert_vel = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the horizontal GPS vertical velocity is reasonable after noise filtering
|
|
|
|
// This check can only be used if the vehicle is stationary
|
|
|
|
bool gpsHorizVelFail;
|
|
|
|
if (onGround) {
|
2021-09-11 07:34:45 -03:00
|
|
|
gpsHorizVelFilt = 0.1f * gpsDataDelayed.vel.xy().length() + 0.9f * gpsHorizVelFilt;
|
2021-05-04 08:12:23 -03:00
|
|
|
gpsHorizVelFilt = constrain_ftype(gpsHorizVelFilt,-10.0f,10.0f);
|
|
|
|
gpsHorizVelFail = (fabsF(gpsHorizVelFilt) > 0.3f*checkScaler) && (frontend->_gpsCheck & MASK_GPS_HORIZ_SPD);
|
2016-07-14 02:08:43 -03:00
|
|
|
} else {
|
|
|
|
gpsHorizVelFail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (gpsHorizVelFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
|
|
|
"GPS horizontal speed %.2fm/s (needs %.2f)", (double)gpsDriftNE, (double)(0.3f*checkScaler));
|
|
|
|
gpsCheckStatus.bad_horiz_vel = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_horiz_vel = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if horiziontal position accuracy not sufficient
|
|
|
|
float hAcc = 0.0f;
|
|
|
|
bool hAccFail;
|
2020-06-23 13:35:34 -03:00
|
|
|
if (gps.horizontal_accuracy(preferred_gps, hAcc)) {
|
2016-07-14 02:08:43 -03:00
|
|
|
hAccFail = (hAcc > 5.0f*checkScaler) && (frontend->_gpsCheck & MASK_GPS_POS_ERR);
|
|
|
|
} else {
|
|
|
|
hAccFail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (hAccFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
|
|
|
"GPS horiz error %.1fm (needs %.1f)", (double)hAcc, (double)(5.0f*checkScaler));
|
|
|
|
gpsCheckStatus.bad_hAcc = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_hAcc = false;
|
|
|
|
}
|
|
|
|
|
2017-02-22 14:53:31 -04:00
|
|
|
|
|
|
|
// Check for vertical GPS accuracy
|
|
|
|
float vAcc = 0.0f;
|
|
|
|
bool vAccFail = false;
|
2020-06-23 13:35:34 -03:00
|
|
|
if (gps.vertical_accuracy(preferred_gps, vAcc)) {
|
2017-02-22 14:53:31 -04:00
|
|
|
vAccFail = (vAcc > 7.5f * checkScaler) && (frontend->_gpsCheck & MASK_GPS_POS_ERR);
|
|
|
|
}
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (vAccFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2017-02-22 14:53:31 -04:00
|
|
|
sizeof(prearm_fail_string),
|
|
|
|
"GPS vert error %.1fm (needs < %.1f)", (double)vAcc, (double)(7.5f * checkScaler));
|
|
|
|
gpsCheckStatus.bad_vAcc = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_vAcc = false;
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:08:43 -03:00
|
|
|
// fail if reported speed accuracy greater than threshold
|
|
|
|
bool gpsSpdAccFail = (gpsSpdAccuracy > 1.0f*checkScaler) && (frontend->_gpsCheck & MASK_GPS_SPD_ERR);
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (gpsSpdAccFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
2018-06-02 20:53:20 -03:00
|
|
|
"GPS speed error %.1f (needs < %.1f)", (double)gpsSpdAccuracy, (double)(1.0f*checkScaler));
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsCheckStatus.bad_sAcc = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_sAcc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if satellite geometry is poor
|
2020-06-23 13:35:34 -03:00
|
|
|
bool hdopFail = (gps.get_hdop(preferred_gps) > 250) && (frontend->_gpsCheck & MASK_GPS_HDOP);
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (hdopFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string, sizeof(prearm_fail_string),
|
2020-06-23 13:35:34 -03:00
|
|
|
"GPS HDOP %.1f (needs 2.5)", (double)(0.01f * gps.get_hdop(preferred_gps)));
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsCheckStatus.bad_hdop = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_hdop = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if not enough sats
|
2020-06-23 13:35:34 -03:00
|
|
|
bool numSatsFail = (gps.num_sats(preferred_gps) < 6) && (frontend->_gpsCheck & MASK_GPS_NSATS);
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (numSatsFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string, sizeof(prearm_fail_string),
|
2020-06-23 13:35:34 -03:00
|
|
|
"GPS numsats %u (needs 6)", gps.num_sats(preferred_gps));
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsCheckStatus.bad_sats = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_sats = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if magnetometer innovations are outside limits indicating bad yaw
|
|
|
|
// with bad yaw we are unable to use GPS
|
|
|
|
bool yawFail;
|
|
|
|
if ((magTestRatio.x > 1.0f || magTestRatio.y > 1.0f || yawTestRatio > 1.0f) && (frontend->_gpsCheck & MASK_GPS_YAW_ERR)) {
|
|
|
|
yawFail = true;
|
|
|
|
} else {
|
|
|
|
yawFail = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report check result as a text string and bitmask
|
|
|
|
if (yawFail) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string,
|
2016-07-14 02:08:43 -03:00
|
|
|
sizeof(prearm_fail_string),
|
|
|
|
"Mag yaw error x=%.1f y=%.1f",
|
|
|
|
(double)magTestRatio.x,
|
|
|
|
(double)magTestRatio.y);
|
|
|
|
gpsCheckStatus.bad_yaw = true;
|
|
|
|
} else {
|
|
|
|
gpsCheckStatus.bad_yaw = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume failed first time through and notify user checks have started
|
|
|
|
if (lastGpsVelFail_ms == 0) {
|
2020-11-07 02:24:13 -04:00
|
|
|
dal.snprintf(prearm_fail_string, sizeof(prearm_fail_string), "EKF starting GPS checks");
|
2016-07-14 02:08:43 -03:00
|
|
|
lastGpsVelFail_ms = imuSampleTime_ms;
|
|
|
|
}
|
|
|
|
|
2019-07-02 00:42:52 -03:00
|
|
|
// record time of pass or fail
|
2017-02-22 14:53:31 -04:00
|
|
|
if (gpsSpdAccFail || numSatsFail || hdopFail || hAccFail || vAccFail || yawFail || gpsDriftFail || gpsVertVelFail || gpsHorizVelFail) {
|
2016-07-14 02:08:43 -03:00
|
|
|
lastGpsVelFail_ms = imuSampleTime_ms;
|
2019-07-02 00:42:52 -03:00
|
|
|
} else {
|
|
|
|
lastGpsVelPass_ms = imuSampleTime_ms;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
2019-07-02 00:42:52 -03:00
|
|
|
// continuous period of 10s without fail required to set healthy
|
|
|
|
// continuous period of 5s without pass required to set unhealthy
|
|
|
|
if (!gpsGoodToAlign && imuSampleTime_ms - lastGpsVelFail_ms > 10000) {
|
|
|
|
gpsGoodToAlign = true;
|
|
|
|
} else if (gpsGoodToAlign && imuSampleTime_ms - lastGpsVelPass_ms > 5000) {
|
|
|
|
gpsGoodToAlign = false;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
2023-02-20 20:31:38 -04:00
|
|
|
|
|
|
|
if (gpsGoodToAlign && waitingForGpsChecks) {
|
|
|
|
waitingForGpsChecks = false;
|
|
|
|
}
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update inflight calculaton that determines if GPS data is good enough for reliable navigation
|
|
|
|
void NavEKF3_core::calcGpsGoodForFlight(void)
|
|
|
|
{
|
2022-07-29 14:49:37 -03:00
|
|
|
// use simple criteria based on the GPS receiver's claimed vertical
|
|
|
|
// position accuracy and speed accuracy and the EKF innovation consistency
|
|
|
|
// checks
|
2016-07-14 02:08:43 -03:00
|
|
|
|
2022-07-29 14:49:37 -03:00
|
|
|
// set up variables and constants used by filter that is applied to GPS speed accuracy
|
2021-05-04 08:12:23 -03:00
|
|
|
const ftype alpha1 = 0.2f; // coefficient for first stage LPF applied to raw speed accuracy data
|
|
|
|
const ftype tau = 10.0f; // time constant (sec) of peak hold decay
|
2016-07-14 02:08:43 -03:00
|
|
|
if (lastGpsCheckTime_ms == 0) {
|
|
|
|
lastGpsCheckTime_ms = imuSampleTime_ms;
|
|
|
|
}
|
2021-05-04 08:12:23 -03:00
|
|
|
ftype dtLPF = (imuSampleTime_ms - lastGpsCheckTime_ms) * 1e-3f;
|
2016-07-14 02:08:43 -03:00
|
|
|
lastGpsCheckTime_ms = imuSampleTime_ms;
|
2021-05-04 08:12:23 -03:00
|
|
|
ftype alpha2 = constrain_ftype(dtLPF/tau,0.0f,1.0f);
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
// get the receivers reported speed accuracy
|
|
|
|
float gpsSpdAccRaw;
|
2020-11-07 02:24:13 -04:00
|
|
|
if (!dal.gps().speed_accuracy(preferred_gps, gpsSpdAccRaw)) {
|
2016-07-14 02:08:43 -03:00
|
|
|
gpsSpdAccRaw = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter the raw speed accuracy using a LPF
|
2021-05-04 08:12:23 -03:00
|
|
|
sAccFilterState1 = constrain_ftype((alpha1 * gpsSpdAccRaw + (1.0f - alpha1) * sAccFilterState1),0.0f,10.0f);
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
// apply a peak hold filter to the LPF output
|
|
|
|
sAccFilterState2 = MAX(sAccFilterState1,((1.0f - alpha2) * sAccFilterState2));
|
|
|
|
|
|
|
|
// Apply a threshold test with hysteresis to the filtered GPS speed accuracy data
|
|
|
|
if (sAccFilterState2 > 1.5f ) {
|
|
|
|
gpsSpdAccPass = false;
|
|
|
|
} else if(sAccFilterState2 < 1.0f) {
|
|
|
|
gpsSpdAccPass = true;
|
|
|
|
}
|
|
|
|
|
2022-07-29 14:49:37 -03:00
|
|
|
// Apply a threshold test with hysteresis to the GPS vertical position accuracy data
|
|
|
|
// Require a fail for one second and a pass for 3 seconds to transition
|
|
|
|
float gpsVAccRaw;
|
|
|
|
ftype gpsVAccThreshold = (ftype)frontend->_gpsVAccThreshold;
|
|
|
|
if (lastGpsVertAccFailTime_ms == 0) {
|
|
|
|
lastGpsVertAccFailTime_ms = imuSampleTime_ms;
|
|
|
|
lastGpsVertAccPassTime_ms = imuSampleTime_ms;
|
|
|
|
}
|
|
|
|
if (!dal.gps().vertical_accuracy(preferred_gps, gpsVAccRaw)) {
|
|
|
|
// No vertical accuracy data yet, let's treat it as a value above the threshold
|
|
|
|
gpsVAccRaw = gpsVAccThreshold + 1.0f;
|
|
|
|
}
|
|
|
|
if (gpsVAccThreshold <= 0 || gpsVAccRaw < gpsVAccThreshold) {
|
|
|
|
lastGpsVertAccPassTime_ms = imuSampleTime_ms;
|
|
|
|
} else {
|
|
|
|
lastGpsVertAccFailTime_ms = imuSampleTime_ms;
|
|
|
|
}
|
|
|
|
if ((imuSampleTime_ms - lastGpsVertAccPassTime_ms) > 1000) {
|
|
|
|
gpsVertAccPass = false;
|
|
|
|
} else if ((imuSampleTime_ms - lastGpsVertAccFailTime_ms) > 3000) {
|
|
|
|
gpsVertAccPass = true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:08:43 -03:00
|
|
|
// Apply a threshold test with hysteresis to the normalised position and velocity innovations
|
|
|
|
// Require a fail for one second and a pass for 10 seconds to transition
|
2022-07-29 14:49:37 -03:00
|
|
|
if (lastGpsInnovFailTime_ms == 0) {
|
|
|
|
lastGpsInnovFailTime_ms = imuSampleTime_ms;
|
|
|
|
lastGpsInnovPassTime_ms = imuSampleTime_ms;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
if (velTestRatio < 1.0f && posTestRatio < 1.0f) {
|
2022-07-29 14:49:37 -03:00
|
|
|
lastGpsInnovPassTime_ms = imuSampleTime_ms;
|
2016-07-14 02:08:43 -03:00
|
|
|
} else if (velTestRatio > 0.7f || posTestRatio > 0.7f) {
|
2022-07-29 14:49:37 -03:00
|
|
|
lastGpsInnovFailTime_ms = imuSampleTime_ms;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
2022-07-29 14:49:37 -03:00
|
|
|
if ((imuSampleTime_ms - lastGpsInnovPassTime_ms) > 1000) {
|
2016-07-14 02:08:43 -03:00
|
|
|
ekfInnovationsPass = false;
|
2022-07-29 14:49:37 -03:00
|
|
|
} else if ((imuSampleTime_ms - lastGpsInnovFailTime_ms) > 10000) {
|
2016-07-14 02:08:43 -03:00
|
|
|
ekfInnovationsPass = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// both GPS speed accuracy and EKF innovations must pass
|
|
|
|
gpsAccuracyGood = gpsSpdAccPass && ekfInnovationsPass;
|
2022-07-29 14:49:37 -03:00
|
|
|
|
|
|
|
// also update whether the GPS fix is good enough for altitude
|
|
|
|
gpsAccuracyGoodForAltitude = gpsAccuracyGood && gpsVertAccPass;
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if we are in flight or on ground
|
|
|
|
void NavEKF3_core::detectFlight()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
If we are a fly forward type vehicle (eg plane), then in-air status can be determined through a combination of speed and height criteria.
|
|
|
|
Because of the differing certainty requirements of algorithms that need the in-flight / on-ground status we use two booleans where
|
|
|
|
onGround indicates a high certainty we are not flying and inFlight indicates a high certainty we are flying. It is possible for
|
|
|
|
both onGround and inFlight to be false if the status is uncertain, but they cannot both be true.
|
|
|
|
|
|
|
|
If we are a plane as indicated by the assume_zero_sideslip() status, then different logic is used
|
|
|
|
|
|
|
|
TODO - this logic should be moved out of the EKF and into the flight vehicle code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (assume_zero_sideslip()) {
|
|
|
|
// To be confident we are in the air we use a criteria which combines arm status, ground speed, airspeed and height change
|
2021-05-04 08:12:23 -03:00
|
|
|
ftype gndSpdSq = sq(gpsDataNew.vel.x) + sq(gpsDataNew.vel.y);
|
2016-07-14 02:08:43 -03:00
|
|
|
bool highGndSpd = false;
|
|
|
|
bool highAirSpd = false;
|
|
|
|
bool largeHgtChange = false;
|
|
|
|
|
|
|
|
// trigger at 8 m/s airspeed
|
2020-11-07 02:24:13 -04:00
|
|
|
const auto *arsp = dal.airspeed();
|
2020-06-23 13:35:34 -03:00
|
|
|
if (arsp && arsp->healthy(selected_airspeed) && arsp->use(selected_airspeed)) {
|
2020-11-07 02:24:13 -04:00
|
|
|
if (arsp->get_airspeed(selected_airspeed) * dal.get_EAS2TAS() > 10.0f) {
|
2016-07-14 02:08:43 -03:00
|
|
|
highAirSpd = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 17:53:28 -04:00
|
|
|
// trigger on ground speed
|
2021-05-04 08:12:23 -03:00
|
|
|
const ftype gndSpdThresholdSq = sq(5.0f);
|
2021-01-02 17:53:28 -04:00
|
|
|
if (gndSpdSq > gndSpdThresholdSq + sq(gpsSpdAccuracy)) {
|
2016-07-14 02:08:43 -03:00
|
|
|
highGndSpd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger if more than 10m away from initial height
|
2021-05-04 08:12:23 -03:00
|
|
|
if (fabsF(hgtMea) > 10.0f) {
|
2016-07-14 02:08:43 -03:00
|
|
|
largeHgtChange = true;
|
|
|
|
}
|
|
|
|
|
2020-06-14 04:10:13 -03:00
|
|
|
if (motorsArmed) {
|
2016-07-14 02:08:43 -03:00
|
|
|
onGround = false;
|
2021-05-28 01:01:20 -03:00
|
|
|
if (highGndSpd && (dal.get_takeoff_expected() || highAirSpd || largeHgtChange)) {
|
2020-06-14 04:10:13 -03:00
|
|
|
// to a high certainty we are flying
|
|
|
|
inFlight = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// to a high certainty we are not flying
|
2016-07-14 02:08:43 -03:00
|
|
|
onGround = true;
|
|
|
|
inFlight = false;
|
|
|
|
}
|
2020-06-14 04:10:13 -03:00
|
|
|
|
2016-07-14 02:08:43 -03:00
|
|
|
} else {
|
|
|
|
// Non fly forward vehicle, so can only use height and motor arm status
|
|
|
|
|
|
|
|
// If the motors are armed then we could be flying and if they are not armed then we are definitely not flying
|
|
|
|
if (motorsArmed) {
|
|
|
|
onGround = false;
|
|
|
|
} else {
|
|
|
|
inFlight = false;
|
|
|
|
onGround = true;
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:56:14 -03:00
|
|
|
if (!onGround) {
|
|
|
|
// If height has increased since exiting on-ground, then we definitely are flying
|
|
|
|
if ((stateStruct.position.z - posDownAtTakeoff) < -1.5f) {
|
|
|
|
inFlight = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If rangefinder has increased since exiting on-ground, then we definitely are flying
|
|
|
|
if ((rangeDataNew.rng - rngAtStartOfFlight) > 0.5f) {
|
|
|
|
inFlight = true;
|
|
|
|
}
|
|
|
|
|
2017-06-19 00:17:31 -03:00
|
|
|
// If more than 5 seconds since likely_flying was set
|
|
|
|
// true, then set inFlight true
|
2020-11-07 02:24:13 -04:00
|
|
|
if (dal.get_time_flying_ms() > 5000) {
|
2017-06-18 02:56:14 -03:00
|
|
|
inFlight = true;
|
|
|
|
}
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store vehicle height and range prior to takeoff for use in post takeoff checks
|
|
|
|
if (onGround) {
|
|
|
|
// store vertical position at start of flight to use as a reference for ground relative checks
|
|
|
|
posDownAtTakeoff = stateStruct.position.z;
|
|
|
|
// store the range finder measurement which will be used as a reference to detect when we have taken off
|
|
|
|
rngAtStartOfFlight = rangeDataNew.rng;
|
|
|
|
// if the magnetic field states have been set, then continue to update the vertical position
|
|
|
|
// quaternion and yaw innovation snapshots to use as a reference when we start to fly.
|
|
|
|
if (magStateInitComplete) {
|
|
|
|
posDownAtLastMagReset = stateStruct.position.z;
|
|
|
|
quatAtLastMagReset = stateStruct.quat;
|
|
|
|
yawInnovAtLastMagReset = innovYaw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 03:48:08 -03:00
|
|
|
// handle reset of counters used to control how many times we will try to reset the yaw to the EKF-GSF value per flight
|
2021-02-09 05:44:47 -04:00
|
|
|
if ((!prevOnGround && onGround) || !gpsSpdAccPass) {
|
2020-11-15 19:58:47 -04:00
|
|
|
// disable filter bank
|
2020-03-10 03:48:08 -03:00
|
|
|
EKFGSF_run_filterbank = false;
|
2021-05-28 01:01:20 -03:00
|
|
|
} else if (yawEstimator != nullptr && !EKFGSF_run_filterbank && (inFlight || dal.get_takeoff_expected()) && gpsSpdAccPass) {
|
2020-11-15 19:58:47 -04:00
|
|
|
// flying or about to fly so reset counters and enable filter bank when GPS is good
|
2020-03-10 03:48:08 -03:00
|
|
|
EKFGSF_yaw_reset_ms = 0;
|
|
|
|
EKFGSF_yaw_reset_request_ms = 0;
|
|
|
|
EKFGSF_yaw_reset_count = 0;
|
2020-06-20 22:58:14 -03:00
|
|
|
EKFGSF_yaw_valid_count = 0;
|
2020-03-10 03:48:08 -03:00
|
|
|
EKFGSF_run_filterbank = true;
|
2020-06-07 19:43:51 -03:00
|
|
|
Vector3f gyroBias;
|
|
|
|
getGyroBias(gyroBias);
|
|
|
|
yawEstimator->setGyroBias(gyroBias);
|
2020-03-10 03:48:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// store current on-ground and in-air status for next time
|
|
|
|
prevOnGround = onGround;
|
|
|
|
prevInFlight = inFlight;
|
|
|
|
|
2016-07-14 02:08:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set to true if the terrain underneath is stable enough to be used as a height reference
|
|
|
|
// in combination with a range finder. Set to false if the terrain underneath the vehicle
|
2020-05-25 07:37:54 -03:00
|
|
|
// cannot be used as a height reference. Use to prevent range finder operation otherwise
|
2020-06-22 01:20:15 -03:00
|
|
|
// enabled by the combination of EK3_RNG_USE_HGT and EK3_RNG_USE_SPD parameters.
|
2016-07-14 02:08:43 -03:00
|
|
|
void NavEKF3_core::setTerrainHgtStable(bool val)
|
|
|
|
{
|
|
|
|
terrainHgtStable = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect takeoff for optical flow navigation
|
|
|
|
void NavEKF3_core::detectOptFlowTakeoff(void)
|
|
|
|
{
|
|
|
|
if (!onGround && !takeOffDetected && (imuSampleTime_ms - timeAtArming_ms) > 1000) {
|
|
|
|
// we are no longer confidently on the ground so check the range finder and gyro for signs of takeoff
|
2020-11-07 02:24:13 -04:00
|
|
|
const auto &ins = dal.ins();
|
2016-07-14 02:08:43 -03:00
|
|
|
Vector3f angRateVec;
|
|
|
|
Vector3f gyroBias;
|
|
|
|
getGyroBias(gyroBias);
|
2020-11-08 06:36:40 -04:00
|
|
|
angRateVec = ins.get_gyro(gyro_index_active) - gyroBias;
|
2016-07-14 02:08:43 -03:00
|
|
|
|
|
|
|
takeOffDetected = (takeOffDetected || (angRateVec.length() > 0.1f) || (rangeDataNew.rng > (rngAtStartOfFlight + 0.1f)));
|
|
|
|
} else if (onGround) {
|
|
|
|
// we are confidently on the ground so set the takeoff detected status to false
|
|
|
|
takeOffDetected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|