การประมาณสถานะด้วย Kalman filter/EKF
Fusing IMU, GNSS and barometer data; diagnosing the EKF
Overview: no single sensor tells the whole truth
A flight controller must know its position, velocity and attitude at every instant, yet it cannot measure these directly. It performs state estimation from several sensors, each with different weaknesses.
| Sensor | Strength | Weakness |
|---|---|---|
| IMU (gyro and accelerometer) | Fast, hundreds of samples per second, continuous | Small biases accumulate into drift when integrated |
| GNSS | Position that does not drift over time | Slower updates, metre-level noise, jumps from multipath, can be blocked or jammed |
| Barometer | Relative height without fast drift | Noisy in propwash and shifts with the weather |
| Compass | Absolute heading | Disturbed by currents and nearby metal |
Sensor fusion combines the strengths of every source. It is like walking through a dark room counting your steps and touching a wall now and then to correct the count. The standard tool is the Kalman filter (Kalman, 1960) and its version for nonlinear systems, the extended Kalman filter (EKF), used by both PX4 (EKF2) and ArduPilot (EKF3).
The Kalman filter: predict, then update
A Kalman filter always carries two things: an estimate of the state, such as height and vertical velocity, and a variance P that says how uncertain that estimate is. It loops through two steps.
- Predict: use a motion model and IMU data to move the state forward. The variance grows each time by the process noise Q, because prediction is imperfect.
- Update: when a measurement z arrives, such as GNSS or barometer, compute the innovation y = z minus the prediction, and move the estimate toward the measurement by the Kalman gain K.
In one dimension, measuring the state directly, K = P/(P + R), where R is the measurement variance. When the prediction is uncertain (large P) the filter trusts the measurement; when the measurement is noisy (large R) it trusts it less. After the update the variance becomes (1 − K)P, smaller than both P and R, so the fused result beats either source alone.
Choosing Q and R is how the filter is tuned. An R smaller than reality makes the filter chase noise. A Q that is too small lets the variance collapse so the filter stops accepting new measurements. If these values do not match the real sensors, results get worse even though the equations are correct.
The EKF in a flight controller
Real systems are nonlinear; rotating body-frame acceleration into the world frame depends on attitude, for example. The EKF handles this by linearising the equations about the current estimate at every step. Autopilot EKFs carry dozens of states: attitude, velocity, position, gyro and accelerometer biases, magnetic field and wind.
- PX4 EKF2 accepts IMU, compass, GNSS, barometer, rangefinder, optical flow, external vision and airspeed. Each auxiliary source must be enabled by parameter.
- ArduPilot EKF3 is the default estimator and can run several instances in parallel, one per IMU, selecting the most consistent one.
Innovation gates and diagnosing the EKF
Before using a measurement, the EKF checks whether the innovation is larger than its uncertainty can explain, using a test ratio = y²/(g²·S), where S is the innovation variance and g is the gate in standard deviations. A ratio above 1 means the measurement is rejected. This protects the estimate from GNSS jumps caused by reflections off buildings.
Diagnosing the EKF from a log means looking at three things:
- Test ratios for position, velocity, height and magnetometer. Values that often approach 1 mean the sensors disagree. ArduPilot logs the square root of these ratios in XKF4 (fields SV, SP, SH, SM); PX4 logs them in the EstimatorStatus message (for example vel_test_ratio, pos_test_ratio, hgt_test_ratio and mag_test_ratio), which you can plot from the ULog file; Flight Review shows only the estimator health flags.
- Innovations biased persistently in one direction point to a systematic error, such as compass interference from motor current or a wrongly configured GNSS antenna offset.
- Accelerometer vibration and clipping, which corrupt the prediction step at its source. No EKF can repair bad input data.
When the EKF's uncertainty exceeds its limits, the autopilot warns or triggers the configured failsafe, for example switching to a mode that does not need position, or landing.
Worked example: height from three sources
Example: drift, update and rejection
All values are assumed for practice.
- Accelerometer alone: a 0.05 m/s² bias integrated twice gives a height error of ½·b·t². After 10 s that is ½ × 0.05 × 10² = 2.5 m; after 30 s, 22.5 m; after 60 s, 90 m. The error grows with the square of time.
- Barometer update: predicted height 50.0 m with variance P = 4 m² (standard deviation 2 m). The barometer reads 51.0 m with variance R = 1 m². K = 4/(4 + 1) = 0.8. The new estimate is 50.0 + 0.8 × (51.0 − 50.0) = 50.8 m with variance (1 − 0.8) × 4 = 0.8 m², a standard deviation of about 0.89 m, smaller than both 2 m and 1 m.
- GNSS gate: with innovation variance S = 4 m² and gate g = 5, a measurement passes if |y| is below 5 × √4 = 10 m. An innovation of 6 m gives a ratio of 36/(25 × 4) = 0.36 (square root 0.6) and is used. An innovation of 12 m gives 144/100 = 1.44 (square root 1.2) and is rejected.
If GNSS is rejected for a long time, the filter leans on the IMU and the estimate drifts as in step 1. Widening the gate to silence the warning is not a fix; find out why the sensors disagree.
Common pitfalls
- Treating one sensor's reading as the truth.
- Setting R smaller than reality so the filter chases noise, or Q so small it stops accepting measurements.
- Widening gates to silence warnings, letting bad measurements in.
- Reading XKF4's SV, SP, SH and SM as variances when they are square roots of test ratios.
- Ignoring flight-controller vibration, which corrupts acceleration at the source.
- Using sensor timestamps from different clocks without synchronisation, or entering wrong sensor positions on the airframe.
- Enabling external vision input without checking its axes and latency.
Continue in the lessons
- UAT 305 Module 3: State estimation and the EKF: a Kalman filter for height, and reading EKF3 test ratios.
- UAT 322 Module 2: Sensor fusion: fusing IMU and GNSS, gating outliers, and camera-IMU calibration.
- UAT 366 Module 2: SLAM and sensor fusion: a two-state Kalman filter with indoor UWB.
Key references
- Kalman, R. E. (1960). A new approach to linear filtering and prediction problems. Journal of Basic Engineering, 82(1), 35–45. link
- Welch, G., & Bishop, G. (2004). An introduction to the Kalman filter (TR 95-041). University of North Carolina at Chapel Hill. link
- Groves, P. D. (2013). Principles of GNSS, inertial, and multisensor integrated navigation systems (2nd ed.). Artech House. link
- Thrun, S., Burgard, W., & Fox, D. (2005). Probabilistic robotics. MIT Press. link
- PX4 Autopilot. Using PX4's navigation filter (EKF2). PX4 user guide (main). link
- ArduPilot Dev Team. Extended Kalman filter (EKF). ArduPilot Copter documentation. link