Visual, LiDAR and inertial localization
UAT 458 Indoor Autonomous UAS and Navigation Technology
Lesson
By the end of this module you will be able to
- Compare indoor position sources (optical flow, VIO, LiDAR SLAM) by what they give the EKF and where they fail
- Compute velocity from optical flow with body-rotation compensation and assess the effect of a wrong height
- Configure three EKF3 source sets and source switching in ArduPilot, with the PX4 EKF2 equivalents
- Convert positions from the ROS ENU frame to the autopilot NED frame and assess the effect of an uncompensated delay
Why this matters
The zone table of module 1 says which position source each zone should use. This module turns that table into real autopilot settings. Most indoor flight problems are not caused by poor sensors but by feeding the EKF wrongly: positions in a frame with swapped axes, a forgotten delay, or a source switch in mid-air that makes the position jump. A drone with good sensors wired up wrongly can be more dangerous than one with no sensors at all, because the autopilot believes bad data with confidence.
This module treats sensors at the level flight needs. Measurement principles and the mathematics of fusion are in modules 6–10 of this course, and the principles of VIO in UAT 307 module 4.
Indoor position sources as the autopilot sees them
What the person configuring the aircraft must know about each source is what it gives the EKF (position, velocity or heading), which parameters enable it, and when it fails.
| Source | Gives the EKF | ArduPilot (EKF3) | PX4 (EKF2) | Indoor weakness |
|---|---|---|---|---|
| Optical flow + rangefinder | Horizontal velocity | FLOW_TYPE, EK3_SRCx_VELXY = 5 | EKF2_OF_CTRL | Plain, dark or water surfaces; needs a valid rangefinder |
| VIO (camera + IMU) | Position, velocity, heading | VISO_TYPE, EK3_SRCx_POSXY = 6 | EKF2_EV_CTRL | Drifts with distance; low light; vibration |
| LiDAR SLAM | Position, heading | Sent over MAVLink as ExternalNav | EKF2_EV_CTRL | Long corridors with identical walls; glass; dust |
| External references (UWB, motion capture) | Position | Beacons or ExternalNav | EKF2_EV_CTRL | Must be installed and surveyed in the building (see modules 6–10 of this course) |
ArduPilot lists the systems that can replace GPS on one page, including the Intel RealSense T265 tracking camera, ModalAI VOXL, optical flow, several beacon brands, motion-capture systems and Cartographer SLAM using a 2D LiDAR through ROS (ArduPilot Dev Team, n.d.). Intel discontinued the T265 (last orders 28 February 2022; Intel Corporation, 2021), so it serves here as a worked example of the settings rather than a product to buy. PX4 takes external data through the MAVLink ODOMETRY or VISION_POSITION_ESTIMATE messages and selects which parts to use with the bits of EKF2_EV_CTRL: bit 0 horizontal position, bit 1 vertical position, bit 2 velocity, bit 3 yaw (PX4 Autopilot, n.d.).
Optical flow: velocity from images and height
Optical flow is the way the floor image in a downward camera slides as the drone moves, like watching the road through a car window: the faster the car or the closer the road, the faster the image moves. The sensor measures an angular rate (rad/s), which must be multiplied by the height from the rangefinder to give metres per second, after removing the part of the image motion caused by the aircraft tilting or turning, known from the gyro. Hence . PX4 states that an optical-flow setup needs both a downward camera and a downward rangefinder, preferably a LiDAR rather than sonar, and EKF2 uses the data when the rangefinder is valid and the image quality exceeds EKF2_OF_QMIN (PX4 Autopilot, n.d.).
Example 1 Optical-flow velocity on the walkway and over a filter bed
The team opens four segments of a test log. Its minimum quality threshold is 50 (on a 0 to 255 scale; all values assumed).
import math
# optical flow: ground speed from image rate, body rotation and height (assumed samples)
Q_MIN = 50 # minimum flow quality the team accepts (0-255 scale, assumed)
samples = [
# place, measured flow rate (rad/s), gyro rate on the same axis (rad/s), rangefinder height (m), quality
("walkway, level", 0.40, 0.00, 2.5, 160),
("walkway, pitching", 0.55, 0.15, 2.5, 150),
("walkway, 10% low h", 0.40, 0.00, 2.25, 160),
("over filter bed", 0.09, 0.00, 4.1, 18),
]
for place, flow, gyro, h, q in samples:
if q < Q_MIN:
print(f"{place:20s} quality {q:3d} < {Q_MIN}: flow rejected")
continue
v = (flow - gyro) * h # remove body rotation, then scale by height
print(f"{place:20s} v = ({flow:.2f} - {gyro:.2f}) x {h:.2f} = {v:.2f} m/s")
print(f"no rotation compensation while pitching: {0.55 * 2.5:.3f} m/s")
walkway, level v = (0.40 - 0.00) x 2.50 = 1.00 m/s
walkway, pitching v = (0.55 - 0.15) x 2.50 = 1.00 m/s
walkway, 10% low h v = (0.40 - 0.00) x 2.25 = 0.90 m/s
over filter bed quality 18 < 50: flow rejected
no rotation compensation while pitching: 1.375 m/s
While the aircraft pitches, the image speeds up to 0.55 rad/s, but 0.15 rad/s of that is the aircraft’s own rotation; removing it gives the same 1.00 m/s. Without compensation the system would think it was doing 1.375 m/s and the controller would brake at the wrong moment. The third row shows that velocity error is proportional to height error: a rangefinder reading 10% low gives a velocity 10% low. Over the filter bed the rangefinder reads 4.1 m to the water, but the water has almost no texture, so quality drops to 18 and the flow is rejected, exactly why module 1’s zone table excluded optical flow from zone C.
On the ArduPilot side, the optical-flow checks start with non-zero flow and quality values, a rangefinder reading between RNGFND1_MIN and RNGFND1_MAX and close to RNGFND1_GNDCLR when on the ground, then setting the EKF origin and moving the drone by hand to see the map icon follow, before calibrating scale with FLOW_FXSCALER and FLOW_FYSCALER (ArduPilot Dev Team, n.d.).
EKF source sets and source switching
ArduPilot’s EKF3 selects sources with five parameters per set: EK3_SRCx_POSXY (horizontal position), EK3_SRCx_VELXY (horizontal velocity), EK3_SRCx_POSZ (vertical position), EK3_SRCx_VELZ (vertical velocity) and EK3_SRCx_YAW (heading). There are three sets (x = 1 to 3). Set 1 is used by default, and the active set can be changed with a transmitter switch configured as RCx_OPTION = 90 (EKF Source Set): low is set 1, middle set 2, high set 3. It can also be changed with the MAVLink command MAV_CMD_SET_EKF_SOURCE_SET or by a Lua script that decides from sensor quality (ArduPilot Dev Team, n.d.).
For the water treatment plant the team turns its zone table into three sets (values from EKF3’s option list: 0 none, 1 baro, 3 GPS, 5 optical flow, 6 ExternalNav).
| Parameter | Set 1 apron (zone A) | Set 2 indoors, LiDAR SLAM | Set 3 optical-flow backup |
|---|---|---|---|
EK3_SRCx_POSXY | 3 GPS | 6 ExternalNav | 0 None |
EK3_SRCx_VELXY | 3 GPS | 6 ExternalNav | 5 OpticalFlow |
EK3_SRCx_POSZ | 1 Baro | 1 Baro | 1 Baro |
EK3_SRCx_VELZ | 3 GPS | 6 ExternalNav | 0 None |
EK3_SRCx_YAW | 1 Compass | 6 ExternalNav | 1 Compass |
Set 2 takes heading from ExternalNav because the compass cannot be trusted in the pump hall. Set 3 uses the compass because if ExternalNav is lost there is no heading from it any more, so set 3 may only be used in zone B, where the magnetic deviation is just 8%; that restriction must be written into the operating procedure. Set 3 has no horizontal position because optical flow gives only velocity, so position becomes relative and slowly drifts. EK3_SRC_OPTIONS is set to 2: bit 1 keeps the ExternalNav position aligned with optical flow so the position does not jump when switching, and bit 0 stays off so velocities from all sources are not fused together.
The ArduPilot documentation tests switching in stages. First on the bench: flip the switch and look for the source-change messages in the ground station, wait 10 seconds to confirm the EKF stays healthy, and check position, height and heading. For automatic switching, walk the vehicle between the outdoor and indoor areas first, then fly slowly across the boundary, ready each time to retake control in Stabilize. Each switch is logged as event 85, 86 or 87 for the set in use, and the SS field of the XKFS message shows the set each core is using (ArduPilot Dev Team, n.d.). The documentation also warns that switching from ExternalNav back to GPS may produce a position jump, but switching from GPS to ExternalNav does not, because while GPS is primary the ExternalNav position is kept aligned with it.
With a T265 on ArduPilot, the documentation recommends the barometer as the height source because the camera is sensitive to high vibration, and lifting the drone about 1 m and putting it down before flight so the camera can calibrate its vertical scale (ArduPilot Dev Team, n.d.). For VIO on PX4, set EKF2_EV_CTRL for the data to be used, set EKF2_HGT_REF to Vision if height comes from the camera, set the camera position relative to the body with EKF2_EV_POS_X/Y/Z, and disable GNSS with EKF2_GPS_CTRL = 0, as in PX4’s VIO test card.
Frames and delay
Most SLAM and VIO software runs on ROS, which uses ENU (x east, y north, z up) for the world and FLU (forward, left, up) for the body, following REP 105. PX4 and ArduPilot use NED (north, east, down) and FRD (forward, right, down) (PX4 Autopilot, n.d.; ROS Enhancement Proposals, n.d.). Send a position without converting it and north and east swap while height changes sign. Heading differs too: ENU measures counter-clockwise from east, NED clockwise from north.
Example 2 Converting a position from ENU to NED
The SLAM system reports the drone at x = 3.0 m (east), y = 4.0 m (north), z = 1.5 m (up), heading 30°, in the ENU frame.
# frame conversion: ROS ENU/FLU pose to the autopilot NED frame
def enu_to_ned(x_e, y_n, z_u, yaw_enu_deg):
north, east, down = y_n, x_e, -z_u
yaw_ned = (90.0 - yaw_enu_deg) % 360 # ENU yaw is counter-clockwise from East, NED yaw clockwise from North
return north, east, down, yaw_ned
n, e, d, yaw = enu_to_ned(3.0, 4.0, 1.5, 30.0)
print(f"ENU (E 3.0, N 4.0, U 1.5, yaw 30) -> NED (N {n}, E {e}, D {d}, yaw {yaw:.0f})")
print("unconverted, the autopilot would read N 3.0, E 4.0, D 1.5 (1.5 m below the floor)")
ENU (E 3.0, N 4.0, U 1.5, yaw 30) -> NED (N 4.0, E 3.0, D -1.5, yaw 60)
unconverted, the autopilot would read N 3.0, E 4.0, D 1.5 (1.5 m below the floor)
The same point in NED is 4.0 m north, 3.0 m east and D = −1.5 m (1.5 m above the floor), heading 60°. Forget the conversion and the autopilot thinks the drone is 1.5 m below the floor and 1.4 m off horizontally (). Worse, when the drone flies north the autopilot sees it moving east, so the position controller corrects in the wrong direction and the drone spirals off its path.
The second problem is latency. Camera or LiDAR data passes through processing on the companion computer and down a cable, so what reaches the EKF is where the drone was a moment ago. If the EKF does not know the delay, it compares an old position with the current state; the mismatch is about . ArduPilot sets the visual odometry delay with VISO_DELAY_MS (0 to 250 ms) and the optical-flow delay with EK3_FLOW_DELAY; PX4 uses EKF2_EV_DELAY, which its documentation says can be zero only with correct timestamps and synchronised clocks, but in practice is tuned from logs (PX4 Autopilot, n.d.). PX4 also requires external position messages at 30 to 50 Hz; slower than that and EKF2 will not fuse them.
Example 3 How much error does an uncompensated delay create?
# position mismatch caused by an unmodelled delay in the external position
for v in (0.5, 1.5, 3.0):
row = " ".join(f"{dt:3d} ms: {v * dt / 1000:.3f} m" for dt in (20, 80, 150))
print(f"v {v:.1f} m/s -> {row}")
v 0.5 m/s -> 20 ms: 0.010 m 80 ms: 0.040 m 150 ms: 0.075 m
v 1.5 m/s -> 20 ms: 0.030 m 80 ms: 0.120 m 150 ms: 0.225 m
v 3.0 m/s -> 20 ms: 0.060 m 80 ms: 0.240 m 150 ms: 0.450 m
At an indoor inspection speed of 0.5 m/s, an 80 ms delay costs only 4 cm. Fly at 3 m/s between inspection points and the error is 24 cm, which the EKF reads as the source disagreeing with the IMU: the position oscillates or is rejected. This is one reason indoor flight is slow, and why the delay must be set correctly before speeds go up.
Pre-flight proof by walk test
Before letting the drone fly on a new position source, the cheapest and safest check is a walk test. Remove the propellers, carry the drone around a shape measured on the floor, such as a 20 × 10 m rectangle, and put it back on the start mark. The track in the ground station should have the right shape and size, no swapped directions, and close at the start.
Example 4 Closure error of a walk test
# walk test: carry the aircraft around a 20 m x 10 m rectangle and back to the start mark
path_len = 2 * (20 + 10)
end_n, end_e = 0.42, -0.31 # reported end position relative to the start (m, assumed)
closure = math.hypot(end_n, end_e)
print(f"closure error {closure:.2f} m over {path_len} m = {100 * closure / path_len:.2f}% of distance")
closure error 0.52 m over 60 m = 0.87% of distance
The system reports the end point 0.52 m from the start after 60 m of walking, 0.87% of the distance. If the team’s criterion is no more than 1%, it can move on to flight tests. A mirrored or 90°-rotated track means a frame error that must be fixed before the propellers go on. The walk test can be repeated after every software change or camera move, and it serves as evidence in the module 5 test report.
Module lab
Lab: configure the sources and prove them by walk test
- In ArduPilot SITL, or on a training drone with the propellers removed, configure the three source sets from the table in the source-set section, adjusted to the sensors the group actually has, and set a switch with
RCx_OPTION= 90. - Save the complete parameter file before and after the changes, together with the firmware version.
- Test switching on the bench: flip through all three positions, watch the ground-station messages, wait 10 seconds, then download the log and check events 85 to 87 and the SS field of XKFS.
- Write a node or script that converts ENU to NED as in Example 2. Test it by moving the drone 1 m north by hand and checking that the autopilot sees the motion on the north axis.
- Walk a measured rectangle at least three times and compute the closure error with the code in Example 4.
- If optical flow is available, apply the code in Example 1 to a real log and compare velocity over a textured floor and a plain one.
Common mistakes
Watch out
- Sending ENU positions to the autopilot unconverted: axes swap, height changes sign and the drone spirals away from its path.
- Not setting the delay and then raising the speed: the position oscillates or is rejected by the EKF.
- Using optical flow over water, plain floors or in the dark, where the image has no texture to track.
- Building a backup set that relies on the compass and using it in the machine room, where the magnetic field is disturbed.
- Fusing velocities from all sources although they use different frames.
- Skipping the walk test and discovering the frame error in mid-air.
Summary
- Each indoor source gives different data: optical flow gives velocity, VIO and LiDAR SLAM give position and heading.
- Optical-flow velocity is ; a height error produces the same proportional velocity error.
- ArduPilot has three source sets in
EK3_SRCx_*, switched withRCx_OPTION= 90; PX4 selects withEKF2_EV_CTRL,EKF2_OF_CTRLandEKF2_HGT_REF. - Convert ENU to NED with and , and set the delay because .
- Prove every configuration change with a walk test before flight.
Check your understanding
- Optical flow reads 0.30 rad/s, the gyro on the same axis 0.10 rad/s, and the height is 3.0 m. What is the ground speed?
- In question 1, how large would the velocity error be without rotation compensation?
- SLAM reports ENU x = 2.0, y = −1.0, z = 2.5 with heading 90°. What is this in NED?
- Flying at 2 m/s with external position data 100 ms late and uncompensated, roughly how large is the error?
- Why should source set 3 (optical flow + compass) not be used in the pump hall?
Answers
- m/s
- The system would compute m/s, an error of 0.30 m/s (50% too high)
- N = −1.0 m, E = 2.0 m, D = −2.5 m, heading (facing north)
- m
- Set 3 takes heading from the compass, but the pump hall’s magnetic field deviates by 42%, so heading would be wrong enough to make the drone circle; and optical flow gives only velocity, so position drifts. The set is restricted to the walkway in the filter hall.
Key formulas
| Ground speed from optical flow | |
| ENU to NED | |
| Position error from delay |
Key references
- ArduPilot Dev Team. Non-GPS navigation. ArduPilot Copter documentation. link
- ArduPilot Dev Team. EKF source selection and switching. ArduPilot Copter documentation. link
- ArduPilot Dev Team. GPS / non-GPS transitions. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Optical flow sensor testing and setup. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Intel RealSense T265. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Cartographer SLAM for non-GPS navigation. ArduPilot developer documentation. link
- PX4 Autopilot. Optical flow. PX4 guide (main). link
- PX4 Autopilot. Visual inertial odometry (VIO). PX4 guide (main). link
- PX4 Autopilot. Using vision or motion capture systems for position estimation. PX4 guide (main). link
- PX4 Autopilot. Using PX4's navigation filter (EKF2). PX4 user guide (main). link
- PX4 Autopilot. Test MC_09: VIO (visual-inertial odometry). PX4 guide (main). link
- ROS Enhancement Proposals. REP 105: Coordinate frames for mobile platforms. link
- ArduPilot Dev Team. Parameter list (Copter stable V4.7.1). ArduPilot Copter documentation. link
- Intel Corporation. (2021, August 31). Product change notification 118463-00: Intel RealSense tracking camera T265 (and other RealSense products), product discontinuance, end of life [PDF; copy hosted by The Robot Report]. link
Further reading
Study the assigned knowledge units in advance, review media and take the module quiz
ระบบระบุตำแหน่งภายในอาคาร
How VIN, VIO and SLAM differ
การประมาณสถานะด้วย Kalman filter/EKF
In class / field
Lab or field practice from worksheets with a safety checklist
Learning evidence: Checked worksheets and quiz results