EKF3 Sensor Matching and Lane Switching
EKF overview
The Extended Kalman Filter (EKF) runs several filter instances, called lanes. One lane — the primary lane — publishes attitude, position and the rest of the state estimate, while the others run in the background, ready to take over at any moment.
By default every lane uses the primary sensors (barometer, GPS, compass). You can specify those sensors with parameters, and if a sensor driver fails the system can switch source in flight on its own.
EKF lane setup
The number of lanes enabled always equals the number of IMUs enabled.
- To keep CPU load down,
EK3_IMU_MASKdefaults to 3, so two IMUs take part in the estimate and IMU3 is a spare. On a controller with three IMUs you can setEK3_IMU_MASKto 7 and enable lane 3.
Sensor affinity example
Sensor affinity applies to EKF3 only. If the EKF will not run, check:
EK3_ENABLE: set to1to enable the EKF3 algorithmAHRS_EKF_TYPE: set to3so AHRS uses EKF3
The example below is an aircraft with 2 barometers, 2 GPS units, 2 airspeed sensors, 3 compasses and 3 IMUs. The default lane-to-sensor binding is (the numbers are sensor indices):
| Sensor | Lane 1 | Lane 2 | Lane 3 |
|---|---|---|---|
| Airspeed | 1 | 2 | 1 |
| Barometer | 1 | 2 | 1 |
| GPS | 1 | 2 | 1 |
| Magnetometer | 1 | 2 | 3 |
Choosing the EKF data sources
Beyond affinity, the EK3_SRCn_xxx parameters assign sensor types to each EKF lane. For the EKF3 primary lane:
| Parameter | What it sets | Values |
|---|---|---|
EK3_SRC1_POSXY | Horizontal position source | 0: none 3: GPS 4: beacon 6: external navigation |
EK3_SRC1_VELXY | Horizontal velocity source | 0: none 3: GPS 5: optical flow 6: external navigation 7: wheel encoder |
EK3_SRC1_POSZ | Vertical position source | 0: none 1: barometer 2: rangefinder 3: GPS 4: beacon 5: external navigation |
EK3_SRC1_VELZ | Vertical velocity source | 0: none 1: barometer 2: GPS 3: external navigation |
EK3_SRC1_YAW | Yaw source | 0: none 1: magnetometer 2: GPS 3: fall back to compass when GPS fails |
Testing EKF switching
ArduPilot provides three sets of EKF position and velocity source parameters, and uses the first (EK3_SRC1) by default.
To run inertial navigation tests — with or without GPS:
- Send the
MAV_CMD_SET_EKF_SOURCE_SETMAVLink command from the ground station or an onboard computer to change the source set. - Or switch source sets from a transmitter auxiliary channel (set an
RCx_OPTIONparameter to90, EKF source set).
Example 1
Use dual-antenna heading instead of a magnetic compass, which solves compass interference or avoids compass calibration altogether.
- Set
EK3_SRC1_YAWto 2 (GPS heading)
Example 2
Use high-accuracy RTK height in place of the barometer for steadier altitude.
Set EK3_SRC1_POSZ to 3 (GPS height).
Example 3
A 3D camera can serve as the external position and velocity source:
EK3_SRC1_POSXY = 6 (external navigation XY position) EK3_SRC1_VELXY = 6 (external navigation XY velocity, assuming the camera outputs velocity) Then set optical flow as the second source set:
EK3_SRC2_POSXY = 0 (none — with optical flow, position is relative) EK3_SRC2_VELXY = 5 (optical flow XY velocity) You can then flip between the two source sets with an auxiliary channel switch on the transmitter.

