Software
Autonomous Target Detection: Landing in 5 Steps
How does raw footage from the Pi Camera turn into a LAND command? We walk through the five steps of our OpenCV pipeline in order. Bonus: which step gave us the most trouble.
Watching what the camera sees on a screen trackside, you see a green contour dancing around the target. That looks simple, but there are five steps underneath it. We run all of them on the Raspberry Pi 5, without sending any data to a ground station, because if a 4G/5G connection actually works on competition day, we count ourselves lucky.
1. Data capture
The Pi Camera Module 3 scans the ground target in real time. It has 12 MP resolution and autofocus, so it captures sharp frames even while the UAV is moving. Mounting the camera directly to the frame is a mistake; in our first test, 'jello' effect kept making contour detection jump around. The fix: anti-vibration mounts we designed ourselves and printed in TPU.
2. Image processing (OpenCV)
The image is first converted to HSV. Working in RGB gave us a lot of headaches with changing light. Once we switched to HSV, a single threshold was enough to handle the difference between sun and shadow. Contour detection comes after the color filter, giving us the target's center coordinates in pixels.
3. Error calculation
The camera's center coordinate is fixed. Subtracting it from the target's center coordinate gives us the error vector. That's a vector in pixels; if we know the flight altitude, it's converted to a physical distance. Altitude comes from a GPS + barometer combination, and averaging the two is more reliable.
4. Action decision
If the error is outside tolerance, the Pi 5 sends a velocity_target command to the Pixhawk over PyMAVLink. Right, left, forward, back, at a speed of around 0.5 m/s. This loop repeats until the error falls within tolerance. The hard part here is gain tuning; too high and it oscillates over the target, too low and it never centers at all.
5. Mission execution
Once the error on the X and Y axes is close to zero, the Pi 5 sends a LAND command to the autopilot. The Pixhawk descends gradually, slowing down in the last meter. In the final stage of landing the camera no longer gives reliable data (the target is too close, the contour disappears), so the last meter is left entirely to the Pixhawk.
- The pipeline runs entirely onboard, no visual data goes to the ground station.
- The TPU anti-vibration mounts improved OpenCV's accuracy by 15% (measured from our own logs).
- In the first SITL run the y-axis sign was wrong, landing 4 m off target.
- The final meter of landing is left entirely to the Pixhawk's barometer.
Send us your questions or collaboration ideas about this post.
Get in Touch →