Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EKF: cope better with GPS jamming #24899

Merged
merged 19 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
fd1398d
AP_NavEKF3: Apply GPS quality checks following loss of 3D fix if velo…
Feb 21, 2023
092b8d8
AP_NavEKF3: Use last observed wind states to enable dead reckoning
Mar 5, 2023
3439de8
AP_NavEKF3: Strengthen protection against GPS jamming
Mar 21, 2023
2b08644
Tools: re-work copter and plane loss of GPS auto tests
Feb 21, 2023
09e84c7
AP_NavEKF3: Fix bug preventing use of default or synthetic airspeed
priseborough Sep 26, 2023
e602a46
AP_NavEKF3: Treat wind as truth when deadreckoning with no airspeed s…
priseborough Sep 26, 2023
115057d
Tools: Allow dead reckoning test longer to learn wind if no aspd sensor
priseborough Sep 26, 2023
409d0c5
AP_NavEKF3: Allow wind to relearn rapidly when GPS is re-enabled
priseborough Sep 26, 2023
810125c
AP_NavEKF3: Fix bug causing in flight yaw align to not complete
priseborough Sep 27, 2023
6d95ec0
AP_NavEKF3: Don't block no compass planes from running GPS alignment …
priseborough Sep 27, 2023
420fb5c
AP_NavEKF3: Log gpsGoodToAlign
priseborough Sep 27, 2023
5e3a063
AP_NavEKF3: Rework GPS jamming resiliency
priseborough Sep 27, 2023
c716578
Tools: Use GPS jamming option in EKF dead reckoning autotests
priseborough Sep 27, 2023
7cd6200
AP_NavEKF3: Rework method of synthesising airspeed for dead reckoning
priseborough Sep 27, 2023
576fb39
Tools: Disable DCM fallback for plane dead reckoning tests
priseborough Sep 27, 2023
de698c3
Tools: relax req accuracy for plane dead reckoning when not using air…
priseborough Sep 27, 2023
314110c
Tools: update AHRS_OPTIONS for dead reckoning test
priseborough Sep 29, 2023
dbfbb0e
autotest: enable LOG_REPLAY in deadreckoning test
tridge Nov 4, 2023
bffbda2
autotest: Reduce time threshold used in plane deadreckoning test
May 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Tools/autotest/arducopter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,22 @@ def GPSGlitchAuto(self, timeout=180):
self.show_gps_and_sim_positions(False)
raise e

# stop and test loss of GPS for a short time - it should resume GPS use without falling back into a non aiding mode
self.change_mode("LOITER")
self.set_parameters({
"SIM_GPS_DISABLE": 1,
})
self.delay_sim_time(2)
self.set_parameters({
"SIM_GPS_DISABLE": 0,
})
# regaining GPS should not result in it falling back to a non-navigation mode
self.wait_ekf_flags(mavutil.mavlink.ESTIMATOR_POS_HORIZ_ABS, 0, timeout=1)
# It should still be navigating after enougnh time has passed for any pending timeouts to activate.
self.delay_sim_time(10)
self.wait_ekf_flags(mavutil.mavlink.ESTIMATOR_POS_HORIZ_ABS, 0, timeout=1)
self.change_mode("AUTO")

# record time and position
tstart = self.get_sim_time()

Expand Down
41 changes: 37 additions & 4 deletions Tools/autotest/arduplane.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,9 @@ def AIRSPEED_AUTOCAL(self):
self.fly_home_land_and_disarm()

def deadreckoning_main(self, disable_airspeed_sensor=False):
self.set_parameter("EK3_OPTIONS", 1)
self.set_parameter("AHRS_OPTIONS", 3)
self.set_parameter("LOG_REPLAY", 1)
self.reboot_sitl()
self.wait_ready_to_arm()
self.gpi = None
Expand All @@ -2153,7 +2156,10 @@ def validate_global_position_int_against_simstate(mav, m):
if self.simstate is None:
return
divergence = self.get_distance_int(self.gpi, self.simstate)
max_allowed_divergence = 200
if disable_airspeed_sensor:
max_allowed_divergence = 300
else:
max_allowed_divergence = 150
if (time.time() - self.last_print > 1 or
divergence > self.max_divergence):
self.progress("position-estimate-divergence=%fm" % (divergence,))
Expand Down Expand Up @@ -2188,15 +2194,42 @@ def validate_global_position_int_against_simstate(mav, m):
frame=mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT,
)
self.wait_location(loc, accuracy=100)
self.progress("Stewing")
self.delay_sim_time(20)
self.progress("Orbit with GPS and learn wind")
# allow longer to learn wind if there is no airspeed sensor
if disable_airspeed_sensor:
self.delay_sim_time(60)
else:
self.delay_sim_time(20)
self.set_parameter("SIM_GPS_DISABLE", 1)
self.progress("Roasting")
self.progress("Continue orbit without GPS")
self.delay_sim_time(20)
self.change_mode("RTL")
self.wait_distance_to_home(100, 200, timeout=200)
# go into LOITER to create additonal time for a GPS re-enable test
self.change_mode("LOITER")
self.set_parameter("SIM_GPS_DISABLE", 0)
t_enabled = self.get_sim_time()
# The EKF should wait for GPS checks to pass when we are still able to navigate using dead reckoning
# to prevent bad GPS being used when coming back after loss of lock due to interence.
self.wait_ekf_flags(mavutil.mavlink.ESTIMATOR_POS_HORIZ_ABS, 0, timeout=15)
if self.get_sim_time() < (t_enabled+9):
raise NotAchievedException("GPS use re-started too quickly")
# wait for EKF and vehicle position to stabilise, then test response to jamming
self.delay_sim_time(20)

self.set_parameter("AHRS_OPTIONS", 1)
self.set_parameter("SIM_GPS_JAM", 1)
self.delay_sim_time(10)
self.set_parameter("SIM_GPS_JAM", 0)
t_enabled = self.get_sim_time()
# The EKF should wait for GPS checks to pass when we are still able to navigate using dead reckoning
# to prevent bad GPS being used when coming back after loss of lock due to interence.
# The EKF_STATUS_REPORT does not tell us when the good to align check passes, so the minimum time
# value of 3.0 seconds is an arbitrary value set on inspection of dataflash logs from this test
self.wait_ekf_flags(mavutil.mavlink.ESTIMATOR_POS_HORIZ_ABS, 0, timeout=15)
time_since_jamming_stopped = self.get_sim_time() - t_enabled
if time_since_jamming_stopped < 3:
raise NotAchievedException("GPS use re-started %f sec after jamming stopped" % time_since_jamming_stopped)
self.set_rc(3, 1000)
self.fly_home_land_and_disarm()
self.progress("max-divergence: %fm" % (self.max_divergence,))
Expand Down
7 changes: 7 additions & 0 deletions libraries/AP_NavEKF3/AP_NavEKF3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,13 @@ const AP_Param::GroupInfo NavEKF3::var_info2[] = {
// @Units: m
AP_GROUPINFO("GPS_VACC_MAX", 10, NavEKF3, _gpsVAccThreshold, 0.0f),

// @Param: OPTIONS
// @DisplayName: Optional EKF behaviour
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally the Display name should start with the feature or subsystem so a better name would be, "EKF3 Optional Behaviour".

For the Description we should remove the first part, "This controls optional EKF behaviour.". The "This controls" part in particular is not necessary because all parameters control something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created a commit at the end of this branch to resolve this and fix two other formatting issues. https://github.com/rmackay9/rmackay9-ardupilot/commits/tridge-pr-ekf-gps-check-fix/

// @Description: This controls optional EKF behaviour. Setting JammingExpected will change the EKF nehaviour such that if dead reckoning navigation is possible it will require the preflight alignment GPS quality checks controlled by EK3_GPS_CHECK and EK3_CHECK_SCALE to pass before resuming GPS use if GPS lock is lost for more than 2 seconds to prevent bad
// @Bitmask: 0:JammingExpected
// @User: Advanced
AP_GROUPINFO("OPTIONS", 11, NavEKF3, _options, 0),

AP_GROUPEND
};

Expand Down
7 changes: 7 additions & 0 deletions libraries/AP_NavEKF3/AP_NavEKF3.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,12 @@ class NavEKF3 {
AP_Int8 _primary_core; // initial core number
AP_Enum<LogLevel> _log_level; // log verbosity level
AP_Float _gpsVAccThreshold; // vertical accuracy threshold to use GPS as an altitude source
AP_Int32 _options; // bit mask of processing options

// enum for processing options
enum class Options {
JammingExpected = (1<<0),
};

// Possible values for _flowUse
#define FLOW_USE_NONE 0
Expand Down Expand Up @@ -487,6 +493,7 @@ class NavEKF3 {
const uint8_t extNavIntervalMin_ms = 20; // The minimum allowed time between measurements from external navigation sensors (msec)
const float maxYawEstVelInnov = 2.0f; // Maximum acceptable length of the velocity innovation returned by the EKF-GSF yaw estimator (m/s)
const uint16_t deadReckonDeclare_ms = 1000; // Time without equivalent position or velocity observation to constrain drift before dead reckoning is declared (msec)
const uint16_t gpsNoFixTimeout_ms = 2000; // Time without a fix required to reset GPS alignment checks when EK3_OPTIONS bit 0 is set (msec)

// time at start of current filter update
uint64_t imuSampleTime_us;
Expand Down
10 changes: 4 additions & 6 deletions libraries/AP_NavEKF3/AP_NavEKF3_AirDataFusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void NavEKF3_core::FuseAirspeed()
zero_range(&Kfusion[0], 16, 21);
}

if (tasDataDelayed.allowFusion && !inhibitWindStates) {
if (tasDataDelayed.allowFusion && !inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = SK_TAS[0]*(P[22][4]*SH_TAS[2] - P[22][22]*SH_TAS[2] + P[22][5]*SK_TAS[1] - P[22][23]*SK_TAS[1] + P[22][6]*vd*SH_TAS[0]);
Kfusion[23] = SK_TAS[0]*(P[23][4]*SH_TAS[2] - P[23][22]*SH_TAS[2] + P[23][5]*SK_TAS[1] - P[23][23]*SK_TAS[1] + P[23][6]*vd*SH_TAS[0]);
} else {
Expand Down Expand Up @@ -212,8 +212,10 @@ void NavEKF3_core::SelectTasFusion()
readAirSpdData();

// if the filter is initialised, wind states are not inhibited and we have data to fuse, then perform TAS fusion

if (tasDataToFuse && statesInitialised && !inhibitWindStates) {
FuseAirspeed();
tasDataToFuse = false;
prevTasStep_ms = imuSampleTime_ms;
}
}
Expand Down Expand Up @@ -256,10 +258,6 @@ void NavEKF3_core::SelectBetaDragFusion()
// we are required to correct only wind states
airDataFusionWindOnly = true;
}
// Fuse estimated airspeed to aid wind estimation
if (usingDefaultAirspeed) {
FuseAirspeed();
}
FuseSideslip();
prevBetaDragStep_ms = imuSampleTime_ms;
}
Expand Down Expand Up @@ -424,7 +422,7 @@ void NavEKF3_core::FuseSideslip()
zero_range(&Kfusion[0], 16, 21);
}

if (!inhibitWindStates) {
if (!inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = SK_BETA[0]*(P[22][0]*SK_BETA[5] + P[22][1]*SK_BETA[4] - P[22][4]*SK_BETA[1] + P[22][5]*SK_BETA[2] + P[22][2]*SK_BETA[6] + P[22][6]*SK_BETA[3] - P[22][3]*SK_BETA[7] + P[22][22]*SK_BETA[1] - P[22][23]*SK_BETA[2]);
Kfusion[23] = SK_BETA[0]*(P[23][0]*SK_BETA[5] + P[23][1]*SK_BETA[4] - P[23][4]*SK_BETA[1] + P[23][5]*SK_BETA[2] + P[23][2]*SK_BETA[6] + P[23][6]*SK_BETA[3] - P[23][3]*SK_BETA[7] + P[23][22]*SK_BETA[1] - P[23][23]*SK_BETA[2]);
} else {
Expand Down
22 changes: 17 additions & 5 deletions libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void NavEKF3_core::setWindMagStateLearningMode()
PV_AidingMode != AID_NONE;
if (!inhibitWindStates && !canEstimateWind) {
inhibitWindStates = true;
lastAspdEstIsValid = false;
updateStateIndexLim();
} else if (inhibitWindStates && canEstimateWind &&
(sq(stateStruct.velocity.x) + sq(stateStruct.velocity.y) > sq(5.0f) || dragFusionEnabled)) {
Expand All @@ -77,7 +78,7 @@ void NavEKF3_core::setWindMagStateLearningMode()
Vector3F tempEuler;
stateStruct.quat.to_euler(tempEuler.x, tempEuler.y, tempEuler.z);
ftype trueAirspeedVariance;
const bool haveAirspeedMeasurement = usingDefaultAirspeed || (tasDataDelayed.allowFusion && (imuDataDelayed.time_ms - tasDataDelayed.time_ms < 500) && useAirspeed());
const bool haveAirspeedMeasurement = (tasDataDelayed.allowFusion && (imuDataDelayed.time_ms - tasDataDelayed.time_ms < 500) && useAirspeed());
if (haveAirspeedMeasurement) {
trueAirspeedVariance = constrain_ftype(tasDataDelayed.tasVariance, WIND_VEL_VARIANCE_MIN, WIND_VEL_VARIANCE_MAX);
const ftype windSpeed = sqrtF(sq(stateStruct.velocity.x) + sq(stateStruct.velocity.y)) - tasDataDelayed.tas;
Expand Down Expand Up @@ -337,8 +338,17 @@ void NavEKF3_core::setAidingMode()
// Check if attitude drift has been constrained by a measurement source
bool attAiding = posUsed || gpsVelUsed || optFlowUsed || airSpdUsed || dragUsed || rngBcnUsed || bodyOdmUsed;

// check if velocity drift has been constrained by a measurement source
bool velAiding = gpsVelUsed || airSpdUsed || dragUsed || optFlowUsed || bodyOdmUsed;
// Check if velocity drift has been constrained by a measurement source
// Currently these are all the same source as will stabilise attitude because we do not currently have
// a sensor that only observes attitude
velAiding = posUsed || gpsVelUsed || optFlowUsed || airSpdUsed || dragUsed || rngBcnUsed || bodyOdmUsed;

// Store the last valid airspeed estimate
windStateIsObservable = !inhibitWindStates && (posUsed || gpsVelUsed || optFlowUsed || rngBcnUsed || bodyOdmUsed);
if (windStateIsObservable) {
lastAirspeedEstimate = (stateStruct.velocity - Vector3F(stateStruct.wind_vel.x, stateStruct.wind_vel.y, 0.0F)).length();
lastAspdEstIsValid = true;
}

// check if position drift has been constrained by a measurement source
bool posAiding = posUsed || rngBcnUsed;
Expand Down Expand Up @@ -684,9 +694,11 @@ bool NavEKF3_core::setOrigin(const Location &loc)
return true;
}

// record a yaw reset event
void NavEKF3_core::recordYawReset()
// record all requested yaw resets completed
void NavEKF3_core::recordYawResetsCompleted()
{
gpsYawResetRequest = false;
magYawResetRequest = false;
yawAlignComplete = true;
if (inFlight) {
finalInflightYawInit = true;
Expand Down
4 changes: 3 additions & 1 deletion libraries/AP_NavEKF3/AP_NavEKF3_Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ void NavEKF3_core::Log_Write_XKFS(uint64_t time_us) const
baro_index : selected_baro,
gps_index : selected_gps,
airspeed_index : getActiveAirspeed(),
source_set : frontend->sources.getPosVelYawSourceSet()
source_set : frontend->sources.getPosVelYawSourceSet(),
gps_good_to_align : gpsGoodToAlign,
wait_for_gps_checks : waitingForGpsChecks
};
AP::logger().WriteBlock(&pkt, sizeof(pkt));
}
Expand Down
40 changes: 19 additions & 21 deletions libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ void NavEKF3_core::realignYawGPS(bool emergency_reset)
// If the angles disagree by more than 45 degrees and GPS innovations are large or no previous yaw alignment, we declare the magnetic yaw as bad
bool badMagYaw = ((yawErr > 0.7854f) && (velTestRatio > 1.0f) && (PV_AidingMode == AID_ABSOLUTE)) || !yawAlignComplete;

// get yaw variance from GPS speed uncertainty
const ftype gpsVelAcc = fmaxF(gpsSpdAccuracy, ftype(frontend->_gpsHorizVelNoise));
const ftype gps_yaw_variance = sq(asinF(constrain_float(gpsVelAcc/gpsDataDelayed.vel.xy().length(), -1.0F, 1.0F)));
if (gps_yaw_variance < sq(radians(GPS_VEL_YAW_ALIGN_MAX_ANG_ERR))) {
yawAlignGpsValidCount++;
} else {
yawAlignGpsValidCount = 0;
}

// correct yaw angle using GPS ground course if compass yaw bad
if (badMagYaw) {
// attempt to use EKF-GSF estimate if available as it is more robust to GPS glitches
Expand All @@ -163,16 +172,6 @@ void NavEKF3_core::realignYawGPS(bool emergency_reset)
return;
}

// get yaw variance from GPS speed uncertainty
const ftype gpsVelAcc = fmaxF(gpsSpdAccuracy, ftype(frontend->_gpsHorizVelNoise));
const ftype gps_yaw_variance = sq(asinF(constrain_float(gpsVelAcc/gpsDataDelayed.vel.xy().length(), -1.0F, 1.0F)));

if (gps_yaw_variance < sq(radians(GPS_VEL_YAW_ALIGN_MAX_ANG_ERR))) {
yawAlignGpsValidCount++;
} else {
yawAlignGpsValidCount = 0;
}

if (yawAlignGpsValidCount >= GPS_VEL_YAW_ALIGN_COUNT_THRESHOLD) {
yawAlignGpsValidCount = 0;
// keep roll and pitch and reset yaw
Expand All @@ -194,6 +193,10 @@ void NavEKF3_core::realignYawGPS(bool emergency_reset)
allMagSensorsFailed = false;
}
}
} else if (yawAlignGpsValidCount >= GPS_VEL_YAW_ALIGN_COUNT_THRESHOLD) {
// There is no need to do a yaw reset
yawAlignGpsValidCount = 0;
recordYawResetsCompleted();
}
} else {
yawAlignGpsValidCount = 0;
Expand Down Expand Up @@ -642,7 +645,7 @@ void NavEKF3_core::FuseMagnetometer()
}

// zero Kalman gains to inhibit wind state estimation
if (!inhibitWindStates) {
if (!inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = SK_MX[0]*(P[22][19] + P[22][1]*SH_MAG[0] - P[22][2]*SH_MAG[1] + P[22][3]*SH_MAG[2] + P[22][0]*SK_MX[2] - P[22][16]*SK_MX[1] + P[22][17]*SK_MX[4] - P[22][18]*SK_MX[3]);
Kfusion[23] = SK_MX[0]*(P[23][19] + P[23][1]*SH_MAG[0] - P[23][2]*SH_MAG[1] + P[23][3]*SH_MAG[2] + P[23][0]*SK_MX[2] - P[23][16]*SK_MX[1] + P[23][17]*SK_MX[4] - P[23][18]*SK_MX[3]);
} else {
Expand Down Expand Up @@ -725,7 +728,7 @@ void NavEKF3_core::FuseMagnetometer()
}

// zero Kalman gains to inhibit wind state estimation
if (!inhibitWindStates) {
if (!inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = SK_MY[0]*(P[22][20] + P[22][0]*SH_MAG[2] + P[22][1]*SH_MAG[1] + P[22][2]*SH_MAG[0] - P[22][3]*SK_MY[2] - P[22][17]*SK_MY[1] - P[22][16]*SK_MY[3] + P[22][18]*SK_MY[4]);
Kfusion[23] = SK_MY[0]*(P[23][20] + P[23][0]*SH_MAG[2] + P[23][1]*SH_MAG[1] + P[23][2]*SH_MAG[0] - P[23][3]*SK_MY[2] - P[23][17]*SK_MY[1] - P[23][16]*SK_MY[3] + P[23][18]*SK_MY[4]);
} else {
Expand Down Expand Up @@ -809,7 +812,7 @@ void NavEKF3_core::FuseMagnetometer()
}

// zero Kalman gains to inhibit wind state estimation
if (!inhibitWindStates) {
if (!inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = SK_MZ[0]*(P[22][21] + P[22][0]*SH_MAG[1] - P[22][1]*SH_MAG[2] + P[22][3]*SH_MAG[0] + P[22][2]*SK_MZ[2] + P[22][18]*SK_MZ[1] + P[22][16]*SK_MZ[4] - P[22][17]*SK_MZ[3]);
Kfusion[23] = SK_MZ[0]*(P[23][21] + P[23][0]*SH_MAG[1] - P[23][1]*SH_MAG[2] + P[23][3]*SH_MAG[0] + P[23][2]*SK_MZ[2] + P[23][18]*SK_MZ[1] + P[23][16]*SK_MZ[4] - P[23][17]*SK_MZ[3]);
} else {
Expand Down Expand Up @@ -1345,7 +1348,7 @@ void NavEKF3_core::FuseDeclination(ftype declErr)
zero_range(&Kfusion[0], 16, 21);
}

if (!inhibitWindStates) {
if (!inhibitWindStates && !treatWindStatesAsTruth) {
Kfusion[22] = -t4*t13*(P[22][16]*magE-P[22][17]*magN);
Kfusion[23] = -t4*t13*(P[23][16]*magE-P[23][17]*magN);
} else {
Expand Down Expand Up @@ -1579,7 +1582,7 @@ bool NavEKF3_core::EKFGSF_resetMainFilterYaw(bool emergency_reset)
}

// record the yaw reset event
recordYawReset();
recordYawResetsCompleted();

// reset velocity and position states to GPS - if yaw is fixed then the filter should start to operate correctly
ResetVelocity(resetDataSource::DEFAULT);
Expand Down Expand Up @@ -1655,10 +1658,5 @@ void NavEKF3_core::resetQuatStateYawOnly(ftype yaw, ftype yawVariance, rotationO
lastYawReset_ms = imuSampleTime_ms;

// record the yaw reset event
recordYawReset();

// clear all pending yaw reset requests
gpsYawResetRequest = false;
magYawResetRequest = false;

recordYawResetsCompleted();
}
Loading
Loading