### Summary
If you power up an optical-flow copter and leave it on the ground b…efore takeoff, the AGL filter that scales optical flow can build up a large downward velocity while its height sits at the on-ground floor. When you take off it spends seconds unwinding that velocity: its height stays at the floor while the copter climbs, flow velocity is scaled by the wrong height, and the EKF height steps by metres when the height source changes back to baro. After a landing the reverse happens: with the range finder below its minimum nothing is fused, so the height coasts up on the touchdown velocity, 1.6 m with the copter sitting on the ground.
Four changes to the AGL KF, in seven commits. At the floor the height and its measurement are equal, so nothing can correct a downward velocity there; the floor clamp now clears it. The velocity decay, meant for a range finder outage, ran on about three filter steps in four and roughly halved the velocity in any climb or descent; it now runs only after 500 ms without a range finder fusion. With the range finder reporting out of range low and no reading arriving, the velocity is zeroed so the height cannot coast, unless the main filter is moving vertically faster than 0.25 m/s. That exception trusts the main filter, which after a hard flight can read a climb on the ground, so the last change holds the height on the floor when a range finder last fused near the floor has read only out of range low for a second and the height has coasted 0.3 m above that reading.
### Classification & Testing
- [x] Checked by a human programmer
- [x] Automated test(s) verify changes (e.g. unit test, autotest)
- [x] Tested manually, description below (e.g. SITL)
- [ ] Tested on hardware
- [x] Logs available on request
The velocity clear, the decay gate and the coast stop have flown on fourteen flights. The floor hold has SITL and Replay evidence only, and has not flown on a range finder that reports out of range low.
| | without | with |
|---|---|---|
| `OpticalFlowAGLKfFloorVelocity`: velocity left at the floor after a 3 m range step and back, bound -0.1 m/s | -0.43 m/s | +0.0001 m/s |
| `OpticalFlowAGLKfNoCoastBelowMin`: height rise over 3.5 s after `RNGFND1_MIN` is raised above the reading in a hover | 0.357 m | 0.000 m |
| `OpticalFlowAGLKfNoCoastAfterTouchdown`: height rise on the ground after touchdown, accel offset running the main filter's velD to 2.8 m/s, bound 0.5 m | 2.13-2.42 m | 0.29-0.30 m |
| `OpticalFlowAGLKfNoFloorHoldInHover`: lowest height in a 10 m hover after a short reading the gate rejects, then out of range low, bound 5 m | 0.10 m (hold counting the rejected reading) | 9.97 m |
| SITL climb and descent, decay gate: mean AGL velocity error against truth (with #33507's bias state) | 0.62 m/s | 0.18 m/s |
| Replay of the flight that exposed the wind-up (with #33507's bias state): worst floor velocity | -9.25 m/s | -0.71 m/s |
| same flight: EKF height rate through the climb, truth +1.02 m/s | +0.22 m/s | +0.82 m/s |
| Replay of four flights, coast stop: AGL height after touchdown while the filter is still valid | 0.16-1.14 m | 0.06-0.17 m |
The coast stop leaves every value in flight and every takeoff of those four flights unchanged.
<img width="1260" height="896" alt="aglkf_1_replay_ab_log9" src="https://github.com/user-attachments/assets/fec36d10-5f5c-452c-8890-b0fc8e3a3c43" />
<img width="1260" height="504" alt="aglkf_2_ground_windup" src="https://github.com/user-attachments/assets/366eb00a-e302-45b0-901b-dc9e6f29151a" />
### Description
Detail for reviewers, not needed to review the change in outline.
The floor clamp is one-sided, and that is the mechanism. An error that points up lifts the height off the floor and restores the innovation; one that points down presses the height into the floor, where the innovation is zero and the error latches. Clearing the downward velocity gives the downward side the behaviour the upward side already had. On master the decay, running on most steps, happens to keep the floor wind-up small, so the floor clear lands before the decay gate that would expose it.
On the motivating flight `XKFA.VAgl` ramped linearly to -7.15 m/s over the 88 s before lift-off with `Valid` 1 throughout. The takeoff then held the height on its 0.05 m floor through a climb to 3 m, and the EKF height stepped +2.21 m when the height source went back to baro. A later 487 s ground dwell with the change held the velocity at worst -0.012 m/s. Confirmed on nine flights.
In-flight aiding is unchanged: replaying an acro flight through both versions leaves the aiding-mode transitions identical (2 and 6 per core) and the flow lane relative-aided for the same 88.5 percent of the flight.
The decay gate keys on time since the last fusion, so readings the innovation gate rejects for more than 500 ms still let the decay run; after 5 s the existing hard reset takes over.
The coast stop needs an out of range low report within the last 500 ms, no range sample for 200 ms, and the main filter moving vertically slower than 0.25 m/s either way; faster is a liftoff or touchdown still below the sensor minimum and is left to the IMU. It does not use the vehicle's landed state, so a missed takeoff cannot hold it on in flight; the filter still goes invalid after 5 s without a reading and re-initialises from the next one, as before.
The floor hold covers what the coast stop leaves to the IMU. On one of the fourteen flights, after an acro sortie, a lane's main filter read 0.3 to 0.57 m/s up for the eight seconds after touchdown, so the stop never fired and the AGL height coasted from 0.28 to 1.8 m before the 5 s timeout cleared it. The hold engages when the last reading the AGL KF fused or re-initialised from was within 0.5 m of the floor, no range sample has arrived for a second, out of range low reports are still arriving, and the AGL height is more than 0.3 m above that last reading. It then holds the height on the floor with zero velocity, latched until a range sample arrives or the low reports stop. Like the coast stop it reads no landed state, and it leaves the validity timeout and the 5 s re-initialisation alone. Replay of the fourteen flights through the hold as first written, which also counted readings the gate rejected: the coast after that touchdown is held at the floor, and every other flight, the liftoffs included, is unchanged. That Replay has not been re-run on the final hold.
The hold tests against the last fused reading rather than the floor because a range finder whose minimum is well above its ground clearance reads out of range low through a normal liftoff, and the height has to climb through that minimum. A reading the innovation gate rejects does not count: counting it let a short reading at height, from an obstruction or a glitch, followed by out of range low reports, pin a hover to the floor while the height stayed valid. Before any fused reading the hold cannot engage. A plain one second timeout without the height test zeroed the velocity of a real liftoff (+0.36 m/s on one flight); without the latch the coast regrew from the IMU and the height ran a 0.35 m sawtooth.
An earlier landed hold, keyed on the land detector and `takeoff_expected`, was dropped before the coast stop. The two are not independent in an automatic throttle mode (a land detector stuck through a flight also keeps `takeoff_expected` latched), that hold stopped the 5 s re-initialisation while the covariance kept being predicted, and keeping a timed-out filter valid made first takeoffs track worse (0.150 -> 0.216 m RMS on one flight). The floor hold uses neither flag and does not touch validity.
Known limits of the hold, left as is. It decides the vehicle is on the ground from the range finder alone, so a range finder that reports out of range low at liftoff while the vehicle is within its range (any reading under the minimum is reported that way, so a sensor that returns zero on a lost return would do it) holds the AGL height on the floor, still valid, until the 5 s timeout. Measured in SITL on #33507's head, with the range finder minimum raised above the reading once the copter was 0.15 m off the ground: the height followed the climb to 1.58 m, then sat at the floor for 3.9 s while the copter climbed to 9.3 m; on the same sequence it read 3.79 m with the hold disabled and 3.37 m on master, so nothing reads well there, but only the hold reads the floor. After a real touchdown a liftoff inside the 5 s window can also start with the hold latched; from the source, not measured, the height then stays on the floor until the range finder reads above its minimum again, so the error is bounded by the minimum's height above the ground clearance. Releasing the hold on a liftoff cue would need a flying flag the EKF cannot trust. A range finder whose minimum is more than 0.5 m above its ground clearance never gets the hold.
The touchdown test sets `RNGFND1_GNDCLR` from the reading on the ground and the minimum 0.4 m above it, because SITL's range finder reads 0.55 m on the ground over the CMAC terrain tile, which CI has, and 0.0 m without it. The hover test runs its provocation at speedup 1: each rejection doubles the height variance until the gate lets the short reading in, so the minimum has to rise within a fraction of a second, 0.2 s here.
The out of range low time stamps the coast stop and the hold read are the same member, reset and update that #34292 adds, so the two merge without a conflict there.
Measured and rejected: bounding `aglKfV` to a fixed range caps the wind-up without stopping it, so the takeoff still starts from a large wrong velocity. The clamp after the measurement update is left alone: the updated height is a convex combination of a prediction and a measurement both already at or above the floor, so it cannot fire.
The clamp is written `!(aglKfH >= rngOnGnd)` so that a NaN height is still pulled back to the floor, as the `MAX()` it replaces did.
The floor test provokes the wind-up with a range step because an injected accelerometer bias is learned back out by the main filter's on-ground zero-velocity fusion.
#33507 (accel-Z bias state in the AGL KF) stacks on this.