AP_NavEKF2.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. 24 state EKF based on the derivation in https://github.com/priseborough/
  3. InertialNav/blob/master/derivations/RotationVectorAttitudeParameterisation/
  4. GenerateNavFilterEquations.m
  5. Converted from Matlab to C++ by Paul Riseborough
  6. EKF Tuning parameters refactored by Tom Cauchois
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma once
  19. #include <AP_Math/AP_Math.h>
  20. #include <AP_Param/AP_Param.h>
  21. #include <GCS_MAVLink/GCS_MAVLink.h>
  22. #include <AP_NavEKF/AP_Nav_Common.h>
  23. #include <AP_Airspeed/AP_Airspeed.h>
  24. #include <AP_Compass/AP_Compass.h>
  25. #include <AP_RangeFinder/AP_RangeFinder.h>
  26. #include <AP_Logger/LogStructure.h>
  27. class NavEKF2_core;
  28. class AP_AHRS;
  29. class NavEKF2 {
  30. friend class NavEKF2_core;
  31. public:
  32. NavEKF2(const AP_AHRS *ahrs, const RangeFinder &rng);
  33. /* Do not allow copies */
  34. NavEKF2(const NavEKF2 &other) = delete;
  35. NavEKF2 &operator=(const NavEKF2&) = delete;
  36. static const struct AP_Param::GroupInfo var_info[];
  37. // allow logging to determine the number of active cores
  38. uint8_t activeCores(void) const {
  39. return num_cores;
  40. }
  41. // Initialise the filter
  42. bool InitialiseFilter(void);
  43. // Update Filter States - this should be called whenever new IMU data is available
  44. void UpdateFilter(void);
  45. // check if we should write log messages
  46. void check_log_write(void);
  47. // Check basic filter health metrics and return a consolidated health status
  48. bool healthy(void) const;
  49. // Ensure that all started cores are considered healthy
  50. bool all_cores_healthy(void) const;
  51. // returns the index of the primary core
  52. // return -1 if no primary core selected
  53. int8_t getPrimaryCoreIndex(void) const;
  54. // returns the index of the IMU of the primary core
  55. // return -1 if no primary core selected
  56. int8_t getPrimaryCoreIMUIndex(void) const;
  57. // Write the last calculated NE position relative to the reference point (m) for the specified instance.
  58. // An out of range instance (eg -1) returns data for the primary instance
  59. // If a calculated solution is not available, use the best available data and return false
  60. // If false returned, do not use for flight control
  61. bool getPosNE(int8_t instance, Vector2f &posNE) const;
  62. // Write the last calculated D position relative to the reference point (m) for the specified instance.
  63. // An out of range instance (eg -1) returns data for the primary instance
  64. // If a calculated solution is not available, use the best available data and return false
  65. // If false returned, do not use for flight control
  66. bool getPosD(int8_t instance, float &posD) const;
  67. // return NED velocity in m/s for the specified instance
  68. // An out of range instance (eg -1) returns data for the primary instance
  69. void getVelNED(int8_t instance, Vector3f &vel) const;
  70. // Return the rate of change of vertical position in the down direction (dPosD/dt) in m/s for the specified instance
  71. // An out of range instance (eg -1) returns data for the primary instance
  72. // This can be different to the z component of the EKF velocity state because it will fluctuate with height errors and corrections in the EKF
  73. // but will always be kinematically consistent with the z component of the EKF position state
  74. float getPosDownDerivative(int8_t instance) const;
  75. // This returns the specific forces in the NED frame
  76. void getAccelNED(Vector3f &accelNED) const;
  77. // return body axis gyro bias estimates in rad/sec for the specified instance
  78. // An out of range instance (eg -1) returns data for the primary instance
  79. void getGyroBias(int8_t instance, Vector3f &gyroBias) const;
  80. // return body axis gyro scale factor error as a percentage for the specified instance
  81. // An out of range instance (eg -1) returns data for the primary instance
  82. void getGyroScaleErrorPercentage(int8_t instance, Vector3f &gyroScale) const;
  83. // return tilt error convergence metric for the specified instance
  84. // An out of range instance (eg -1) returns data for the primary instance
  85. void getTiltError(int8_t instance, float &ang) const;
  86. // reset body axis gyro bias estimates
  87. void resetGyroBias(void);
  88. // Resets the baro so that it reads zero at the current height
  89. // Resets the EKF height to zero
  90. // Adjusts the EKf origin height so that the EKF height + origin height is the same as before
  91. // Returns true if the height datum reset has been performed
  92. // If using a range finder for height no reset is performed and it returns false
  93. bool resetHeightDatum(void);
  94. // Commands the EKF to not use GPS.
  95. // This command must be sent prior to arming as it will only be actioned when the filter is in static mode
  96. // This command is forgotten by the EKF each time it goes back into static mode (eg the vehicle disarms)
  97. // Returns 0 if command rejected
  98. // Returns 1 if attitude, vertical velocity and vertical position will be provided
  99. // Returns 2 if attitude, 3D-velocity, vertical position and relative horizontal position will be provided
  100. uint8_t setInhibitGPS(void);
  101. // Set the argument to true to prevent the EKF using the GPS vertical velocity
  102. // This can be used for situations where GPS velocity errors are causing problems with height accuracy
  103. void setInhibitGpsVertVelUse(const bool varIn) { inhibitGpsVertVelUse = varIn; };
  104. // return the horizontal speed limit in m/s set by optical flow sensor limits
  105. // return the scale factor to be applied to navigation velocity gains to compensate for increase in velocity noise with height when using optical flow
  106. void getEkfControlLimits(float &ekfGndSpdLimit, float &ekfNavVelGainScaler) const;
  107. // return the Z-accel bias estimate in m/s^2 for the specified instance
  108. // An out of range instance (eg -1) returns data for the primary instance
  109. void getAccelZBias(int8_t instance, float &zbias) const;
  110. // return the NED wind speed estimates in m/s (positive is air moving in the direction of the axis)
  111. // An out of range instance (eg -1) returns data for the primary instance
  112. void getWind(int8_t instance, Vector3f &wind) const;
  113. // return earth magnetic field estimates in measurement units / 1000 for the specified instance
  114. // An out of range instance (eg -1) returns data for the primary instance
  115. void getMagNED(int8_t instance, Vector3f &magNED) const;
  116. // return body magnetic field estimates in measurement units / 1000 for the specified instance
  117. // An out of range instance (eg -1) returns data for the primary instance
  118. void getMagXYZ(int8_t instance, Vector3f &magXYZ) const;
  119. // return the magnetometer in use for the specified instance
  120. // An out of range instance (eg -1) returns data for the primary instance
  121. uint8_t getActiveMag(int8_t instance) const;
  122. // Return estimated magnetometer offsets
  123. // Return true if magnetometer offsets are valid
  124. bool getMagOffsets(uint8_t mag_idx, Vector3f &magOffsets) const;
  125. // Return the last calculated latitude, longitude and height in WGS-84
  126. // If a calculated location isn't available, return a raw GPS measurement
  127. // The status will return true if a calculation or raw measurement is available
  128. // The getFilterStatus() function provides a more detailed description of data health and must be checked if data is to be used for flight control
  129. bool getLLH(struct Location &loc) const;
  130. // Return the latitude and longitude and height used to set the NED origin for the specified instance
  131. // An out of range instance (eg -1) returns data for the primary instance
  132. // All NED positions calculated by the filter are relative to this location
  133. // Returns false if the origin has not been set
  134. bool getOriginLLH(int8_t instance, struct Location &loc) const;
  135. // set the latitude and longitude and height used to set the NED origin
  136. // All NED positions calculated by the filter will be relative to this location
  137. // The origin cannot be set if the filter is in a flight mode (eg vehicle armed)
  138. // Returns false if the filter has rejected the attempt to set the origin
  139. bool setOriginLLH(const Location &loc);
  140. // return estimated height above ground level
  141. // return false if ground height is not being estimated.
  142. bool getHAGL(float &HAGL) const;
  143. // return the Euler roll, pitch and yaw angle in radians for the specified instance
  144. // An out of range instance (eg -1) returns data for the primary instance
  145. void getEulerAngles(int8_t instance, Vector3f &eulers) const;
  146. // return the transformation matrix from XYZ (body) to NED axes
  147. void getRotationBodyToNED(Matrix3f &mat) const;
  148. // return the transformation matrix from XYZ (body) to NED axes
  149. void getQuaternionBodyToNED(int8_t instance, Quaternion &quat) const;
  150. // return the quaternions defining the rotation from NED to autopilot axes
  151. void getQuaternion(int8_t instance, Quaternion &quat) const;
  152. // return the innovations for the specified instance
  153. // An out of range instance (eg -1) returns data for the primary instance
  154. void getInnovations(int8_t index, Vector3f &velInnov, Vector3f &posInnov, Vector3f &magInnov, float &tasInnov, float &yawInnov) const;
  155. // publish output observer angular, velocity and position tracking error
  156. void getOutputTrackingError(int8_t instance, Vector3f &error) const;
  157. // return the innovation consistency test ratios for the specified instance
  158. // An out of range instance (eg -1) returns data for the primary instance
  159. void getVariances(int8_t instance, float &velVar, float &posVar, float &hgtVar, Vector3f &magVar, float &tasVar, Vector2f &offset) const;
  160. // should we use the compass? This is public so it can be used for
  161. // reporting via ahrs.use_compass()
  162. bool use_compass(void) const;
  163. // write the raw optical flow measurements
  164. // rawFlowQuality is a measured of quality between 0 and 255, with 255 being the best quality
  165. // rawFlowRates are the optical flow rates in rad/sec about the X and Y sensor axes.
  166. // rawGyroRates are the sensor rotation rates in rad/sec measured by the sensors internal gyro
  167. // The sign convention is that a RH physical rotation of the sensor about an axis produces both a positive flow and gyro rate
  168. // msecFlowMeas is the scheduler time in msec when the optical flow data was received from the sensor.
  169. // posOffset is the XYZ flow sensor position in the body frame in m
  170. void writeOptFlowMeas(const uint8_t rawFlowQuality, const Vector2f &rawFlowRates, const Vector2f &rawGyroRates, const uint32_t msecFlowMeas, const Vector3f &posOffset);
  171. // return data for debugging optical flow fusion for the specified instance
  172. // An out of range instance (eg -1) returns data for the primary instance
  173. void getFlowDebug(int8_t instance, float &varFlow, float &gndOffset, float &flowInnovX, float &flowInnovY, float &auxInnov, float &HAGL, float &rngInnov, float &range, float &gndOffsetErr) const;
  174. /*
  175. Returns the following data for debugging range beacon fusion from the specified instance
  176. An out of range instance (eg -1) returns data for the primary instance
  177. ID : beacon identifier
  178. rng : measured range to beacon (m)
  179. innov : range innovation (m)
  180. innovVar : innovation variance (m^2)
  181. testRatio : innovation consistency test ratio
  182. beaconPosNED : beacon NED position (m)
  183. returns true if data could be found, false if it could not
  184. */
  185. bool getRangeBeaconDebug(int8_t instance, uint8_t &ID, float &rng, float &innov, float &innovVar, float &testRatio, Vector3f &beaconPosNED, float &offsetHigh, float &offsetLow) const;
  186. // called by vehicle code to specify that a takeoff is happening
  187. // causes the EKF to compensate for expected barometer errors due to ground effect
  188. void setTakeoffExpected(bool val);
  189. // called by vehicle code to specify that a touchdown is expected to happen
  190. // causes the EKF to compensate for expected barometer errors due to ground effect
  191. void setTouchdownExpected(bool val);
  192. // Set to true if the terrain underneath is stable enough to be used as a height reference
  193. // in combination with a range finder. Set to false if the terrain underneath the vehicle
  194. // cannot be used as a height reference
  195. void setTerrainHgtStable(bool val);
  196. /*
  197. return the filter fault status as a bitmasked integer for the specified instance
  198. An out of range instance (eg -1) returns data for the primary instance
  199. 0 = quaternions are NaN
  200. 1 = velocities are NaN
  201. 2 = badly conditioned X magnetometer fusion
  202. 3 = badly conditioned Y magnetometer fusion
  203. 5 = badly conditioned Z magnetometer fusion
  204. 6 = badly conditioned airspeed fusion
  205. 7 = badly conditioned synthetic sideslip fusion
  206. 7 = filter is not initialised
  207. */
  208. void getFilterFaults(int8_t instance, uint16_t &faults) const;
  209. /*
  210. return filter timeout status as a bitmasked integer for the specified instance
  211. An out of range instance (eg -1) returns data for the primary instance
  212. 0 = position measurement timeout
  213. 1 = velocity measurement timeout
  214. 2 = height measurement timeout
  215. 3 = magnetometer measurement timeout
  216. 5 = unassigned
  217. 6 = unassigned
  218. 7 = unassigned
  219. 7 = unassigned
  220. */
  221. void getFilterTimeouts(int8_t instance, uint8_t &timeouts) const;
  222. /*
  223. return filter gps quality check status for the specified instance
  224. An out of range instance (eg -1) returns data for the primary instance
  225. */
  226. void getFilterGpsStatus(int8_t instance, nav_gps_status &faults) const;
  227. /*
  228. return filter status flags for the specified instance
  229. An out of range instance (eg -1) returns data for the primary instance
  230. */
  231. void getFilterStatus(int8_t instance, nav_filter_status &status) const;
  232. // send an EKF_STATUS_REPORT message to GCS
  233. void send_status_report(mavlink_channel_t chan);
  234. // provides the height limit to be observed by the control loops
  235. // returns false if no height limiting is required
  236. // this is needed to ensure the vehicle does not fly too high when using optical flow navigation
  237. bool getHeightControlLimit(float &height) const;
  238. // return the amount of yaw angle change (in radians) due to the last yaw angle reset or core selection switch
  239. // returns the time of the last yaw angle reset or 0 if no reset has ever occurred
  240. uint32_t getLastYawResetAngle(float &yawAngDelta);
  241. // return the amount of NE position change due to the last position reset in metres
  242. // returns the time of the last reset or 0 if no reset has ever occurred
  243. uint32_t getLastPosNorthEastReset(Vector2f &posDelta);
  244. // return the amount of NE velocity change due to the last velocity reset in metres/sec
  245. // returns the time of the last reset or 0 if no reset has ever occurred
  246. uint32_t getLastVelNorthEastReset(Vector2f &vel) const;
  247. // return the amount of vertical position change due to the last reset in metres
  248. // returns the time of the last reset or 0 if no reset has ever occurred
  249. uint32_t getLastPosDownReset(float &posDelta);
  250. // report any reason for why the backend is refusing to initialise
  251. const char *prearm_failure_reason(void) const;
  252. // set and save the _baroAltNoise parameter
  253. void set_baro_alt_noise(float noise) { _baroAltNoise.set_and_save(noise); };
  254. // allow the enable flag to be set by Replay
  255. void set_enable(bool enable) { _enable.set(enable); }
  256. // are we doing sensor logging inside the EKF?
  257. bool have_ekf_logging(void) const { return logging.enabled && _logging_mask != 0; }
  258. // get timing statistics structure
  259. void getTimingStatistics(int8_t instance, struct ekf_timing &timing) const;
  260. /*
  261. * Write position and quaternion data from an external navigation system
  262. *
  263. * pos : position in the RH navigation frame. Frame is assumed to be NED if frameIsNED is true. (m)
  264. * quat : quaternion desribing the rotation from navigation frame to body frame
  265. * posErr : 1-sigma spherical position error (m)
  266. * angErr : 1-sigma spherical angle error (rad)
  267. * timeStamp_ms : system time the measurement was taken, not the time it was received (mSec)
  268. * resetTime_ms : system time of the last position reset request (mSec)
  269. *
  270. */
  271. void writeExtNavData(const Vector3f &sensOffset, const Vector3f &pos, const Quaternion &quat, float posErr, float angErr, uint32_t timeStamp_ms, uint32_t resetTime_ms);
  272. /*
  273. check if switching lanes will reduce the normalised
  274. innovations. This is called when the vehicle code is about to
  275. trigger an EKF failsafe, and it would like to avoid that by
  276. using a different EKF lane
  277. */
  278. void checkLaneSwitch(void);
  279. // write EKF information to on-board logs
  280. void Log_Write();
  281. // check if external navigation is being used for yaw observation
  282. bool isExtNavUsedForYaw(void) const;
  283. private:
  284. uint8_t num_cores; // number of allocated cores
  285. uint8_t primary; // current primary core
  286. NavEKF2_core *core = nullptr;
  287. const AP_AHRS *_ahrs;
  288. const RangeFinder &_rng;
  289. uint32_t _frameTimeUsec; // time per IMU frame
  290. uint8_t _framesPerPrediction; // expected number of IMU frames per prediction
  291. // EKF Mavlink Tuneable Parameters
  292. AP_Int8 _enable; // zero to disable EKF2
  293. AP_Float _gpsHorizVelNoise; // GPS horizontal velocity measurement noise : m/s
  294. AP_Float _gpsVertVelNoise; // GPS vertical velocity measurement noise : m/s
  295. AP_Float _gpsHorizPosNoise; // GPS horizontal position measurement noise m
  296. AP_Float _baroAltNoise; // Baro height measurement noise : m
  297. AP_Float _magNoise; // magnetometer measurement noise : gauss
  298. AP_Float _easNoise; // equivalent airspeed measurement noise : m/s
  299. AP_Float _windVelProcessNoise; // wind velocity state process noise : m/s^2
  300. AP_Float _wndVarHgtRateScale; // scale factor applied to wind process noise due to height rate
  301. AP_Float _magEarthProcessNoise; // Earth magnetic field process noise : gauss/sec
  302. AP_Float _magBodyProcessNoise; // Body magnetic field process noise : gauss/sec
  303. AP_Float _gyrNoise; // gyro process noise : rad/s
  304. AP_Float _accNoise; // accelerometer process noise : m/s^2
  305. AP_Float _gyroBiasProcessNoise; // gyro bias state process noise : rad/s
  306. AP_Float _accelBiasProcessNoise;// accel bias state process noise : m/s^2
  307. AP_Int16 _hgtDelay_ms; // effective average delay of Height measurements relative to inertial measurements (msec)
  308. AP_Int8 _fusionModeGPS; // 0 = use 3D velocity, 1 = use 2D velocity, 2 = use no velocity
  309. AP_Int16 _gpsVelInnovGate; // Percentage number of standard deviations applied to GPS velocity innovation consistency check
  310. AP_Int16 _gpsPosInnovGate; // Percentage number of standard deviations applied to GPS position innovation consistency check
  311. AP_Int16 _hgtInnovGate; // Percentage number of standard deviations applied to height innovation consistency check
  312. AP_Int16 _magInnovGate; // Percentage number of standard deviations applied to magnetometer innovation consistency check
  313. AP_Int16 _tasInnovGate; // Percentage number of standard deviations applied to true airspeed innovation consistency check
  314. AP_Int8 _magCal; // Sets activation condition for in-flight magnetometer calibration
  315. AP_Int8 _gpsGlitchRadiusMax; // Maximum allowed discrepancy between inertial and GPS Horizontal position before GPS glitch is declared : m
  316. AP_Float _flowNoise; // optical flow rate measurement noise
  317. AP_Int16 _flowInnovGate; // Percentage number of standard deviations applied to optical flow innovation consistency check
  318. AP_Int8 _flowDelay_ms; // effective average delay of optical flow measurements rel to IMU (msec)
  319. AP_Int16 _rngInnovGate; // Percentage number of standard deviations applied to range finder innovation consistency check
  320. AP_Float _maxFlowRate; // Maximum flow rate magnitude that will be accepted by the filter
  321. AP_Int8 _altSource; // Primary alt source during optical flow navigation. 0 = use Baro, 1 = use range finder.
  322. AP_Float _gyroScaleProcessNoise;// gyro scale factor state process noise : 1/s
  323. AP_Float _rngNoise; // Range finder noise : m
  324. AP_Int8 _gpsCheck; // Bitmask controlling which preflight GPS checks are bypassed
  325. AP_Int8 _imuMask; // Bitmask of IMUs to instantiate EKF2 for
  326. AP_Int16 _gpsCheckScaler; // Percentage increase to be applied to GPS pre-flight accuracy and drift thresholds
  327. AP_Float _noaidHorizNoise; // horizontal position measurement noise assumed when synthesised zero position measurements are used to constrain attitude drift : m
  328. AP_Int8 _logging_mask; // mask of IMUs to log
  329. AP_Float _yawNoise; // magnetic yaw measurement noise : rad
  330. AP_Int16 _yawInnovGate; // Percentage number of standard deviations applied to magnetic yaw innovation consistency check
  331. AP_Int8 _tauVelPosOutput; // Time constant of output complementary filter : csec (centi-seconds)
  332. AP_Int8 _useRngSwHgt; // Maximum valid range of the range finder as a percentage of the maximum range specified by the sensor driver
  333. AP_Float _terrGradMax; // Maximum terrain gradient below the vehicle
  334. AP_Float _rngBcnNoise; // Range beacon measurement noise (m)
  335. AP_Int16 _rngBcnInnovGate; // Percentage number of standard deviations applied to range beacon innovation consistency check
  336. AP_Int8 _rngBcnDelay_ms; // effective average delay of range beacon measurements rel to IMU (msec)
  337. AP_Float _useRngSwSpd; // Maximum horizontal ground speed to use range finder as the primary height source (m/s)
  338. AP_Int8 _magMask; // Bitmask forcng specific EKF core instances to use simple heading magnetometer fusion.
  339. AP_Int8 _originHgtMode; // Bitmask controlling post alignment correction and reporting of the EKF origin height.
  340. AP_Int8 _extnavDelay_ms; // effective average delay of external nav system measurements relative to inertial measurements (msec)
  341. AP_Int8 _flowUse; // Controls if the optical flow data is fused into the main navigation estimator and/or the terrain estimator.
  342. AP_Int16 _mag_ef_limit; // limit on difference between WMM tables and learned earth field.
  343. // Possible values for _flowUse
  344. #define FLOW_USE_NONE 0
  345. #define FLOW_USE_NAV 1
  346. #define FLOW_USE_TERRAIN 2
  347. // Tuning parameters
  348. const float gpsNEVelVarAccScale = 0.05f; // Scale factor applied to NE velocity measurement variance due to manoeuvre acceleration
  349. const float gpsDVelVarAccScale = 0.07f; // Scale factor applied to vertical velocity measurement variance due to manoeuvre acceleration
  350. const float gpsPosVarAccScale = 0.05f; // Scale factor applied to horizontal position measurement variance due to manoeuvre acceleration
  351. const uint8_t magDelay_ms = 60; // Magnetometer measurement delay (msec)
  352. const uint8_t tasDelay_ms = 240; // Airspeed measurement delay (msec)
  353. const uint16_t tiltDriftTimeMax_ms = 15000; // Maximum number of ms allowed without any form of tilt aiding (GPS, flow, TAS, etc)
  354. const uint16_t posRetryTimeUseVel_ms = 10000; // Position aiding retry time with velocity measurements (msec)
  355. const uint16_t posRetryTimeNoVel_ms = 7000; // Position aiding retry time without velocity measurements (msec)
  356. const uint16_t hgtRetryTimeMode0_ms = 10000; // Height retry time with vertical velocity measurement (msec)
  357. const uint16_t hgtRetryTimeMode12_ms = 5000; // Height retry time without vertical velocity measurement (msec)
  358. const uint16_t tasRetryTime_ms = 5000; // True airspeed timeout and retry interval (msec)
  359. const uint16_t magFailTimeLimit_ms = 10000; // number of msec before a magnetometer failing innovation consistency checks is declared failed (msec)
  360. const float magVarRateScale = 0.005f; // scale factor applied to magnetometer variance due to angular rate
  361. const float gyroBiasNoiseScaler = 2.0f; // scale factor applied to gyro bias state process noise when on ground
  362. const uint8_t hgtAvg_ms = 100; // average number of msec between height measurements
  363. const uint8_t betaAvg_ms = 100; // average number of msec between synthetic sideslip measurements
  364. const float covTimeStepMax = 0.1f; // maximum time (sec) between covariance prediction updates
  365. const float covDelAngMax = 0.05f; // maximum delta angle between covariance prediction updates
  366. const float DCM33FlowMin = 0.71f; // If Tbn(3,3) is less than this number, optical flow measurements will not be fused as tilt is too high.
  367. const float fScaleFactorPnoise = 1e-10f; // Process noise added to focal length scale factor state variance at each time step
  368. const uint8_t flowTimeDeltaAvg_ms = 100; // average interval between optical flow measurements (msec)
  369. const uint8_t flowIntervalMax_ms = 100; // maximum allowable time between flow fusion events
  370. const uint16_t gndEffectTimeout_ms = 1000; // time in msec that ground effect mode is active after being activated
  371. const float gndEffectBaroScaler = 4.0f; // scaler applied to the barometer observation variance when ground effect mode is active
  372. const uint8_t fusionTimeStep_ms = 10; // The minimum time interval between covariance predictions and measurement fusions in msec
  373. // origin set by one of the cores
  374. struct Location common_EKF_origin;
  375. bool common_origin_valid;
  376. struct {
  377. bool enabled:1;
  378. bool log_compass:1;
  379. bool log_baro:1;
  380. bool log_imu:1;
  381. } logging;
  382. // time at start of current filter update
  383. uint64_t imuSampleTime_us;
  384. struct {
  385. uint32_t last_function_call; // last time getLastYawYawResetAngle was called
  386. bool core_changed; // true when a core change happened and hasn't been consumed, false otherwise
  387. uint32_t last_primary_change; // last time a primary has changed
  388. float core_delta; // the amount of yaw change between cores when a change happened
  389. } yaw_reset_data;
  390. struct {
  391. uint32_t last_function_call; // last time getLastPosNorthEastReset was called
  392. bool core_changed; // true when a core change happened and hasn't been consumed, false otherwise
  393. uint32_t last_primary_change; // last time a primary has changed
  394. Vector2f core_delta; // the amount of NE position change between cores when a change happened
  395. } pos_reset_data;
  396. struct {
  397. uint32_t last_function_call; // last time getLastPosDownReset was called
  398. bool core_changed; // true when a core change happened and hasn't been consumed, false otherwise
  399. uint32_t last_primary_change; // last time a primary has changed
  400. float core_delta; // the amount of D position change between cores when a change happened
  401. } pos_down_reset_data;
  402. bool runCoreSelection; // true when the primary core has stabilised and the core selection logic can be started
  403. bool inhibitGpsVertVelUse; // true when GPS vertical velocity use is prohibited
  404. // time of last lane switch
  405. uint32_t lastLaneSwitch_ms;
  406. // update the yaw reset data to capture changes due to a lane switch
  407. // new_primary - index of the ekf instance that we are about to switch to as the primary
  408. // old_primary - index of the ekf instance that we are currently using as the primary
  409. void updateLaneSwitchYawResetData(uint8_t new_primary, uint8_t old_primary);
  410. // update the position reset data to capture changes due to a lane switch
  411. // new_primary - index of the ekf instance that we are about to switch to as the primary
  412. // old_primary - index of the ekf instance that we are currently using as the primary
  413. void updateLaneSwitchPosResetData(uint8_t new_primary, uint8_t old_primary);
  414. // update the position down reset data to capture changes due to a lane switch
  415. // new_primary - index of the ekf instance that we are about to switch to as the primary
  416. // old_primary - index of the ekf instance that we are currently using as the primary
  417. void updateLaneSwitchPosDownResetData(uint8_t new_primary, uint8_t old_primary);
  418. // logging functions shared by cores:
  419. void Log_Write_EKF1(uint8_t core, LogMessages msg_id, uint64_t time_us) const;
  420. void Log_Write_NKF2(uint8_t core, LogMessages msg_id, uint64_t time_us) const;
  421. void Log_Write_NKF3(uint8_t core, LogMessages msg_id, uint64_t time_us) const;
  422. void Log_Write_NKF4(uint8_t core, LogMessages msg_id, uint64_t time_us) const;
  423. void Log_Write_NKF5(uint64_t time_us) const;
  424. void Log_Write_Quaternion(uint8_t core, LogMessages msg_id, uint64_t time_us) const;
  425. void Log_Write_Beacon(uint64_t time_us) const;
  426. };