Softivation

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.

Furkan IŞIKAY Updated: 4 min read

The real vehicle that will carry the code written in simulation
Look at the green box on top of the boards: in the field, the code written in simulation runs on this companion computer.

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.

Virtual flight with SITL and Gazebo
Look at the fixed-wing vehicle on the runway: ArduPilot running in SITL flies this model in Gazebo like a real plane.

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

Python code sends commands to SITL, Gazebo simulates the vehicle and the camera, OpenCV finds the target in the image
Look at the direction of the arrows: in the field, only the SITL box is replaced by the Pixhawk, and the code runs over the same connection.
Camera field of view of the fixed-wing vehicle in Gazebo
Look at the white lines coming out of the nose: this is how Gazebo draws the area the camera sees, and OpenCV works with this image.

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

  1. Fly the code in simulation from start to finish, including the failure cases.
  2. On the real vehicle, test without propellers first: see that the commands go through and the modes change.
  3. Make the first flight in an open area, with the pilot holding the transmitter and the software failsafes enabled.
  4. Compare the board's flight log (.bin file) with the behavior in simulation.

Common sticking points in 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.

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?

Free resourceArduPilot SITL setup guideSet up the simulation on your own computer: Ubuntu, Windows WSL, or a virtual machine, start to finish.