mirror of
https://github.com/ALLFATHER-BV/wadamesh.git
synced 2026-11-23 04:39:32 +00:00
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>
This commit is contained in:
3 files changed
+33
-8
No files matched your search
+18
-1
@@ -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
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user