Repository navigation
Setup
| 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.
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.
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-AutopilotmacOS:
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_sitlIf 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_sitlStart Unity first, then PX4. QGroundControl, ROS 2 and scripts can connect at any time.
-
In Unity, open
Assets/_Project/Scenes/Flight.unityand press Play. The vehicle is theVehiclefield on theVehicleLoadercomponent ofManagers,x500by default. The overlay in the top left of the Game view readsPX4 SITL: waiting on TCP 4560.Vehicle What PX4_SIM_MODEL, stock airframePX4_SIM_MODEL, generated airframex500PX4's X500 quadcopter none_irisunity_x500quadplaneQuadplane standing in for the team's aircraft gazebo-classic_standard_vtolunity_quadplaneThe 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).
-
Start PX4 from the
PX4-Autopilotdirectory with the airframe that matches the vehicle:PX4_SIM_MODEL=none_iris ./build/px4_sitl_default/bin/px4 -d
-druns PX4 without its interactive shell; drop it to get thepxh>prompt. -
The overlay changes to
PX4 SITL: connected lockstep=on. Wait forReady for takeoff!in the PX4 output before arming.
Notes:
- Run the
px4binary directly, as above.make px4_sitl gazebo-classic_standard_vtolwould 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.
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 # macOSOn 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.
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/.
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, landThe 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.
Press M in the Game view to toggle keyboard flying. Keys are listed in
Development.
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
GeoOrigincomponent of theManagersobject 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.
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 to127.0.0.1:10000; change the address on theROSConnectioncomponent ofManagersif 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.
| 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 |
Published from docs/ in the repository. Edit the pages there through a pull request; edits made on the wiki are overwritten by the next publish.