Skip to content

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_MASK defaults to 3, so two IMUs take part in the estimate and IMU3 is a spare. On a controller with three IMUs you can set EK3_IMU_MASK to 7 and enable lane 3.

Sensor affinity example ​

Sensor affinity applies to EKF3 only. If the EKF will not run, check:

  • EK3_ENABLE: set to 1 to enable the EKF3 algorithm
  • AHRS_EKF_TYPE: set to 3 so 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):

SensorLane 1Lane 2Lane 3
Airspeed121
Barometer121
GPS121
Magnetometer123

Choosing the EKF data sources ​

Beyond affinity, the EK3_SRCn_xxx parameters assign sensor types to each EKF lane. For the EKF3 primary lane:

ParameterWhat it setsValues
EK3_SRC1_POSXYHorizontal position source0: none
3: GPS
4: beacon
6: external navigation
EK3_SRC1_VELXYHorizontal velocity source0: none
3: GPS
5: optical flow
6: external navigation
7: wheel encoder
EK3_SRC1_POSZVertical position source0: none
1: barometer
2: rangefinder
3: GPS
4: beacon
5: external navigation
EK3_SRC1_VELZVertical velocity source0: none
1: barometer
2: GPS
3: external navigation
EK3_SRC1_YAWYaw source0: 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_SET MAVLink 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_OPTION parameter to 90, 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_YAW to 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.