urml-px4-runtime 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ # --- Python ---
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ dist/
9
+ develop-eggs/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+ MANIFEST
23
+
24
+ # Virtual envs
25
+ .venv/
26
+ venv/
27
+ env/
28
+ ENV/
29
+ .python-version
30
+
31
+ # Test / coverage / mypy / pyright
32
+ .coverage
33
+ .coverage.*
34
+ .cache
35
+ .pytest_cache/
36
+ .mypy_cache/
37
+ .dmypy.json
38
+ .pyre/
39
+ .pyright/
40
+ htmlcov/
41
+ nosetests.xml
42
+ coverage.xml
43
+ *.cover
44
+ .hypothesis/
45
+ .tox/
46
+
47
+ # --- Node / web tooling (kept out of core repo; here for safety) ---
48
+ node_modules/
49
+ .pnp.*
50
+ .yarn/cache/
51
+ .yarn/unplugged/
52
+ .yarn/build-state.yml
53
+ .yarn/install-state.gz
54
+ npm-debug.log*
55
+ yarn-debug.log*
56
+ yarn-error.log*
57
+
58
+ # --- C++ / Rust build artifacts (in case reference runtimes land here in early Phase 1) ---
59
+ target/
60
+ *.o
61
+ *.obj
62
+ *.a
63
+ *.lib
64
+ *.dll
65
+ *.dylib
66
+ *.exe
67
+ *.pdb
68
+ cmake-build-*/
69
+ CMakeCache.txt
70
+ CMakeFiles/
71
+ CMakeScripts/
72
+ Testing/
73
+ cmake_install.cmake
74
+ install_manifest.txt
75
+ compile_commands.json
76
+
77
+ # --- ROS 2 build dirs ---
78
+ build/
79
+ install/
80
+ log/
81
+
82
+ # --- Editor and OS junk ---
83
+ .vscode/
84
+ .idea/
85
+ *.swp
86
+ *.swo
87
+ *~
88
+ .DS_Store
89
+ Thumbs.db
90
+ desktop.ini
91
+
92
+ # --- Local-only artifacts ---
93
+ .env
94
+ .env.*
95
+ *.log
96
+ *.tmp
97
+ .local/
98
+ .scratch/
99
+
100
+ # --- Outreach campaign artifacts (operational, not part of the open standard) ---
101
+ # Target lists, message drafts, tracking data, and the launch-approval record
102
+ # stay local. They are private to the maintainer; committing them would
103
+ # telegraph the campaign before launch and could embarrass listed projects.
104
+ outreach/
105
+
106
+ # Outreach dashboard SQLite mirror (RFC-0275). Derived from
107
+ # examples/lighthouses/outreach*.yaml via tools/scripts/refresh_outreach_db.py.
108
+ # Regenerated on demand by `make outreach-refresh`.
109
+ tools/outreach.db
110
+ tools/.outreach.db
111
+
112
+ # Local Claude Code settings (per-machine; permission rules, etc.). Not committed.
113
+ .claude/settings.json
114
+ .claude/settings.local.json
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.5
2
+ Name: urml-px4-runtime
3
+ Version: 0.5.0
4
+ Summary: PX4 / MAVLink reference runtime for URML — Universal Robot Language.
5
+ Project-URL: Homepage, https://github.com/urml-dev/URML
6
+ Project-URL: Repository, https://github.com/urml-dev/URML
7
+ Project-URL: Issues, https://github.com/urml-dev/URML/issues
8
+ Author: URML Maintainers
9
+ License: BUSL-1.1
10
+ Keywords: drone,mavlink,px4,robotics,runtime,urml
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: Other/Proprietary License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Scientific/Engineering
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: pydantic<3,>=2.6
21
+ Requires-Dist: pyyaml<7,>=6.0
22
+ Requires-Dist: urml-ros2-runtime>=0.5.0
23
+ Requires-Dist: urml-validator>=0.5.0
24
+ Provides-Extra: dev
25
+ Requires-Dist: mypy>=1.10; extra == 'dev'
26
+ Requires-Dist: pytest-cov>=5; extra == 'dev'
27
+ Requires-Dist: pytest>=8; extra == 'dev'
28
+ Requires-Dist: ruff>=0.5; extra == 'dev'
29
+ Provides-Extra: px4
30
+ Requires-Dist: pymavlink<3,>=2.4; extra == 'px4'
31
+ Description-Content-Type: text/markdown
32
+
33
+ <p align="center">
34
+ <a href="https://urml.dev"><img src="https://urml.dev/favicon.svg" alt="URML" width="72" height="72"></a>
35
+ </p>
36
+
37
+ <p align="center">
38
+ A small, opinionated, human-readable language for describing robot intent.
39
+ </p>
40
+
41
+ <p align="center">
42
+ <a href="https://urml.dev"><b>urml.dev</b></a>
43
+ </p>
44
+
45
+ ---
46
+
47
+ # urml-px4-runtime
48
+
49
+ **PX4 / MAVLink reference runtime for URML** — the second reference substrate, after [urml-ros2-runtime](../ros2-runtime/). Proves URML's substrate-neutrality concretely: this adapter has **no ROS 2 dependency**. It talks MAVLink directly via [pymavlink](https://github.com/ArduPilot/pymavlink) to a PX4 autopilot (real hardware, or PX4 SITL simulator).
50
+
51
+ A single reference runtime — no matter how clean — risks the spec accidentally encoding substrate assumptions. A second runtime on an entirely different stack (no rclpy, no Nav2, no ROS topics, just MAVLink frames over UDP/serial) keeps the spec honest. The two reference runtimes share the same `ROSAdapter` Protocol (the "ROS" in the name is vestigial — the Protocol is substrate-neutral) and pass the same conformance fixtures via the `ConformanceRunner.adapter_factory` hook.
52
+
53
+ ## Method coverage
54
+
55
+ Full `ROSAdapter` Protocol (12 core methods + 3 drone-profile methods), wired against MAVLink commands. A flight primitive succeeds only when PX4's telemetry shows it happened; a `COMMAND_ACK` alone is never reported as success (see [Flight completion](#flight-completion)).
56
+
57
+ | URML primitive | MAVLink mapping | Done when |
58
+ |---|---|---|
59
+ | `take_off` | `MAV_CMD_COMPONENT_ARM_DISARM` if disarmed, then `MAV_CMD_NAV_TAKEOFF` (altitude AMSL) | armed bit on `HEARTBEAT`, then `GLOBAL_POSITION_INT.relative_alt` within tolerance of the target |
60
+ | `move_to` | `MAV_CMD_DO_REPOSITION` as `COMMAND_INT` (lat / lon in degE7) | inside `arrival_radius_m` and `arrival_alt_tolerance_m` of the target |
61
+ | `hover` over a place | same as `move_to`; PX4 then holds in Hold mode | as `move_to` |
62
+ | `return_to_home` | `MAV_CMD_NAV_RETURN_TO_LAUNCH` | inside `arrival_radius_m` of home, in the air or landed |
63
+ | `land` | `MAV_CMD_NAV_LAND` (none if already on the ground) | `EXTENDED_SYS_STATE.landed_state` is ON_GROUND |
64
+ | `wait` | timed sleep (PX4 holds position in Hold mode) | |
65
+ | `wait_for` `emergency_stop` | the autopilot's `HEARTBEAT.system_status` | `MAV_STATE_FLIGHT_TERMINATION` or `MAV_STATE_EMERGENCY` from the autopilot itself; otherwise a timeout |
66
+ | `wait_for` battery `sensor_threshold` | `BATTERY_STATUS` | a sample that meets the threshold; samples that do not are skipped until the timeout |
67
+ | `measure` | sensor telemetry stream (`DISTANCE_SENSOR`, `BATTERY_STATUS`, etc.) | |
68
+ | `report` | `STATUSTEXT` MAVLink message | |
69
+
70
+ The not-applicable primitives (`dock`, `grasp`, `release`, `detect`, `scan`, `capture`, `speak`, `listen`) return a result with `success=False` and `reason="not_supported_on_bare_autopilot: ..."` rather than raising. A scan is waypoints plus a capture at each one, and a bare autopilot has no camera to capture with, so `scan` does not report success. Real drone deployments pair a PX4 autopilot with a ROS 2 companion computer for perception / manipulation / speech; in those stacks, dispatch through `CompositeAdapter` (below) instead of `PX4Adapter` alone.
71
+
72
+ PX4 v1.17 reports `MAV_STATE_FLIGHT_TERMINATION` for a terminated flight, an engaged kill switch, and the motor lockdown that holds a throw launch (`COM_THROW_EN`, off by default). It does not send `MAV_STATE_EMERGENCY`; the adapter accepts it for autopilots that do. A heartbeat from a ground station or another vehicle never counts.
73
+
74
+ ## Flight completion
75
+
76
+ Each flight primitive sends its command, then reads telemetry until the action is complete or a timeout expires. A refusal or a timeout is returned as `success=False` with a reason; nothing is raised.
77
+
78
+ - **take_off** arms the vehicle if it is disarmed and confirms the armed bit, re-reads home (PX4 moves home to the vehicle when it arms), and sends `MAV_CMD_NAV_TAKEOFF` with param 7 = home altitude + the requested height, because PX4 reads that parameter as AMSL. Latitude, longitude and yaw are sent as NaN, which PX4 reads as "here, current heading". The step succeeds when `relative_alt` is within `arrival_alt_tolerance_m` of the target (capped at half the target, so a low take-off still has to leave the ground). If PX4 refuses the take-off after this adapter armed it, the adapter disarms it again. `climb_rate` has no MAVLink take-off parameter; PX4 climbs at its `MPC_TKO_SPEED`.
79
+ - **move_to** resolves a location through `location_to_pose` (or takes `pose` x = north, y = east, z = metres above home), converts that offset from home to WGS84 with the projection PX4 uses, and sends `MAV_CMD_DO_REPOSITION` as `COMMAND_INT` with the change-mode flag PX4 requires. A pose without `z` keeps the current altitude. PX4 executes a reposition only while armed, so a disarmed vehicle fails at once with `not_airborne`.
80
+ - **return_to_home** succeeds when the vehicle is within `arrival_radius_m` of home. PX4's own RTL then descends and lands. On the ground at home it reports `already_at_home` without commanding anything.
81
+ - **land** reports `already_on_ground` (success, no command) when PX4 already says ON_GROUND, for example after an RTL that landed by itself. `land(at=...)` flies to that location at the current altitude first. `precision` is not mapped; PX4 lands the way it is configured to.
82
+
83
+ Refusal and timeout reasons carry PX4's `MAV_RESULT` or what telemetry last showed, followed by the warnings PX4 sent about it as `STATUSTEXT` (severity WARNING or worse; PX4's INFO lines such as "Takeoff detected" narrate and stay out). For example, a take-off requested before PX4 v1.17 SITL had passed its pre-arm checks returned:
84
+
85
+ ```
86
+ arm_rejected: mav_result_temporarily_rejected; PX4 said: "Arming denied: Resolve system health failures first"
87
+ ```
88
+
89
+ A reason quotes only what PX4 sent from the command on. After a refusal the adapter listens for up to a second, because PX4 can send its explanation just after the `COMMAND_ACK`, and it joins texts PX4 splits into 50-character chunks. PX4 names a failing pre-arm check ("Preflight Fail: ...") when the check starts failing, which can be before the arm command; the adapter reads that text but leaves it out of the reason, because it cannot tell whether an older warning still holds.
90
+
91
+ The adapter talks to PX4 only: it refuses an autopilot whose heartbeat is not `MAV_AUTOPILOT_PX4`, because ArduPilot reads the same take-off parameter as a relative altitude. Use `ArduCopterAdapter` for ArduPilot.
92
+
93
+ ### Ground-station heartbeat and PX4's data-link-loss failsafe
94
+
95
+ PX4 sends `STATUSTEXT` only on links where it sees a ground-station heartbeat. So from the moment it connects until `close()`, the adapter sends a `MAV_TYPE_GCS` heartbeat once a second from a daemon thread. Every send on the connection (the heartbeat, commands, `report`) takes one lock, because pymavlink sends are not thread-safe. `close()` stops the thread before it closes the connection, and a closed adapter does not reconnect. An adapter that is garbage-collected without `close()` stops its heartbeat too.
96
+
97
+ This makes URML a ground station in PX4's eyes, with a consequence to plan for. If URML stops mid-flight (the program ends, `close()` runs, the process dies, the link drops), the heartbeat stops, and after `COM_DL_LOSS_T` seconds (10 by default) PX4 logs "Connection to ground station lost" and applies its data-link-loss action, `NAV_DLL_ACT`:
98
+
99
+ - PX4 ships with `NAV_DLL_ACT = 0`: no action. The vehicle carries on with what it was doing, for example holding position.
100
+ - An operator who wants the vehicle to return or land when its controller dies sets `NAV_DLL_ACT` (2 = Return, 3 = Land). That is the intended behavior, not a side effect to suppress.
101
+ - `COM_DLL_EXCEPT` exempts modes from the action. Its Hold bit (1) also covers take-off, and `take_off` and `move_to` leave the vehicle in Hold, so leave that bit clear if you want the action while URML flies.
102
+ - With `NAV_DLL_ACT` set, PX4 also refuses to arm while no ground station is connected. The adapter's heartbeat counts as one.
103
+
104
+ ### Flight settings
105
+
106
+ These live in `px4_adapter.yaml` next to `connection_url` and `location_to_pose`. Every value must be positive.
107
+
108
+ | Key | Default | Meaning |
109
+ |---|---|---|
110
+ | `arm_timeout_seconds` | 10 | Wait for the armed bit after PX4 accepts the arm command. |
111
+ | `takeoff_timeout_seconds` | 120 | Climb time allowed. PX4 climbs at 1.5 m/s by default, so this covers a 120 m take-off. |
112
+ | `arrival_radius_m` | 1.5 | Horizontal distance that counts as arrived (move_to, hover, return_to_home). |
113
+ | `arrival_alt_tolerance_m` | 1.0 | Altitude difference that counts as arrived (take_off, move_to). |
114
+ | `arrival_timeout_seconds` | 120 | Time allowed to reach a target or home. Raise it for long legs; PX4 cruises at 5 m/s by default. |
115
+ | `land_timeout_seconds` | 180 | Time allowed from the land command to ON_GROUND. |
116
+ | `ack_timeout_seconds` | 5 | Wait for a `COMMAND_ACK`. |
117
+ | `heartbeat_timeout_seconds` | 5 | Wait for PX4's heartbeat when connecting. |
118
+ | `message_timeout_seconds` | 5 | Wait for a telemetry message the adapter needs (home, position, landed state). |
119
+
120
+ `relative_alt` comes from PX4 and moves with PX4's home altitude. PX4 v1.17 corrects home altitude against GNSS in flight; in SIH simulation that made `relative_alt` wander by about a metre around a steady hover. The arrival checks need one telemetry sample inside the band, which was enough in the SITL runs so far, and this wander is why the altitude band is not tighter.
121
+
122
+ ## CompositeAdapter — PX4 flight + ROS 2 companion
123
+
124
+ `CompositeAdapter` implements the full `ROSAdapter` Protocol by holding two backing adapters and routing each method to whichever one owns that capability. The URML program, manifest, and validator are unchanged and unaware — the split lives entirely at the deployment boundary.
125
+
126
+ ```python
127
+ from urml_px4_runtime import CompositeAdapter, PX4Adapter, PX4AdapterConfig
128
+ from urml_ros2_runtime.substrate.rclpy_adapter import RclpyAdapter
129
+ from urml_ros2_runtime.substrate.adapter_config import load_adapter_config
130
+
131
+ flight = PX4Adapter(PX4AdapterConfig(connection_url="udp:127.0.0.1:14540"))
132
+ companion = RclpyAdapter(load_adapter_config("adapter.yaml"))
133
+
134
+ with CompositeAdapter(flight=flight, companion=companion) as adapter:
135
+ # take_off/land/RTH/move_to -> PX4 (MAVLink); detect/grasp/capture/
136
+ # speak -> ROS 2 companion. One URML program, two substrates.
137
+ runtime = URMLRuntime(adapter)
138
+ runtime.execute(program, manifest, envelope, profiles=("drone",))
139
+ ```
140
+
141
+ Default routing (the drone-stack policy) sends flight primitives (`take_off`, `land`, `return_to_home`, `move_to`/`hover`, `wait`) to the flight adapter and perception/manipulation/speech (`detect`, `grasp`, `release`, `scan`, `measure`, `capture`, `report`, `speak`, `listen`, `wait_for`) to the companion. It is explicit and overridable per method:
142
+
143
+ ```python
144
+ # This airframe reads its rangefinder over MAVLink, not the companion:
145
+ CompositeAdapter(flight=flight, companion=companion,
146
+ routing={"take_measurement": "flight"})
147
+ ```
148
+
149
+ Unknown method names or backend values other than `flight`/`companion` are rejected at construction, so a routing typo can't silently send a goal to the wrong box. `CompositeAdapter` imports neither pymavlink nor rclpy — it depends only on the substrate-neutral Protocol, loads on every host, and is hermetically unit-tested against two mock backends (24 tests).
150
+
151
+ ## Install
152
+
153
+ ```bash
154
+ pip install -e reference/px4-runtime[px4]
155
+ ```
156
+
157
+ The `[px4]` extra installs `pymavlink` — a normal PyPI package, unlike `rclpy` which ships with the ROS 2 distribution.
158
+
159
+ ## Use
160
+
161
+ ```python
162
+ from urml_px4_runtime import PX4Adapter, PX4AdapterConfig
163
+
164
+ config = PX4AdapterConfig(
165
+ connection_url="udp:127.0.0.1:14540", # PX4 SITL's offboard (API) port
166
+ )
167
+ with PX4Adapter(config) as adapter:
168
+ result = adapter.send_takeoff_goal(altitude=30.0) # arms, climbs, returns at 30 m
169
+ print(result.success, result.reason)
170
+ ```
171
+
172
+ Drop the adapter into the URML runtime:
173
+
174
+ ```python
175
+ from urml_ros2_runtime import URMLRuntime
176
+ runtime = URMLRuntime(adapter)
177
+ runtime.execute(program, manifest, envelope, profiles=("drone",))
178
+ ```
179
+
180
+ Or through the conformance suite:
181
+
182
+ ```python
183
+ from urml_conformance import ConformanceRunner
184
+ runner = ConformanceRunner(adapter_factory=lambda: PX4Adapter(config))
185
+ report = runner.run()
186
+ ```
187
+
188
+ ## Status
189
+
190
+ **v0.1 (this release):**
191
+ - Adapter loads on every host (lazy pymavlink import; clear actionable error if `pymavlink` is missing).
192
+ - Unit tests with a scripted fake PX4 cover all 15 Protocol methods (including the not-applicable ones). The fake arms, climbs, repositions and lands on a simulated clock, so each flight primitive's success and failure paths (arm refused, take-off timeout, reposition never arriving, landing timeout) are tested against vehicle state, not against acks. Like PX4, the fake sends STATUSTEXT only after it has seen a ground-station heartbeat, so a test reason that quotes PX4 shows the adapter's heartbeat reached it; other tests check the heartbeat thread's period, that it stops on `close()`, and that every send holds the send lock.
193
+ - Live PX4 SITL e2e test flies the `drone/flight_only_positive` conformance fixture through `ConformanceRunner` with a real `PX4Adapter` (`tests/integration/test_px4_sitl_e2e.py`, gated by `URML_PX4_SITL=1`). A listen-only witness on PX4's ground-station port checks that the vehicle armed, climbed to at least 90 percent of the take-off altitude, came within twice the acceptance radius of the waypoint, and ended on the ground.
194
+
195
+ **Verified in simulation (2026-09-27):** the e2e test passed locally in WSL2 against PX4 v1.17.0 SITL (SIH quadrotor), and PX4's own log shows the arm, the take-off, the return to launch, the landing and the disarm; the witness measured a highest relative altitude of 31.79 m for the 30 m take-off and a closest approach of 0.92 m to the waypoint. The record is [`tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih.md`](tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih.md). An earlier version of the same test also passed, in 2.37 s, while the adapter counted acks as success and the simulated vehicle never armed; that result does not count as a flight. The CI job has not run.
196
+
197
+ **With the ground-station heartbeat (2026-09-27):** the same test passed again with the heartbeat running (a highest relative altitude of 31.778 m, a closest approach of 0.95 m), PX4 sent its STATUSTEXT to the adapter's link during the flight, and PX4 logged "Connection to ground station lost" after the adapter closed. A no-flight probe got `arm_rejected: mav_result_temporarily_rejected; PX4 said: "Arming denied: Resolve system health failures first"` from a take-off requested before PX4 was ready, saw `wait_for(emergency_stop)` time out on a healthy vehicle, and saw it fire on `MAV_STATE_FLIGHT_TERMINATION` after a flight termination commanded on the ground. The record is [`tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih-heartbeat.md`](tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih-heartbeat.md).
198
+
199
+ **Landed since v0.1:**
200
+ - `CompositeAdapter` for stacks that pair PX4 with a ROS 2 companion (see above).
201
+ - Geofence polygon-containment, 3D altitude bands, and people-occupancy zones in the safety-envelope pass (validator Pass 3).
202
+ - Gated `.github/workflows/px4-integration.yml` with three jobs: `px4-smoke` (real pymavlink, no SITL — the `rclpy-smoke` analog), `px4-sitl-e2e` (the flight e2e — the `gazebo-e2e` analog), and `px4-arm64-build` (the hermetic suite under linux/arm64 QEMU emulation — the pre-hardware Jetson-class signal).
203
+
204
+ **Follow-ups (not yet):**
205
+ - First run of `px4-sitl-e2e` on a Linux runner. The job follows the local recipe (PX4 v1.17.0 cloned shallow and recursive, PX4's Python requirements in a venv, the build-only `make px4_sitl_default`, then `sleep infinity | HEADLESS=1 make px4_sitl sihsim_quadx`), but it has not run yet.
206
+ - Real Jetson + real-robot hardware-in-the-loop. QEMU emulation is a faithful proxy for our pure-Python + pymavlink code, not a hardware-verification claim.
207
+ - A fly-and-capture `scan` (waypoint expansion and a capture at each waypoint) for an autopilot with a camera. Until then `scan` returns not-supported on a bare autopilot, and `CompositeAdapter` routes it to the companion.
208
+
209
+ ## Core Commitment
210
+
211
+ This runtime is licensed under the Business Source License 1.1: free for non-production use, commercial license for production (see [`LICENSING.md`](../../LICENSING.md)). No vendor coupling, no cloud dependency.
212
+
213
+ ## Related documents
214
+
215
+ - [`/spec/profiles/drone/`](../../spec/profiles/drone/) — the profile this runtime targets.
216
+ - [`/spec/layer-1-hal/`](../../spec/layer-1-hal/) — manifest schema, including drone-specific fields.
217
+ - [`/conformance/`](../../conformance/) — the test suite that decides conformance.
218
+ - [`/reference/ros2-runtime/INTEGRATION.md`](../ros2-runtime/INTEGRATION.md) — the ROS 2 runtime's parallel design notes.
219
+ - [`MANIFESTO.md`](../../MANIFESTO.md) §Motivating Scenarios — *Drone: the citizen inspector*.
@@ -0,0 +1,187 @@
1
+ <p align="center">
2
+ <a href="https://urml.dev"><img src="https://urml.dev/favicon.svg" alt="URML" width="72" height="72"></a>
3
+ </p>
4
+
5
+ <p align="center">
6
+ A small, opinionated, human-readable language for describing robot intent.
7
+ </p>
8
+
9
+ <p align="center">
10
+ <a href="https://urml.dev"><b>urml.dev</b></a>
11
+ </p>
12
+
13
+ ---
14
+
15
+ # urml-px4-runtime
16
+
17
+ **PX4 / MAVLink reference runtime for URML** — the second reference substrate, after [urml-ros2-runtime](../ros2-runtime/). Proves URML's substrate-neutrality concretely: this adapter has **no ROS 2 dependency**. It talks MAVLink directly via [pymavlink](https://github.com/ArduPilot/pymavlink) to a PX4 autopilot (real hardware, or PX4 SITL simulator).
18
+
19
+ A single reference runtime — no matter how clean — risks the spec accidentally encoding substrate assumptions. A second runtime on an entirely different stack (no rclpy, no Nav2, no ROS topics, just MAVLink frames over UDP/serial) keeps the spec honest. The two reference runtimes share the same `ROSAdapter` Protocol (the "ROS" in the name is vestigial — the Protocol is substrate-neutral) and pass the same conformance fixtures via the `ConformanceRunner.adapter_factory` hook.
20
+
21
+ ## Method coverage
22
+
23
+ Full `ROSAdapter` Protocol (12 core methods + 3 drone-profile methods), wired against MAVLink commands. A flight primitive succeeds only when PX4's telemetry shows it happened; a `COMMAND_ACK` alone is never reported as success (see [Flight completion](#flight-completion)).
24
+
25
+ | URML primitive | MAVLink mapping | Done when |
26
+ |---|---|---|
27
+ | `take_off` | `MAV_CMD_COMPONENT_ARM_DISARM` if disarmed, then `MAV_CMD_NAV_TAKEOFF` (altitude AMSL) | armed bit on `HEARTBEAT`, then `GLOBAL_POSITION_INT.relative_alt` within tolerance of the target |
28
+ | `move_to` | `MAV_CMD_DO_REPOSITION` as `COMMAND_INT` (lat / lon in degE7) | inside `arrival_radius_m` and `arrival_alt_tolerance_m` of the target |
29
+ | `hover` over a place | same as `move_to`; PX4 then holds in Hold mode | as `move_to` |
30
+ | `return_to_home` | `MAV_CMD_NAV_RETURN_TO_LAUNCH` | inside `arrival_radius_m` of home, in the air or landed |
31
+ | `land` | `MAV_CMD_NAV_LAND` (none if already on the ground) | `EXTENDED_SYS_STATE.landed_state` is ON_GROUND |
32
+ | `wait` | timed sleep (PX4 holds position in Hold mode) | |
33
+ | `wait_for` `emergency_stop` | the autopilot's `HEARTBEAT.system_status` | `MAV_STATE_FLIGHT_TERMINATION` or `MAV_STATE_EMERGENCY` from the autopilot itself; otherwise a timeout |
34
+ | `wait_for` battery `sensor_threshold` | `BATTERY_STATUS` | a sample that meets the threshold; samples that do not are skipped until the timeout |
35
+ | `measure` | sensor telemetry stream (`DISTANCE_SENSOR`, `BATTERY_STATUS`, etc.) | |
36
+ | `report` | `STATUSTEXT` MAVLink message | |
37
+
38
+ The not-applicable primitives (`dock`, `grasp`, `release`, `detect`, `scan`, `capture`, `speak`, `listen`) return a result with `success=False` and `reason="not_supported_on_bare_autopilot: ..."` rather than raising. A scan is waypoints plus a capture at each one, and a bare autopilot has no camera to capture with, so `scan` does not report success. Real drone deployments pair a PX4 autopilot with a ROS 2 companion computer for perception / manipulation / speech; in those stacks, dispatch through `CompositeAdapter` (below) instead of `PX4Adapter` alone.
39
+
40
+ PX4 v1.17 reports `MAV_STATE_FLIGHT_TERMINATION` for a terminated flight, an engaged kill switch, and the motor lockdown that holds a throw launch (`COM_THROW_EN`, off by default). It does not send `MAV_STATE_EMERGENCY`; the adapter accepts it for autopilots that do. A heartbeat from a ground station or another vehicle never counts.
41
+
42
+ ## Flight completion
43
+
44
+ Each flight primitive sends its command, then reads telemetry until the action is complete or a timeout expires. A refusal or a timeout is returned as `success=False` with a reason; nothing is raised.
45
+
46
+ - **take_off** arms the vehicle if it is disarmed and confirms the armed bit, re-reads home (PX4 moves home to the vehicle when it arms), and sends `MAV_CMD_NAV_TAKEOFF` with param 7 = home altitude + the requested height, because PX4 reads that parameter as AMSL. Latitude, longitude and yaw are sent as NaN, which PX4 reads as "here, current heading". The step succeeds when `relative_alt` is within `arrival_alt_tolerance_m` of the target (capped at half the target, so a low take-off still has to leave the ground). If PX4 refuses the take-off after this adapter armed it, the adapter disarms it again. `climb_rate` has no MAVLink take-off parameter; PX4 climbs at its `MPC_TKO_SPEED`.
47
+ - **move_to** resolves a location through `location_to_pose` (or takes `pose` x = north, y = east, z = metres above home), converts that offset from home to WGS84 with the projection PX4 uses, and sends `MAV_CMD_DO_REPOSITION` as `COMMAND_INT` with the change-mode flag PX4 requires. A pose without `z` keeps the current altitude. PX4 executes a reposition only while armed, so a disarmed vehicle fails at once with `not_airborne`.
48
+ - **return_to_home** succeeds when the vehicle is within `arrival_radius_m` of home. PX4's own RTL then descends and lands. On the ground at home it reports `already_at_home` without commanding anything.
49
+ - **land** reports `already_on_ground` (success, no command) when PX4 already says ON_GROUND, for example after an RTL that landed by itself. `land(at=...)` flies to that location at the current altitude first. `precision` is not mapped; PX4 lands the way it is configured to.
50
+
51
+ Refusal and timeout reasons carry PX4's `MAV_RESULT` or what telemetry last showed, followed by the warnings PX4 sent about it as `STATUSTEXT` (severity WARNING or worse; PX4's INFO lines such as "Takeoff detected" narrate and stay out). For example, a take-off requested before PX4 v1.17 SITL had passed its pre-arm checks returned:
52
+
53
+ ```
54
+ arm_rejected: mav_result_temporarily_rejected; PX4 said: "Arming denied: Resolve system health failures first"
55
+ ```
56
+
57
+ A reason quotes only what PX4 sent from the command on. After a refusal the adapter listens for up to a second, because PX4 can send its explanation just after the `COMMAND_ACK`, and it joins texts PX4 splits into 50-character chunks. PX4 names a failing pre-arm check ("Preflight Fail: ...") when the check starts failing, which can be before the arm command; the adapter reads that text but leaves it out of the reason, because it cannot tell whether an older warning still holds.
58
+
59
+ The adapter talks to PX4 only: it refuses an autopilot whose heartbeat is not `MAV_AUTOPILOT_PX4`, because ArduPilot reads the same take-off parameter as a relative altitude. Use `ArduCopterAdapter` for ArduPilot.
60
+
61
+ ### Ground-station heartbeat and PX4's data-link-loss failsafe
62
+
63
+ PX4 sends `STATUSTEXT` only on links where it sees a ground-station heartbeat. So from the moment it connects until `close()`, the adapter sends a `MAV_TYPE_GCS` heartbeat once a second from a daemon thread. Every send on the connection (the heartbeat, commands, `report`) takes one lock, because pymavlink sends are not thread-safe. `close()` stops the thread before it closes the connection, and a closed adapter does not reconnect. An adapter that is garbage-collected without `close()` stops its heartbeat too.
64
+
65
+ This makes URML a ground station in PX4's eyes, with a consequence to plan for. If URML stops mid-flight (the program ends, `close()` runs, the process dies, the link drops), the heartbeat stops, and after `COM_DL_LOSS_T` seconds (10 by default) PX4 logs "Connection to ground station lost" and applies its data-link-loss action, `NAV_DLL_ACT`:
66
+
67
+ - PX4 ships with `NAV_DLL_ACT = 0`: no action. The vehicle carries on with what it was doing, for example holding position.
68
+ - An operator who wants the vehicle to return or land when its controller dies sets `NAV_DLL_ACT` (2 = Return, 3 = Land). That is the intended behavior, not a side effect to suppress.
69
+ - `COM_DLL_EXCEPT` exempts modes from the action. Its Hold bit (1) also covers take-off, and `take_off` and `move_to` leave the vehicle in Hold, so leave that bit clear if you want the action while URML flies.
70
+ - With `NAV_DLL_ACT` set, PX4 also refuses to arm while no ground station is connected. The adapter's heartbeat counts as one.
71
+
72
+ ### Flight settings
73
+
74
+ These live in `px4_adapter.yaml` next to `connection_url` and `location_to_pose`. Every value must be positive.
75
+
76
+ | Key | Default | Meaning |
77
+ |---|---|---|
78
+ | `arm_timeout_seconds` | 10 | Wait for the armed bit after PX4 accepts the arm command. |
79
+ | `takeoff_timeout_seconds` | 120 | Climb time allowed. PX4 climbs at 1.5 m/s by default, so this covers a 120 m take-off. |
80
+ | `arrival_radius_m` | 1.5 | Horizontal distance that counts as arrived (move_to, hover, return_to_home). |
81
+ | `arrival_alt_tolerance_m` | 1.0 | Altitude difference that counts as arrived (take_off, move_to). |
82
+ | `arrival_timeout_seconds` | 120 | Time allowed to reach a target or home. Raise it for long legs; PX4 cruises at 5 m/s by default. |
83
+ | `land_timeout_seconds` | 180 | Time allowed from the land command to ON_GROUND. |
84
+ | `ack_timeout_seconds` | 5 | Wait for a `COMMAND_ACK`. |
85
+ | `heartbeat_timeout_seconds` | 5 | Wait for PX4's heartbeat when connecting. |
86
+ | `message_timeout_seconds` | 5 | Wait for a telemetry message the adapter needs (home, position, landed state). |
87
+
88
+ `relative_alt` comes from PX4 and moves with PX4's home altitude. PX4 v1.17 corrects home altitude against GNSS in flight; in SIH simulation that made `relative_alt` wander by about a metre around a steady hover. The arrival checks need one telemetry sample inside the band, which was enough in the SITL runs so far, and this wander is why the altitude band is not tighter.
89
+
90
+ ## CompositeAdapter — PX4 flight + ROS 2 companion
91
+
92
+ `CompositeAdapter` implements the full `ROSAdapter` Protocol by holding two backing adapters and routing each method to whichever one owns that capability. The URML program, manifest, and validator are unchanged and unaware — the split lives entirely at the deployment boundary.
93
+
94
+ ```python
95
+ from urml_px4_runtime import CompositeAdapter, PX4Adapter, PX4AdapterConfig
96
+ from urml_ros2_runtime.substrate.rclpy_adapter import RclpyAdapter
97
+ from urml_ros2_runtime.substrate.adapter_config import load_adapter_config
98
+
99
+ flight = PX4Adapter(PX4AdapterConfig(connection_url="udp:127.0.0.1:14540"))
100
+ companion = RclpyAdapter(load_adapter_config("adapter.yaml"))
101
+
102
+ with CompositeAdapter(flight=flight, companion=companion) as adapter:
103
+ # take_off/land/RTH/move_to -> PX4 (MAVLink); detect/grasp/capture/
104
+ # speak -> ROS 2 companion. One URML program, two substrates.
105
+ runtime = URMLRuntime(adapter)
106
+ runtime.execute(program, manifest, envelope, profiles=("drone",))
107
+ ```
108
+
109
+ Default routing (the drone-stack policy) sends flight primitives (`take_off`, `land`, `return_to_home`, `move_to`/`hover`, `wait`) to the flight adapter and perception/manipulation/speech (`detect`, `grasp`, `release`, `scan`, `measure`, `capture`, `report`, `speak`, `listen`, `wait_for`) to the companion. It is explicit and overridable per method:
110
+
111
+ ```python
112
+ # This airframe reads its rangefinder over MAVLink, not the companion:
113
+ CompositeAdapter(flight=flight, companion=companion,
114
+ routing={"take_measurement": "flight"})
115
+ ```
116
+
117
+ Unknown method names or backend values other than `flight`/`companion` are rejected at construction, so a routing typo can't silently send a goal to the wrong box. `CompositeAdapter` imports neither pymavlink nor rclpy — it depends only on the substrate-neutral Protocol, loads on every host, and is hermetically unit-tested against two mock backends (24 tests).
118
+
119
+ ## Install
120
+
121
+ ```bash
122
+ pip install -e reference/px4-runtime[px4]
123
+ ```
124
+
125
+ The `[px4]` extra installs `pymavlink` — a normal PyPI package, unlike `rclpy` which ships with the ROS 2 distribution.
126
+
127
+ ## Use
128
+
129
+ ```python
130
+ from urml_px4_runtime import PX4Adapter, PX4AdapterConfig
131
+
132
+ config = PX4AdapterConfig(
133
+ connection_url="udp:127.0.0.1:14540", # PX4 SITL's offboard (API) port
134
+ )
135
+ with PX4Adapter(config) as adapter:
136
+ result = adapter.send_takeoff_goal(altitude=30.0) # arms, climbs, returns at 30 m
137
+ print(result.success, result.reason)
138
+ ```
139
+
140
+ Drop the adapter into the URML runtime:
141
+
142
+ ```python
143
+ from urml_ros2_runtime import URMLRuntime
144
+ runtime = URMLRuntime(adapter)
145
+ runtime.execute(program, manifest, envelope, profiles=("drone",))
146
+ ```
147
+
148
+ Or through the conformance suite:
149
+
150
+ ```python
151
+ from urml_conformance import ConformanceRunner
152
+ runner = ConformanceRunner(adapter_factory=lambda: PX4Adapter(config))
153
+ report = runner.run()
154
+ ```
155
+
156
+ ## Status
157
+
158
+ **v0.1 (this release):**
159
+ - Adapter loads on every host (lazy pymavlink import; clear actionable error if `pymavlink` is missing).
160
+ - Unit tests with a scripted fake PX4 cover all 15 Protocol methods (including the not-applicable ones). The fake arms, climbs, repositions and lands on a simulated clock, so each flight primitive's success and failure paths (arm refused, take-off timeout, reposition never arriving, landing timeout) are tested against vehicle state, not against acks. Like PX4, the fake sends STATUSTEXT only after it has seen a ground-station heartbeat, so a test reason that quotes PX4 shows the adapter's heartbeat reached it; other tests check the heartbeat thread's period, that it stops on `close()`, and that every send holds the send lock.
161
+ - Live PX4 SITL e2e test flies the `drone/flight_only_positive` conformance fixture through `ConformanceRunner` with a real `PX4Adapter` (`tests/integration/test_px4_sitl_e2e.py`, gated by `URML_PX4_SITL=1`). A listen-only witness on PX4's ground-station port checks that the vehicle armed, climbed to at least 90 percent of the take-off altitude, came within twice the acceptance radius of the waypoint, and ended on the ground.
162
+
163
+ **Verified in simulation (2026-09-27):** the e2e test passed locally in WSL2 against PX4 v1.17.0 SITL (SIH quadrotor), and PX4's own log shows the arm, the take-off, the return to launch, the landing and the disarm; the witness measured a highest relative altitude of 31.79 m for the 30 m take-off and a closest approach of 0.92 m to the waypoint. The record is [`tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih.md`](tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih.md). An earlier version of the same test also passed, in 2.37 s, while the adapter counted acks as success and the simulated vehicle never armed; that result does not count as a flight. The CI job has not run.
164
+
165
+ **With the ground-station heartbeat (2026-09-27):** the same test passed again with the heartbeat running (a highest relative altitude of 31.778 m, a closest approach of 0.95 m), PX4 sent its STATUSTEXT to the adapter's link during the flight, and PX4 logged "Connection to ground station lost" after the adapter closed. A no-flight probe got `arm_rejected: mav_result_temporarily_rejected; PX4 said: "Arming denied: Resolve system health failures first"` from a take-off requested before PX4 was ready, saw `wait_for(emergency_stop)` time out on a healthy vehicle, and saw it fire on `MAV_STATE_FLIGHT_TERMINATION` after a flight termination commanded on the ground. The record is [`tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih-heartbeat.md`](tests/integration/sitl-runs/2026-09-27-px4-v1.17.0-sih-heartbeat.md).
166
+
167
+ **Landed since v0.1:**
168
+ - `CompositeAdapter` for stacks that pair PX4 with a ROS 2 companion (see above).
169
+ - Geofence polygon-containment, 3D altitude bands, and people-occupancy zones in the safety-envelope pass (validator Pass 3).
170
+ - Gated `.github/workflows/px4-integration.yml` with three jobs: `px4-smoke` (real pymavlink, no SITL — the `rclpy-smoke` analog), `px4-sitl-e2e` (the flight e2e — the `gazebo-e2e` analog), and `px4-arm64-build` (the hermetic suite under linux/arm64 QEMU emulation — the pre-hardware Jetson-class signal).
171
+
172
+ **Follow-ups (not yet):**
173
+ - First run of `px4-sitl-e2e` on a Linux runner. The job follows the local recipe (PX4 v1.17.0 cloned shallow and recursive, PX4's Python requirements in a venv, the build-only `make px4_sitl_default`, then `sleep infinity | HEADLESS=1 make px4_sitl sihsim_quadx`), but it has not run yet.
174
+ - Real Jetson + real-robot hardware-in-the-loop. QEMU emulation is a faithful proxy for our pure-Python + pymavlink code, not a hardware-verification claim.
175
+ - A fly-and-capture `scan` (waypoint expansion and a capture at each waypoint) for an autopilot with a camera. Until then `scan` returns not-supported on a bare autopilot, and `CompositeAdapter` routes it to the companion.
176
+
177
+ ## Core Commitment
178
+
179
+ This runtime is licensed under the Business Source License 1.1: free for non-production use, commercial license for production (see [`LICENSING.md`](../../LICENSING.md)). No vendor coupling, no cloud dependency.
180
+
181
+ ## Related documents
182
+
183
+ - [`/spec/profiles/drone/`](../../spec/profiles/drone/) — the profile this runtime targets.
184
+ - [`/spec/layer-1-hal/`](../../spec/layer-1-hal/) — manifest schema, including drone-specific fields.
185
+ - [`/conformance/`](../../conformance/) — the test suite that decides conformance.
186
+ - [`/reference/ros2-runtime/INTEGRATION.md`](../ros2-runtime/INTEGRATION.md) — the ROS 2 runtime's parallel design notes.
187
+ - [`MANIFESTO.md`](../../MANIFESTO.md) §Motivating Scenarios — *Drone: the citizen inspector*.
@@ -0,0 +1,75 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.27"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "urml-px4-runtime"
7
+ version = "0.5.0"
8
+ description = "PX4 / MAVLink reference runtime for URML — Universal Robot Language."
9
+ readme = "README.md"
10
+ license = { text = "BUSL-1.1" }
11
+ requires-python = ">=3.11"
12
+ authors = [{ name = "URML Maintainers" }]
13
+ keywords = ["robotics", "drone", "px4", "mavlink", "urml", "runtime"]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Intended Audience :: Developers",
17
+ "License :: Other/Proprietary License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3 :: Only",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Topic :: Scientific/Engineering",
23
+ ]
24
+ dependencies = [
25
+ "pydantic>=2.6,<3",
26
+ "PyYAML>=6.0,<7",
27
+ "urml-validator>=0.5.0",
28
+ "urml-ros2-runtime>=0.5.0",
29
+ ]
30
+
31
+ [project.optional-dependencies]
32
+ # pymavlink is a normal PyPI package (unlike rclpy, which ships with a ROS 2
33
+ # distribution). Installing `urml-px4-runtime[px4]` gives you everything needed
34
+ # to construct PX4Adapter against a real PX4 autopilot or PX4 SITL simulator.
35
+ px4 = [
36
+ "pymavlink>=2.4,<3",
37
+ ]
38
+ dev = [
39
+ "pytest>=8",
40
+ "pytest-cov>=5",
41
+ "ruff>=0.5",
42
+ "mypy>=1.10",
43
+ ]
44
+
45
+ [project.urls]
46
+ Homepage = "https://github.com/urml-dev/URML"
47
+ Repository = "https://github.com/urml-dev/URML"
48
+ Issues = "https://github.com/urml-dev/URML/issues"
49
+
50
+ [tool.hatch.build.targets.wheel]
51
+ packages = ["src/urml_px4_runtime"]
52
+
53
+ [tool.ruff]
54
+ line-length = 120
55
+ target-version = "py311"
56
+ src = ["src", "tests"]
57
+
58
+ [tool.ruff.lint]
59
+ select = ["E", "F", "W", "I", "B", "UP", "N", "ANN", "RUF"]
60
+ ignore = ["ANN401"]
61
+
62
+ [tool.ruff.lint.per-file-ignores]
63
+ "tests/*" = ["ANN"]
64
+
65
+ [tool.mypy]
66
+ strict = true
67
+ python_version = "3.11"
68
+ plugins = ["pydantic.mypy"]
69
+ mypy_path = "src"
70
+ packages = ["urml_px4_runtime"]
71
+
72
+ [tool.pytest.ini_options]
73
+ minversion = "8.0"
74
+ testpaths = ["tests"]
75
+ addopts = ["-q", "--strict-markers"]
@@ -0,0 +1,38 @@
1
+ """urml_px4_runtime — PX4 / MAVLink reference runtime for URML.
2
+
3
+ Public API:
4
+
5
+ PX4Adapter(config)
6
+ Real MAVLink adapter via pymavlink. Implements the same ROSAdapter
7
+ Protocol as the ROS 2 runtime's RclpyAdapter — substrate-neutral by
8
+ design. pymavlink is imported lazily so this module loads on every
9
+ host; constructing the adapter without pymavlink raises a clear,
10
+ actionable error.
11
+
12
+ PX4AdapterConfig
13
+ Connection + identity config (URL, system_id, component_id, default
14
+ timeouts). Loaded from YAML via load_px4_config(path).
15
+
16
+ CompositeAdapter(flight=..., companion=..., routing=None)
17
+ Routes one URML program across two substrates — a PX4 flight
18
+ adapter and a ROS 2 companion adapter — by dispatching each
19
+ ROSAdapter method to whichever backend owns that capability.
20
+ Imports neither pymavlink nor rclpy; loads on every host.
21
+ """
22
+
23
+ from __future__ import annotations
24
+
25
+ from urml_px4_runtime._version import __version__
26
+ from urml_px4_runtime.adapter import PX4Adapter
27
+ from urml_px4_runtime.composite import COMPANION, FLIGHT, CompositeAdapter
28
+ from urml_px4_runtime.config import PX4AdapterConfig, load_px4_config
29
+
30
+ __all__ = [
31
+ "COMPANION",
32
+ "FLIGHT",
33
+ "CompositeAdapter",
34
+ "PX4Adapter",
35
+ "PX4AdapterConfig",
36
+ "__version__",
37
+ "load_px4_config",
38
+ ]
@@ -0,0 +1,3 @@
1
+ """Package version. Bumped per release."""
2
+
3
+ __version__ = "0.5.0"