Developing autonomous drone software in SITL and Gazebo
Testing autonomous flight code in simulation with SITL, Gazebo and OpenCV before giving it to a real drone: what you test, which tools you use, and how to move to the field safely.

Running autonomous flight code on a real drone on the first try is an expensive mistake: a wrong sign, a flipped coordinate or a forgotten failsafe brings the vehicle down. Flying the code in simulation first saves both money and time. You run dozens of tests a day without waiting for good weather, a field, batteries and a pilot.
What to test in simulation
- Mission logic: the order of takeoff, going to the target, payload drop and return home.
- Failure cases (failsafe): transmitter loss, low battery, loss of GPS signal. Testing these on a real vehicle is risky.
- Image processing: finding a target in the camera image and steering the vehicle toward it.
- Swarm: multiple vehicles flying together without colliding.
The most common mistake: testing only the flight where everything goes right in simulation. In SITL, you simulate transmitter loss in MAVProxy with param set SIM_RC_FAIL 1, and low battery by lowering the SIM_BATT_VOLTAGE value. See what your code does at these moments there, before you go to the field.

Which tools to use
ArduPilot's SITL runs the autopilot itself on your computer, and your code connects to it as if it were connecting to a real Pixhawk. Gazebo simulates the vehicle's physics, camera and surroundings in 3D. OpenCV lets you find objects in the camera image. Setting them all up together takes time, and version mismatches come up. That's why a preinstalled virtual machine gets you started faster; the course's virtual machine comes with all three installed together.
How the code runs in simulation

Example: testing code that takes off to 10 meters and holds in SITL
In a terminal, start SITL with the command sim_vehicle.py -v ArduCopter --console --map. MAVProxy also broadcasts the vehicle to 127.0.0.1:14550. Your Python code connects to this address with connect('udp:127.0.0.1:14550', wait_ready=True).
The code switches to GUIDED mode, ARMs and sends simple_takeoff(10). On the map the vehicle takes off, and in the console the altitude climbs to 10 meters and stays there. In the field you run the same code, changing only the connection address to the radio's port.
From simulation to the field
- Fly the code in simulation from start to finish, including the failure cases.
- On the real vehicle, test without propellers first: see that the commands go through and the modes change.
- Make the first flight in an open area, with the pilot holding the transmitter and the software failsafes enabled.
- Compare the board's flight log (.bin file) with the behavior in simulation.
Common sticking points in simulation
- SITL is running, but Mission Planner won't connect
- In SITL the drone always starts in Australia
- The second SITL vehicle won't connect in a swarm simulation
Error Messages and Their Meanings
No heartbeat in 30 seconds, aborting.
DroneKit didn't receive a single MAVLink packet from the vehicle for 30 seconds. Either the connection address is wrong or SITL hasn't started yet. Check MAVProxy's broadcast address and port.
Link timeout, no heartbeat in last 5 seconds
The connection had been established, but no packets have arrived for 5 seconds. SITL stopped, the computer came under heavy load, or in the field the radio went out of range. When the connection comes back, DroneKit carries on by itself.
PreArm: AHRS: waiting for home
The autopilot hasn't set the home position yet, so the ARM command is rejected. This takes a few seconds when SITL first starts. In your code, wait for vehicle.is_armable to be True before ARMing.
What's next
We build an AI-powered autonomous mission with SITL, Gazebo and OpenCV in the AI and Drone Simulation course. The first lesson is free for members. A short video of the sample mission built at the end of the course:
Was this article helpful?
Learn this topic from start to finish
Season plan: all six courses for 9 months, ₺790 a month. ₺390 a month for high school and university students, ₺351 the first month with your member code. You can stop in any month you like. Start the Season plan · I'm a student