1414 * +-----------------------+
1515 */
1616
17+ #include < algorithm>
18+ #include < array>
19+ #include < cmath>
1720#include " AdcSampler.hpp"
1821#include " sdkconfig.h"
19- #include < cmath>
22+
23+
24+ /* *
25+ * @struct BatteryStatus
26+ * @brief Battery status information
27+ */
28+ struct BatteryStatus
29+ {
30+ int voltage_mv; // Battery voltage in millivolts
31+ float percentage; // State of charge percentage (0-100%)
32+ bool valid; // Whether the reading is valid
33+ };
2034
2135/* *
2236 * @class BatteryMonitor
23- * @brief Monitors battery voltage through a resistor divider
37+ * @brief Monitors battery voltage and calculates state of charge for Li-ion batteries
2438 *
2539 * Uses AdcSampler (BSP layer) for hardware abstraction.
40+ * Includes voltage-to-SOC lookup table for typical Li-ion/Li-Po batteries.
41+ *
2642 * Configuration is done via Kconfig options:
2743 * - CONFIG_MONITORING_BATTERY_ENABLE
2844 * - CONFIG_MONITORING_BATTERY_ADC_GPIO
@@ -39,10 +55,29 @@ class BatteryMonitor
3955 // Initialize battery monitoring hardware
4056 bool setup ();
4157
42- // Read once, update filter, and return battery voltage in mV (after divider compensation), 0 on failure
58+ /* *
59+ * @brief Read battery voltage (with divider compensation)
60+ * @return Battery voltage in millivolts, 0 on failure
61+ */
4362 int getBatteryMilliVolts () const ;
4463
45- // Whether monitoring is enabled by Kconfig and supported by BSP
64+ /* *
65+ * @brief Calculate battery state of charge from voltage
66+ * @param voltage_mv Battery voltage in millivolts
67+ * @return State of charge percentage (0-100%)
68+ */
69+ static float voltageToPercentage (int voltage_mv);
70+
71+ /* *
72+ * @brief Get complete battery status (voltage + percentage)
73+ * @return BatteryStatus struct with voltage, percentage, and validity
74+ */
75+ BatteryStatus getBatteryStatus () const ;
76+
77+ /* *
78+ * @brief Check if battery monitoring is enabled and supported
79+ * @return true if enabled and ADC is supported
80+ */
4681 static constexpr bool isEnabled ()
4782 {
4883#ifdef CONFIG_MONITORING_BATTERY_ENABLE
@@ -53,6 +88,34 @@ class BatteryMonitor
5388 }
5489
5590private:
56- float scale_{1 .0f }; // Voltage divider scaling factor
91+ /* *
92+ * @brief Li-ion/Li-Po voltage to SOC lookup table entry
93+ */
94+ struct VoltageSOC
95+ {
96+ float voltage_mv;
97+ float soc;
98+ };
99+
100+ /* *
101+ * @brief Typical Li-ion single cell discharge curve lookup table
102+ * Based on typical 3.7V nominal Li-ion/Li-Po cell characteristics
103+ */
104+ static constexpr std::array<VoltageSOC, 12 > soc_lookup_ = {{
105+ {4200 .0f , 100 .0f }, // Fully charged
106+ {4060 .0f , 90 .0f },
107+ {3980 .0f , 80 .0f },
108+ {3920 .0f , 70 .0f },
109+ {3870 .0f , 60 .0f },
110+ {3820 .0f , 50 .0f },
111+ {3790 .0f , 40 .0f },
112+ {3770 .0f , 30 .0f },
113+ {3740 .0f , 20 .0f },
114+ {3680 .0f , 10 .0f },
115+ {3450 .0f , 5 .0f }, // Low battery warning
116+ {3300 .0f , 0 .0f }, // Empty / cutoff voltage
117+ }};
118+
119+ float scale_{1 .0f }; // Voltage divider scaling factor
57120 mutable AdcSampler adc_; // ADC sampler instance (BSP layer)
58121};
0 commit comments