Introduce checkAndFixCovarianceUpdate(KHP) function

This commit is contained in:
kamilritz
2020-07-16 18:25:06 +02:00
committed by Mathieu Bresciani
parent 5c4a3d4576
commit 89bfcc2167
8 changed files with 44 additions and 123 deletions
+2 -14
View File
@@ -177,21 +177,9 @@ void Ekf::fuseAirspeed()
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
_fault_status.flags.bad_airspeed = false;
for (int i = 0; i < _k_num_states; i++) { _fault_status.flags.bad_airspeed = !healthy;
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
_fault_status.flags.bad_airspeed = true;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections
+14
View File
@@ -848,6 +848,20 @@ void Ekf::fixCovarianceErrors(bool force_symmetry)
} }
} }
// if the covariance correction will result in a negative variance, then
// the covariance matrix is unhealthy and must be corrected
bool Ekf::checkAndFixCovarianceUpdate(const SquareMatrix24f& KHP) {
bool healthy = true;
for (int i = 0; i < _k_num_states; i++) {
if (P(i, i) < KHP(i, i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
}
}
return healthy;
}
void Ekf::resetMagRelatedCovariances() void Ekf::resetMagRelatedCovariances()
{ {
resetQuatCov(); resetQuatCov();
+1 -11
View File
@@ -239,17 +239,7 @@ void Ekf::fuseDrag()
} }
} }
// if the covariance correction will result in a negative variance, then bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
for (int i = 0; i < _k_num_states; i++) {
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections
+4
View File
@@ -662,6 +662,10 @@ private:
Vector3f getVisionVelocityVarianceInEkfFrame() const; Vector3f getVisionVelocityVarianceInEkfFrame() const;
// if the covariance correction will result in a negative variance, then
// the covariance matrix is unhealthy and must be corrected
bool checkAndFixCovarianceUpdate(const SquareMatrix24f& KHP);
// limit the diagonal of the covariance matrix // limit the diagonal of the covariance matrix
// force symmetry when the argument is true // force symmetry when the argument is true
void fixCovarianceErrors(bool force_symmetry); void fixCovarianceErrors(bool force_symmetry);
+2 -14
View File
@@ -232,21 +232,9 @@ void Ekf::fuseGpsYaw()
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
_fault_status.flags.bad_hdg = false;
for (int i = 0; i < _k_num_states; i++) { _fault_status.flags.bad_hdg = !healthy;
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
_fault_status.flags.bad_hdg = true;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections
+14 -53
View File
@@ -143,13 +143,13 @@ void Ekf::fuseMag()
} }
// Perform an innovation consistency check and report the result // Perform an innovation consistency check and report the result
bool healthy = true; bool all_innovation_checks_passed = true;
for (uint8_t index = 0; index <= 2; index++) { for (uint8_t index = 0; index <= 2; index++) {
_mag_test_ratio(index) = sq(_mag_innov(index)) / (sq(math::max(_params.mag_innov_gate, 1.0f)) * _mag_innov_var(index)); _mag_test_ratio(index) = sq(_mag_innov(index)) / (sq(math::max(_params.mag_innov_gate, 1.0f)) * _mag_innov_var(index));
if (_mag_test_ratio(index) > 1.0f) { if (_mag_test_ratio(index) > 1.0f) {
healthy = false; all_innovation_checks_passed = false;
_innov_check_fail_status.value |= (1 << (index + 3)); _innov_check_fail_status.value |= (1 << (index + 3));
} else { } else {
@@ -161,7 +161,7 @@ void Ekf::fuseMag()
_yaw_test_ratio = 0.0f; _yaw_test_ratio = 0.0f;
// if any axis fails, abort the mag fusion // if any axis fails, abort the mag fusion
if (!healthy) { if (!all_innovation_checks_passed) {
return; return;
} }
@@ -169,10 +169,6 @@ void Ekf::fuseMag()
// before they are used to constrain heading drift // before they are used to constrain heading drift
const bool update_all_states = ((_imu_sample_delayed.time_us - _flt_mag_align_start_time) > (uint64_t)5e6); const bool update_all_states = ((_imu_sample_delayed.time_us - _flt_mag_align_start_time) > (uint64_t)5e6);
_fault_status.flags.bad_mag_x = false;
_fault_status.flags.bad_mag_y = false;
_fault_status.flags.bad_mag_z = false;
// Observation jacobian and Kalman gain vectors // Observation jacobian and Kalman gain vectors
Vector24f H_MAG; Vector24f H_MAG;
Vector24f Kfusion; Vector24f Kfusion;
@@ -367,28 +363,16 @@ void Ekf::fuseMag()
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
for (int i = 0; i < _k_num_states; i++) { if (index == 0) {
if (P(i,i) < KHP(i,i)) { _fault_status.flags.bad_mag_x = !healthy;
// zero rows and columns
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
//flag as unhealthy } else if (index == 1) {
healthy = false; _fault_status.flags.bad_mag_y = !healthy;
// update individual measurement health status } else if (index == 2) {
if (index == 0) { _fault_status.flags.bad_mag_z = !healthy;
_fault_status.flags.bad_mag_x = true;
} else if (index == 1) {
_fault_status.flags.bad_mag_y = true;
} else if (index == 2) {
_fault_status.flags.bad_mag_z = true;
}
}
} }
if (healthy) { if (healthy) {
@@ -698,21 +682,9 @@ void Ekf::updateQuaternion(const float innovation, const float variance, const f
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
_fault_status.flags.bad_hdg = false;
for (int i = 0; i < _k_num_states; i++) { _fault_status.flags.bad_hdg = !healthy;
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
_fault_status.flags.bad_hdg = true;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections
@@ -983,20 +955,9 @@ void Ekf::fuseDeclination(float decl_sigma)
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
_fault_status.flags.bad_mag_decl = false;
for (int i = 0; i < _k_num_states; i++) {
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false; _fault_status.flags.bad_mag_decl = !healthy;
_fault_status.flags.bad_mag_decl = true;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections
+5 -18
View File
@@ -425,9 +425,6 @@ void Ekf::fuseOptFlow()
} }
_fault_status.flags.bad_optflow_X = false;
_fault_status.flags.bad_optflow_Y = false;
for (uint8_t obs_index = 0; obs_index <= 1; obs_index++) { for (uint8_t obs_index = 0; obs_index <= 1; obs_index++) {
// copy the Kalman gain vector for the axis we are fusing // copy the Kalman gain vector for the axis we are fusing
@@ -465,23 +462,13 @@ void Ekf::fuseOptFlow()
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
for (int i = 0; i < _k_num_states; i++) { if (obs_index == 0) {
if (P(i,i) < KHP(i,i)) { _fault_status.flags.bad_optflow_X = !healthy;
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false; } else if (obs_index == 1) {
_fault_status.flags.bad_optflow_Y = !healthy;
if (obs_index == 0) {
_fault_status.flags.bad_optflow_X = true;
} else if (obs_index == 1) {
_fault_status.flags.bad_optflow_Y = true;
}
}
} }
if (healthy) { if (healthy) {
+2 -13
View File
@@ -224,20 +224,9 @@ void Ekf::fuseSideslip()
} }
} }
// if the covariance correction will result in a negative variance, then const bool healthy = checkAndFixCovarianceUpdate(KHP);
// the covariance matrix is unhealthy and must be corrected
bool healthy = true;
_fault_status.flags.bad_sideslip = false;
for (int i = 0; i < _k_num_states; i++) { _fault_status.flags.bad_sideslip = !healthy;
if (P(i,i) < KHP(i,i)) {
P.uncorrelateCovarianceSetVariance<1>(i, 0.0f);
healthy = false;
_fault_status.flags.bad_sideslip = true;
}
}
if (healthy) { if (healthy) {
// apply the covariance corrections // apply the covariance corrections