docs(tools): Mode Requirements detailed docs (#26084)

This commit is contained in:
Hamish Willee authored and GitHub committed 2026-08-06 16:03:39 +10:00
1 parent 545c122fb2
commit 0eb7984e17
40 files changed
+2211 -21

No files matched your search

@@ -0,0 +1,219 @@
# get_mode_requirements
## Purpose
`get_mode_requirements.py` parses the PX4 flight mode requirements defined in
`src/modules/commander/ModeUtil/mode_requirements.cpp` and generates Markdown documentation.
It produces two kinds of output under `docs/en/`:
- `flight_modes/mode_requirements.md` — a consolidated overview table for all vehicle types.
- Per-mode requirement text injected directly into individual flight-mode pages using include markers.
## Running
From the repo root:
```sh
python3 docs/scripts/get_mode_requirements/get_mode_requirements.py
```
Or from the `docs/` directory:
```sh
python3 scripts/get_mode_requirements/get_mode_requirements.py
```
## Inline Injection
The script locates each parent flight-mode doc (e.g. `flight_modes_fw/manual.md`) by scanning
`docs/en/flight_modes/`, `docs/en/flight_modes_fw/`, `docs/en/flight_modes_mc/`, and
`docs/en/advanced_features/` for a VitePress `<!--@include:-->` directive (or an existing
`AUTO-GENERATED` sentinel block) that references the corresponding snippet name
(`build_snippet_to_parent_map()`). This directory scan is what actually determines where a
mode's requirements get injected — it is **independent** of the `doc` field in
`mode_nav_state_defns.json`, which only controls headings/links in `mode_requirements.md`. A
mode can have a `doc` pointing at a page that isn't scanned (or has no injection marker on it
yet) and still trigger a "not mapped to a doc" warning; fix this by adding an
`<!-- AUTO-GENERATED: snippet_stem --><!-- END AUTO-GENERATED: snippet_stem -->` marker pair
(content between them can be empty — the script fills it in) to the actual doc page, and if
that page lives outside the four scanned directories, add its directory to `search_dirs` in
`build_snippet_to_parent_map()`.
A single doc page can contain multiple injection points for different nav-states (e.g.
`flight_modes/offboard.md` has one per vehicle type; `flight_modes_mc/manual_stabilized.md`
has one for `NAVIGATION_STATE_STAB` and one for `NAVIGATION_STATE_MANUAL`, since those are
distinct nav-states with different requirements that happen to share one doc page). Each
generated block always starts its own `### Mode Requirements` heading, so when adding a second
block to the same page, add distinguishing surrounding text/labels manually to avoid duplicate
headings, then hand-write the marker pair; the script replaces the content and heading between
the markers.
It replaces that directive with the rendered content, wrapped in sentinel HTML comments:
```markdown
<!-- AUTO-GENERATED: mode_requirements_fixed_wing_manual -->
### Mode Requirements
The following requirements must be met to arm in this mode, or to switch to this mode when it is armed.
- [`mode_req_manual_control`](xxx) - Requires stick input
<!-- END AUTO-GENERATED: mode_requirements_fixed_wing_manual -->
```
On subsequent runs the sentinels are detected and the content between them is updated in-place,
so the script is safe to re-run whenever `mode_requirements.cpp` changes.
**Do not edit the text between the sentinel comments manually** — it will be overwritten on the
next run. Make changes in `requirement_defns.json` or in `mode_requirements.cpp`.
## Path Calculation
The script resolves the repository root at runtime using `Path(__file__).resolve()` and
navigating **three** parent directories up:
```
docs/scripts/get_mode_requirements/get_mode_requirements.py
-> docs/scripts/get_mode_requirements/ (parent)
-> docs/scripts/ (parent.parent)
-> docs/ (parent.parent.parent)
-> PX4-Autopilot/ (parent.parent.parent.parent) ← repo root
```
If this script is ever moved, update the `repo_root` line accordingly.
## Adding or Changing Requirement Definitions
`requirement_defns.json` (in the same directory as the script) maps each `mode_req_*` flag
name to a human-readable `text`, an example `sensor`, and an extended `detail` string. Add
new entries here when new requirement flags are introduced in `mode_requirements.cpp`.
If the script encounters a flag in `mode_requirements.cpp` that has no entry in
`requirement_defns.json`, it prints a `WARNING:` line to stderr and continues with empty
`text`, `sensor`, and `detail` strings. If the JSON file is missing or malformed the script
exits immediately with a clear `ERROR:` message.
## Nav-State / Vehicle-Type Metadata
`mode_nav_state_defns.json` (in the same directory as the script) maps every
`(VEHICLE_TYPE_*, NAVIGATION_STATE_*)` pair to display metadata used when generating
`mode_requirements.md` section headings and controlling per-mode warning behaviour.
Each entry may contain:
| Key | Type | Default | Meaning |
|-----|------|---------|---------|
| `label` | string | — | Human-readable mode name (e.g. `"Mission Mode"`) |
| `doc` | string | — | Relative doc path used as a heading hyperlink (e.g. `"../flight_modes_fw/mission.md"`) |
| `warn` | bool | `true` | Set to `false` to suppress warnings for this mode |
| `status` | string | — | Short implementation-status note, used only when `doc` is absent (see below) |
**Heading generation** (in `mode_requirements.md`):
A mode is only given a full heading + requirement list in the main per-vehicle-type list if it
has a `doc`. Modes with no `doc` are **not** given a bare heading in the main list — instead
they are collected into a trailing `### Modes Without a Dedicated Page` summary for that
vehicle type, rendered as `- **NAV_STATE** — {status}`. Use `status` to explain *why* there's
no dedicated page — e.g. the mode isn't implemented for this vehicle type, or it behaves
identically to another mode already documented, or it's an internal/failsafe state that isn't
user-selectable. If a no-doc mode has no `status` text, the script prints a `WARNING:` to
stderr and falls back to "Not currently documented."
**Skipping snippet injection:** if an entry has `"warn": false` and no `"doc"` key, the script
skips snippet processing for that mode without printing a warning. Use this for modes that
are not real operational modes (e.g. `TERMINATION`) or that deliberately have no doc page.
(This is orthogonal to `status`: `warn`/`doc` control snippet-injection behaviour, while
`status` controls what's shown in the `mode_requirements.md` summary.)
If the script encounters a `(vehicle_type, nav_state)` pair that has no entry at all in
`mode_nav_state_defns.json`, it prints a `WARNING:` line to stderr. Add a new entry whenever
a new navigation state is introduced in `mode_requirements.cpp`.
**Ordering:** the key order of `NAVIGATION_STATE_*` entries within each vehicle-type block in
`mode_nav_state_defns.json` — not the order `setRequirement()` calls appear in
`mode_requirements.cpp` — determines the order modes are rendered in `mode_requirements.md`,
for both the headed list and the trailing "Modes Without a Dedicated Page" summary. The
intended order is low-to-high autonomy (e.g. `MANUAL` → `STAB` → `ACRO` → ... → `OFFBOARD`).
`VEHICLE_TYPE_FIXED_WING` and `VEHICLE_TYPE_ROTARY_WING` are separate JSON objects, so keep
their key order in sync manually when adding or reordering a mode. Any nav_state the parser
finds that has no entry at all in `mode_nav_state_defns.json` is appended after all
JSON-ordered modes (in addition to the existing "no entry" warning).
The same principle applies one level up: the **top-level** key order of
`mode_nav_state_defns.json` (currently `VEHICLE_TYPE_FIXED_WING` before
`VEHICLE_TYPE_ROTARY_WING`) determines the order the `## Fixed-wing` / `## Multicopter`
sections are rendered in `mode_requirements.md`. This is deliberate — vehicle types are
discovered from `mode_requirements.cpp` into a Python `set`, whose iteration order is
randomized per-process, so the render loops must never iterate that set/dict directly or the
section order will vary between runs for no code reason. Any vehicle type found in the C++ but
absent from the JSON is appended afterwards in sorted order.
## Parser design
`parse_requirements()` uses a single-pass token scanner. A single compiled regex (`_TOKEN`)
identifies all meaningful tokens in the `getModeRequirements()` body in left-to-right order:
single-line comments (skipped), `if (vehicle_type == X) {` (push condition), `} else {` (flip
condition), `}` (pop), `{` (push None for non-conditional blocks), and `setRequirement(…)`
calls. A condition stack tracks the innermost active vehicle-type condition; requirements are
assigned to the appropriate vehicle-type set based on whether we are inside an if-branch, an
else-branch, or unconditional code.
This replaces an earlier two-pass regex approach that was fragile: its non-greedy `[\s\S]*?`
pattern would span across unrelated blocks when a standalone `if` appeared before an `if-else`,
producing silently wrong output for several modes.
## Tests
The test suite lives in `tests/` and is a pytest regression suite that guards against
unintended changes to the parser output. It compares the live output of the parser against
golden JSON snapshots:
```
tests/
__init__.py
test_get_mode_requirements.py ← the test module
test_data/
requirement_defns_golden.json ← expected requirement_defns contents
vehicle_modes_golden.json ← expected parse_requirements() output
```
- `test_data/requirement_defns_golden.json` — expected contents of the `requirement_defns`
dict (flag name → text / sensor / detail).
- `test_data/vehicle_modes_golden.json` — expected output of `parse_requirements()` applied
to the checked-in `mode_requirements.cpp`: a per-vehicle-type, per-mode mapping of sorted
requirement flag names.
`conftest.py` at the package root adds this directory to `sys.path` so pytest can import
`get_mode_requirements` without any install step.
The suite contains three tests:
| Test | What it checks |
|---|---|
| `test_requirement_defns_matches_golden` | `requirement_defns` exactly matches the golden snapshot |
| `test_vehicle_modes_matches_golden` | `parse_requirements()` output matches the golden snapshot; prints a per-mode diff on failure |
| `test_all_requirement_flags_are_defined` | Every flag found in `mode_requirements.cpp` has an entry in `requirement_defns` |
### Running the tests
From this directory (`get_mode_requirements/`):
```sh
pytest tests/ -v
```
### When to run and update
**Run the tests** whenever you change:
- the parser logic in `get_mode_requirements.py`, or
- `src/modules/commander/ModeUtil/mode_requirements.cpp`.
**Update the golden files** after any intentional change to either of the above so that the
snapshots stay in sync with the new expected output:
```sh
python tests/test_get_mode_requirements.py --update-golden
```
Then re-run the tests to confirm they pass, and commit the updated golden files alongside the
parser or C++ changes.
@@ -0,0 +1,11 @@
"""
pytest configuration for get_mode_requirements tests.
Adds the get_mode_requirements/ directory to sys.path so that
from get_mode_requirements import ...
works inside tests/ without any path manipulation in the test files.
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
@@ -0,0 +1,499 @@
#!/usr/bin/env python3
"""
Parses mode requirements in
https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/commander/ModeUtil/mode_requirements.cpp
and uses the result to generate Markdown documentation.
Mode requirements are injected directly into parent flight-mode docs, wrapped in sentinel
comments so subsequent runs update the content in-place.
"""
import re
import argparse
import sys
from pathlib import Path
import json
# ---------------------------------------------------------------------------
# Regex patterns for inline-injection mode
# ---------------------------------------------------------------------------
# Matches a VitePress include directive pointing at a mode_requirements snippet.
# Captures the snippet stem (e.g. mode_requirements_fixed_wing_manual).
_INCLUDE_RE = re.compile(
r'<!--@include:\s*\.\./flight_modes_(?:fw|mc)/(mode_requirements_\w+)\.md-->'
)
# Matches an existing sentinel block for a given stem (built dynamically per stem).
def _sentinel_re(stem: str) -> re.Pattern:
return re.compile(
r'<!-- AUTO-GENERATED: ' + re.escape(stem) + r' -->.*?'
r'<!-- END AUTO-GENERATED: ' + re.escape(stem) + r' -->',
re.DOTALL,
)
# Load requirement definitions from the JSON file alongside this script.
_DEFNS_PATH = Path(__file__).resolve().parent / "requirement_defns.json"
try:
with _DEFNS_PATH.open() as _f:
requirement_defns: dict = json.load(_f)
except FileNotFoundError:
sys.exit(f"ERROR: requirement definitions file not found: {_DEFNS_PATH}")
except json.JSONDecodeError as e:
sys.exit(f"ERROR: requirement definitions file is not valid JSON ({_DEFNS_PATH}): {e}")
# Load nav-state / vehicle-type metadata from the JSON file alongside this script.
_NAV_STATE_DEFNS_PATH = Path(__file__).resolve().parent / "mode_nav_state_defns.json"
try:
with _NAV_STATE_DEFNS_PATH.open() as _f:
mode_nav_state_defns: dict = json.load(_f)
except FileNotFoundError:
sys.exit(f"ERROR: mode nav state definitions file not found: {_NAV_STATE_DEFNS_PATH}")
except json.JSONDecodeError as e:
sys.exit(f"ERROR: mode nav state definitions file is not valid JSON ({_NAV_STATE_DEFNS_PATH}): {e}")
def _get_nav_state_def(vehicle_type: str, nav_state: str) -> dict:
"""Return the nav-state definition for *(vehicle_type, nav_state)*.
Emits a WARNING to stderr if the pair has no entry in mode_nav_state_defns.json.
Returns an empty dict for unknown entries so callers can use .get() safely.
"""
vt_map = mode_nav_state_defns.get(vehicle_type)
if vt_map is None:
print(
f"WARNING: vehicle type '{vehicle_type}' has no entries in "
"mode_nav_state_defns.json.",
file=sys.stderr,
)
return {}
entry = vt_map.get(nav_state)
if entry is None:
print(
f"WARNING: no entry in mode_nav_state_defns.json for "
f"({vehicle_type}, {nav_state}).",
file=sys.stderr,
)
return {}
return entry
def _get_req_def(flag: str) -> dict:
"""Return the requirement definition for *flag*, warning if it is unknown."""
if flag not in requirement_defns:
print(
f"WARNING: requirement '{flag}' found in mode_requirements.cpp "
"but has no entry in requirement_defns.json — text/detail will be empty.",
file=sys.stderr,
)
return {"text": "", "sensor": "", "detail": ""}
return requirement_defns[flag]
# 1. Resolve the absolute path of the current script file.
script_path = Path(__file__).resolve()
# 2. Go up three directories from the script's location (docs/scripts/get_mode_requirements/ -> PX4-Autopilot/).
# This finds the repository root.
repo_root = script_path.parent.parent.parent.parent
# 3. Construct the target file path relative to the repository root.
SRC = repo_root / "src/modules/commander/ModeUtil/mode_requirements.cpp"
docs_output_path_base = repo_root / "docs/en/"
docs_output_path_flight_modes = docs_output_path_base / "flight_modes"
# Tokeniser for getModeRequirements() function body.
#
# Walks the body left-to-right and emits one of six token kinds:
# comment //… – ignored
# if_vtype if (vehicle_type == X) { – push condition onto stack
# else_open } else { – flip top of stack to else-side
# close } – pop stack
# open { – push None (non-conditional block)
# req setRequirement(nav, flag) – record requirement
#
# \s* in the req pattern intentionally matches newlines so that multi-line
# setRequirement() calls (e.g. NAVIGATION_STATE_ALTITUDE_CRUISE) are handled.
_TOKEN = re.compile(
r'(?P<comment>//[^\n]*)'
r'|(?P<if_vtype>if\s*\(\s*vehicle_type\s*==\s*vehicle_status_s::'
r'(?P<vtype>VEHICLE_TYPE_\w+)\s*\)\s*\{)'
r'|(?P<else_open>\}\s*else\s*\{)'
r'|(?P<close>\})'
r'|(?P<open>\{)'
r'|(?P<req>setRequirement\(\s*'
r'(?P<nav_state>vehicle_status_s::\w+)\s*,\s*'
r'flags\.(?P<req_flag>\w+)\s*\))'
)
def parse_requirements():
try:
text = SRC.read_text()
except FileNotFoundError:
print(f"Error: Source file not found at {SRC.resolve()}")
return {}
# Extract getModeRequirements() body
m = re.search(r"getModeRequirements\s*\([^)]*\)\s*\{(.*)\}", text, re.S)
if not m:
raise RuntimeError("Could not find getModeRequirements() body")
body = m.group(1)
# Discover all vehicle types mentioned in if-conditions
all_vtypes = set(re.findall(r'vehicle_status_s::(VEHICLE_TYPE_\w+)', body))
vehicle_modes = {vt: {} for vt in all_vtypes}
# Single-pass token scan with a condition stack.
#
# cond_stack entries:
# None – inside a non-vehicle-type block (or function body)
# (vtype, False) – inside if (vehicle_type == vtype) { … }
# (vtype, True) – inside } else { … } (i.e. NOT vtype)
cond_stack = []
for tok in _TOKEN.finditer(body):
kind = tok.lastgroup
if kind == 'comment':
pass # skip
elif kind == 'if_vtype':
cond_stack.append((tok.group('vtype'), False))
elif kind == 'else_open':
if cond_stack and cond_stack[-1] is not None:
# Flip the current vehicle-type condition to its else side
cond_stack[-1] = (cond_stack[-1][0], True)
else:
# else on a non-vehicle-type if – treat as a plain block
if cond_stack:
cond_stack.pop()
cond_stack.append(None)
elif kind == 'close':
if cond_stack:
cond_stack.pop()
elif kind == 'open':
cond_stack.append(None)
elif kind == 'req':
mode = tok.group('nav_state').replace('vehicle_status_s::', '')
req = tok.group('req_flag')
# The innermost vehicle-type condition determines which types apply
active = next((c for c in reversed(cond_stack) if c is not None), None)
if active is None:
vtypes = all_vtypes # default: all types
elif not active[1]:
vtypes = {active[0]} # if-branch: only this type
else:
vtypes = all_vtypes - {active[0]} # else-branch: all other types
for vt in vtypes:
vehicle_modes[vt].setdefault(mode, set()).add(req)
return vehicle_modes
def build_snippet_to_parent_map(docs_base: Path) -> dict:
"""Scan flight-mode doc directories and return {snippet_stem: parent_file_path}.
Recognises both the original VitePress include directive and existing
AUTO-GENERATED sentinel blocks so that re-runs work correctly.
"""
mapping = {}
search_dirs = [
docs_base / "flight_modes",
docs_base / "flight_modes_fw",
docs_base / "flight_modes_mc",
docs_base / "advanced_features",
]
for search_dir in search_dirs:
for md_file in sorted(search_dir.glob("*.md")):
text = md_file.read_text(encoding="utf-8")
# Check for @include directives
for m in _INCLUDE_RE.finditer(text):
stem = m.group(1)
mapping[stem] = md_file
# Check for existing sentinel blocks (covers re-runs after first inline)
for m in re.finditer(r'<!-- AUTO-GENERATED: (mode_requirements_\w+) -->', text):
stem = m.group(1)
mapping[stem] = md_file
return mapping
def inline_requirements_into_doc(parent_path: Path, snippet_stem: str, content: str) -> None:
"""Inject *content* into *parent_path*, replacing an @include directive or
an existing AUTO-GENERATED sentinel block for *snippet_stem*.
The injected region is wrapped in sentinel HTML comments so subsequent runs
can find and replace it without any manual markers remaining in the file.
"""
sentinel_start = f'<!-- AUTO-GENERATED: {snippet_stem} -->'
sentinel_end = f'<!-- END AUTO-GENERATED: {snippet_stem} -->'
replacement = f'{sentinel_start}\n\n{content}\n{sentinel_end}'
original = parent_path.read_text(encoding="utf-8")
# Try existing sentinel block first (re-run case)
updated, n = _sentinel_re(snippet_stem).subn(replacement, original)
if n == 0:
# Try @include directive (first-time case)
include_pattern = re.compile(
r'<!--@include:\s*\.\./flight_modes_(?:fw|mc)/' + re.escape(snippet_stem) + r'\.md-->'
)
updated, n = include_pattern.subn(replacement, original)
if n == 0:
print(
f"⚠️ Warning: could not update '{parent_path}' for '{snippet_stem}'.\n"
f" The doc was found but contains neither an AUTO-GENERATED sentinel block\n"
f" nor an @include directive for this snippet.\n"
f" Add an injection point manually or remove the stale mapping."
)
return
if updated != original:
parent_path.write_text(updated, encoding="utf-8")
print(f"✅ Injected {snippet_stem} into {parent_path}")
# else: already up to date, no output needed
if __name__ == "__main__":
_parser = argparse.ArgumentParser(
description="Generate PX4 flight-mode requirement documentation."
)
_parser.add_argument(
'--suppress_warnings',
action='store_true',
help='Suppress warnings about modes not listed in INVALID_MODES, NO_DOC_MODES, '
'or the doc mapping. Resolve warnings by adding the mode to one of those '
'lists or by providing a doc page with an injection point.',
)
args = _parser.parse_args()
vehicle_modes = parse_requirements()
# Convert sets to sorted lists for JSON output - i.e. all reqs are listed in the same order.
vehicle_modes_sorted = {
vtype: {mode: sorted(list(reqs)) for mode, reqs in modes.items()}
for vtype, modes in vehicle_modes.items()
}
print(json.dumps(vehicle_modes_sorted, indent=4))
# Vehicle-type render order: keyed off mode_nav_state_defns.json's top-level key order so
# it doesn't depend on set/dict iteration order (which is randomized per-process and was
# previously causing the Fixed-wing/Multicopter sections to swap between runs).
_vtype_order = list(mode_nav_state_defns.keys())
ordered_vtypes = [vt for vt in _vtype_order if vt in vehicle_modes_sorted] + sorted(
vt for vt in vehicle_modes_sorted if vt not in _vtype_order
)
# Warn about requirements defined in the JSON but absent from the parsed C++ file.
all_found_flags = {
req
for vtype in vehicle_modes_sorted.values()
for reqs in vtype.values()
for req in reqs
}
for flag in requirement_defns:
if flag not in all_found_flags:
print(
f"WARNING: '{flag}' is defined in requirement_defns.json "
"but was not found in mode_requirements.cpp.",
file=sys.stderr,
)
requirement_table = "| Requirement | Example |\n| --- | --- |\n"
for requirement, info in requirement_defns.items():
sensor_info = f" ({info['sensor']})" if info['sensor'] else ''
detail = f" <br><br>{info['detail']}" if info['detail'] else ''
requirement_table += (
f'| <a id="{requirement}">`{requirement}`</a>'
f' | {info["text"]}{sensor_info}{detail} |\n'
)
mode_requirements_markdown = f"""# Mode Requirements
::: info
This documentation was auto-generated from the source code (see [docs/scripts/get_mode_requirements](https://github.com/PX4/PX4-Autopilot/tree/main/docs/scripts/get_mode_requirements)).
:::
Mode requirements define the set of conditions that must be met in order to arm in a particular flight mode, or to switch to the mode if it is already armed.
Requirements are defined for internal modes in [mode_requirements.cpp](https://github.com/PX4/PX4-Autopilot/blob/main/src/modules/commander/ModeUtil/mode_requirements.cpp), and for ROS 2 external modes in [requirement_flags.hpp](https://github.com/Auterion/px4-ros2-interface-lib/blob/main/px4_ros2_cpp/include/px4_ros2/common/requirement_flags.hpp) (Github `Auterion/px4-ros2-interface-lib` repository).
The mode requirements are the same in both cases.
The following sections provide an overview of the requirements and what modes they are used in.
## Requirements Definitions
{requirement_table}
### Naming Conventions
In general requirement flag names are abstracted from specific sensors.
This is done because particular requirements can often be met by several sensors.
For example, GNSS is the most common source of global position, but it isn't the only one.
The requirements include frame and accuracy information hints in their names:
- `global` means an absolute world frame, such as that provided by GNSS.
- `local` means a frame that relative to an initialization point, such as the position of an IMU on boot.
- `relaxed` means that the mode does not require or rely on accurate data: as long as sensors are providing some data the state is considered valid.
Relaxed conditions are used for modes where some sensor data is considered more important than none at all, such as when calculating position via optical flow velocity measurements.
By contrast, a position mode that is not relaxed requires reliable sensor data, and will block arming if inaccuracy is detected.
Note that a global position requirement can be met if you have a valid _local position_, by mapping the local frame to a global position.
This can be done by setting the global position of the local origin using the MAVLink message [SET_GPS_GLOBAL_ORIGIN](https://mavlink.io/en/messages/common.html#SET_GPS_GLOBAL_ORIGIN), either directly or via a GCS (see [External Position Estimate > Enabling Auto Modes with a Local Position](../ros/external_position_estimation#enabling-auto-modes-with-a-local-position)).
Similarly, if the vehicle has `mode_req_local_position_relaxed`, then you can map to a global position in order to meet the `global_position_relaxed` requirement.
This allows PX4 automatic flight modes that require a global position to be used locally, such as Mission and Return.
"""
for vehicle_type in ordered_vtypes:
if vehicle_type == 'VEHICLE_TYPE_ROTARY_WING':
mode_requirements_markdown += f"\n## Multicopter ({vehicle_type})\n"
elif vehicle_type == 'VEHICLE_TYPE_FIXED_WING':
mode_requirements_markdown += f"\n## Fixed-wing ({vehicle_type})\n"
else:
mode_requirements_markdown += f"\n## {vehicle_type}\n"
undocumented_modes = []
# Iterate in the order modes are listed in mode_nav_state_defns.json (rather than the
# order setRequirement() calls happen to appear in mode_requirements.cpp) so headings
# follow the intended low-to-high autonomy progression. Any mode the parser found that
# has no entry at all in mode_nav_state_defns.json is appended at the end, preserving
# the existing "no entry" warning via _get_nav_state_def().
nav_defs_for_vt = mode_nav_state_defns.get(vehicle_type, {})
modes_present = vehicle_modes_sorted[vehicle_type]
ordered_modes = [m for m in nav_defs_for_vt if m in modes_present]
extra_modes = [m for m in modes_present if m not in nav_defs_for_vt]
for flight_mode in ordered_modes + extra_modes:
defn = _get_nav_state_def(vehicle_type, flight_mode)
label = defn.get("label")
doc = defn.get("doc")
# Modes with no doc page have no meaningful (or no implemented) behaviour to
# document on this vehicle type: list them in a trailing summary instead of
# giving them a full heading in the main list.
if not doc:
undocumented_modes.append((flight_mode, defn.get("status")))
continue
if label:
mode_heading_markdown = f"\n### [{label}]({doc}) ({flight_mode})\n\n"
else:
mode_heading_markdown = f"\n### {flight_mode}\n\n"
mode_requirements_markdown += mode_heading_markdown
for requirement in vehicle_modes_sorted[vehicle_type][flight_mode]:
#mode_requirements_markdown += f"- {requirement}: {requirement_defns[requirement]}\n"
mode_requirements_markdown += f"- [`{requirement}`](#{requirement})\n"
if undocumented_modes:
mode_requirements_markdown += "\n### Modes Without a Dedicated Page\n\n"
mode_requirements_markdown += (
"The following internal navigation states have no distinct user-facing "
"behaviour or documentation page on this frame type:\n\n"
)
for flight_mode, status in undocumented_modes:
if not status:
print(
f"WARNING: no 'status' text in mode_nav_state_defns.json for "
f"({vehicle_type}, {flight_mode}) — it has no doc and will show "
"with no explanation in the 'Modes Without a Dedicated Page' summary.",
file=sys.stderr,
)
status = "Not currently documented."
mode_requirements_markdown += f"- **{flight_mode}** — {status}\n"
# 2. Define the filename for the overview topic: mode_requirements
filename = "mode_requirements.md"
filename = docs_output_path_flight_modes / filename
try:
with open(filename, 'w', encoding='utf-8') as f:
# 'w' stands for 'write' mode, which creates the file or overwrites an existing one.
f.write(mode_requirements_markdown)
print(f"✅ Successfully wrote content to {filename}")
except Exception as e:
print(f"❌ An error occurred: {e}")
# Generate per-doc mode requirements, injected inline into parent docs
snippet_to_parent = build_snippet_to_parent_map(docs_output_path_base)
# Display labels for vehicle types used in shared-doc headings.
_VT_DISPLAY_LABEL = {
"VEHICLE_TYPE_FIXED_WING": "Fixed-Wing",
"VEHICLE_TYPE_ROTARY_WING": "Multicopter",
}
for vehicle_type in ordered_vtypes:
for flight_mode in vehicle_modes_sorted[vehicle_type]:
vehicle_part = vehicle_type.split("VEHICLE_TYPE_")[-1]
mode_part = flight_mode.split("NAVIGATION_STATE_")[-1]
snippet_stem = f"mode_requirements_{vehicle_part}_{mode_part}".lower()
print(f"Processing: {snippet_stem}")
# Skip modes that have warn=false and no doc in mode_nav_state_defns.json.
# This replaces the former INVALID_MODES / NO_DOC_MODES hard-coded sets.
nav_defn = mode_nav_state_defns.get(vehicle_type, {}).get(flight_mode, {})
if not nav_defn.get("warn", True) and not nav_defn.get("doc"):
print(f" Skipping {snippet_stem} (warn=false, no doc in mode_nav_state_defns.json)")
continue
# Resolve parent doc early so we can choose the right heading.
parent = snippet_to_parent.get(snippet_stem)
# Use a labeled heading when injecting into a shared doc (flight_modes/)
# so readers know which vehicle type each section applies to.
is_shared_doc = parent is not None and parent.parent.name == "flight_modes"
if is_shared_doc:
vt_label = _VT_DISPLAY_LABEL.get(vehicle_type, vehicle_type)
req_heading = f"### Mode Requirements — {vt_label}"
else:
req_heading = "### Mode Requirements"
vehicle_mode_markdown = f"""{req_heading}
The following requirements must be met to arm in this mode, or to switch to this mode when it is armed.
"""
for requirement in vehicle_modes_sorted[vehicle_type][flight_mode]:
text = _get_req_def(requirement)['text']
vehicle_mode_markdown += f"- [`{requirement}`](../flight_modes/mode_requirements.md#{requirement}) — {text}\n"
if parent:
inline_requirements_into_doc(parent, snippet_stem, vehicle_mode_markdown)
else:
if not args.suppress_warnings and nav_defn.get("warn", True):
print(
f"⚠️ Missing mode: '{snippet_stem}' is not mapped to a doc.\n"
f" Resolve by:\n"
f" • Adding a doc page with an injection point for this mode\n"
f" • Setting \"warn\": false in mode_nav_state_defns.json for "
f"({vehicle_type}, {flight_mode})\n"
f" • Re-running with --suppress_warnings to silence this warning"
)
@@ -0,0 +1,166 @@
{
"VEHICLE_TYPE_FIXED_WING": {
"NAVIGATION_STATE_MANUAL": {
"label": "Manual Mode",
"doc": "../flight_modes_fw/manual.md"
},
"NAVIGATION_STATE_STAB": {
"label": "Stabilized Mode",
"doc": "../flight_modes_fw/stabilized.md"
},
"NAVIGATION_STATE_ACRO": {
"label": "Acro Mode",
"doc": "../flight_modes_fw/acro.md"
},
"NAVIGATION_STATE_ALTCTL": {
"label": "Altitude Mode",
"doc": "../flight_modes_fw/altitude.md"
},
"NAVIGATION_STATE_ALTITUDE_CRUISE": {
"warn": false,
"status": "Behaves identically to [Altitude Mode](../flight_modes_fw/altitude.md) on fixed-wing frames — the control-mode flags are the same and there is no separate implementation."
},
"NAVIGATION_STATE_POSCTL": {
"label": "Cruise Mode",
"doc": "../flight_modes_fw/cruise.md"
},
"NAVIGATION_STATE_POSITION_SLOW": {
"warn": false,
"status": "Behaves identically to [Position Mode](../flight_modes_fw/position.md) on fixed-wing frames — the control-mode flags are the same and there is no separate implementation."
},
"NAVIGATION_STATE_GUIDED_COURSE": {
"label": "Guided Course Mode",
"doc": "../flight_modes_fw/guided_course.md"
},
"NAVIGATION_STATE_AUTO_LOITER": {
"label": "Hold Mode",
"doc": "../flight_modes_fw/hold.md"
},
"NAVIGATION_STATE_ORBIT": {
"warn": false,
"status": "Not implemented for fixed-wing frames; [Hold Mode](../flight_modes_fw/hold.md) is used instead for orbiting a point."
},
"NAVIGATION_STATE_AUTO_TAKEOFF": {
"label": "Takeoff Mode",
"doc": "../flight_modes_fw/takeoff.md"
},
"NAVIGATION_STATE_AUTO_VTOL_TAKEOFF": {
"warn": false,
"status": "VTOL-specific transition state used for vertical takeoff before transitioning to forward flight; not applicable to plain fixed-wing frames."
},
"NAVIGATION_STATE_DESCEND": {
"label": "Descend Mode",
"doc": "../flight_modes_fw/descend.md"
},
"NAVIGATION_STATE_AUTO_LAND": {
"label": "Land Mode",
"doc": "../flight_modes_fw/land.md"
},
"NAVIGATION_STATE_AUTO_PRECLAND": {
"warn": false,
"status": "Not implemented for fixed-wing frames (precision landing requires hover capability)."
},
"NAVIGATION_STATE_AUTO_RTL": {
"label": "Return Mode",
"doc": "../flight_modes_fw/return.md"
},
"NAVIGATION_STATE_AUTO_MISSION": {
"label": "Mission Mode",
"doc": "../flight_modes_fw/mission.md"
},
"NAVIGATION_STATE_AUTO_FOLLOW_TARGET": {
"warn": false,
"status": "Not implemented for fixed-wing frames (Follow Me is a multicopter-only flight task)."
},
"NAVIGATION_STATE_OFFBOARD": {
"label": "Offboard Mode",
"doc": "../flight_modes_fw/offboard.md"
},
"NAVIGATION_STATE_TERMINATION": {
"warn": false,
"status": "Internal flight-termination failsafe state. Not user-selectable; entered automatically when a failsafe action escalates to termination."
}
},
"VEHICLE_TYPE_ROTARY_WING": {
"NAVIGATION_STATE_MANUAL": {
"label": "Manual/Stabilized Mode",
"doc": "../flight_modes_mc/manual_stabilized.md"
},
"NAVIGATION_STATE_STAB": {
"label": "Manual/Stabilized Mode",
"doc": "../flight_modes_mc/manual_stabilized.md"
},
"NAVIGATION_STATE_ACRO": {
"label": "Acro Mode",
"doc": "../flight_modes_mc/acro.md"
},
"NAVIGATION_STATE_ALTCTL": {
"label": "Altitude Mode",
"doc": "../flight_modes_mc/altitude.md"
},
"NAVIGATION_STATE_ALTITUDE_CRUISE": {
"label": "Altitude Cruise Mode",
"doc": "../flight_modes_mc/altitude_cruise.md"
},
"NAVIGATION_STATE_POSCTL": {
"label": "Position Mode",
"doc": "../flight_modes_mc/position.md"
},
"NAVIGATION_STATE_POSITION_SLOW": {
"label": "Position Slow Mode",
"doc": "../flight_modes_mc/position_slow.md"
},
"NAVIGATION_STATE_GUIDED_COURSE": {
"warn": false,
"status": "Not implemented for multicopters (course-hold is a fixed-wing-only flight task)."
},
"NAVIGATION_STATE_AUTO_LOITER": {
"label": "Hold Mode",
"doc": "../flight_modes_mc/hold.md"
},
"NAVIGATION_STATE_ORBIT": {
"label": "Orbit Mode",
"doc": "../flight_modes_mc/orbit.md"
},
"NAVIGATION_STATE_AUTO_TAKEOFF": {
"label": "Takeoff Mode",
"doc": "../flight_modes_mc/takeoff.md"
},
"NAVIGATION_STATE_AUTO_VTOL_TAKEOFF": {
"warn": false,
"status": "VTOL-specific transition state used for vertical takeoff before transitioning to forward flight; not applicable to plain multicopter frames."
},
"NAVIGATION_STATE_DESCEND": {
"label": "Descend Mode",
"doc": "../flight_modes_mc/descend.md"
},
"NAVIGATION_STATE_AUTO_LAND": {
"label": "Land Mode",
"doc": "../flight_modes_mc/land.md"
},
"NAVIGATION_STATE_AUTO_PRECLAND": {
"label": "Precision Landing",
"doc": "../advanced_features/precland.md"
},
"NAVIGATION_STATE_AUTO_RTL": {
"label": "Return Mode",
"doc": "../flight_modes_mc/return.md"
},
"NAVIGATION_STATE_AUTO_MISSION": {
"label": "Mission Mode",
"doc": "../flight_modes_mc/mission.md"
},
"NAVIGATION_STATE_AUTO_FOLLOW_TARGET": {
"label": "Follow Me Mode",
"doc": "../flight_modes_mc/follow_me.md"
},
"NAVIGATION_STATE_OFFBOARD": {
"label": "Offboard Mode",
"doc": "../flight_modes_mc/offboard.md"
},
"NAVIGATION_STATE_TERMINATION": {
"warn": false,
"status": "Internal flight-termination failsafe state. Not user-selectable; entered automatically when a failsafe action escalates to termination."
}
}
}
@@ -0,0 +1,72 @@
{
"mode_req_angular_velocity": {
"text": "Angular velocity",
"sensor": "gyroscope",
"detail": ""
},
"mode_req_attitude": {
"text": "Attitude/pose",
"sensor": "IMU, or theoretically a motion capture system",
"detail": ""
},
"mode_req_local_position": {
"text": "Position relative to EKF2 origin ('0') point",
"sensor": "GNSS, VIO, mocap",
"detail": ""
},
"mode_req_local_position_relaxed": {
"text": "Position relative to EKF2 origin ('0') point but accepts poor accuracy",
"sensor": "Optical flow",
"detail": "Typically optical flow. You set zero when you take off and just integrate up the flow. So the absolute position can be very inaccurate but better than nothing. Useful to not drift away quickly and roughly know where you are."
},
"mode_req_global_position": {
"text": "Position measurement updates in a global coordinate frame",
"sensor": "GNSS, or local position and global reference to EKF 0",
"detail": ""
},
"mode_req_global_position_relaxed": {
"text": "Position measurement updates in a global coordinate frame but accepts poor accuracy",
"sensor": "",
"detail": "Local position and global reference to EKF2 origin"
},
"mode_req_local_alt": {
"text": "Local altitude relative to EKF2 origin ('0') position",
"sensor": "Barometer corrected by GNSS altitude over time, distance sensor",
"detail": "Usually not the distance sensor because if the ground shifts then the coordinate frame would shift with it (range aid problem)."
},
"mode_req_mission": {
"text": "Valid mission in autopilot's storage",
"sensor": "Mission mode only requirement",
"detail": "can be from last time no need to upload fresh"
},
"mode_req_offboard_signal": {
"text": "Offboard heartbeat",
"sensor": "",
"detail": "MAVLink messages SET_ATTITUDE_TARGET or SET_POSITION_TARGET_LOCAL_NED or SET_POSITION_TARGET_GLOBAL_INT not timing out. Offboard mode specific requirement."
},
"mode_req_home_position": {
"text": "Global home reference must be set",
"sensor": "",
"detail": "Specific requirement for Return mode"
},
"mode_req_wind_and_flight_time_compliance": {
"text": "Safety compliance limits on wind and flight time.",
"sensor": "",
"detail": "Blocks arming or switching into the mode when the estimated wind speed exceeds `COM_WIND_MAX` (if `COM_WIND_MAX_ACT` is set to an action stronger than a warning), or when the flight time exceeds `COM_FLT_TIME_MAX`. Both limits are disabled by default. Modes commanded by a failsafe are exempt (see also `COM_WIND_WARN`)."
},
"mode_req_prevent_arming": {
"text": "Mode prevents arming",
"sensor": "",
"detail": "The vehicle cannot be armed while this mode is selected. Set for modes such as Land, Orbit, and Return that you can't take off in (they can only be entered in flight)."
},
"mode_req_manual_control": {
"text": "Requires stick input",
"sensor": "",
"detail": "Manual control can come from an RC system (RC driver -> channel mapping/calibration -> [`manual_control_input`](../msg_docs/ManualControlSetpoint.md)) or from a joystick (MAVLink [MANUAL_CONTROL](https://mavlink.io/en/messages/common.html#MANUAL_CONTROL) message -> `manual_control_input`). A selector configured with [COM_RC_IN_MODE](../advanced_config/parameter_reference.md#COM_RC_IN_MODE) chooses which source is used and publishes it as `manual_control_setpoint`. The requirement is met while the selected source has not timed out."
},
"mode_req_other": {
"text": "Others requirement.",
"sensor": "",
"detail": "This is used by external modes. It is intended to specify additional requirements not covered by the existing flags"
}
}
Whitespace-only changes.
@@ -0,0 +1,72 @@
{
"mode_req_angular_velocity": {
"text": "Angular velocity",
"sensor": "gyroscope",
"detail": ""
},
"mode_req_attitude": {
"text": "Attitude/pose",
"sensor": "IMU, or theoretically a motion capture system",
"detail": ""
},
"mode_req_local_position": {
"text": "Position relative to EKF2 origin ('0') point",
"sensor": "GNSS, VIO, mocap",
"detail": ""
},
"mode_req_local_position_relaxed": {
"text": "Position relative to EKF2 origin ('0') point but accepts poor accuracy",
"sensor": "Optical flow",
"detail": "Typically optical flow. You set zero when you take off and just integrate up the flow. So the absolute position can be very inaccurate but better than nothing. Useful to not drift away quickly and roughly know where you are."
},
"mode_req_global_position": {
"text": "Position measurement updates in a global coordinate frame",
"sensor": "GNSS, or local position and global reference to EKF 0",
"detail": ""
},
"mode_req_global_position_relaxed": {
"text": "Position measurement updates in a global coordinate frame but accepts poor accuracy",
"sensor": "",
"detail": "Local position and global reference to EKF2 origin"
},
"mode_req_local_alt": {
"text": "Local altitude relative to EKF2 origin ('0') position",
"sensor": "Barometer corrected by GNSS altitude over time, distance sensor",
"detail": "Usually not the distance sensor because if the ground shifts then the coordinate frame would shift with it (range aid problem)."
},
"mode_req_mission": {
"text": "Valid mission in autopilot's storage",
"sensor": "Mission mode only requirement",
"detail": "can be from last time no need to upload fresh"
},
"mode_req_offboard_signal": {
"text": "Offboard heartbeat",
"sensor": "",
"detail": "MAVLink messages SET_ATTITUDE_TARGET or SET_POSITION_TARGET_LOCAL_NED or SET_POSITION_TARGET_GLOBAL_INT not timing out. Offboard mode specific requirement."
},
"mode_req_home_position": {
"text": "Global home reference must be set",
"sensor": "",
"detail": "Specific requirement for Return mode"
},
"mode_req_wind_and_flight_time_compliance": {
"text": "Safety compliance limits on wind and flight time.",
"sensor": "",
"detail": "Autonomous flight prevented in high winds (20m/s?) or if exceeds planned flight time, except for failsafe flight. See COM_FLT_TIME_MAX or COM_WIND_MAX for limits (also see COM_WIND_WARN, COM_WIND_MAX_ACT)."
},
"mode_req_prevent_arming": {
"text": "Mode prevents arming (vehicle must be armed to switch to this mode)",
"sensor": "",
"detail": "Set for modes like Land, Orbit, Return that you can't take off in"
},
"mode_req_manual_control": {
"text": "Requires stick input",
"sensor": "",
"detail": "This can come from multiple channels: RC driver -> channels -> RC mapping/calibration -> [`manual_control_input` or `manual_control_setpoint` topic](../msg_docs/ManualControlSetpoint.md) -> Selector using [COM_RC_IN_MODE](../advanced_config/parameter_reference.md#COM_RC_IN_MODE) -> Joystick (MAVLink [MANUAL_CONTROL](https://mavlink.io/en/messages/common.html#MANUAL_CONTROL) message) -> ..."
},
"mode_req_other": {
"text": "Others requirement.",
"sensor": "",
"detail": "This is used by external modes. It is intended to specify additional requirements not covered by the existing flags"
}
}
@@ -0,0 +1,257 @@
{
"VEHICLE_TYPE_FIXED_WING": {
"NAVIGATION_STATE_MANUAL": [
"mode_req_manual_control"
],
"NAVIGATION_STATE_ALTCTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_manual_control"
],
"NAVIGATION_STATE_ALTITUDE_CRUISE": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_manual_control"
],
"NAVIGATION_STATE_POSCTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_manual_control"
],
"NAVIGATION_STATE_POSITION_SLOW": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_manual_control"
],
"NAVIGATION_STATE_AUTO_MISSION": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position_relaxed",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_mission",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_LOITER": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position_relaxed",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_GUIDED_COURSE": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_RTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position_relaxed",
"mode_req_home_position",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_ACRO": [
"mode_req_angular_velocity",
"mode_req_manual_control"
],
"NAVIGATION_STATE_DESCEND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_TERMINATION": [
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_OFFBOARD": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_offboard_signal"
],
"NAVIGATION_STATE_STAB": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_manual_control"
],
"NAVIGATION_STATE_AUTO_TAKEOFF": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt"
],
"NAVIGATION_STATE_AUTO_LAND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_AUTO_FOLLOW_TARGET": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_PRECLAND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_ORBIT": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_VTOL_TAKEOFF": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed"
]
},
"VEHICLE_TYPE_ROTARY_WING": {
"NAVIGATION_STATE_MANUAL": [
"mode_req_manual_control"
],
"NAVIGATION_STATE_ALTCTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_manual_control"
],
"NAVIGATION_STATE_ALTITUDE_CRUISE": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_manual_control"
],
"NAVIGATION_STATE_POSCTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_manual_control"
],
"NAVIGATION_STATE_POSITION_SLOW": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_manual_control"
],
"NAVIGATION_STATE_AUTO_MISSION": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_mission",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_LOITER": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_GUIDED_COURSE": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_RTL": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_global_position",
"mode_req_home_position",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_ACRO": [
"mode_req_angular_velocity",
"mode_req_manual_control"
],
"NAVIGATION_STATE_DESCEND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_TERMINATION": [
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_OFFBOARD": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_offboard_signal"
],
"NAVIGATION_STATE_STAB": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_manual_control"
],
"NAVIGATION_STATE_AUTO_TAKEOFF": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position"
],
"NAVIGATION_STATE_AUTO_LAND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position_relaxed",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_AUTO_FOLLOW_TARGET": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_PRECLAND": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming"
],
"NAVIGATION_STATE_ORBIT": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position",
"mode_req_prevent_arming",
"mode_req_wind_and_flight_time_compliance"
],
"NAVIGATION_STATE_AUTO_VTOL_TAKEOFF": [
"mode_req_angular_velocity",
"mode_req_attitude",
"mode_req_local_alt",
"mode_req_local_position"
]
}
}
@@ -0,0 +1,187 @@
#!/usr/bin/env python3
"""
Regression tests for get_mode_requirements.py.
Two golden JSON files in test_data/ capture the expected parser output:
test_data/requirement_defns_golden.json
The static requirement_defns mapping (flag name → text/sensor/detail).
Update this when adding or editing entries in requirement_defns.
test_data/vehicle_modes_golden.json
The parsed output of mode_requirements.cpp: per-vehicle-type,
per-mode lists of requirement flags (sorted alphabetically).
Update this when mode_requirements.cpp changes.
Running the tests
-----------------
From the get_mode_requirements/ directory:
pytest # discover and run all tests
pytest tests/ -v # verbose
Updating the golden files after intentional changes
----------------------------------------------------
python tests/test_get_mode_requirements.py --update-golden
This rewrites both golden files with the current parser output and exits.
"""
import argparse
import json
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Import the module under test
# ---------------------------------------------------------------------------
_TESTS_DIR = Path(__file__).resolve().parent
_SCRIPT_DIR = _TESTS_DIR.parent # get_mode_requirements/
# Ensure the script directory is on sys.path when run directly.
# conftest.py does the same when running under pytest.
if str(_SCRIPT_DIR) not in sys.path:
sys.path.insert(0, str(_SCRIPT_DIR))
from get_mode_requirements import parse_requirements, requirement_defns # noqa: E402
# ---------------------------------------------------------------------------
# Golden file paths
# ---------------------------------------------------------------------------
_TEST_DATA = _TESTS_DIR / "test_data"
_GOLDEN_REQUIREMENT_DEFNS = _TEST_DATA / "requirement_defns_golden.json"
_GOLDEN_VEHICLE_MODES = _TEST_DATA / "vehicle_modes_golden.json"
# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
def _vehicle_modes_as_sorted_lists(vehicle_modes: dict) -> dict:
"""Convert the sets returned by parse_requirements() to sorted lists."""
return {
vtype: {mode: sorted(reqs) for mode, reqs in modes.items()}
for vtype, modes in vehicle_modes.items()
}
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
def test_requirement_defns_matches_golden():
"""requirement_defns must match the golden snapshot.
Fails when a flag is added, removed, or its text/sensor/detail is edited
without also updating the golden file.
"""
with _GOLDEN_REQUIREMENT_DEFNS.open(encoding="utf-8") as fh:
expected = json.load(fh)
assert requirement_defns == expected, (
"requirement_defns has diverged from the golden file.\n"
"If this change is intentional run:\n"
" python tests/test_get_mode_requirements.py --update-golden"
)
def test_vehicle_modes_matches_golden():
"""parse_requirements() applied to the checked-in mode_requirements.cpp
must produce output matching the golden snapshot.
Fails when mode_requirements.cpp is edited in a way that adds, removes, or
changes the requirement flags for any mode/vehicle-type combination.
"""
with _GOLDEN_VEHICLE_MODES.open(encoding="utf-8") as fh:
expected = json.load(fh)
vehicle_modes = parse_requirements()
actual = _vehicle_modes_as_sorted_lists(vehicle_modes)
# Collect all differences for a readable failure message
diffs = []
all_vtypes = set(expected) | set(actual)
for vtype in sorted(all_vtypes):
if vtype not in actual:
diffs.append(f" Vehicle type removed: {vtype}")
continue
if vtype not in expected:
diffs.append(f" Vehicle type added: {vtype}")
continue
all_modes = set(expected[vtype]) | set(actual[vtype])
for mode in sorted(all_modes):
exp_reqs = expected[vtype].get(mode)
act_reqs = actual[vtype].get(mode)
if exp_reqs != act_reqs:
diffs.append(
f" {vtype} / {mode}:\n"
f" expected: {exp_reqs}\n"
f" actual: {act_reqs}"
)
assert not diffs, (
"Parsed vehicle modes have diverged from the golden file:\n"
+ "\n".join(diffs)
+ "\nIf this change is intentional run:\n"
" python tests/test_get_mode_requirements.py --update-golden"
)
def test_all_requirement_flags_are_defined():
"""Every requirement flag that appears in parse_requirements() output must
have an entry in requirement_defns.
This catches the case where a new flag is added to mode_requirements.cpp
but not documented in requirement_defns.
"""
vehicle_modes = parse_requirements()
missing = set()
for modes in vehicle_modes.values():
for reqs in modes.values():
for req in reqs:
if req not in requirement_defns:
missing.add(req)
assert not missing, (
"The following requirement flags are used in mode_requirements.cpp but "
"have no entry in requirement_defns:\n " + "\n ".join(sorted(missing))
)
# ---------------------------------------------------------------------------
# Golden-file updater (run directly, not via pytest)
# ---------------------------------------------------------------------------
def _update_golden():
_TEST_DATA.mkdir(exist_ok=True)
# requirement_defns
with _GOLDEN_REQUIREMENT_DEFNS.open("w", encoding="utf-8") as fh:
json.dump(requirement_defns, fh, indent=2, ensure_ascii=False)
fh.write("\n")
print(f"Updated {_GOLDEN_REQUIREMENT_DEFNS}")
# vehicle_modes
vehicle_modes = parse_requirements()
vehicle_modes_sorted = _vehicle_modes_as_sorted_lists(vehicle_modes)
with _GOLDEN_VEHICLE_MODES.open("w", encoding="utf-8") as fh:
json.dump(vehicle_modes_sorted, fh, indent=2, ensure_ascii=False)
fh.write("\n")
print(f"Updated {_GOLDEN_VEHICLE_MODES}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Manage golden files for get_mode_requirements tests.")
parser.add_argument(
"--update-golden",
action="store_true",
help="Rewrite the golden JSON files with the current parser output.",
)
args = parser.parse_args()
if args.update_golden:
_update_golden()
else:
parser.print_help()