AI & Autonomy · 3 min read

Test Autonomous Code Safely With SITL and Gazebo

Cover illustration for Test Autonomous Code Safely With SITL and Gazebo: a monitor showing an FPV simulator course on a tactical HUD display

Software-in-the-loop (SITL) simulation runs the real ArduPilot or PX4 autopilot code on your computer, flying a virtual drone. Pair it with Gazebo for 3D physics and cameras, and you can test autonomous code, missions and AI without risking a single prop.

Why simulate

  • A code bug in simulation costs nothing. On a real drone, it can cost the drone or worse.
  • You can test failsafes, like a lost companion computer or a dead battery, that you’d never want to trigger for real.
  • You can iterate quickly: change code, restart, fly again in seconds.

What you need

  • A computer running Linux (Ubuntu is the most common) or Windows with WSL2.
  • ArduPilot or PX4 source code and build tools.
  • Gazebo for 3D simulation with sensors and cameras.
  • A ground station: Mission Planner or QGroundControl.

ArduPilot SITL: the quick start

  1. Clone ArduPilot and install the prerequisites using the scripts in the repository.
  2. Start a simulated copter with sim_vehicle.py -v ArduCopter --console --map.
  3. Connect QGroundControl or Mission Planner to the UDP port the simulator prints.
  4. Arm and take off from the console or your ground station, then fly a mission.

To use Gazebo, install the ArduPilot Gazebo plugin and launch a world with a drone model. The ArduPilot docs describe the current setup.

PX4 SITL

PX4 builds its simulator target directly: make px4_sitl gz_x500 builds PX4 and launches Gazebo with the X500 quad model. QGroundControl connects automatically.

Connect your companion code

Your companion computer scripts can connect to SITL by changing one line: the connection string. Instead of /dev/serial0, use the simulator’s UDP address, such as udp:127.0.0.1:14550. Everything else, including MAVLink commands, stays the same. See MAVLink on a Raspberry Pi.

Simulate cameras for AI

Gazebo can render a camera on your simulated drone and publish the images. Feed those frames into your detection model to test object tracking logic end to end before you ever fly.

What simulation can’t tell you

  • Real vibration and sensor noise.
  • Wind gusts and turbulence.
  • How your real camera behaves in sunlight.
  • Hardware issues like loose wires or bad solder joints.

Treat simulation as the first test, not the last. After it works in SITL, test on the real drone at low altitude in an open space.

A testing workflow that works

  1. Unit test your logic with recorded data, no simulator needed.
  2. SITL without Gazebo for quick checks of commands and modes.
  3. SITL with Gazebo for full missions, cameras and sensors.
  4. Hardware-in-the-loop (optional): your real flight controller connected to a simulator.
  5. Real flight, low and slow in an open area.

Simulating failures

SITL lets you test what happens when things go wrong:

  • Turn off the simulated RC link to test radio failsafe.
  • Lower the simulated battery to test battery failsafe.
  • Disable GPS to see how the autopilot reacts.
  • Kill your companion script mid-mission to test heartbeat failsafes.

Each of these is dangerous to try on a real drone and easy in simulation.

Performance tips

  • Gazebo with cameras needs a capable computer and GPU.
  • Lower camera resolution and frame rate in simulation if your machine struggles.
  • Run headless (without graphics) for automated tests.

Recording and replaying

Record simulation logs exactly as you would real flights, and review them with the same tools. Comparing simulated and real logs later shows where your simulation differs from reality.

Building a custom simulation world

Gazebo worlds can include buildings, trees and terrain. To test missions realistically:

  • Create a world that resembles your real flying site.
  • Add obstacles to test avoidance logic.
  • Place visual targets to test detection and tracking.

Simulating sensors

Gazebo can simulate cameras, depth cameras, lidar and rangefinders. Match their settings (resolution, field of view, range) to your real hardware so results transfer better.

Automated testing

For larger projects, run simulations automatically:

  1. Start SITL and Gazebo headless.
  2. Run a mission or script.
  3. Check the result: did the drone reach each point, land safely, avoid obstacles?
  4. Repeat after every code change.

Simulation for learning, not just testing

SITL is also a great way to learn ArduPilot or PX4 before you build anything: practice setup, flight modes, failsafes and mission planning on your laptop.

Frequently Asked Questions

What is SITL?

Software-in-the-loop simulation. The real autopilot code runs on your computer and flies a virtual drone, so you can test missions and code safely.

Do I need Gazebo to use SITL?

No. ArduPilot SITL runs on its own with a simple physics model. Gazebo adds 3D worlds, realistic sensors and cameras.

Can I test AI code in a drone simulator?

Yes. Gazebo can simulate cameras, and you can run your detection and tracking code against the simulated images.

Does SITL work on Windows?

Yes, through WSL2, though many developers find native Linux simpler.

MR
Founder & Lead Builder

Marcus Reyes

Founder of DroneCoreGuide. Marcus has spent three years building, flying, crashing and rebuilding FPV and GPS drones, and he reviews and approves every guide on the site.

© 2026 DroneCoreGuide · An MD5TO property · A&M Olivieri LLC