From cc6ae5900a7308fb79b49492811be06f0a1d0ee8 Mon Sep 17 00:00:00 2001 From: TinkeringDao <203040288+TinkeringDao@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:14:17 +0100 Subject: [PATCH] fix(m9): prevent TX sag and 20s publish hold from triggering false 3% shutoff - On HAS_THINKNODE_M9, only snap batteryMvSampled()'s EMA on hardware charger state transitions (GPIO1 5V_TEST / GPIO8 DONE#) rather than >70 mV voltage steps, so a transient IR drop during a +22 dBm LR1110 transmission is smoothed by the EMA instead of snapping downward. - Sample batteryMvSampled() directly in pagerLowBatteryTick() rather than batteryMvSmoothed()'s 20 s held value, since the 3-read (6 s) debounce plus 10 s countdown (16 s total) is shorter than the 20 s hold window and would otherwise repeat a single sagged reading. Signed-off-by: TinkeringDao <203040288+TinkeringDao@users.noreply.github.com> --- src/ui-touch/UITask.cpp | 19 ++++++++++++++++++- test/test_m9_board.cpp | 8 ++++++++ variants/thinknode_m9/M9Board.h | 14 +++++++------- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/ui-touch/UITask.cpp b/src/ui-touch/UITask.cpp index 2b6c0f23..90d60fd2 100644 --- a/src/ui-touch/UITask.cpp +++ b/src/ui-touch/UITask.cpp @@ -26136,11 +26136,24 @@ static uint16_t batteryMvSampled() { s_ema = (float)raw; // seed on first read } else { const float d = (float)raw - s_ema; +#if defined(HAS_THINKNODE_M9) + // On the M9, charger plug/unplug is read directly from GPIO1 (5V_TEST) and + // GPIO8 (DONE#), not inferred from voltage steps. Only snap s_ema when the + // charger state changes so a >70 mV IR sag during a +22 dBm LoRa TX burst + // is smoothed by the EMA instead of snapping downward to raw. + static int s_ext_prev = -2; + const int ext_now = ((board.externalPowerPresent() & 1) << 2) | (board.isCharging() & 3); + const bool charge_flip = (s_ext_prev != -2 && ext_now != s_ext_prev); + s_ext_prev = ext_now; + if (charge_flip) s_ema = (float)raw; + else s_ema += d * 0.15f; // ~7-sample time constant +#else // A charger plug/unplug steps the rail ~100 mV+ — snap straight to it so the // charge state is recognized within a tick instead of crawling there over // the EMA. Smaller deltas are ADC noise, so keep smoothing those. if (d > 70.0f || d < -70.0f) s_ema = (float)raw; else s_ema += d * 0.15f; // ~7-sample time constant +#endif } return (uint16_t)(s_ema + 0.5f); } @@ -51720,7 +51733,11 @@ static void pagerLowBatteryTick(uint32_t now) { #if defined(TLORA_PAGER) const int soc = board.getBattStateOfCharge(); #else - const uint16_t mv = batteryMvSmoothed(); + // Sample the live EMA directly rather than batteryMvSmoothed()'s 20 s held + // value: a 3-read (6 s) debounce + 10 s countdown is 16 s total, so reading + // a 20 s latch would repeat the same sample 8 times and could not see the + // rail recover after a transmit burst before the 16 s window expires. + const uint16_t mv = batteryMvSampled(); const int soc = mv ? batteryPercentFromMv(mv) : -1; #endif const bool on_usb = board.externalPowerPresent() != 0; // 1 = USB, -1 = charger silent: do nothing diff --git a/test/test_m9_board.cpp b/test/test_m9_board.cpp index 6672d9e7..68dbddb5 100644 --- a/test/test_m9_board.cpp +++ b/test/test_m9_board.cpp @@ -140,6 +140,14 @@ static void testChargerAndBattery() { assert(smoother.smooth(4100, 0, 0, 12000) == 4100); assert(smoother.smooth(4000, 0, 0, 14000) == 4100); assert(smoother.smooth(4000, 0, 0, 15000) == 4000); + + // Transient >70 mV LoRa TX sag while discharging on battery (ext=0, chg=0, + // no charger state flip) must be EMA-smoothed rather than snapping downward: + // at 3420 mV (~12%), a single +22 dBm TX sample sagging 180 mV to 3240 mV + // must stay well above the 3% switch-off threshold (3327 mV). + M9BatterySmoother tx_smoother; + assert(tx_smoother.smooth(3420, 0, 0, 1000) == 3420); + assert(tx_smoother.sample(3240) >= 3380); } static void testCompassHeading() { diff --git a/variants/thinknode_m9/M9Board.h b/variants/thinknode_m9/M9Board.h index 11313d50..841c1c84 100644 --- a/variants/thinknode_m9/M9Board.h +++ b/variants/thinknode_m9/M9Board.h @@ -53,29 +53,29 @@ struct M9BatterySmoother { unsigned long flip_ms = 0; int ext_prev = -2; - uint16_t sample(uint16_t raw) { + uint16_t sample(uint16_t raw, bool snap = false) { if (raw == 0) return 0; - if (ema < 1.0f) { + if (ema < 1.0f || snap) { ema = (float)raw; } else { const float d = (float)raw - ema; - if (d > 70.0f || d < -70.0f) ema = (float)raw; - else ema += d * 0.15f; + ema += d * 0.15f; } return (uint16_t)(ema + 0.5f); } uint16_t smooth(uint16_t raw, int ext_present, int chg_state, unsigned long now_ms) { - const uint16_t cur = sample(raw); - if (cur == 0) return 0; + if (raw == 0) return 0; const int ext_now = ((ext_present & 1) << 2) | (chg_state & 3); const bool charge_flip = (pub != 0) && ext_prev != -2 && ext_now != ext_prev; ext_prev = ext_now; + const bool settle_due = flip_ms && !charge_flip && (unsigned long)(now_ms - flip_ms) >= 3000UL; + const uint16_t cur = sample(raw, charge_flip || settle_due); if (pub == 0 || charge_flip || (unsigned long)(now_ms - pub_ms) >= 20000UL) { pub = cur; pub_ms = now_ms; flip_ms = charge_flip ? (now_ms ? now_ms : 1UL) : 0UL; - } else if (flip_ms && (unsigned long)(now_ms - flip_ms) >= 3000UL) { + } else if (settle_due) { pub = cur; pub_ms = now_ms; flip_ms = 0UL;