Skip to content
github-actions[bot] edited this page Nov 14, 2026 · 8 revisions

Setup

Requirements

Component Version Needed for
Unity 6000.6.0f1 (install through Unity Hub) The sim
PX4-Autopilot v1.17.0, built for SITL Flight control
Python 3 with pymavlink tools/px4_flight_test.py
QGroundControl Optional: monitoring, parameters, missions
ROS 2 with Micro XRCE-DDS agent v2.4.x and ros_tcp_endpoint Optional: ROS 2 topics

The sim has been run on macOS (Apple silicon) only. PX4 SITL also builds on Ubuntu; Windows needs WSL2 for PX4. Neither has been tried with this project.

Unity project

Add file directly into Unity Hub: Add > Add from repository > Add project

Do not git clone the unity sim repository, since it would require you to delete it from disk.

PX4 SITL

Build once, with the same PX4 release as the flight controller.

git clone --recursive -b v1.17.0 https://github.com/PX4/PX4-Autopilot.git
cd PX4-Autopilot

macOS:

xcode-select --install
brew install cmake ninja
python3 -m venv .venv && .venv/bin/pip install -r Tools/setup/requirements.txt
source .venv/bin/activate
ulimit -S -n 2048
make px4_sitl

If the build fails with C++17 errors in Gazebo code, CMake has found Homebrew's protobuf. The sim does not use Gazebo, so disable it:

make px4_sitl CMAKE_ARGS="-DCMAKE_DISABLE_FIND_PACKAGE_Protobuf=TRUE"

Ubuntu:

bash ./Tools/setup/ubuntu.sh --no-sim-tools
make px4_sitl

Running

Start Unity first, then PX4. QGroundControl, ROS 2 and scripts can connect at any time.

  1. In Unity, open Assets/_Project/Scenes/Flight.unity and press Play. The vehicle is the Vehicle field on the VehicleLoader component of Managers, x500 by default. The overlay in the top left of the Game view reads PX4 SITL: waiting on TCP 4560.

    Vehicle What PX4_SIM_MODEL, stock airframe PX4_SIM_MODEL, generated airframe
    x500 PX4's X500 quadcopter none_iris unity_x500
    quadplane Quadplane standing in for the team's aircraft gazebo-classic_standard_vtol unity_quadplane

    The stock airframes work as they are. The generated ones match the sim's rotor geometry and have to be installed into the PX4 build first (PX4 airframe).

  2. Start PX4 from the PX4-Autopilot directory with the airframe that matches the vehicle:

    PX4_SIM_MODEL=none_iris ./build/px4_sitl_default/bin/px4 -d

    -d runs PX4 without its interactive shell; drop it to get the pxh> prompt.

  3. The overlay changes to PX4 SITL: connected lockstep=on. Wait for Ready for takeoff! in the PX4 output before arming.

Notes:

  • Run the px4 binary directly, as above. make px4_sitl gazebo-classic_standard_vtol would also launch Gazebo.
  • The gz_* airframes do not work: they talk to Gazebo over its own transport, not MAVLink.
  • If Unity runs on another machine, set PX4_SIM_HOSTNAME=<unity host> in PX4's environment.
  • Unity's Run In Background is enabled, so the sim keeps stepping while another window has focus.

Standalone build

A built player runs the sim without the Unity editor. Builds for Linux, Windows and macOS are attached to each release on GitHub, or can be built from the Actions tab (Build, Run workflow). Start the player instead of pressing Play, then start PX4 as above.

Options go after the executable name:

Option Default Effect
--vehicle NAME x500 Vehicle spec: x500, quadplane or a path to a .json file (Vehicles)
--px4-port PORT 4560 TCP port PX4 SITL connects to
--ros-ip IP 127.0.0.1 ROS TCP endpoint address
--ros-port PORT 10000 ROS TCP endpoint port
--no-ros Don't connect or publish to ROS
--config PATH StreamingAssets/sim_config.json Camera config file, see Camera config
./drone-sim.x86_64 --vehicle quadplane --ros-ip 192.168.1.20                       # Linux
drone-sim.app/Contents/MacOS/drone-unity-sim --vehicle quadplane                   # macOS

On macOS, clear the quarantine flag on a downloaded build first: xattr -cr drone-sim.app. The PX4_HOME_* variables (Home location) work the same way for a player. Unity's own player options also apply, for example -screen-fullscreen 0 -screen-width 1280 -screen-height 720 for a window.

Flying

QGroundControl

Open QGC. PX4 SITL sends MAVLink to UDP 14550 on localhost, where QGC listens by default. For QGC on another machine, set PX4 parameter MAV_0_BROADCAST to 1 or add a UDP link in QGC.

missions/vtol_flyaround.plan is a demo mission for the quadplane at the default home: VTOL takeoff to 50 m, a lap of about 1.2 km in fixed-wing flight, VTOL landing at home. In QGC: Plan view, File, Open, then Upload; then Start Mission in the Fly view.

PX4 writes flight logs (.ulg) to PX4-Autopilot/build/px4_sitl_default/rootfs/log/.

Flight test script

Flies the vehicle through PX4's MAVLink API port (UDP 14540), so it can run alongside QGC. From the project folder, with Unity playing and PX4 connected:

pip install pymavlink
python3 tools/px4_flight_test.py hover   # any vehicle: take off to 5 m, hover, land
python3 tools/px4_flight_test.py vtol    # quadplane: take off to 40 m, transition, 60 s fixed wing, transition back, land

The script prints PX4's state estimate once a second and ends with PASS, or with FAIL and a non-zero exit code naming the step that failed. --help lists the options.

Keyboard, without PX4

Press M in the Game view to toggle keyboard flying. Keys are listed in Development.

Home location

The default home is PX4's stock SITL location in Zurich (47.397742, 8.545594, 488 m).

PX4 derives its home from the GPS that Unity sends, so PX4_HOME_LAT, PX4_HOME_LON and PX4_HOME_ALT must be set in Unity's environment, not PX4's. Either:

  • Export them and launch the editor from the same terminal. An editor launched from the Dock or Unity Hub does not see them.

    export PX4_HOME_LAT=45.5048 PX4_HOME_LON=-73.5772 PX4_HOME_ALT=30   # Montreal
    /Applications/Unity/Hub/Editor/6000.6.0f1/Unity.app/Contents/MacOS/Unity -projectPath .   # from the project folder
  • Or edit the fields on the GeoOrigin component of the Managers object in the scene. This edit is lost when the scene is rebuilt (see Scenes).

The simulated magnetic field follows the home location automatically. drone-SW-26-27's check_lap uses Montreal waypoints and needs the Montreal home above.

ROS 2

Optional. The sim runs without it. Two separate bridges are involved:

  • PX4's topics (/fmu/...), for code that commands PX4, go through the Micro XRCE-DDS agent. Use agent v2.4.x; v3 does not work with PX4 v1.17's client.

    MicroXRCEAgent udp4 -p 8888
  • Unity's topics (cameras, ground truth, /clock) go through the ROS TCP endpoint. Unity connects to 127.0.0.1:10000; change the address on the ROSConnection component of Managers if the endpoint runs elsewhere.

    ros2 run ros_tcp_endpoint default_server_endpoint --ros-args -p ROS_IP:=0.0.0.0

On macOS, run Unity and PX4 natively and ROS 2 in a Linux container with UDP 8888 and TCP 10000 reachable from the host. Every port and topic is listed in Interfaces.

Troubleshooting

Symptom Cause and fix
PX4 stops after selected parameter backup file and never connects A PX4 startup command hung on PX4's local socket, or an old PX4 is still running. Kill everything and relaunch: pkill -9 -f px4_sitl_default/bin/px4; pkill -9 -f px4-; rm -f /tmp/px4_lock-0 /tmp/px4-sock-0
Preflight checks fail after switching vehicles Parameters reset when the airframe changes, but the stored mission does not. Stop PX4 and delete build/px4_sitl_default/rootfs/dataman
Unity console: [Vehicle] ... has problems, no vehicle built The spec failed its checks; the error lists each one. See Vehicles
PX4: ERROR [init] Unknown model unity_quadplane The generated airframe is not installed, or a PX4 rebuild removed it. Install it again (PX4 airframe)
Unity console: can't listen on TCP 4560 Another Unity instance or simulator holds the port
PX4 logs poll timeout Unity is not stepping: play mode is paused or stopped
Unity console: lockstep paused PX4 sent no controls for three consecutive 1 s timeouts. Unity steps on its own until controls arrive again. Usually PX4 was killed without closing the socket
QGC stays disconnected Check that PX4 is running and nothing else holds UDP 14550
drone-SW-26-27 checks lose the XRCE-DDS agent in Docker on macOS Docker Desktop's UDP forwarding breaks when the agent restarts, and each check starts and stops its own agent. Run the agent as the container's main process and pass --no-agent to each check

Clone this wiki locally