Simulation first: how we test drone software before it ever flies
Testing drone software on a real drone first is backwards. We get why people do it. The code is written, the drone is sitting on the desk, and the field is just down the road. So you charge the batteries, drive out and
Testing drone software on a real drone first is backwards.
We get why people do it. The code is written, the drone is sitting on the desk, and the field is just down the road. So you charge the batteries, drive out and fly.
Then something goes wrong, and you're standing in a field trying to work out why. Was it the code? The wind? A loose cable? The GPS? You can't rewind the flight. You can't fly those same seconds again with one thing changed. And if the drone hit the ground, you're done for the day.
A real flight is the most expensive way there is to find a software bug. It's slow, it depends on the weather, and every mistake costs hardware.
So we work the other way round. Our software flies in simulation first: again and again, through wind and bad light, against rules we wrote down before it took off. By the time it goes anywhere near a real aircraft, it has earned it.
Here's the loop everything below fits into, and seven lessons from building it. If you're getting into robotics, we hope they save you a few broken propellers.
1. Use the real autopilot, not a toy
Every drone has an autopilot: a small onboard computer that reads the sensors, keeps the drone level and flies it where it's told. We use ArduPilot, one of the big open-source autopilots.
ArduPilot can run as SITL, short for software in the loop: the same code that flies real drones, built to run on an ordinary computer. Its sensor readings come from a simulator, and it flies a virtual drone exactly as it would fly a real one.
That matters more than it sounds. Flight modes, failsafes (the rules for what to do when something goes wrong), take-off and landing logic: your code has to cope with all of it, just like in the field. We even talk to SITL the way we'd talk to real hardware, over MAVLink, the message protocol ArduPilot and many other autopilots speak.
Do: test against the real autopilot code. Don't: write your own simplified drone and trust it.
*2. Build worlds from real places
*
For the world itself we use Gazebo, an open-source robot simulator, and ROS 2 (the Robot Operating System) as the plumbing that carries camera images and other data between programs.
Gazebo draws what a camera under the drone would see, so the ground had better look like real ground. Ours is made from orthophotos: aerial photos corrected so every pixel sits exactly where it belongs on a map. We've built worlds from a park in Ohio, part of Amman, the edge of El Paso in Texas and a block of San Francisco.
One trick we like: build the world from one year's photos and give the software maps from another year. Fields get ploughed, shadows fall the other way, cars park somewhere else. Software that only ever sees perfect copies of its own maps learns nothing about the real world.
Do: use real imagery of real places, from more than one date. Don't: test on a texture you made up.
3. Then add the third dimension
A photo laid on a flat plane looks perfect from straight above. But real buildings and trees stick up, and as the drone moves, a rooftop slides across the image faster than the street below it. That's called parallax, and a flat world simply doesn't have it.
So we build 3D versions of our worlds too. The heights come from lidar (laser scans from aircraft, which many governments publish for free) and from photogrammetry, which rebuilds 3D shapes from lots of overlapping photos. The photos are then draped over those shapes, so the buildings stand up.
Before we trust a 3D world, we check it against real photos that weren't used to build it. If they don't line up, the world isn't ready.
Do: test in 3D before you believe a flat-world result. Don't: assume the ground is flat because your simulator's is.
4. Remember that the simulator lies too
A simulator is software, and software has bugs and shortcuts. Simulated sensors are often too clean: no vibration, perfect timing, no blur. Sometimes they're worse than the real thing in odd ways. A simulated gyroscope can drift far more than a real one, and a simulated GPS may use a simpler model of the Earth than a real receiver.
So we test the simulator before we test our software. Every new world has to pass its own checks. Is the ground image the right way up and not mirrored? Does the drone start where the map says? When it flies north, does it actually go north? That last one catches a classic: Gazebo counts east, north and up, ArduPilot counts north, east and down, and mixing them up fails silently.
Then we make conditions harder on purpose: wind and gusts, blur, noise, changes in exposure, even the camera going blank mid-flight.
Do: write tests for your simulator, not just your code. Don't: trust perfect sensors.
5. Record everything, and make replays exact
Every flight records every input our software sees: each camera frame, each attitude reading (the drone's tilt and heading), each timestamp. Feed that recording back through the same code, offline, and you get exactly the same answer.
That changes how you debug. When something odd happens halfway through a flight, you don't try to recreate it. You replay that exact moment, add logging and look. When you change the code, you replay every recorded flight overnight and see what got better and what got worse.
Exact replays take discipline: no hidden randomness, no reading the computer's clock, the same inputs in the same order every time. It's worth it.
Do: make every run replayable. Don't: debug by flying again and hoping it happens twice.
6. Write the pass mark before you fly
This is the one we feel strongest about. Decide what "pass" means before the test, write it down and commit it to git. Then leave it alone.
If you set the bar after you've seen the result, you'll find a way to pass. Everyone does. Machine learning people call it tuning on the test set.
So every test of ours starts with a rules file. We also keep sealed test routes, planned and locked in advance, with a fingerprint (a hash) of each route file stored so any change shows up. Nobody flies them or tunes anything on them until the software is frozen. Then they're flown under rules written in advance, and the result is the result. A bad number is never a reason to fly again.
Here's the idea in a few lines of Python. The scoring script refuses to judge a run if the rules changed after they were committed:
import json
import subprocess
def committed_and_unchanged(path):
"""True if the file is in git and hasn't changed since the last commit."""
tracked = subprocess.run(["git", "ls-files", "--error-unmatch", path],
capture_output=True).returncode == 0
unchanged = subprocess.run(["git", "diff", "--quiet", "HEAD", "--", path]).returncode == 0
return tracked and unchanged
def judge(run, rules_path="rules/sealed_route.json"):
if not committed_and_unchanged(rules_path):
raise SystemExit("The rules changed after they were committed. Not scoring this run.")
with open(rules_path) as f:
rules = json.load(f)
checks = {
"finished the route": run["completed"],
"average error": run["mean_error_m"] <= rules["max_mean_error_m"],
"worst error": run["max_error_m"] <= rules["max_error_m"],
"landed close to the target": run["landing_error_m"] <= rules["max_landing_error_m"],
}
for name, ok in checks.items():
print("PASS" if ok else "FAIL", name)
return all(checks.values())
Do: write the rules first, in version control. Don't: move the goalposts after the flight.
7. Simulation doesn't replace flying. It decides what's worth flying.
A simulator can't tell you everything. It doesn't know how your real lens bends the picture, how your frame vibrates, how the light looks on a hazy morning or exactly how your camera is mounted. Those things only show up on real hardware, and they matter.
So we treat simulation as the place where software earns the right to fly. A real flight shouldn't be a guessing game. It should answer specific questions the simulator couldn't, with the pass mark already written down and every input recorded for replay back at the desk.
Do: fly real hardware with a plan. Don't: fly just to see what happens.
Try it yourself
Everything here is free and open source. Gazebo, ArduPilot SITL and ROS 2 run on a normal Linux computer, and many countries publish aerial photos and lidar for free. You can have a simulated drone flying over a real place without spending anything but time, and crash it as often as you like.
We're Nabla Technologies, and we build software for drones and critical operations. If you're learning this stuff, building something similar or think we've got something wrong, we'd love to hear from you at [email protected]
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.