fix(power): re-seed analog battery filter on charging-state change#11010
Open
truongsinh wants to merge 1 commit into
Open
fix(power): re-seed analog battery filter on charging-state change#11010truongsinh wants to merge 1 commit into
truongsinh wants to merge 1 commit into
Conversation
The analog battery voltage is intentionally smoothed (15-sample ADC average plus a 0.5-coefficient low-pass filter) to reject noise. But a charging-state transition steps the real voltage sharply (charge voltage vs loaded/rested voltage), and blending that step in means the reported voltage - and the SoC derived from it via the OCV table - keeps tracking the previous power state for several 20 s polls (roughly one to two minutes) after plugging or unplugging external power. Detect the charging-state edge in readPowerStatus() (after all detection variants, including NRF_APM, have resolved) and let the battery-level sensor re-seed its filter with an immediate fresh reading, so telemetry sent shortly after a plug/unplug reflects the new state within one poll.
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Problem
The analog battery voltage is intentionally smoothed (15-sample ADC average + a 0.5-coefficient low-pass filter in
AnalogBatteryLevel::getBattVoltage()) to reject noise. A charging-state transition, however, steps the real voltage sharply (charge voltage vs loaded/rested voltage), and the filter blends that step in by halves perPower::runOncepoll (20 s) — so the reported voltage, and the SoC derived from it via the OCV table, keep tracking the previous power state for roughly 1–2 minutes after plugging or unplugging external power. Telemetry sent (or requested) shortly after a power change reports these stale values.Fix
Power::readPowerStatus()tracks the resolved charging state across polls (checked after all detection variants, including theNRF_APMUSB-regulator path) and, on a transition, calls a newHasBatteryLevel::notifyChargingStateChanged()hook, then takes an immediate fresh voltage/percent reading.AnalogBatteryLevelimplements the hook by bypassing the 5 s read throttle and replacing the filtered value with the next raw reading instead of blending it.No behavior change when the charging state is steady (the filter path is identical), and battery sensors with their own gauge (PMU/fuel-gauge boards) are unaffected — the base-class hook is a no-op.
Testing
Tested on two Seeed T1000-E tracker cards (one flashed with this patch on the v2.7.26 base, plus a build from current master), with external power switched by a BLE-controlled inline USB power switch so the disconnect moment is timestamped:
battery_level=101, voltage ≈ 4.02 V (charge voltage) — unchanged.🤝 Attestations
I don't have the other listed devices; the touched code paths are shared (
Power.cpp), with the analog re-seed only reachable on boards usingAnalogBatteryLeveland only on a charging-state edge.