Skip to content

feat(iwr6843): phone-assisted TI radar tilt calibration - #298

Merged
jewbetcha merged 6 commits into
open-flight:mainfrom
btripp:feat/phone-tilt-calibration
Oct 27, 2026
Merged

jewbetcha merged 6 commits into
open-flight:mainfrom
btripp:feat/phone-tilt-calibration

Conversation

@btripp

@btripp btripp commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Reviewing this PR

  • Start with src/openflight/phone_orientation.py (new, 178 lines): payload validation, gravity recomputation and atomic save/load.
  • Then src/openflight/server.py (+143/-2): the /api/calibration/iwr6843/orientation route and apply_iwr6843_orientation_calibration, one new entry in dispatch_phone_control_command, and _resolve_iwr_mount_tilt used by init_iwr6843.
  • Security decision to review: this is the first BLE command that writes persisted configuration. feat(ble): Bluetooth LE connection for phone apps #285 documented BLE as "read-and-select only". This PR keeps calibration on BLE and says so explicitly in docs/ios-ble.md → Security and scope and in the dispatcher docstring. If you'd rather keep BLE strictly read-and-select, the change is to drop one line from the handlers dict (and the loopback test) and leave HTTP only.
  • Independent of feat(phone): network shot stream, club API and reconnect catch-up #297 (network shot stream) and the mock hardware simulation PR. It touches neither.

What does this PR do?

Adds phone-assisted tilt calibration for the TI IWR6843 radar.

  • A phone sends one stable accelerometer measurement: the averaged gravity vector, its computed mount_tilt_deg / roll_deg, sample_count, tilt/roll standard deviations, measured_at, and an optional device_model. It goes over BLE as iwr6843_orientation_calibration or over HTTP as POST /api/calibration/iwr6843/orientation. GET on that route returns the current configured tilt, the azimuth offset, and the last phone calibration applied in this process. Both transports call the same server function.
  • The Pi recomputes tilt and roll from gravity and rejects the measurement with 400 if:
    • the submitted angles differ from the recomputed ones by more than 0.25°
    • |g| is outside 0.9–1.1
    • either standard deviation is above 0.5°
    • fewer than 30 samples were averaged
    • roll is above 3°
    • tilt is outside −30° to 45°
  • If the radar isn't enabled, the server returns 409.
  • If the enclosure LIS3DH is enabled, the server waits up to 2 s for a stable reading and subtracts the calibrated enclosure pitch, so the saved value stays TI-relative-to-enclosure. If the sensor is unstable, it returns 409 and changes nothing.
  • An accepted tilt:
    • takes effect immediately (the Calibration is replaced, not mutated)
    • is emitted on Socket.IO as iwr6843_orientation_calibrated
    • is logged as a config change with source ios_companion
    • is saved atomically to ~/.config/openflight/iwr6843_phone_orientation.json
  • At startup the tilt comes from --iwr6843-tilt-deg first, then the saved phone calibration, then the calibration JSON. The source is recorded as tilt_source in the IWR6843 runtime config. A corrupt saved file is ignored with a warning.
  • Docs:
    • docs/ios-ble.md: new "Calibrate TI radar tilt with the phone" section, a row in the commands table, and updated Security and scope.
    • The precedence note is added to docs/iwr6843/mounting.md, docs/build/inclinometer.md and docs/how-it-works/positioning.md.
    • Changelog entry.

Why was this required?

This is follow-up 3 from #285. Measuring the TI antenna-face tilt means holding a separate inclinometer against the board and typing the number into --iwr6843-tilt-deg. A wrong tilt doesn't raise an error. It quietly produces plausible but wrong launch angles. The phone's accelerometer is accurate enough for this and is already in the user's hand, so the companion app can measure, check stability and roll, and apply the tilt in one step.

This only covers pitch. It deliberately does not touch --iwr6843-azimuth-offset-deg: an accelerometer can't give yaw relative to the target line.

Automated tests

  • tests/test_phone_orientation_calibration.py (new, 9 test functions, one parametrized):
    • angles are recomputed from gravity
    • untrustworthy samples are rejected
    • 409 without the TI radar
    • apply → persist → session-log → tilt_source == "ios_companion", with the original Calibration left untouched
    • live enclosure pitch is subtracted
    • fails safe when the enclosure sensor is unstable (nothing persisted, tilt unchanged)
    • on restart the saved tilt is used unless the CLI flag overrides it
    • after a restart, GET still reports the saved calibration record, and reports null when --iwr6843-tilt-deg overrides it
  • tests/test_ble_loopback.py: test_calibration_without_iwr6843_returns_the_409_error. The real BLE dispatch path returns ok:false with TI IWR6843 radar is not enabled.
  • tests/test_server.py: the two init_iwr6843 tests in TestIWR6843ShotIntegration now point PHONE_ORIENTATION_CALIBRATION_PATH at a missing file in tmp_path. Without that, a real saved calibration on the dev machine would change their results.
  • No BLE golden changes. The command reuses the existing request/response envelope, and uv run python scripts/ble/generate_goldens.py --check passes.

Manual (human) testing

  • uv run pytest -q: 1817 passed, 10 skipped (after merging current main).
  • uv run ruff check src/openflight/: all checks passed. uv run ruff format --check on every touched Python file: already formatted.
  • uv run pylint src/openflight/ --fail-under=9: 9.60/10, with no messages on added lines.
  • Mock server smoke test: uv run openflight-server --mock --host 127.0.0.1 --web-port 8766, then:
    • GET /api/calibration/iwr6843/orientation → 409 {"error":"TI IWR6843 radar is not enabled"}
    • POST with a JSON body → 409 {"error":"TI IWR6843 radar is not enabled"}
    • No calibration file was written.
  • No IWR6843 or Pi was available for this PR, so the success path has not run on real hardware. To get as close as possible, I ran the real server process on macOS with only the radar's hardware layer faked:
    • Faked: IWR6843CaptureMonitor (serial/GPIO; does nothing), tx_order_from_config → "normal", and Calibration.load → identity with a 10.4° tilt. The calibration file path pointed at a temp dir. main() rejects --iwr6843 with --mock, so a small launcher called init_iwr6843(...) with the same arguments main() uses, then ran server.main() with --mock.
    • Real: init_iwr6843, tilt resolution, IWR6843Runtime, the Flask route, validation, atomic save/load, the session logger and the Socket.IO emit, all exercised over real HTTP and Socket.IO.
    • Results:
      • GET before calibrating → 200, tilt 10.4, calibration: null.
      • POST with a valid 12.25° measurement → 200 applied. The file was written, a following GET returned the record, the log showed Applied iOS phone calibration … configured TI tilt 12.250deg, and the session log recorded a config_change from ios_companion.
      • Invalid measurements → 400 with the expected messages (10 samples, stddev 0.9°, roll 4°, gravity/angle mismatch, non-object body). The saved file was unchanged (same sha256).
      • Restart with the saved file → tilt 12.25, tilt_source: ios_companion, and GET returns the saved record.
      • Restart with a CLI tilt of 8.0 → tilt 8.0, tilt_source: command_line, calibration: null, and the saved file untouched.
      • Exactly one iwr6843_orientation_calibrated Socket.IO event per successful POST, with a payload equal to the response; none for rejected ones.
  • Still owed: a calibration run with a real IWR6843 and the phone app on a Pi. I'll add the results here once the hardware is set up.

Checklist

  • Single feature/fix — phone-assisted IWR6843 tilt calibration only
  • Automated tests included — new or updated tests cover this change
  • Manual testing described — real server with a faked radar layer (above); on-hardware test still owed
  • Python tests pass (uv run pytest tests/ -v)
  • Pylint passes (uv run pylint src/openflight/ --fail-under=9)
  • Ruff passes (uv run ruff check src/openflight/)
  • UI builds (cd ui && npm run build) — no UI changes; not run
  • UI lint passes (cd ui && npm run lint) — no UI changes; not run
  • Updated docs or CHANGELOG if needed
  • No unrelated changes mixed in

🤖 Generated with Claude Code

btripp and others added 2 commits October 9, 2026 11:57
A phone can now measure the IWR6843 mount tilt with its accelerometer and
send it over BLE (`iwr6843_orientation_calibration`) or HTTP
(`POST /api/calibration/iwr6843/orientation`; `GET` reads the current tilt).
Both call one server function.

The Pi recomputes tilt and roll from the gravity vector and rejects
inconsistent, unstable, out-of-range or unlevel measurements. When the
enclosure LIS3DH is enabled it waits for a stable reading and subtracts the
calibrated enclosure pitch, failing safe with 409 if the sensor is unstable.
The tilt is applied immediately, broadcast on Socket.IO, logged as a config
change with source `ios_companion`, and saved atomically to
~/.config/openflight/iwr6843_phone_orientation.json.

At startup the TI tilt resolves as `--iwr6843-tilt-deg` > saved phone
calibration > calibration JSON, recorded as `tilt_source` in the runtime
config. An unreadable saved file is ignored with a warning.

This is the first BLE command that writes persisted configuration; the
BLE security docs now say so explicitly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On startup the saved phone tilt was applied, but the record itself was
dropped, so GET /api/calibration/iwr6843/orientation returned
"calibration": null until the phone recalibrated. Carry the record into
the runtime config and calibration meta when it supplies the tilt; a
--iwr6843-tilt override still reports null because the record is unused.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@btripp
btripp marked this pull request as ready for review October 12, 2026 17:56

@jewbetcha jewbetcha left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this, thanks @btripp

@jewbetcha
jewbetcha merged commit 1467b90 into open-flight:main Oct 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants