From 9dc4ff7d4ef86af29c985b8865636dd49368d4fd Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Mon, 28 Feb 2022 00:26:17 +0100 Subject: [PATCH] Reworked battery curves --- main/battery.cpp | 178 +++++++++++++++++++++++++---------------------- main/battery.h | 116 +++++++++++++++++++++++++++++- 2 files changed, 207 insertions(+), 87 deletions(-) diff --git a/main/battery.cpp b/main/battery.cpp index 9ed64d4..7bd2f33 100644 --- a/main/battery.cpp +++ b/main/battery.cpp @@ -9,10 +9,6 @@ #include "globals.h" #include "newsettings.h" -#define CURVE(higherVoltage,lowerVoltage,fromAh,toAh) \ -if (cellVoltage >= lowerVoltage && cellVoltage <= higherVoltage) \ - return 100 * (expected_ah - cpputils::mapValue(cellVoltage, higherVoltage, lowerVoltage, fromAh, toAh)) / expected_ah; - float getBatteryPercentage(float batVoltage, BatteryCellType cellType) { const float cellVoltage = batVoltage / configs.battery.cellsSeries.value; @@ -21,116 +17,52 @@ float getBatteryPercentage(float batVoltage, BatteryCellType cellType) { case BatteryCellType::_22P: { - const float expected_ah = 2.2; + const float expected_ah = BAT_MIN_AH_22P; if (cellVoltage > 4.15) return 100; - - CURVE(4.15, 4.04, 0, 0.25) - CURVE(4.04, 3.95, 0.25, 0.5) - CURVE(3.95, 3.86, 0.5, 0.75) - CURVE(3.86, 3.74, 0.75, 1.0) - CURVE(3.74, 3.64, 1.0, 1.25) - CURVE(3.64, 3.59, 1.25, 1.5) - CURVE(3.59, 3.54, 1.5, 1.75) - CURVE(3.54, 3.43, 1.75, 2.0) - CURVE(3.43, 3.35, 2.0, 2.1) - CURVE(3.35, 2.50, 2.1, 2.2) + BAT_CURVE_22P(PERCENTAGE); break; } case BatteryCellType::MH1: { - const float expected_ah = 3.2; + const float expected_ah = BAT_MIN_AH_MH1; if (cellVoltage > 4.15) return 100; - - CURVE(4.15, 4.09, 0, 0.25) - CURVE(4.09, 4.04, 0.25, 0.5) - CURVE(4.04, 3.95, 0.5, 0.75) - CURVE(3.95, 3.88, 0.75, 1.0) - CURVE(3.88, 3.79, 1.0, 1.25) - CURVE(3.79, 3.70, 1.25, 1.5) - CURVE(3.70, 3.65, 1.5, 1.75) - CURVE(3.65, 3.60, 1.75, 2.0) - CURVE(3.60, 3.56, 2.0, 2.25) - CURVE(3.56, 3.50, 2.25, 2.5) - CURVE(3.50, 3.40, 2.5, 2.75) - CURVE(3.40, 3.30, 2.75, 3.0) - CURVE(3.30, 2.5, 3.0, 3.2) + BAT_CURVE_MH1(PERCENTAGE); break; } case BatteryCellType::HG2: { - const float expected_ah = 3.0; + const float expected_ah = BAT_MIN_AH_HG2; if (cellVoltage > 4.15) return 100; - - CURVE(4.15, 4.08, 0, 0.25) - CURVE(4.08, 4.01, 0.25, 0.5) - CURVE(4.01, 3.92, 0.5, 0.75) - CURVE(3.92, 3.84, 0.75, 1.0) - CURVE(3.84, 3.75, 1.0, 1.25) - CURVE(3.75, 3.67, 1.25, 1.5) - CURVE(3.67, 3.62, 1.5, 1.75) - CURVE(3.62, 3.55, 1.75, 2.0) - CURVE(3.55, 3.44, 2.0, 2.25) - CURVE(3.44, 3.30, 2.25, 2.5) - CURVE(3.30, 3.05, 2.5, 2.75) - CURVE(3.05, 2.50, 2.75, 3.0) + BAT_CURVE_HG2(PERCENTAGE); break; } case BatteryCellType::VTC5: { - const float expected_ah = 2.6; + const float expected_ah = BAT_MIN_AH_VTC5; if (cellVoltage > 4.15) return 100; - - CURVE(4.15, 4.08, 0, 0.25) - CURVE(4.08, 3.98, 0.25, 0.5) - CURVE(3.98, 3.89, 0.5, 0.75) - CURVE(3.89, 3.79, 0.75, 1.0) - CURVE(3.79, 3.71, 1.0, 1.25) - CURVE(3.71, 3.64, 1.25, 1.5) - CURVE(3.64, 3.53, 1.5, 1.75) - CURVE(3.53, 3.44, 1.75, 2.0) - CURVE(3.44, 3.20, 2.0, 2.25) - CURVE(3.20, 2.80, 2.25, 2.5) - CURVE(2.80, 2.50, 2.5, 2.60) + BAT_CURVE_VTC5(PERCENTAGE); break; } case BatteryCellType::BAK_25R: { - const float expected_ah = 2.5; + const float expected_ah = BAT_MIN_AH_BAK_25R; if(cellVoltage > 4.15){ return 100; } - CURVE(4.15, 4.06, 0, 0.25) - CURVE(4.06, 3.96, 0.25, 0.5) - CURVE(3.96, 3.88, 0.5, 0.75) - CURVE(3.88, 3.77, 0.75, 1) - CURVE(3.77, 3.68, 1, 1.25) - CURVE(3.68, 3.62, 1.25, 1.5) - CURVE(3.62, 3.56, 1.5, 1.75) - CURVE(3.56, 3.47, 1.75, 2) - CURVE(3.47, 3.31, 2, 2.25) - CURVE(3.31, 2.50, 2.25, 2.5) + BAT_CURVE_25R(PERCENTAGE); break; } case BatteryCellType::HE4: { - const float expected_ah = 2.3; + const float expected_ah = BAT_MIN_AH_HE4; if(cellVoltage > 4.15){ return 100; } - CURVE(4.15, 4.02, 0, 0.25) - CURVE(4.02, 3.91, 0.25, 0.5) - CURVE(3.91, 3.81, 0.5, 0.75) - CURVE(3.81, 3.72, 0.75, 1) - CURVE(3.72, 3.61, 1, 1.25) - CURVE(3.61, 3.62, 1.25, 1.5) - CURVE(3.62, 3.53, 1.5, 1.75) - CURVE(3.53, 3.45, 1.75, 2) - CURVE(3.45, 3.21, 2, 2.25) - CURVE(3.21, 2.80, 2.25, 2.3) + BAT_CURVE_HE4(PERCENTAGE); break; } } @@ -194,11 +126,6 @@ std::string getBatteryRemainingWattHoursString() return fmt::format("{:.1f}Wh", getRemainingWattHours()); } -std::string getBatteryCellTypeString() -{ - return fmt::format("Cells: {}", toString(BatteryCellType(configs.battery.cellType.value))); -} - std::string getRemainingRangeString() { return fmt::format("{:.1f} km", getRemainingWattHours() / configs.battery.watthoursPerKilometer.value); @@ -213,6 +140,87 @@ std::string getBatteryDebugString() return "No Battery"; } +float getMinBatVoltage(BatteryCellType cellType) { + switch (cellType) + { + case BatteryCellType::_22P: + { + const float expected_ah = BAT_MIN_AH_22P; + BAT_CURVE_22P(GET_MINIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::HG2: + { + const float expected_ah = BAT_MIN_AH_HG2; + BAT_CURVE_HG2(GET_MINIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::MH1: + { + const float expected_ah = BAT_MIN_AH_MH1; + BAT_CURVE_MH1(GET_MINIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::VTC5: + { + const float expected_ah = BAT_MIN_AH_VTC5; + BAT_CURVE_VTC5(GET_MINIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::BAK_25R: + { + const float expected_ah = BAT_MIN_AH_BAK_25R; + BAT_CURVE_25R(GET_MINIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::HE4: + { + const float expected_ah = BAT_MIN_AH_HE4; + BAT_CURVE_HE4(GET_MINIMUM_BAT_VOLTAGE); + break; + } + } + return 0.f; +} + +float getMaxBatVoltage(BatteryCellType cellType) +{ + switch (cellType) + { + case BatteryCellType::_22P: + { + BAT_CURVE_22P(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::HG2: + { + BAT_CURVE_HG2(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::MH1: + { + BAT_CURVE_MH1(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::VTC5: + { + BAT_CURVE_VTC5(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::BAK_25R: + { + BAT_CURVE_25R(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + case BatteryCellType::HE4: + { + BAT_CURVE_HE4(GET_MAXIMUM_BAT_VOLTAGE); + break; + } + } + return 0.f; +} + namespace battery { std::optional bootBatPercentage; std::optional bootBatWh; diff --git a/main/battery.h b/main/battery.h index 190024f..a898ff8 100644 --- a/main/battery.h +++ b/main/battery.h @@ -6,6 +6,117 @@ // local includes #include +// battery curves +#define PERCENTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ +if (cellVoltage >= lowerVoltage && cellVoltage <= higherVoltage) \ + return 100 * (expected_ah - cpputils::mapValue(cellVoltage, higherVoltage, lowerVoltage, fromAh, toAh)) / expected_ah; + +#define GET_MINIMUM_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ + if (expected_ah >= toAh) \ + return lowerVoltage; + +#define GET_MAXIMUM_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ + if (fromAh == 0) \ + return higherVoltage; + +#define GET_MINIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ + if (expected_ah >= toAh) \ + return lowerVoltage * configs.battery.cellsSeries.value; + +#define GET_MAXIMUM_BAT_VOLTAGE(higherVoltage,lowerVoltage,fromAh,toAh) \ + if (fromAh == 0) \ + return higherVoltage * configs.battery.cellsSeries.value; + +// 22P +#define BAT_MIN_AH_22P 2.2 +#define BAT_CURVE_22P(func) \ + func(4.15, 4.04, 0, 0.25) \ + func(4.04, 3.95, 0.25, 0.5) \ + func(3.95, 3.86, 0.5, 0.75) \ + func(3.86, 3.74, 0.75, 1.0) \ + func(3.74, 3.64, 1.0, 1.25) \ + func(3.64, 3.59, 1.25, 1.5) \ + func(3.59, 3.54, 1.5, 1.75) \ + func(3.54, 3.43, 1.75, 2.0) \ + func(3.43, 3.35, 2.0, 2.1) \ + func(3.35, 2.50, 2.1, 2.2) + +// MH1 +#define BAT_MIN_AH_MH1 3.2 +#define BAT_CURVE_MH1(func) \ + func(4.15, 4.09, 0, 0.25) \ + func(4.09, 4.04, 0.25, 0.5) \ + func(4.04, 3.95, 0.5, 0.75) \ + func(3.95, 3.88, 0.75, 1.0) \ + func(3.88, 3.79, 1.0, 1.25) \ + func(3.79, 3.70, 1.25, 1.5) \ + func(3.70, 3.65, 1.5, 1.75) \ + func(3.65, 3.60, 1.75, 2.0) \ + func(3.60, 3.56, 2.0, 2.25) \ + func(3.56, 3.50, 2.25, 2.5) \ + func(3.50, 3.40, 2.5, 2.75) \ + func(3.40, 3.30, 2.75, 3.0) \ + func(3.30, 2.5, 3.0, 3.2) + +// HG2 +#define BAT_MIN_AH_HG2 3.0 +#define BAT_CURVE_HG2(func) \ + func(4.15, 4.08, 0, 0.25) \ + func(4.08, 4.01, 0.25, 0.5) \ + func(4.01, 3.92, 0.5, 0.75) \ + func(3.92, 3.84, 0.75, 1.0) \ + func(3.84, 3.75, 1.0, 1.25) \ + func(3.75, 3.67, 1.25, 1.5) \ + func(3.67, 3.62, 1.5, 1.75) \ + func(3.62, 3.55, 1.75, 2.0) \ + func(3.55, 3.44, 2.0, 2.25) \ + func(3.44, 3.30, 2.25, 2.5) \ + func(3.30, 3.05, 2.5, 2.75) \ + func(3.05, 2.50, 2.75, 3.0) + +// VTC5 +#define BAT_MIN_AH_VTC5 2.6 +#define BAT_CURVE_VTC5(func) \ + func(4.15, 4.08, 0, 0.25) \ + func(4.08, 3.98, 0.25, 0.5) \ + func(3.98, 3.89, 0.5, 0.75) \ + func(3.89, 3.79, 0.75, 1.0) \ + func(3.79, 3.71, 1.0, 1.25) \ + func(3.71, 3.64, 1.25, 1.5) \ + func(3.64, 3.53, 1.5, 1.75) \ + func(3.53, 3.44, 1.75, 2.0) \ + func(3.44, 3.20, 2.0, 2.25) \ + func(3.20, 2.80, 2.25, 2.5) \ + func(2.80, 2.50, 2.5, 2.60) + +// BAK_25R +#define BAT_MIN_AH_BAK_25R 2.5 +#define BAT_CURVE_25R(func) \ + func(4.15, 4.06, 0, 0.25) \ + func(4.06, 3.96, 0.25, 0.5) \ + func(3.96, 3.88, 0.5, 0.75) \ + func(3.88, 3.77, 0.75, 1) \ + func(3.77, 3.68, 1, 1.25) \ + func(3.68, 3.62, 1.25, 1.5) \ + func(3.62, 3.56, 1.5, 1.75) \ + func(3.56, 3.47, 1.75, 2) \ + func(3.47, 3.31, 2, 2.25) \ + func(3.31, 2.50, 2.25, 2.5) + +// HE4 +#define BAT_MIN_AH_HE4 2.3 +#define BAT_CURVE_HE4(func) \ + func(4.15, 4.02, 0, 0.25) \ + func(4.02, 3.91, 0.25, 0.5) \ + func(3.91, 3.81, 0.5, 0.75) \ + func(3.81, 3.72, 0.75, 1) \ + func(3.72, 3.61, 1, 1.25) \ + func(3.61, 3.62, 1.25, 1.5) \ + func(3.62, 3.53, 1.5, 1.75) \ + func(3.53, 3.45, 1.75, 2) \ + func(3.45, 3.21, 2, 2.25) \ + func(3.21, 2.80, 2.25, 2.3) + #define BatteryCellTypeValues(x) \ x(_22P) \ x(HG2) \ @@ -21,12 +132,13 @@ float getRemainingWattHours(); float getBatteryWattHours(); +float getMinBatVoltage(BatteryCellType cellType); +float getMaxBatVoltage(BatteryCellType cellType); + std::string getBatteryPercentageString(); std::string getBatteryRemainingWattHoursString(); -std::string getBatteryCellTypeString(); - std::string getRemainingRangeString(); std::string getBatteryDebugString();