Repository navigation
feat(battery): optional low-battery shutdown service - #321
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Adds an optional low-battery shutdown service for UPS builds.
scripts/battery/install_battery_guard.shinstallsopenflight-battery-guard.service, which runsscripts/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 runssystemctl poweroff. WithPOWER_OFF_ON_HALT=1from the Geekworm setup, the Pi then removes power./etc/default/openflight-battery-guard.--uninstallremoves the service./sys/class/power_supplydevices, the same ones the Geekworm overlays expose.docs/using/battery.mdalready 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
Automated tests
New
tests/test_battery_guard.pyruns the real script against a fakepower_supplytree, in dry-run mode with a 0 s interval:Restart=always. Both scripts passbash -n.Test run
On a Raspberry Pi 5 (Raspberry Pi OS Trixie, arm64) with this branch's
srconPYTHONPATH:The 13 failures are the camera tests (
test_camera_ball_flight,test_camera_club_delivery,test_camera_club_motion), allModuleNotFoundError: No module named 'cv2'. That Pi's venv doesn't have the optional camera extra, so they're unrelated to this change. No Python undersrc/changed.Manual (human) testing
On a Raspberry Pi 5 with a Geekworm X1202 and 4 × Akyga INR18650-35M (unprotected):
scripts/battery/install_battery_guard.sh. It wrote/etc/default/openflight-battery-guard, and the service isenabledandactive. The journal showsPowering off on battery at or below 10% or 3400000 uV (3 readings, 20s apart).charger/online=0, 93 %, 4.05 V). The service stayed quiet, correctly.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
systemctl poweroff. The docs describe how to test it by raising the limit and unplugging the UPS input.Checklist
tests/test_battery_guard.pyuv run pytest tests/ -v): all pass except the 13 OpenCV-dependent camera tests on a venv without the camera extrasrc/openflight/changessrc/openflight/changesui/changesui/changesdocs/build/battery.md(new "Low-Battery Shutdown" section),docs/using/battery.md, changelog🤖 Generated with Claude Code