diff --git a/libraries/AP_Compass/AP_Compass_Backend.cpp b/libraries/AP_Compass/AP_Compass_Backend.cpp index 920eee63cd..bbca45ddb3 100644 --- a/libraries/AP_Compass/AP_Compass_Backend.cpp +++ b/libraries/AP_Compass/AP_Compass_Backend.cpp @@ -90,13 +90,15 @@ void AP_Compass_Backend::correct_field(Vector3f &mag, uint8_t i) #if AP_COMPASS_DIAGONALS_ENABLED // apply eliptical correction - Matrix3f mat( - diagonals.x, offdiagonals.x, offdiagonals.y, - offdiagonals.x, diagonals.y, offdiagonals.z, - offdiagonals.y, offdiagonals.z, diagonals.z - ); + if (!diagonals.is_zero()) { + Matrix3f mat( + diagonals.x, offdiagonals.x, offdiagonals.y, + offdiagonals.x, diagonals.y, offdiagonals.z, + offdiagonals.y, offdiagonals.z, diagonals.z + ); - mag = mat * mag; + mag = mat * mag; + } #endif #if COMPASS_MOT_ENABLED diff --git a/libraries/AP_Compass/AP_Compass_Calibration.cpp b/libraries/AP_Compass/AP_Compass_Calibration.cpp index 56c1236a03..9943fc8740 100644 --- a/libraries/AP_Compass/AP_Compass_Calibration.cpp +++ b/libraries/AP_Compass/AP_Compass_Calibration.cpp @@ -463,18 +463,20 @@ bool Compass::get_uncorrected_field(uint8_t instance, Vector3f &field) const // needed to remove the effects of the eliptical correction // when calculating new offsets const Vector3f &diagonals = get_diagonals(instance); - const Vector3f &offdiagonals = get_offdiagonals(instance); - Matrix3f mat { - diagonals.x, offdiagonals.x, offdiagonals.y, - offdiagonals.x, diagonals.y, offdiagonals.z, - offdiagonals.y, offdiagonals.z, diagonals.z - }; - if (!mat.invert()) { - return false; - } + if (!diagonals.is_zero()) { + const Vector3f &offdiagonals = get_offdiagonals(instance); + Matrix3f mat { + diagonals.x, offdiagonals.x, offdiagonals.y, + offdiagonals.x, diagonals.y, offdiagonals.z, + offdiagonals.y, offdiagonals.z, diagonals.z + }; + if (!mat.invert()) { + return false; + } - // remove impact of diagonals and off-diagonals - field = mat * field; + // remove impact of diagonals and off-diagonals + field = mat * field; + } #endif // remove impact of offsets