Files
PX4-Autopilot/test/mavsdk_tests/conftest.py
T
Jacob Dahl e192036345 fix(commander): ack calibration cancels once and stop promptly (#29514)
* feat(simulator_sih): add orient command to pose the vehicle for calibration

Accel and mag calibration need the vehicle held still on each side and
rotated, which SIH could not do, so neither could be tested in
simulation. `simulator_sih orient` sets the attitude kinematically,
keeping the IMU and the simulated mag consistent with it, and `orient
all` runs every side in turn so a calibration started from a GCS
completes on its own.

Assisted-by: Claude:claude-opus-5-5

* fix(commander): hand a calibration cancel to the running calibration

A cancel got TEMPORARILY_REJECTED from Commander and ACCEPTED from the
calibration, in either order. The calibration polled for it with a
fresh vehicle_command subscription that only saw the newest command,
filtered it by a per-side start time, and did not poll while waiting
for the vehicle to be still or while measuring a side. Baro and level
calibration never polled. A cancel could be lost or take until the
vehicle was held still.

Commander now hands the cancel to the running calibration, which acks
it once when it stops, or rejects it if it ends without stopping.
Every calibration loop checks for it, and accel calibration no longer
reports a failure after a cancel.

Fixes #29424

Assisted-by: Claude:claude-opus-5-5

* test(mavsdk_tests): run accel and mag calibration and cancels on SIH

Covers a full accel and mag calibration, and a cancel in each place it
used to be missed or delayed, checking it gets exactly one ack.

Assisted-by: Claude:claude-opus-5-5

* perf(simulator_sih): build the orient command for SITL only

Commander denies sensor calibration when SIH runs on flight controller
hardware, so orient was about 1.8 KB of unusable code on every NuttX
board that builds SIH.

Assisted-by: Claude:claude-opus-5-5

* fix(commander): open calibration cancels before the worker thread starts

Commander handles one command per loop and the worker thread runs at
low priority, so a cancel queued right behind the start could be
rejected before the worker opened it, and the calibration kept running.

Assisted-by: Claude:claude-opus-5-5

* fix(commander): stop reporting a cancelled gyro calibration as failed

A cancel broke out of the retry loop into the failure report, as accel
calibration did before.

Assisted-by: Claude:claude-opus-5-5

* test(mavsdk_tests): cover gyro cancel and remove two calibration races

A retried start is rejected once the calibration runs, and that late
ack could be counted against the cancel, so the start is sent once.
The baro cancel landed 1 s into a 3 s calibration, about 200 ms of
host time at speed factor 10; it is now sent right after the start.

Assisted-by: Claude:claude-opus-5-5

* style(mavsdk_tests): wrap the calibration start assert for flake8

The 104-character line failed the Python CI checks.

Assisted-by: Claude:claude-opus-5-5

* fix(commander): require param7 zero for a calibration cancel

An ESC calibration (param7 = 1) or baro temperature calibration
(param7 = 3) sent while a calibration ran matched the cancel test. It
stopped the running calibration and was acked ACCEPTED, though the
requested calibration never started. `commander calibrate esc` did the
same.

Assisted-by: Claude:claude-fable-5-2
2026-11-11 14:38:01 -07:00

85 lines
3.5 KiB
Python

from typing import Iterator, Optional, Type, TypeVar
import pytest
# Show the values in failed asserts inside the testers, not just in tests.
pytest.register_assert_rewrite("autopilot_tester", "autopilot_tester_calibration",
"autopilot_tester_flow", "autopilot_tester_follow_me",
"autopilot_tester_loiter", "autopilot_tester_rtl",
"autopilot_tester_vision_target")
from autopilot_tester import AutopilotTester # noqa: E402
from autopilot_tester_calibration import AutopilotTesterCalibration # noqa: E402
from autopilot_tester_flow import AutopilotTesterFlow # noqa: E402
from autopilot_tester_follow_me import AutopilotTesterFollowMe # noqa: E402
from autopilot_tester_loiter import AutopilotTesterLoiter # noqa: E402
from autopilot_tester_rtl import AutopilotTesterRtl # noqa: E402
from autopilot_tester_vision_target import AutopilotTesterVisionTarget # noqa: E402
T = TypeVar("T", bound=AutopilotTester)
def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption("--url", default="udpin://0.0.0.0:14540",
help="MAVLink connection URL of the vehicle")
parser.addoption("--speed-factor", type=float, default=None,
help="simulation speed factor, for information only")
def connected_tester(request: pytest.FixtureRequest, tester_class: Type[T]) -> Iterator[T]:
speed_factor: Optional[float] = request.config.getoption("speed_factor")
url: str = request.config.getoption("url")
tester = tester_class(speed_factor)
try:
tester.connect(url)
yield tester
finally:
failures = tester.close()
if failures:
pytest.fail("{} check{} failed in callbacks, first: {}".format(
len(failures), "s" if len(failures) > 1 else "", failures[0]))
@pytest.fixture
def tester(request: pytest.FixtureRequest) -> Iterator[AutopilotTester]:
"""A tester connected to the vehicle."""
yield from connected_tester(request, AutopilotTester)
@pytest.fixture
def tester_calibration(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterCalibration]:
"""A tester posing the SIH vehicle for sensor calibration, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterCalibration)
@pytest.fixture
def tester_rtl(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterRtl]:
"""A tester with rally point and geofence support, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterRtl)
@pytest.fixture
def tester_loiter(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterLoiter]:
"""A tester for fixed-wing loiter, reposition and figure eight, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterLoiter)
@pytest.fixture
def tester_flow(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterFlow]:
"""A tester for optical flow aided navigation, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterFlow)
@pytest.fixture
def tester_follow_me(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterFollowMe]:
"""A tester with follow-me support, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterFollowMe)
@pytest.fixture
def tester_vision_target(request: pytest.FixtureRequest) -> Iterator[AutopilotTesterVisionTarget]:
"""A tester simulating landing target sensors, connected to the vehicle."""
yield from connected_tester(request, AutopilotTesterVisionTarget)