MAVLINKHUD

Optical Flow Fusion

Executive Summary

Optical Flow is the primary method for non-GPS navigation (FlowHold) and precision landing. The sensor reports the "angular velocity" of the ground texture. The EKF fuses this with altitude data to estimate the vehicle's horizontal velocity.

Theory & Concepts

1. Visual Odometry

Visual Odometry is the process of estimating movement by analyzing changes in camera images.

  • The Math: By tracking the position of a feature (like a rock or a rug pattern) across multiple frames, the sensor calculates a 2D vector of movement in pixels.
  • The Problem: The sensor doesn't know how big a pixel is in "meters."
  • The Scaler: This is why Altitude is required. One pixel of movement at 1 meter height represents a much smaller physical distance than one pixel at 10 meters height.

2. Relative vs. Absolute

Optical flow provides Relative Position. It knows you moved 1 meter, but it doesn't know where you are on the planet. This is why Flow-only flight (no GPS) will always drift slowly over time (Dead Reckoning error accumulation).

Core Concepts (The Engineer's View)

1. The Scaling Problem

The flow sensor sees "pixels per second" (Angular Rate).

  • Formula: Velocity = Angular_Rate * Distance_To_Ground.
  • The Issue: If you don't know the distance (Altitude), you can't know the speed.
    • 10 rad/s at 1m height = 10 m/s.
    • 10 rad/s at 10m height = 100 m/s.
  • Correction: The EKF must have a reliable terrainState (Height AGL).

2. Terrain State Estimation (EstimateTerrainOffset)

The EKF tracks a specific state for "Terrain Height" (relative to the EKF origin).

  • Source: Rangefinder (Lidar/Sonar) is the primary source.
  • Logic: Height_AGL = EKF_Alt - Terrain_Alt.
  • Estimation: As the drone flies, the EKF updates the Terrain Altitude.
    • Flat Ground: The Terrain Alt stays constant.
    • Sloped Ground: If EK3_TERR_GRAD is set > 0, the EKF expects the terrain to change as the drone moves, increasing the uncertainty of the terrain state.

3. Fusion Logic (FuseOptFlow)

  • Prediction: The EKF predicts what the flow should be based on its current estimated Velocity and Height.
    • Predicted_Flow = EKF_Velocity / EKF_Height_AGL.
  • Correction: It compares this to the Measured Flow (corrected for body rotation).
  • Update: The difference (Innovation) is used to correct the Velocity states.

Why Rangefinders are Critical

While the EKF can estimate scale without a rangefinder (using GPS or IMU integration), it is extremely fragile.

  • Without Lidar: The EKF is guessing the scale factor. If it guesses wrong, a small drift becomes a massive acceleration command ("Runaway").
  • With Lidar: The scale factor is mathematically locked. The velocity estimate becomes rock solid.

Key Parameters

Parameter Default Description
EK3_FLOW_USE 1 0=Disabled, 1=Use for Velocity, 2=Use for Position Holding.
EK3_TERR_GRAD 0.1 (m/m) Expected terrain slope. 0.1 = 10% slope. Higher values make the EKF adapt faster to hills but maybe noisier.
FLOW_POS_X/Y/Z 0 Position of the sensor relative to COG. Critical for compensating for lever-arm effects during rotation.

Source Code Reference