How to Use Gazebo with PX4 for Drone Simulation
To use
Gazebo with PX4, first install PX4 and Gazebo simulator, then run PX4's simulation environment using make px4_sitl gazebo. This launches PX4's software-in-the-loop with Gazebo, allowing you to simulate drone flight and test autopilot code.Syntax
To start PX4 with Gazebo simulation, use the following command in your PX4 firmware directory:
make px4_sitl gazebo: Builds and runs PX4 SITL (Software In The Loop) with Gazebo simulator.make px4_sitl gazebo_: Runs PX4 SITL with a specific Gazebo drone model (e.g., iris, typhoon).gazebo: The simulator that visualizes the drone and environment.
This command compiles PX4 SITL and launches Gazebo with the selected drone model for simulation.
bash
make px4_sitl gazebo
# or for a specific model
make px4_sitl gazebo_irisExample
This example shows how to run PX4 SITL with the default Gazebo drone model (iris) and start the simulation.
bash
cd ~/PX4-Autopilot make px4_sitl gazebo_iris
Output
[px4] INFO [px4] Starting simulation with Gazebo Iris model...
[px4] INFO [commander] Startup complete
[px4] INFO [mavlink] MAVLink protocol started
[px4] INFO [gazebo] Gazebo simulation running with PX4 SITL
Common Pitfalls
Common mistakes when using Gazebo with PX4 include:
- Not installing Gazebo or PX4 dependencies properly, causing simulation launch failures.
- Running
make px4_sitlwithout specifyinggazebo, which won't start the simulator. - Using incompatible Gazebo versions; PX4 supports Gazebo 11 and newer.
- Not sourcing the PX4 environment or setup scripts before running commands.
Always check your installation and environment setup before running simulations.
bash
## Wrong way (missing gazebo): make px4_sitl ## Right way: make px4_sitl gazebo
Quick Reference
| Command | Description |
|---|---|
| make px4_sitl gazebo | Build and run PX4 SITL with default Gazebo model |
| make px4_sitl gazebo_iris | Run PX4 SITL with Iris drone model in Gazebo |
| make px4_sitl gazebo_typhoon | Run PX4 SITL with Typhoon drone model |
| gazebo | Launch Gazebo simulator separately (if needed) |
| source ~/PX4-Autopilot/Tools/setup_gazebo.bash | Set up Gazebo environment variables for PX4 |
Key Takeaways
Use 'make px4_sitl gazebo' to start PX4 simulation with Gazebo.
Specify drone model like 'gazebo_iris' to simulate different drones.
Ensure Gazebo and PX4 dependencies are installed and environment is sourced.
Use Gazebo 11 or newer for compatibility with PX4.
Check for common mistakes like missing 'gazebo' in make command.