Set up PX4 SITL

Optional. The X500 simulates and takes motor commands from ROS 2 without an autopilot; this page adds PX4, the flight code the real airframe runs, built for the PC (“software in the loop”, SITL), so the simulated X500 can be armed, flown and sent on missions like the real one.

Note

Written and tried on Ubuntu 26.04 with ROS 2 Lyrical and its Gazebo (Jetty). It has not been tried inside the drydock container yet.

Prerequisites

Install the models into a colcon workspace; ~/ws below.

The Python environment below needs the venv module, which a fresh Ubuntu does not have:

sudo apt install python3-venv

What gets installed

Piece

What it is

How

Where

PX4

the flight code, built as a PC program, px4

source, make

~/ws/thirdparty/PX4-Autopilot

A Python environment

the tools PX4’s build needs

venv and pip

inside the PX4 checkout

QGroundControl

the ground station, to watch and command the vehicle

AppImage download

anywhere; optional

Nothing is added to Gazebo: PX4’s own program connects to the simulation.

Get the sources

PX4 is pinned to the release v1.18.0. It is the first release that builds against the Gazebo that ROS 2 Lyrical ships; older ones do not.

mkdir -p ~/ws/thirdparty
cd ~/ws/thirdparty
git clone --branch v1.18.0 --depth 1 https://github.com/PX4/PX4-Autopilot.git
git -C PX4-Autopilot submodule update --init --recursive --depth 1

About 1.8 GB.

The Python environment

PX4’s build wants versions of some Python packages that differ from the system’s (empy 3, where Ubuntu 26.04 has 4), so they go in an environment of their own, never in the system Python:

cd ~/ws/thirdparty/PX4-Autopilot
python3 -m venv --system-site-packages venv-px4
venv-px4/bin/pip install -r Tools/setup/requirements.txt

--system-site-packages keeps ROS visible from inside it.

Build

ROS first, so the build finds Lyrical’s Gazebo, then the environment:

source /opt/ros/lyrical/setup.bash
cd ~/ws/thirdparty/PX4-Autopilot
source venv-px4/bin/activate
make px4_sitl_default

Near the top of the output it must say which Gazebo it found:

-- Looking for gz-transport -- found version 15.1.0
-- Looking for gz-sim -- found version 10.5.0

and it ends by linking bin/px4. Between 1200 and 1500 steps, a few minutes.

Environment

One script to source in every shell that runs PX4, after the workspace. Save this as ~/ws/thirdparty/setup-px4.sh:

# Source before running PX4 SITL, after the colcon workspace.
export PX4_DIR=$HOME/ws/thirdparty/PX4-Autopilot
[ "$VIRTUAL_ENV" = "$PX4_DIR/venv-px4" ] || . "$PX4_DIR/venv-px4/bin/activate"

and source both:

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

PX4_DIR is where this repository’s tools look for PX4.

Check it

$PX4_DIR/build/px4_sitl_default/bin/px4 -h

prints PX4’s usage. If instead it says error while loading shared libraries: libgz-utils.so..., ROS was not sourced in this shell.

Next: Fly the X500 with PX4.

Troubleshooting

Fetched in submodule path ..., but it did not contain <sha> while getting the submodules: a submodule is pinned to a commit that is not at the tip of a branch, which a shallow fetch cannot reach. Run the same command without --depth 1:

git -C PX4-Autopilot submodule update --init --recursive

ModuleNotFoundError: No module named 'menuconfig' during the build: the environment is not active; menuconfig comes with kconfiglib, which is in it. source venv-px4/bin/activate, then make again.

The build stops in uxrce_dds_client with does not match the source ... used to generate cache: the ROS 2 link line was added after a first build. Remove that part of the build and build again:

rm -rf build/px4_sitl_default/src/modules/uxrce_dds_client
make px4_sitl_default

The build succeeds, but it printed Could NOT find gz-transport and built no gz_bridge: ROS was not sourced before make. Nothing fails here: the build leaves the bridge to Gazebo out and exits with 0, and PX4 then cannot attach to a simulation. Remove build/px4_sitl_default and build again with ROS sourced.

Reference

Why this release

PX4’s bridge to Gazebo looks for gz-transport 13, 14 or 15. ROS 2 Lyrical ships 15, and v1.18.0 is the first release that knows it. When the pin moves, this page and the spec move together.

Why a line in a board file

PX4 builds its Micro XRCE-DDS client for version 2 by default, which matches ROS 2 up to Kilted. Lyrical’s DDS needs version 3 on both ends: this option on PX4’s side, and the agent v3.0.1 on ROS 2’s.