Fly the X500 with PX4

Take off, hold and land the simulated X500 under PX4, to confirm that the autopilot and the model work together.

Prerequisites

Set up PX4 SITL.

Steps

Two shells. Both need the workspace and then the PX4 environment, in that order:

source ~/ws/install/setup.bash
source ~/ws/thirdparty/setup-px4.sh

First, the simulation:

gz sim -r $(ros2 pkg prefix --share x500_gazebo)/worlds/x500_px4.sdf

The quad sits on its landing gear and nothing moves.

Then the autopilot:

ros2 run x500_gazebo px4_sitl.py

PX4 prints its banner, finds the model, and leaves you at its prompt:

INFO  [init] Gazebo world is ready
INFO  [gz_bridge] world: x500_px4, model: x500
...
INFO  [commander] Ready for takeoff!
pxh>

Ready for takeoff! comes a few seconds after the prompt, when PX4’s estimator has settled. Then, at the prompt:

commander takeoff

Command

Expected

commander takeoff

Armed by internal command, the rotors spin up, Takeoff detected; the quad climbs to about 2.5 m in 8 s and holds there within a few centimetres

listener vehicle_local_position -n 1

z close to -2.5: PX4 counts down as positive, from where it started

commander land

it comes down where it is; Landing detected, then Disarmed by landing and the rotors stop

shutdown

PX4 exits; Gazebo keeps running

To see what PX4 commands the motors, in a third shell:

gz topic -e -t /x500/command/motor_speed

Four velocities in rad/s, about 150 when armed on the ground and about 690 in hover, of the 1000 the motors can do.

With a ground station

QGroundControl connects by itself to a PX4 on the same computer: open it at any point and the vehicle appears on the map, in Zurich, where PX4 puts its simulated vehicles and where the world has its origin. Take off and land from its toolbar.

Note

The pages here were tried without QGroundControl, with the commands above.

Things to know

  • Start Gazebo first. PX4 attaches to a model that is already there. Started alone it waits, printing Waiting for Gazebo world... once a second, and after 30 seconds it gives up with Timed out waiting for Gazebo world and exits. Start it again once Gazebo runs.

  • PX4 remembers parameters between runs, in build/px4_sitl_default/rootfs/<instance>/ of its checkout. The ones in this package’s parameters file are given again at every start, so they win; one you set by hand with param set stays until you set it back.

  • Preflight Fail: system power unavailable is printed once at start, for a battery the simulation does not have. It stops nothing.

  • A second run does not need Gazebo restarted: shutdown, then px4_sitl.py again.

What px4_sitl.py does

It starts PX4’s own program, told to attach to a model in a running Gazebo and not to start one:

--world

the world the model is in, by name or SDF file; x500_px4 by default

--name

the model; x500 by default

--instance

PX4’s instance number, one per vehicle

--params

the parameters file; this package’s by default

--param NAME=VALUE

one more parameter, over the file; may be repeated

--px4-dir

the PX4 checkout; $PX4_DIR by default

--daemon

no prompt; send commands with $PX4_DIR/build/px4_sitl_default/bin/px4-commander and the other px4-* programs. PX4 still runs in the foreground of its shell: add & to have the shell back

--print-env

print the environment PX4 would get, and stop

The airframe is PX4’s own simulated X500 (4001_gz_x500). What this model needs different is in x500_gazebo/params/x500_px4.params: where its rotors are, and two settings so the first flight needs no ground station and no remote control.

It also gives PX4 GZ_IP=127.0.0.1, unless GZ_IP is already set: Gazebo is on this computer. On a computer with several network interfaces, Docker’s bridges for one, Gazebo’s transport otherwise has several to choose from, and PX4 misses one sensor or another and never arms. With Gazebo on another computer, set GZ_IP yourself in both, each to its own address on the network they share; yours is kept.

Troubleshooting

Arming denied: Resolve system health failures first: ask PX4 what fails with commander check.

  • No connection to the GCS: a parameters file of yours without NAV_DLL_ACT 0, or a value saved from another run. Open QGroundControl, or param set NAV_DLL_ACT 0.

  • No valid data from Gyro 0, No valid data from Accel 0, barometer 0 missing, Found 0 compass (required: 1): a sensor does not reach PX4. If it is another one on every run, Gazebo’s transport is picking a network interface on a computer with several: px4_sitl.py sets GZ_IP=127.0.0.1 for PX4 against that; check that the shell did not have another GZ_IP, and set the same in Gazebo’s shell if it persists. If it is all four on every run, see never published below.

  • Gyro #0 fail: STALE!, BARO #0 failed: STALE!: PX4 gets the same sample again and again and takes the sensor for dead. The model’s PX4 sensors lost their noise.

PX4 starts, and listener sensor_gyro says never published: PX4 is listening to topics nobody publishes. The world’s name and the model’s name are part of them. Check what PX4 was told (gz_bridge] world: ..., model: ...) against gz topic -l | grep base_link/sensor. A model generated for one world does not work in a world of another name (the PX4 variant).

Waiting for Gazebo world..., then Timed out waiting for Gazebo world: Gazebo is not running, or the two shells do not share GZ_PARTITION.

It takes off and flips, or spins: the rotor order, spin or positions PX4 has do not match the model. Compare param show CA_ROTOR* with x500_px4.params.