Battery estimate: scale the sanity cap to current SoC — fixes "9% = 3 days" (#99)#118
Merged
Conversation
… days" (#99) The runtime estimate (currentSoC ÷ discharge-rate) was clamped only by a FLAT `ratedHours × 1.5`, which ignored how much charge was actually left. So a too-slow measured slope at low charge (idle/off-wrist spans, sparse 5/MG SoC readings) extrapolated 9% to ~3 days — more than a full 12-day MG charge. Scale the cap to the current SoC: `ratedHours × 1.5 × (current/100)`. At 9% on an MG that bounds to ~1.6 days instead of 3+, while healthy mid-range and rated-fallback estimates are unchanged (a rated estimate = ratedHours×(current/100), always ≤ the new cap). Both platforms (Swift + Kotlin twin). Tests: the existing clamp test updated (90% W4 → 145.8h, was 162), plus a new low-SoC case (25%→9% slow drain → 38.88h, not ~56h). Android unit tests green; iOS is the byte-identical twin.
|
Sweet!! |
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.
Fixes #99 — on a WHOOP MG, 9% battery showed "~3 days" remaining.
Cause
BatteryEstimator.estimatecomputesremaining = currentSoC ÷ discharge-rate, clamped only by a flatratedHours × 1.5— which ignores current SoC. A too-slow measured slope at low charge (idle/off-wrist spans, or sparse 5/MG SoC readings → a tiny %/h) extrapolated 9% to ~3 days = more than a full 12-day MG charge (ratedLifeHoursWhoop5 = 288). The flat cap (432h for an MG) never caught it. Discharge is also non-linear (faster near empty), which a straight-line fit misses exactly at low SoC.Fix
Scale the sanity cap to the current SoC:
ratedHours × 1.5 × (current/100). You can't realistically stretch the rated per-% runtime past ~1.5×, so this bounds the estimate to a physically-plausible fraction of the rated life. At 9% on an MG → ~1.6 days instead of 3+, while:ratedHours×(current/100), always ≤ the new cap);Applied identically on both platforms (Swift + Kotlin twin).
Re-review + testing
Checked against all existing cases: only the one
clamps…test changes (90% W4: 162 → 145.8h — its exact premise being refined), and the trace tests don't assert the clamp value. Added a new low-SoC test pinning the report (25%→9% slow drain → 38.88h, not ~56h). Android unit tests green locally; iOS is the byte-identical twin (same fixtures) — a testing build verifies the Swift compiles.On royabby365's question (in-thread)
No new battery-diagnostic test needed — the existing Test Centre → Battery panel already exposes the SoC series, fitted slope, and slope source for self-diagnosis. This targeted clamp fixes the reported case without new UI.
Files:
BatteryEstimator.swift/.kt(+ both test files).