-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
batterystate.cpp
61 lines (52 loc) · 1.74 KB
/
batterystate.cpp
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
#include <Arduino.h>
#include "log.h"
#include "config.h"
#include "batterystate.h"
#define ADC_RESOLUTION 1023.f
// Entry count should be low enough to get updates often (1 entry per second),
// but high enough so that the voltage readings don't fluctuate too much.
// Too many items might result in slow response to low battery. This would only
// be problematic with high current draw.
#define VOLTAGE_HISTORY_ENTRIES 7
float voltageHistory[VOLTAGE_HISTORY_ENTRIES];
uint8_t voltageHistoryIndex = 0;
void storeCurrentVoltageInHistory() {
voltageHistoryIndex++;
if (voltageHistoryIndex > (VOLTAGE_HISTORY_ENTRIES - 1)) {
voltageHistoryIndex = 0;
}
voltageHistory[voltageHistoryIndex] = batteryStateGetVoltageNow();
}
void batteryStateInit() {
batteryStateGetVoltageNow(); // discard first result because it is inaccurate
// Fill the buffer with readings
for (int i=0; i<VOLTAGE_HISTORY_ENTRIES; ++i) {
storeCurrentVoltageInHistory();
}
}
void batteryStateUpdate() {
storeCurrentVoltageInHistory();
#ifdef BATTERY_STATE_LOG
batteryStateLog();
#endif
}
void batteryStateLog() {
log("battery: ");
log(batteryStateGetVoltageAverage());
logLn(" V");
}
float batteryStateGetVoltageAverage() {
float sum = 0.0f;
for (int i=0; i<VOLTAGE_HISTORY_ENTRIES; ++i) {
sum += voltageHistory[i];
}
return sum / (float)VOLTAGE_HISTORY_ENTRIES;
}
float batteryStateGetVoltageNow() {
// read the value from the sensor:
int adcValue = analogRead(VBAT_ADC_PIN);
float adcValueScaled = (float)adcValue / ADC_RESOLUTION;
float voltageReadOnPin = adcValueScaled * VBAT_ADC_MAX_READABLE_PIN_VOLTAGE;
float voltage = voltageReadOnPin / VBAT_ADC_MAX_BATTERY_PIN_VOLTAGE * VBAT_ADC_MAX_BATTERY_REAL_VOLTAGE;
return voltage;
}