From 35832b07b9faba9d2cd13431e16d6b874975c875 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 4 Jan 2023 19:57:33 +0100 Subject: [PATCH] UM Battery: basic support for LiPo cells * Lipo cells (1S) should not be discharged below 3V * LiPo cells have a different voltage/discharge rate curve --- usermods/Battery/battery_defaults.h | 7 ++++++- usermods/Battery/readme.md | 2 ++ usermods/Battery/usermod_v2_Battery.h | 27 +++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/usermods/Battery/battery_defaults.h b/usermods/Battery/battery_defaults.h index f1abe56c..c682cb45 100644 --- a/usermods/Battery/battery_defaults.h +++ b/usermods/Battery/battery_defaults.h @@ -18,7 +18,12 @@ // https://batterybro.com/blogs/18650-wholesale-battery-reviews/18852515-when-to-recycle-18650-batteries-and-how-to-start-a-collection-center-in-your-vape-shop // Discharge voltage: 2.5 volt + .1 for personal safety #ifndef USERMOD_BATTERY_MIN_VOLTAGE - #define USERMOD_BATTERY_MIN_VOLTAGE 2.6f + #ifdef USERMOD_BATTERY_USE_LIPO + // LiPo "1S" Batteries should not be dischared below 3V !! + #define USERMOD_BATTERY_MIN_VOLTAGE 3.2f + #else + #define USERMOD_BATTERY_MIN_VOLTAGE 2.6f + #endif #endif #ifndef USERMOD_BATTERY_MAX_VOLTAGE diff --git a/usermods/Battery/readme.md b/usermods/Battery/readme.md index ea346a74..d55573ab 100644 --- a/usermods/Battery/readme.md +++ b/usermods/Battery/readme.md @@ -36,6 +36,7 @@ define `USERMOD_BATTERY` in `wled00/my_config.h` | Name | Unit | Description | | ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- | | `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp | +| `USERMOD_BATTERY_USE_LIPO` | | define this (in `my_config.h`) if you use LiPo rechargeables (1S) | | `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO35 on ESP32 | | `USERMOD_BATTERY_MEASUREMENT_INTERVAL` | ms | battery check interval. defaults to 30 seconds | | `USERMOD_BATTERY_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) | @@ -81,6 +82,7 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3. 2023-01-04 +- basic support for LiPo rechargeable batteries ( `-D USERMOD_BATTERY_USE_LIPO`) - improved support for esp32 (read calibrated voltage) - corrected config saving (measurement pin, and battery min/max were lost) - various bugfixes diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index 7dbc4952..ac34a7e4 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -190,15 +190,30 @@ class UsermodBattery : public Usermod // calculate the voltage voltage = ((rawValue / getAdcPrecision()) * maxBatteryVoltage) + calibration; #endif - // check if voltage is within specified voltage range - voltage = ((voltagemaxBatteryVoltage)) ? -1.0f : voltage; + // check if voltage is within specified voltage range, allow 10% over/under voltage + voltage = ((voltage < minBatteryVoltage * 0.85f) || (voltage > maxBatteryVoltage * 1.1f)) ? -1.0f : voltage; // translate battery voltage into percentage /* the standard "map" function doesn't work https://www.arduino.cc/reference/en/language/functions/math/map/ notes and warnings at the bottom */ - batteryLevel = mapf(voltage, minBatteryVoltage, maxBatteryVoltage, 0, 100); + #ifdef USERMOD_BATTERY_USE_LIPO + batteryLevel = mapf(voltage, minBatteryVoltage, maxBatteryVoltage, 0, 100); // basic mapping + // LiPo batteries have a differnt dischargin curve, see + // https://blog.ampow.com/lipo-voltage-chart/ + if (batteryLevel < 40.0f) + batteryLevel = mapf(batteryLevel, 0, 40, 0, 12); // last 45% -> drops very quickly + else { + if (batteryLevel < 90.0f) + batteryLevel = mapf(batteryLevel, 40, 90, 12, 95); // 90% ... 40% -> almost linear drop + else // level > 90% + batteryLevel = mapf(batteryLevel, 90, 105, 95, 100); // highest 15% -> drop slowly + } + #else + batteryLevel = mapf(voltage, minBatteryVoltage, maxBatteryVoltage, 0, 100); + #endif + if (voltage > -1.0f) batteryLevel = constrain(batteryLevel, 0.0f, 110.0f); // if (calculateTimeLeftEnabled) { // float currentBatteryCapacity = totalBatteryCapacity; @@ -556,7 +571,11 @@ class UsermodBattery : public Usermod */ void setMaxBatteryVoltage(float voltage) { - maxBatteryVoltage = max(getMinBatteryVoltage()+1.0f, voltage); + #ifdef USERMOD_BATTERY_USE_LIPO + maxBatteryVoltage = max(getMinBatteryVoltage()+0.7f, voltage); + #else + maxBatteryVoltage = max(getMinBatteryVoltage()+1.0f, voltage); + #endif }