Skip to content

feat(battery): optional low-battery shutdown service - #321

Merged
jewbetcha merged 1 commit into
open-flight:mainfrom
HuggeK:feat/low-battery-shutdown
Oct 30, 2026
Merged

jewbetcha merged 1 commit into
open-flight:mainfrom
HuggeK:feat/low-battery-shutdown

Conversation

@HuggeK

@HuggeK HuggeK commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds an optional low-battery shutdown service for UPS builds. scripts/battery/install_battery_guard.sh installs openflight-battery-guard.service, which runs scripts/battery/battery_guard.sh. When external power is offline and the battery is ≤ 10% or ≤ 3.40 V for three readings 20 s apart, it stops OpenFlight cleanly and runs systemctl poweroff. With POWER_OFF_ON_HALT=1 from the Geekworm setup, the Pi then removes power.

  • Never acts while external power is connected, or when the battery or charger state can't be read.
  • Limits are configurable in /etc/default/openflight-battery-guard. --uninstall removes the service.
  • Provider-agnostic: it reads the standard /sys/class/power_supply devices, the same ones the Geekworm overlays expose.
  • Separate from the read-only battery monitor: it's an independent service, as docs/using/battery.md already required of any shutdown policy. OpenFlight's in-app 20% / 10% warnings are unchanged.

Why was this required?

X1202/X1206 take unprotected 18650/21700 cells, and OpenFlight's battery warnings are informational and dismissible. A unit left running on battery, or a kiosk nobody is watching, drains the cells until the UPS browns out. That's an unclean shutdown for the Pi and a deep discharge for cells with no protection circuit. This gives builders an opt-in clean power-off well above the cells' cutoff.

Why these defaults
  • 10%: matches OpenFlight's existing "critical" warning level.
  • 3.40 V: a backstop for a MAX17040 state-of-charge model that hasn't learned the cells yet. It's well above typical 2.5–2.75 V cell cutoffs.
  • Three readings, 20 s apart: ignores a single sag under load, such as a radar starting, while still acting within about a minute.
  • Unknown external power or unreadable battery → do nothing: a missing driver must not power off a Pi that's plugged in and charging a flat pack at boot.

Automated tests

New tests/test_battery_guard.py runs the real script against a fake power_supply tree, in dry-run mode with a 0 s interval:

  • Low percentage on battery shuts down on exactly the 3rd reading.
  • Low voltage with a normal-looking percentage shuts down.
  • Low battery on external power does nothing.
  • A healthy battery just above both limits (11% / 3.41 V) does nothing.
  • Unreadable external power, or an unreadable battery, does nothing and logs why.
  • The installer's rendered unit runs the repo script with the config file and Restart=always. Both scripts pass bash -n.
Test run

On a Raspberry Pi 5 (Raspberry Pi OS Trixie, arm64) with this branch's src on PYTHONPATH:

tests/test_battery_guard.py + tests/test_geekworm_setup.py   18 passed
tests/                                                        2061 passed, 13 failed, 8 skipped

The 13 failures are the camera tests (test_camera_ball_flight, test_camera_club_delivery, test_camera_club_motion), all ModuleNotFoundError: No module named 'cv2'. That Pi's venv doesn't have the optional camera extra, so they're unrelated to this change. No Python under src/ changed.

Manual (human) testing

On a Raspberry Pi 5 with a Geekworm X1202 and 4 × Akyga INR18650-35M (unprotected):

  1. Ran scripts/battery/install_battery_guard.sh. It wrote /etc/default/openflight-battery-guard, and the service is enabled and active. The journal shows Powering off on battery at or below 10% or 3400000 uV (3 readings, 20s apart).
  2. The Pi was running on battery at the time (charger/online=0, 93 %, 4.05 V). The service stayed quiet, correctly.
  3. Ran the guard against the real sysfs with MIN_PERCENT=99, dry run, 2 s interval. Output: Low battery on battery power: 93% 4051250 uV (1/3) → (2/3) → (3/3) → Battery critically low; shutting down. → Dry run: would stop OpenFlight and power off. The installed service was unaffected.
Not exercised by hand
  • A real power-off. It wasn't triggered, so the device wouldn't be powered down remotely without the owner present. The final step is a plain systemctl poweroff. The docs describe how to test it by raising the limit and unplugging the UPS input.
  • Testing was carried out remotely over SSH by Claude Code on the PR author's Pi.

Checklist

  • Single feature/fix: the optional low-battery shutdown service
  • Automated tests included: new tests/test_battery_guard.py
  • Manual testing described: I documented what was verified by hand above
  • Python tests pass (uv run pytest tests/ -v): all pass except the 13 OpenCV-dependent camera tests on a venv without the camera extra
  • Pylint passes: no src/openflight/ changes
  • Ruff passes: no src/openflight/ changes
  • UI builds: no ui/ changes
  • UI lint passes: no ui/ changes
  • Updated docs or CHANGELOG if needed: docs/build/battery.md (new "Low-Battery Shutdown" section), docs/using/battery.md, changelog
  • No unrelated changes mixed in

🤖 Generated with Claude Code

The X1202/X1206 take unprotected cells and OpenFlight's 20%/10% warnings
are informational, so nothing stops a UPS build from draining the cells.

Add scripts/battery/battery_guard.sh, run as a systemd service by
scripts/battery/install_battery_guard.sh. When external power is offline
and the battery is at or below 10% or 3.40 V for three readings 20 s
apart, it stops OpenFlight and runs systemctl poweroff. It never acts on
external power or when the battery or charger state is unreadable.
Limits live in /etc/default/openflight-battery-guard.

It reads the standard /sys/class/power_supply devices, so it is
provider-agnostic, and stays a separate policy from the read-only
battery monitor as docs/using/battery.md requires.

Co-authored-by: HuggeK <[email protected]>

@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.

👌

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.

3 participants