From b8c61b52366a567d57860ada080c30ced9725777 Mon Sep 17 00:00:00 2001 From: Maximilian Mewes Date: Sat, 9 Sep 2023 21:01:55 +0200 Subject: [PATCH] =?UTF-8?q?Move=20battery=20types=20to=20a=20separate=20fo?= =?UTF-8?q?lder=20=F0=9F=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usermods/Battery/battery.h | 17 +---- usermods/Battery/battery_defaults.h | 16 +--- usermods/Battery/{ => types}/lion.h | 6 +- usermods/Battery/{ => types}/lipo.h | 14 ++-- usermods/Battery/{ => types}/unkown.h | 6 +- usermods/Battery/usermod_v2_Battery.h | 105 ++++++-------------------- 6 files changed, 37 insertions(+), 127 deletions(-) rename usermods/Battery/{ => types}/lion.h (87%) rename usermods/Battery/{ => types}/lipo.h (82%) rename usermods/Battery/{ => types}/unkown.h (83%) diff --git a/usermods/Battery/battery.h b/usermods/Battery/battery.h index 3a792aadb..8a8042ff3 100644 --- a/usermods/Battery/battery.h +++ b/usermods/Battery/battery.h @@ -14,7 +14,6 @@ class Battery protected: float minVoltage; float maxVoltage; - unsigned int capacity; float voltage; int8_t level = 100; float calibration; // offset or calibration value to fine tune the calculated voltage @@ -34,7 +33,7 @@ class Battery /** * Corresponding battery curves - * calculates the capacity in % (0-100) with given voltage and possible voltage range + * calculates the level in % (0-100) with given voltage and possible voltage range */ virtual float mapVoltage(float v, float min, float max) = 0; // { @@ -86,20 +85,6 @@ class Battery this->maxVoltage = max(getMinVoltage()+.5f, voltage); } - /* - * Get the capacity of all cells in parralel sumed up - * unit: mAh - */ - unsigned int getCapacity() - { - return this->capacity; - } - - void setCapacity(unsigned int capacity) - { - this->capacity = capacity; - } - float getVoltage() { return this->voltage; diff --git a/usermods/Battery/battery_defaults.h b/usermods/Battery/battery_defaults.h index 668009680..199ee3432 100644 --- a/usermods/Battery/battery_defaults.h +++ b/usermods/Battery/battery_defaults.h @@ -40,9 +40,6 @@ #ifndef USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE #define USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE 4.2f #endif -#ifndef USERMOD_BATTERY_UNKOWN_CAPACITY - #define USERMOD_BATTERY_UNKOWN_CAPACITY 2500 -#endif #ifndef USERMOD_BATTERY_UNKOWN_CALIBRATION // offset or calibration value to fine tune the calculated voltage #define USERMOD_BATTERY_UNKOWN_CALIBRATION 0 @@ -59,9 +56,6 @@ #ifndef USERMOD_BATTERY_LIPO_MAX_VOLTAGE #define USERMOD_BATTERY_LIPO_MAX_VOLTAGE 4.2f #endif -#ifndef USERMOD_BATTERY_LIPO_CAPACITY - #define USERMOD_BATTERY_LIPO_CAPACITY 5000 -#endif #ifndef USERMOD_BATTERY_LIPO_CALIBRATION #define USERMOD_BATTERY_LIPO_CALIBRATION 0 #endif @@ -77,10 +71,6 @@ #ifndef USERMOD_BATTERY_LION_MAX_VOLTAGE #define USERMOD_BATTERY_LION_MAX_VOLTAGE 4.2f #endif -#ifndef USERMOD_BATTERY_LION_CAPACITY - // a common capacity for single 18650 battery cells is between 2500 and 3600 mAh - #define USERMOD_BATTERY_LION_CAPACITY 3100 -#endif #ifndef USERMOD_BATTERY_LION_CALIBRATION // offset or calibration value to fine tune the calculated voltage #define USERMOD_BATTERY_LION_CALIBRATION 0 @@ -109,11 +99,6 @@ #define USERMOD_BATTERY_CALIBRATION 0 #endif -// calculate remaining time / the time that is left before the battery runs out of power -// #ifndef USERMOD_BATTERY_CALCULATE_TIME_LEFT_ENABLED -// #define USERMOD_BATTERY_CALCULATE_TIME_LEFT_ENABLED false -// #endif - // auto-off feature #ifndef USERMOD_BATTERY_AUTO_OFF_ENABLED #define USERMOD_BATTERY_AUTO_OFF_ENABLED true @@ -157,6 +142,7 @@ typedef struct bconfig_t float voltage; // current voltage int8_t level; // current level float calibration; // offset or calibration value to fine tune the calculated voltage + float voltageMultiplier; } batteryConfig; diff --git a/usermods/Battery/lion.h b/usermods/Battery/types/lion.h similarity index 87% rename from usermods/Battery/lion.h rename to usermods/Battery/types/lion.h index 17a4b3593..2ff54a1ea 100644 --- a/usermods/Battery/lion.h +++ b/usermods/Battery/types/lion.h @@ -1,8 +1,8 @@ #ifndef UMBLion_h #define UMBLion_h -#include "battery_defaults.h" -#include "battery.h" +#include "../battery_defaults.h" +#include "../battery.h" /** * Lion Battery @@ -17,7 +17,6 @@ class Lion : public Battery { this->setMinVoltage(USERMOD_BATTERY_LION_MIN_VOLTAGE); this->setMaxVoltage(USERMOD_BATTERY_LION_MAX_VOLTAGE); - this->setCapacity(USERMOD_BATTERY_LION_CAPACITY); this->setVoltage(this->getVoltage()); this->setCalibration(USERMOD_BATTERY_LION_CALIBRATION); } @@ -26,7 +25,6 @@ class Lion : public Battery { if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); - if(cfg.calibration) this->setCapacity(cfg.calibration); if(cfg.level) this->setLevel(cfg.level); if(cfg.calibration) this->setCalibration(cfg.calibration); } diff --git a/usermods/Battery/lipo.h b/usermods/Battery/types/lipo.h similarity index 82% rename from usermods/Battery/lipo.h rename to usermods/Battery/types/lipo.h index dcd44567f..264d3251e 100644 --- a/usermods/Battery/lipo.h +++ b/usermods/Battery/types/lipo.h @@ -1,8 +1,8 @@ #ifndef UMBLipo_h #define UMBLipo_h -#include "battery_defaults.h" -#include "battery.h" +#include "../battery_defaults.h" +#include "../battery.h" /** * Lipo Battery @@ -17,7 +17,6 @@ class Lipo : public Battery { this->setMinVoltage(USERMOD_BATTERY_LIPO_MIN_VOLTAGE); this->setMaxVoltage(USERMOD_BATTERY_LIPO_MAX_VOLTAGE); - this->setCapacity(USERMOD_BATTERY_LIPO_CAPACITY); this->setVoltage(this->getVoltage()); this->setCalibration(USERMOD_BATTERY_LIPO_CALIBRATION); } @@ -26,13 +25,12 @@ class Lipo : public Battery { if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); - if(cfg.calibration) this->setCapacity(cfg.calibration); if(cfg.level) this->setLevel(cfg.level); if(cfg.calibration) this->setCalibration(cfg.calibration); } /** - * LiPo batteries have a differnt dischargin curve, see + * LiPo batteries have a differnt discharge curve, see * https://blog.ampow.com/lipo-voltage-chart/ */ float mapVoltage(float v, float min, float max) override @@ -41,12 +39,12 @@ class Lipo : public Battery lvl = this->linearMapping(v, min, max); // basic mapping if (lvl < 40.0f) - lvl = this->linearMapping(lvl, 0, 40, 0, 12); // last 45% -> drops very quickly + lvl = this->linearMapping(lvl, 0, 40, 0, 12); // last 45% -> drops very quickly else { if (lvl < 90.0f) - lvl = this->linearMapping(lvl, 40, 90, 12, 95); // 90% ... 40% -> almost linear drop + lvl = this->linearMapping(lvl, 40, 90, 12, 95); // 90% ... 40% -> almost linear drop else // level > 90% - lvl = this->linearMapping(lvl, 90, 105, 95, 100); // highest 15% -> drop slowly + lvl = this->linearMapping(lvl, 90, 105, 95, 100); // highest 15% -> drop slowly } return lvl; diff --git a/usermods/Battery/unkown.h b/usermods/Battery/types/unkown.h similarity index 83% rename from usermods/Battery/unkown.h rename to usermods/Battery/types/unkown.h index f36c3195e..2b38da96c 100644 --- a/usermods/Battery/unkown.h +++ b/usermods/Battery/types/unkown.h @@ -1,8 +1,8 @@ #ifndef UMBUnkown_h #define UMBUnkown_h -#include "battery_defaults.h" -#include "battery.h" +#include "../battery_defaults.h" +#include "../battery.h" /** * Lion Battery @@ -17,7 +17,6 @@ class Unkown : public Battery { this->setMinVoltage(USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE); this->setMaxVoltage(USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE); - this->setCapacity(USERMOD_BATTERY_UNKOWN_CAPACITY); this->setVoltage(this->getVoltage()); this->setCalibration(USERMOD_BATTERY_UNKOWN_CALIBRATION); } @@ -26,7 +25,6 @@ class Unkown : public Battery { if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage); else this->setMinVoltage(USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE); if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage); else this->setMaxVoltage(USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE); - if(cfg.calibration) this->setCapacity(cfg.calibration); else this->setCapacity(USERMOD_BATTERY_UNKOWN_CAPACITY); if(cfg.calibration) this->setCalibration(cfg.calibration); else this->setCalibration(USERMOD_BATTERY_UNKOWN_CALIBRATION); } diff --git a/usermods/Battery/usermod_v2_Battery.h b/usermods/Battery/usermod_v2_Battery.h index 7f6738e10..a91331cb7 100644 --- a/usermods/Battery/usermod_v2_Battery.h +++ b/usermods/Battery/usermod_v2_Battery.h @@ -3,9 +3,9 @@ #include "wled.h" #include "battery_defaults.h" #include "battery.h" -#include "unkown.h" -#include "lion.h" -#include "lipo.h" +#include "types/unkown.h" +#include "types/lion.h" +#include "types/lipo.h" /* * Usermod by Maximilian Mewes @@ -28,7 +28,7 @@ class UsermodBattery : public Usermod unsigned long nextReadTime = 0; unsigned long lastReadTime = 0; // battery min. voltage - float minBatteryVoltage = USERMOD_BATTERY_MIN_VOLTAGE; + float minBatteryVoltage = 3.3f; // battery max. voltage float maxBatteryVoltage = USERMOD_BATTERY_MAX_VOLTAGE; // all battery cells summed up @@ -39,16 +39,10 @@ class UsermodBattery : public Usermod float voltage = maxBatteryVoltage; // between 0 and 1, to control strength of voltage smoothing filter float alpha = 0.05f; - // multiplier for the voltage divider that is in place between ADC pin and battery, default will be 2 but might be adapted to readout voltages over ~5v ESP32 or ~6.6v ESP8266 - float voltageMultiplier = USERMOD_BATTERY_VOLTAGE_MULTIPLIER; // mapped battery level based on voltage int8_t batteryLevel = 100; // offset or calibration value to fine tune the calculated voltage float calibration = USERMOD_BATTERY_CALIBRATION; - - // time left estimation feature - // bool calculateTimeLeftEnabled = USERMOD_BATTERY_CALCULATE_TIME_LEFT_ENABLED; - // float estimatedTimeLeft = 0.0; // auto shutdown/shutoff/master off feature bool autoOffEnabled = USERMOD_BATTERY_AUTO_OFF_ENABLED; @@ -114,18 +108,22 @@ class UsermodBattery : public Usermod } } - float readVoltage() - { - #ifdef ARDUINO_ARCH_ESP32 - // use calibrated millivolts analogread on esp32 (150 mV ~ 2450 mV default attentuation) and divide by 1000 to get from milivolts to volts and multiply by voltage multiplier and apply calibration value - return (analogReadMilliVolts(batteryPin) / 1000.0f) * voltageMultiplier + calibration; - #else - // use analog read on esp8266 ( 0V ~ 1V no attenuation options) and divide by ADC precision 1023 and multiply by voltage multiplier and apply calibration value - return (analogRead(batteryPin) / 1023.0f) * voltageMultiplier + calibration; - #endif - } + // float readVoltage() + // { + // #ifdef ARDUINO_ARCH_ESP32 + // // use calibrated millivolts analogread on esp32 (150 mV ~ 2450 mV default attentuation) and divide by 1000 to get from milivolts to volts and multiply by voltage multiplier and apply calibration value + // return (analogReadMilliVolts(batteryPin) / 1000.0f) * voltageMultiplier + calibration; + // #else + // // use analog read on esp8266 ( 0V ~ 1V no attenuation options) and divide by ADC precision 1023 and multiply by voltage multiplier and apply calibration value + // return (analogRead(batteryPin) / 1023.0f) * voltageMultiplier + calibration; + // #endif + // } public: + UsermodBattery() + { + bat = new Unkown(); + } //Functions called by WLED /* @@ -152,14 +150,13 @@ class UsermodBattery : public Usermod } #else //ESP8266 boards have only one analog input pin A0 pinMode(batteryPin, INPUT); - voltage = readVoltage(); + // voltage = readVoltage(); #endif //this could also be handled with a factory class but for only 2 types it should be sufficient for now if(bcfg.type == (batteryType)lipo) { bat = new Lipo(); - } else - if(bcfg.type == (batteryType)lion) { + } else if(bcfg.type == (batteryType)lion) { bat = new Lion(); } else { bat = new Unkown(); // nullObject pattern @@ -218,14 +215,8 @@ class UsermodBattery : public Usermod // calculate the voltage voltage = ((rawValue / getAdcPrecision()) * bat->getMaxVoltage()) + bat->getCalibration(); #endif - // initializing = false; - - // rawValue = readVoltage(); - // // filter with exponential smoothing because ADC in esp32 is fluctuating too much for a good single readout - // voltage = voltage + alpha * (rawValue - voltage); - - // check if voltage is within specified voltage range, allow 10% over/under voltage - removed cause this just makes it hard for people to troubleshoot as the voltage in the web gui will say invalid instead of displaying a voltage - //voltage = ((voltage < minBatteryVoltage * 0.85f) || (voltage > maxBatteryVoltage * 1.1f)) ? -1.0f : voltage; + // filter with exponential smoothing because ADC in esp32 is fluctuating too much for a good single readout + voltage = voltage + alpha * (rawValue - voltage); bat->setVoltage(voltage); // translate battery voltage into percentage @@ -298,7 +289,6 @@ class UsermodBattery : public Usermod if(forJsonState) { battery[F("type")] = bcfg.type; } else {battery[F("type")] = (String)bcfg.type; } // has to be a String otherwise it won't get converted to a Dropdown battery[F("min-voltage")] = bat->getMinVoltage(); battery[F("max-voltage")] = bat->getMaxVoltage(); - battery[F("capacity")] = bat->getCapacity(); battery[F("calibration")] = bat->getCalibration(); battery[FPSTR(_readInterval)] = readingInterval; @@ -318,8 +308,8 @@ class UsermodBattery : public Usermod getJsonValue(battery[F("type")], bcfg.type); getJsonValue(battery[F("min-voltage")], bcfg.minVoltage); getJsonValue(battery[F("max-voltage")], bcfg.maxVoltage); - getJsonValue(battery[F("capacity")], bcfg.capacity); getJsonValue(battery[F("calibration")], bcfg.calibration); + setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); JsonObject ao = battery[F("auto-off")]; @@ -421,26 +411,18 @@ class UsermodBattery : public Usermod #ifdef ARDUINO_ARCH_ESP32 battery[F("pin")] = batteryPin; #endif - - // battery[F("time-left")] = calculateTimeLeftEnabled; - battery[F("min-voltage")] = minBatteryVoltage; - battery[F("max-voltage")] = maxBatteryVoltage; - battery[F("capacity")] = totalBatteryCapacity; - battery[F("calibration")] = calibration; - battery[F("voltage-multiplier")] = voltageMultiplier; - battery[FPSTR(_readInterval)] = readingInterval; addBatteryToJsonObject(battery, false); // read voltage in case calibration or voltage multiplier changed to see immediate effect - voltage = readVoltage(); + // voltage = readVoltage(); DEBUG_PRINTLN(F("Battery config saved.")); } void appendConfigData() { - // Total: 501 Bytes + // Total: 462 Bytes oappend(SET_F("td=addDropdown('Battery', 'type');")); // 35 Bytes oappend(SET_F("addOption(td, 'Unkown', '0');")); // 30 Bytes oappend(SET_F("addOption(td, 'LiPo', '1');")); // 28 Bytes @@ -448,7 +430,6 @@ class UsermodBattery : public Usermod oappend(SET_F("addInfo('Battery:type',1,'requires reboot');")); // 81 Bytes oappend(SET_F("addInfo('Battery:min-voltage', 1, 'v');")); // 40 Bytes oappend(SET_F("addInfo('Battery:max-voltage', 1, 'v');")); // 40 Bytes - oappend(SET_F("addInfo('Battery:capacity', 1, 'mAh');")); // 39 Bytes oappend(SET_F("addInfo('Battery:interval', 1, 'ms');")); // 38 Bytes oappend(SET_F("addInfo('Battery:auto-off:threshold', 1, '%');")); // 47 Bytes oappend(SET_F("addInfo('Battery:indicator:threshold', 1, '%');")); // 48 Bytes @@ -503,9 +484,7 @@ class UsermodBattery : public Usermod // calculateTimeLeftEnabled = battery[F("time-left")] | calculateTimeLeftEnabled; setMinBatteryVoltage(battery[F("min-voltage")] | minBatteryVoltage); setMaxBatteryVoltage(battery[F("max-voltage")] | maxBatteryVoltage); - setTotalBatteryCapacity(battery[F("capacity")] | totalBatteryCapacity); setCalibration(battery[F("calibration")] | calibration); - setVoltageMultiplier(battery[F("voltage-multiplier")] | voltageMultiplier); setReadingInterval(battery[FPSTR(_readInterval)] | readingInterval); getUsermodConfigFromJsonObject(battery); @@ -655,22 +634,6 @@ class UsermodBattery : public Usermod } - /* - * Get the capacity of all cells in parralel sumed up - * unit: mAh - */ - unsigned int getTotalBatteryCapacity() - { - return totalBatteryCapacity; - } - - void setTotalBatteryCapacity(unsigned int capacity) - { - totalBatteryCapacity = capacity; - } - - - /* * Get the calculated voltage * formula: (adc pin value / adc precision * max voltage) + calibration @@ -707,24 +670,6 @@ class UsermodBattery : public Usermod calibration = offset; } - /* - * Set the voltage multiplier value - * A multiplier that may need adjusting for different voltage divider setups - */ - void setVoltageMultiplier(float multiplier) - { - voltageMultiplier = multiplier; - } - - /* - * Get the voltage multiplier value - * A multiplier that may need adjusting for different voltage divider setups - */ - float getVoltageMultiplier() - { - return voltageMultiplier; - } - /* * Get auto-off feature enabled status * is auto-off enabled, true/false