|
6 | 6 | * @date 2025-06 |
7 | 7 | */ |
8 | 8 |
|
9 | | - |
10 | 9 | #include "battery.hpp" |
11 | 10 |
|
12 | 11 | /** |
13 | | - * @brief Battery Class constructor |
14 | | - * |
| 12 | + * @brief Constructs a Battery monitoring object for voltage measurement and percentage calculation. |
15 | 13 | */ |
16 | 14 | Battery::Battery() {} |
17 | 15 |
|
18 | 16 | /** |
19 | | - * @brief Configure ADC Channel for battery reading |
| 17 | + * @brief Initializes the ADC channel(s) required for battery voltage measurement. |
20 | 18 | * |
| 19 | + * Configures the hardware ADC based on the ESP32 chip (ADC1 or ADC2). |
21 | 20 | */ |
22 | 21 | void Battery::initADC() |
23 | 22 | { |
24 | 23 | #ifdef ADC1 |
25 | | - adc1_config_width(ADC_WIDTH_BIT_12); |
26 | | - adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12); |
| 24 | + adc1_config_width(ADC_WIDTH_BIT_12); |
| 25 | + adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12); |
27 | 26 | #endif |
28 | 27 |
|
29 | 28 | #ifdef ADC2 |
30 | | - adc2_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12); |
| 29 | + adc2_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12); |
31 | 30 | #endif |
32 | 31 | } |
33 | 32 |
|
34 | 33 | /** |
35 | | - * @brief Set battery voltage levels |
| 34 | + * @brief Sets the maximum and minimum voltage levels for battery charge calculation. |
36 | 35 | * |
37 | | - * @param maxVoltage -> Full Charge voltage |
38 | | - * @param minVoltage -> Min Charge voltage |
| 36 | + * @param maxVoltage Voltage considered as fully charged. |
| 37 | + * @param minVoltage Voltage considered as minimum safe level. |
39 | 38 | */ |
40 | 39 | void Battery::setBatteryLevels(float maxVoltage, float minVoltage) |
41 | 40 | { |
42 | | - batteryMax = maxVoltage; |
43 | | - batteryMin = minVoltage; |
| 41 | + batteryMax = maxVoltage; |
| 42 | + batteryMin = minVoltage; |
44 | 43 | } |
45 | 44 |
|
46 | 45 | /** |
47 | | - * @brief Read battery charge and return %. |
| 46 | + * @brief Reads and computes the current battery charge as a percentage. |
| 47 | + * |
| 48 | + * Takes 100 ADC samples, averages them, compensates for the voltage divider, and calculates the charge percentage. |
| 49 | + * Returns a value between 0 and 100 (values above 160 are treated as 0). |
48 | 50 | * |
49 | | - * @return float -> % Charge |
| 51 | + * @return float Percentage of battery charge (0–100% typically). |
50 | 52 | */ |
51 | 53 | float Battery::readBattery() |
52 | 54 | { |
53 | | - long sum = 0; // Sum of samples taken |
54 | | - float voltage = 0.0; // Calculated voltage |
55 | | - float output = 0.0; // Output value |
56 | 55 |
|
57 | | - for (int i = 0; i < 100; i++) |
58 | | - { |
59 | | - #ifdef ADC1 |
60 | | - sum += static_cast<long>(adc1_get_raw(BATT_PIN)); |
61 | | - #endif |
| 56 | + long sum = 0; /**< Sum of samples taken. */ |
| 57 | + float voltage = 0.0; /**< Calculated voltage. */ |
| 58 | + float output = 0.0; /**< Output value. */ |
| 59 | + |
| 60 | + |
| 61 | + for (int i = 0; i < 100; i++) |
| 62 | + { |
| 63 | + #ifdef ADC1 |
| 64 | + sum += static_cast<long>(adc1_get_raw(BATT_PIN)); |
| 65 | + #endif |
62 | 66 |
|
63 | | - #ifdef ADC2 |
64 | | - int readRaw; |
65 | | - esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw); |
66 | | - if (r == ESP_OK) |
67 | | - sum += static_cast<long>(readRaw); |
68 | | - #endif |
| 67 | + #ifdef ADC2 |
| 68 | + int readRaw; |
| 69 | + esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw); |
| 70 | + if (r == ESP_OK) |
| 71 | + sum += static_cast<long>(readRaw); |
| 72 | + #endif |
69 | 73 |
|
70 | | - delayMicroseconds(150); |
71 | | - } |
| 74 | + delayMicroseconds(150); |
| 75 | + } |
72 | 76 |
|
73 | | - voltage = sum / 100.0; |
74 | | - // Custom board has a divider circuit |
75 | | - constexpr float R1 = 100000.0; // Resistance of R1 (100K) |
76 | | - constexpr float R2 = 100000.0; // Resistance of R2 (100K) |
77 | | - voltage = (voltage * V_REF) / 4096.0; |
78 | | - voltage = voltage / (R2 / (R1 + R2)); |
79 | | - voltage = roundf(voltage * 100) / 100; |
| 77 | + voltage = sum / 100.0; |
| 78 | + /**< Custom board has a divider circuit */ |
| 79 | + constexpr float R1 = 100000.0; /**< Resistance of R1 (100K) */ |
| 80 | + constexpr float R2 = 100000.0; /**< Resistance of R2 (100K) */ |
| 81 | + voltage = (voltage * V_REF) / 4096.0; |
| 82 | + voltage = voltage / (R2 / (R1 + R2)); |
| 83 | + voltage = roundf(voltage * 100) / 100; |
80 | 84 |
|
81 | | - output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100; |
82 | | - return (output <= 160) ? output : 0.0f; |
| 85 | + output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100; |
| 86 | + return (output <= 160) ? output : 0.0f; |
83 | 87 | } |
0 commit comments