From 28e851c3fda30409ace7366c9ef6d997a8194329 Mon Sep 17 00:00:00 2001 From: claes Date: Sun, 19 Sep 2021 15:03:37 +0200 Subject: [PATCH] Low battery level counter for NRF52 Added a counter that counts low battery level detections. If there are 4 in a row we go to deep sleep. The battery sense on the RAK4631 seems to be a bit unstable and may generate 'false' low voltage readings. My RAK4631 has been running for 7 days now with this fix. It did 3 days without it. I still do not have a T-Echo so on that board IT IS NOT TESTED. (But I hope it will improve things there too) --- src/Power.cpp | 16 ++++++++++++++++ src/power.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/Power.cpp b/src/Power.cpp index 1fdefe29..4d2eca29 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -192,6 +192,7 @@ bool Power::setup() found = analogInit(); } enabled = found; + low_voltage_counter = 0; return found; } @@ -238,9 +239,24 @@ void Power::readPowerStatus() powerStatus.getIsCharging(), powerStatus.getBatteryVoltageMv(), powerStatus.getBatteryChargePercent()); newStatus.notifyObservers(&powerStatus); + + // If we have a battery at all and it is less than 10% full, force deep sleep if we have more than 3 low readings in a row + // Supect fluctuating voltage on the RAK4631 to force it to deep sleep even if battery is at 85% after only a few days + #ifdef NRF52_SERIES + if (powerStatus.getHasBattery() && !powerStatus.getHasUSB()){ + if (batteryLevel->getBattVoltage() < MIN_BAT_MILLIVOLTS){ + low_voltage_counter++; + if (low_voltage_counter>3) + powerFSM.trigger(EVENT_LOW_BATTERY); + } else { + low_voltage_counter = 0; + } + } + #else // If we have a battery at all and it is less than 10% full, force deep sleep if (powerStatus.getHasBattery() && !powerStatus.getHasUSB() && batteryLevel->getBattVoltage() < MIN_BAT_MILLIVOLTS) powerFSM.trigger(EVENT_LOW_BATTERY); + #endif } else { // No power sensing on this board - tell everyone else we have no idea what is happening const PowerStatus powerStatus = PowerStatus(OptUnknown, OptUnknown, OptUnknown, -1, -1); diff --git a/src/power.h b/src/power.h index 815e2267..8168e09c 100644 --- a/src/power.h +++ b/src/power.h @@ -37,6 +37,9 @@ class Power : private concurrency::OSThread /// Setup a simple ADC input based battery sensor bool analogInit(); + + private: + uint8_t low_voltage_counter; }; extern Power *power; \ No newline at end of file