From f4c9b12d7860cdd94d9d524c4286db19f1f42862 Mon Sep 17 00:00:00 2001 From: MickiusMousius Date: Mon, 11 Dec 2023 17:30:53 +1030 Subject: [PATCH] fix: Use a formula table to better approximate remaining battery capacity --- .../drivers/sensor/battery/battery_common.c | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/module/drivers/sensor/battery/battery_common.c b/app/module/drivers/sensor/battery/battery_common.c index 9afe2d5b..5bc4aaa8 100644 --- a/app/module/drivers/sensor/battery/battery_common.c +++ b/app/module/drivers/sensor/battery/battery_common.c @@ -29,15 +29,36 @@ int battery_channel_get(const struct battery_value *value, enum sensor_channel c return 0; } -uint8_t lithium_ion_mv_to_pct(int16_t bat_mv) { - // Simple linear approximation of a battery based off adafruit's discharge graph: - // https://learn.adafruit.com/li-ion-and-lipoly-batteries/voltages +typedef struct { + int16_t millivolts; + float slope; + float offset; +} batt_est_formula; - if (bat_mv >= 4200) { - return 100; - } else if (bat_mv <= 3450) { - return 0; +uint8_t lithium_ion_mv_to_pct(int16_t bat_mv) { + // Lookup table of slope formulas for calculating remaining battery capacity. + // The original values used to calculate slopes come from: + // + // https://blog.ampow.com/lipo-voltage-chart/ + + const batt_est_formula lipo_formula_lookup[9] = { + {.millivolts = 4200, .slope = 0, .offset = 100}, + {.millivolts = 4150, .slope = 0.1, .offset = -320}, + {.millivolts = 4110, .slope = 0.125, .offset = -423.75}, + {.millivolts = 4020, .slope = 0.111, .offset = -366.22}, + {.millivolts = 3870, .slope = 0.133, .offset = -454.68}, + {.millivolts = 3690, .slope = 0.278, .offset = -1015.84}, + {.millivolts = 3610, .slope = 0.063, .offset = -222.47}, + {.millivolts = 3270, .slope = 0.015, .offset = -48.09}, + }; + + for (int i = 0; i < 9; i++) { + batt_est_formula current_formula = lipo_formula_lookup[i]; + if (bat_mv >= current_formula.millivolts) { + float capacity = bat_mv * current_formula.slope + current_formula.offset; + return (uint8_t)capacity; + } } - return bat_mv * 2 / 15 - 459; + return 0; } \ No newline at end of file