Guides / autonomy
Building Hawk without the drone
Everything in the AI drone except the aircraft runs on a laptop against PX4's own simulator. What to install on Linux, a Mac or Windows, how to start and stop it, the quirks that cost me days, and the test every control script should pass before it meets a real flight controller.
autonomy20 min15 November 2026
Built and measured on two Apple Silicon Macs, October and November 2026, PX4 from the main branch, Gazebo Harmonic. The Linux and Windows paths are PX4’s own and I have not timed them; where a number is mine I say so. The same setup flew the real Holybro X500 later with no code changes on the aircraft side.
Nothing in the AI drone needs the drone until the last step. The flight controller firmware, PX4, builds and runs on the laptop as a normal process, Gazebo gives it a physics world and a quadcopter to fly, and the control script talks to it over MAVLink on a UDP port exactly as it later talks to the real aircraft over a telemetry radio. Every rule, limit and failure case in the project was written and tested this way first. Here is the setup.
What you need
- A laptop with about 15 GB free and a GPU that can draw a 3D window. Any of the three operating systems below; the simulator and your script are the same on all of them.
- PX4-Autopilot, cloned with submodules. The repository is large, around 2 GB after the build.
- Nothing else by hand. PX4 ships a setup script per platform that installs the compiler toolchain and Gazebo.
git clone --recursive https://github.com/PX4/PX4-Autopilot.git
cd PX4-Autopilot
Linux. Ubuntu 22.04 or 24.04 is PX4’s first-class platform and the path with the fewest surprises. One script installs everything, Gazebo included, and asks for your password once:
bash ./Tools/setup/ubuntu.sh
Log out and in again afterwards so the group changes take, then carry on at step 1.
Mac. The same idea, one script, with one flag that matters:
./Tools/setup/macos.sh --sim-tools
--sim-tools is the part that matters. Gazebo on a Mac is fragile when it comes from Homebrew, because Homebrew rebuilds it whenever a dependency moves and the pieces stop matching. The setup script instead pulls one pinned set of Gazebo, protobuf and GStreamer binaries from conda-forge into Tools/setup/macos/.pixi. Everything Gazebo loads comes from that one place. You need Xcode’s Command Line Tools and Homebrew first. On one of my Macs the build also wanted brew install ninja pkgconf ccache, and the Command Line Tools had to be updated before Homebrew would build one small tool. softwareupdate --install did that without an administrator password.
Windows. PX4 does not build natively on Windows. Install Ubuntu under WSL 2, which on Windows 11 is one command in an administrator terminal, wsl --install, then follow the Linux path inside it. The Gazebo window comes through WSLg on its own. Clone the repository inside the Linux filesystem, not under /mnt/c, or the build takes several times longer.
1. Start it
cd PX4-Autopilot
source .venv/bin/activate
make px4_sitl gz_x500
The first run compiles PX4, a few minutes, then opens the Gazebo window with an X500 quadcopter sitting on a flat world. In the terminal you get the PX4 shell. When it prints Ready for takeoff! the autopilot is live and listening for a ground station on UDP 14550 and for a control script on UDP 14540.
Stop it with Ctrl-C in that terminal, then pkill -f "gz sim", because the Gazebo server often outlives PX4 and the next start will quietly attach to the stale world.
2. Talk to it
MAVSDK is the library the project uses, from Python. Install it in a virtual environment of your own, not PX4’s:
python3 -m venv .venv && source .venv/bin/activate
pip install mavsdk
The smallest possible flight:
import asyncio
from mavsdk import System
async def main():
drone = System()
await drone.connect(system_address="udpin://0.0.0.0:14540")
async for state in drone.core.connection_state():
if state.is_connected:
break
async for health in drone.telemetry.health():
if health.is_global_position_ok and health.is_home_position_ok:
break
await drone.action.arm()
await drone.action.takeoff()
await asyncio.sleep(10)
await drone.action.land()
asyncio.run(main())
The connection string is the only line that changes for the real aircraft. On the radio it becomes serial:///dev/cu.usbserial-XXXX:57600. That is the whole point of the exercise: the script that passes in the simulator is the script that flies.
3. Watch it like you will in the field
QGroundControl connects to the simulator on its own, on 14550, and shows the same map, the same parameters and the same preflight messages you will see at the launch site. Install it now and keep it open. Two things it gives you that the terminal does not: the flight mode as PX4 sees it, which is what the override logic below is about, and the ability to change parameters, which in this project is always a human’s job and never the script’s.
If your script and QGroundControl both want the aircraft’s attention you will eventually see them fight, especially when testing what happens when a second ground station sends Land. For those tests close QGroundControl.
The quirks
Each of these cost me at least an hour, one of them a day. The first and the last apply everywhere; the middle three are the Mac.
-
The first launch times out. The first time Gazebo loads its plugins, macOS inspects every new library, and that can take two or three minutes. PX4 waits thirty seconds for the world and gives up with
Timed out waiting. The world is fine, it is still coming up. Run the samemakeagain and PX4 attaches to it. After the first time it is cached and starts in seconds. -
The 3D window dies with “unable to find Metal Rendering Subsystem”. The rendering plugin is installed, Gazebo looks in the wrong place for it. Export
OGRE2_RESOURCE_PATH=<PX4>/Tools/setup/macos/.pixi/envs/default/lib/OGRE-Nextbefore starting and the window works. Without a window,HEADLESS=1 make px4_sitl gz_x500runs the same physics with no rendering, which is what I use on the second machine. -
Gazebo’s command line sees nothing from another terminal.
gz topic -lfrom a second shell finds no topics unlessGZ_IP=127.0.0.1is set there. Discovery goes out on the wrong interface otherwise. -
The log eats the disk. PX4’s shell prompt is written to the terminal thousands of times a minute. Pipe the output through
head -corgrep -v 'pxh>'into a file, or one session fills gigabytes. -
There is no transmitter. On every platform the simulated airframe takes its stick input over MAVLink, so manual flight modes need a joystick in QGroundControl: a USB gamepad, your real transmitter through a simulator cable, or QGroundControl’s on-screen joystick. You want this early, because the most important tests are the ones where a human takes over mid-command.
A script that wraps all of this, with the timeout relaunch, the platform’s environment variables and the camera following the aircraft, is twenty lines of shell. Write it on day one.
What to build against the simulator
Not the fun parts. The rules.
- Reposition, never stream. A move is one
goto_locationcall in Hold mode. If the laptop dies mid-move the aircraft holds where it is and every PX4 failsafe, radio loss, battery, geofence, stays in force. Offboard mode, which needs a setpoint every few hundred milliseconds, is off unless something specific needs it. - Limits in code, not in prompts or comments. Maximum step per axis, ceiling, floor, radius from home, minimum interval between commands. A function that returns “refused” is a rule. Anything else is a suggestion.
- Judge by telemetry, not by acknowledgement. Over a radio an arm or take-off command can succeed while its reply is lost. The simulator never loses one, so write the code as if it will: send, then watch the state for a few seconds, then decide.
- The transmitter always wins. Subscribe to the flight mode and treat any mode you did not command as the pilot taking over: abort every running routine, send nothing more, and do not even send a stop, because a stop is a Hold command and a Hold would cancel the pilot’s Land.
The test that matters
Before any script of mine meets the real flight controller it passes this in the simulator, with the joystick plugged in:
- Command a take-off. While the aircraft is still climbing, select Land on the joystick. The script must go silent and the aircraft must land.
- Command a five-metre move. Halfway, select Land. Same result.
- Command a move, and from a second ground station send Land. Same result, and the next arm after touchdown must succeed.
The second one is the one that found the bug. An altitude top-up that ran on a timer sent a reposition twelve seconds after the pilot had selected Land, and the MAVSDK call it used switches the aircraft to Hold first. A metre from the ground, the real aircraft climbed hard. The simulator would have shown it in an afternoon if I had written this test first.
What the simulator cannot tell you
- Radio. The simulated link is perfect. Lost acknowledgements, stale telemetry, a heartbeat timeout at seven metres because both antennas were pointing at each other end-on, all of that arrived with the real aircraft.
- Compass. The simulated world has a clean magnetic field. A rooftop with steel underneath makes the heading creep and the aircraft orbit its hold point, and no code change fixes that.
- Take-off twist. The real airframe yaws left during every thrust ramp because one motor is a degree off vertical. The simulator’s motors are perfect.
- Batteries, wind, props, and what your own hands do under pressure.
Build the rules here. Expect the field to find the rest.