-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBatteryMonitor.cpp
More file actions
170 lines (136 loc) · 5.37 KB
/
BatteryMonitor.cpp
File metadata and controls
170 lines (136 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "BatteryMonitor.h"
BatteryMonitor::BatteryMonitor(uint8_t batteryPin) : batteryPin(batteryPin) {
lastVoltage = 0.0f;
lastUpdate = 0;
}
void BatteryMonitor::begin() {
Serial.println("Initializing Battery Monitor...");
// Configure ADC pin and settings
pinMode(batteryPin, INPUT);
analogReadResolution(12); // Use 12-bit resolution (0-4095)
analogSetAttenuation(ADC_11db); // 0-3.3V range for better accuracy
// Load calibration from preferences
preferences.begin("battery", false);
loadCalibration();
preferences.end();
// Take initial reading
update();
Serial.printf("Battery Monitor initialized on GPIO%d\n", batteryPin);
Serial.printf("Initial voltage: %.2fV (%d%%)\n", getBatteryVoltage(), getBatteryPercentage());
}
void BatteryMonitor::update() {
unsigned long currentTime = millis();
// Limit update frequency to reduce noise
if (currentTime - lastUpdate < UPDATE_INTERVAL) {
return;
}
float newVoltage = readRawVoltage();
// Simple smoothing filter (exponential moving average)
if (lastVoltage == 0.0f) {
lastVoltage = newVoltage; // First reading
} else {
lastVoltage = (lastVoltage * 0.8f) + (newVoltage * 0.2f); // 80/20 smoothing
}
lastUpdate = currentTime;
}
float BatteryMonitor::readRawVoltage() {
// Take multiple readings for accuracy
int totalReading = 0;
const int samples = 10;
for (int i = 0; i < samples; i++) {
totalReading += analogRead(batteryPin);
delayMicroseconds(100); // Small delay between readings
}
int avgReading = totalReading / samples;
// Convert ADC reading to voltage
float voltage = ((float)avgReading / ADC_RESOLUTION) * ADC_REFERENCE * VOLTAGE_DIVIDER_RATIO;
// Apply calibration offset
voltage += calibrationOffset;
return voltage;
}
float BatteryMonitor::getBatteryVoltage() {
return lastVoltage;
}
int BatteryMonitor::getBatteryPercentage() {
float voltage = getBatteryVoltage();
// Convert voltage to percentage using Li-ion discharge curve
int percentage;
if (voltage >= BATTERY_FULL) {
percentage = 100;
} else if (voltage >= BATTERY_GOOD) {
// 100% to 75% range
percentage = 75 + (int)((voltage - BATTERY_GOOD) / (BATTERY_FULL - BATTERY_GOOD) * 25);
} else if (voltage >= BATTERY_NOMINAL) {
// 75% to 50% range
percentage = 50 + (int)((voltage - BATTERY_NOMINAL) / (BATTERY_GOOD - BATTERY_NOMINAL) * 25);
} else if (voltage >= BATTERY_LOW) {
// 50% to 25% range
percentage = 25 + (int)((voltage - BATTERY_LOW) / (BATTERY_NOMINAL - BATTERY_LOW) * 25);
} else if (voltage >= BATTERY_CRITICAL) {
// 25% to 5% range
percentage = 5 + (int)((voltage - BATTERY_CRITICAL) / (BATTERY_LOW - BATTERY_CRITICAL) * 20);
} else if (voltage >= BATTERY_EMPTY) {
// 5% to 0% range
percentage = (int)((voltage - BATTERY_EMPTY) / (BATTERY_CRITICAL - BATTERY_EMPTY) * 5);
} else {
percentage = 0; // Below 3.2V threshold
}
return constrain(percentage, 0, 100);
}
String BatteryMonitor::getBatteryStatus() {
float voltage = getBatteryVoltage();
if (voltage >= BATTERY_FULL) {
return "Full";
} else if (voltage >= BATTERY_GOOD) {
return "Good"; // 4.0V+ - Reliable ESP32 operation
} else if (voltage >= BATTERY_NOMINAL) {
return "Fair"; // 3.8V+ - Normal operation
} else if (voltage >= BATTERY_LOW) {
return "Low"; // 3.6V+ - Consider charging
} else if (voltage >= BATTERY_CRITICAL) {
return "Critical"; // 3.4V+ - Charge immediately
} else {
return "Empty"; // <3.4V - Below critical threshold
}
}
bool BatteryMonitor::isCharging() {
// Future implementation: detect if voltage is increasing over time
// For now, return false (would need additional circuitry to detect charging)
return false;
}
bool BatteryMonitor::isLowBattery() {
return getBatteryVoltage() < BATTERY_LOW;
}
bool BatteryMonitor::isCriticalBattery() {
return getBatteryVoltage() < BATTERY_CRITICAL;
}
int BatteryMonitor::getBatterySegments() {
int percentage = getBatteryPercentage();
// Convert percentage to 3-segment display
if (percentage >= 75) {
return 3; // Full battery - 3 segments
} else if (percentage >= 50) {
return 2; // Good battery - 2 segments
} else if (percentage >= 25) {
return 1; // Low battery - 1 segment
} else {
return 0; // Critical battery - empty/flashing
}
}
void BatteryMonitor::calibrateVoltage(float actualVoltage) {
float measuredVoltage = readRawVoltage() - calibrationOffset; // Get uncalibrated reading
calibrationOffset = actualVoltage - measuredVoltage;
// Save calibration
preferences.begin("battery", false);
saveCalibration();
preferences.end();
Serial.printf("Battery calibrated: offset = %.3fV\n", calibrationOffset);
}
void BatteryMonitor::loadCalibration() {
calibrationOffset = preferences.getFloat("cal_offset", 0.0f);
Serial.printf("Battery calibration loaded: offset = %.3fV\n", calibrationOffset);
}
void BatteryMonitor::saveCalibration() {
preferences.putFloat("cal_offset", calibrationOffset);
Serial.println("Battery calibration saved");
}