The mission
The sub has to steer through a gate it finds with its camera. Detection runs at about 54 Hz and the gate is plainly visible.
What happened
Asking the camera driver for a frame after the consumer had been busy for 400 ms.
It steers to where the gate was about a third of a second ago, and hunts from side to side.
What I believed
Drain the queue and take the last frame, as every reference recommends, and you're caught up.
- The detector's too slow. Use a smaller model. A faster model barely moved the lag. He measured where the time went before changing anything else, and it wasn't the model.
- Drain the camera queue and use the last frame, as the references recommend. 396 ms became 348 ms. The standard fix recovers 12 %, because a full queue holds the oldest frames, not the newest.
What was true
A full V4L2 queue keeps the oldest frames and drops every new one. Draining hands you the least-old fossil: 348 ms stale instead of 396. A keep-up thread got 16.9 ms.
Try it yourself
Loading the diagram. It needs JavaScript; the text around it says the same thing in words.
The fix
When the driver's buffers are full it keeps the oldest frames and throws away every new one, so draining the queue hands you the least-old fossil. A thread that keeps only the newest frame makes the picture current.
Frame age: 396.1 ms as shipped, 348.1 ms with the standard fix, 16.9 ms with a keep-up thread.
Evidence: 50d69a8, the frame-age note
Keep doing
The standard recipe recovered 12 % of the problem. Read the driver's own sequence counter before trusting the recipe.
Do differently
Keep only the newest frame; never let a slow stage queue behind a fast one.