From a3100251a84230eb5a18ef8f08c6eb809c724edc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 20 Jan 2020 15:52:12 +1100 Subject: [PATCH] AP_NavEKF3: use enum class for EK3_MAG_CAL values and use effective_magCal() everywhere for consistency --- libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp | 21 +++++++++---------- libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp | 2 +- libraries/AP_NavEKF3/AP_NavEKF3_core.h | 12 ++++++++++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp b/libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp index f7eed9ff85..cdb450e0ae 100644 --- a/libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp +++ b/libraries/AP_NavEKF3/AP_NavEKF3_Control.cpp @@ -40,14 +40,13 @@ void NavEKF3_core::controlFilterModes() /* return effective value for _magCal for this core */ -uint8_t NavEKF3_core::effective_magCal(void) const +NavEKF3_core::MagCal NavEKF3_core::effective_magCal(void) const { // force use of simple magnetic heading fusion for specified cores if (frontend->_magMask & core_index) { - return 2; - } else { - return frontend->_magCal; + return MagCal::NEVER; } + return MagCal(frontend->_magCal.get()); } // Determine if learning of wind and magnetic field will be enabled and set corresponding indexing limits to @@ -92,16 +91,16 @@ void NavEKF3_core::setWindMagStateLearningMode() } // Determine if learning of magnetic field states has been requested by the user - uint8_t magCal = effective_magCal(); + MagCal magCal = effective_magCal(); bool magCalRequested = - ((magCal == 0) && inFlight) || // when flying - ((magCal == 1) && manoeuvring) || // when manoeuvring - ((magCal == 3) && finalInflightYawInit && finalInflightMagInit) || // when initial in-air yaw and mag field reset is complete - (magCal == 4); // all the time + ((magCal == MagCal::WHEN_FLYING) && inFlight) || // when flying + ((magCal == MagCal::WHEN_MANOEUVRING) && manoeuvring) || // when manoeuvring + ((magCal == MagCal::AFTER_FIRST_CLIMB) && finalInflightYawInit && finalInflightMagInit) || // when initial in-air yaw and mag field reset is complete + (magCal == MagCal::ALWAYS); // all the time // Deny mag calibration request if we aren't using the compass, it has been inhibited by the user, // we do not have an absolute position reference or are on the ground (unless explicitly requested by the user) - bool magCalDenied = !use_compass() || (magCal == 2) || (onGround && magCal != 4); + bool magCalDenied = !use_compass() || (magCal == MagCal::NEVER) || (onGround && magCal != MagCal::ALWAYS); // Inhibit the magnetic field calibration if not requested or denied bool setMagInhibit = !magCalRequested || magCalDenied; @@ -450,7 +449,7 @@ bool NavEKF3_core::readyToUseRangeBeacon(void) const // return true if we should use the compass bool NavEKF3_core::use_compass(void) const { - return (frontend->_magCal != 5) && _ahrs->get_compass() && _ahrs->get_compass()->use_for_yaw(magSelectIndex) && !allMagSensorsFailed; + return effective_magCal() != MagCal::EXTERNAL_YAW && _ahrs->get_compass() && _ahrs->get_compass()->use_for_yaw(magSelectIndex) && !allMagSensorsFailed; } /* diff --git a/libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp b/libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp index 245894c9ea..39193bfb52 100644 --- a/libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp +++ b/libraries/AP_NavEKF3/AP_NavEKF3_MagFusion.cpp @@ -264,7 +264,7 @@ void NavEKF3_core::SelectMagFusion() magFusePerformed = false; // Handle case where we are using an external yaw sensor instead of a magnetomer - if ((frontend->_magCal == 5)) { + if (effective_magCal() == MagCal::EXTERNAL_YAW) { if (storedYawAng.recall(yawAngDataDelayed,imuDataDelayed.time_ms)) { if (tiltAlignComplete && !yawAlignComplete) { alignYawAngle(); diff --git a/libraries/AP_NavEKF3/AP_NavEKF3_core.h b/libraries/AP_NavEKF3/AP_NavEKF3_core.h index a5cc29dd30..da8724e09b 100644 --- a/libraries/AP_NavEKF3/AP_NavEKF3_core.h +++ b/libraries/AP_NavEKF3/AP_NavEKF3_core.h @@ -367,6 +367,16 @@ public: // get timing statistics structure void getTimingStatistics(struct ekf_timing &timing); + // values for EK3_MAG_CAL + enum class MagCal { + WHEN_FLYING = 0, + WHEN_MANOEUVRING = 1, + NEVER = 2, + AFTER_FIRST_CLIMB = 3, + ALWAYS = 4, + EXTERNAL_YAW = 5, + }; + private: // Reference to the global EKF frontend for parameters NavEKF3 *frontend; @@ -830,7 +840,7 @@ private: void recordMagReset(); // effective value of MAG_CAL - uint8_t effective_magCal(void) const; + MagCal effective_magCal(void) const; // calculate the variances for the rotation vector equivalent Vector3f calcRotVecVariances(void);