-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattery.cpp
147 lines (123 loc) · 2.62 KB
/
Battery.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
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
/*
* Battery.cpp
*
* Created on: 19 Mar 2021
* Author: michael
*/
#include "Battery.h"
/**
* Constructor
*/
Battery::Battery() {
timestamp = 0;
restTimestamp = 0;
soc = 0;
ampereMilliseconds = 0;
ampereHours = 0;
current = 0;
voltage = 0;
voltageSCC = 0;
}
/**
* Destructor
*/
Battery::~Battery() {
}
/**
* Initialize the class
*/
void Battery::init() {
timestamp = millis();
}
/**
* the main loop
*/
void Battery::loop() {
updateSoc();
checkBatteryResting();
}
/**
* Calculate the state of charge based on current and elapsed time since last measurement.
* The calculation is done in an Ampere-miliseconds buffer and moved over from there to the Ah counter
* (which counts in 0.1Ah)
*
* 1Ah = 60Am = 3600As = 3600000Ams
*/
void Battery::updateSoc() {
ampereMilliseconds += current * (millis() - timestamp);
timestamp = millis();
if (ampereMilliseconds > 360000) {
if (ampereHours < config.batteryCapacity * 10) {
ampereHours++;
}
ampereMilliseconds -= 360000;
} else if (ampereMilliseconds < -3600000) {
if (ampereHours > 0) {
ampereHours--;
}
ampereMilliseconds += 360000;
}
if (isEmpty()) {
ampereHours = 0;
}
if (isFullyCharged()) {
ampereHours = config.batteryCapacity * 10;
}
if (config.batterySocCalculateInternally) {
soc = ampereHours * 100 / config.batteryCapacity;
}
}
void Battery::checkBatteryResting() {
if (current <= config.batteryRestCurrent * -1) {
restTimestamp = millis(); // we're not resting, update the timestamp
}
}
bool Battery::isFullyCharged() {
return voltage >= config.batteryVoltageFullCharge && current < config.batteryRestCurrent;
}
bool Battery::isEmpty() {
return voltage <= config.batteryVoltageEmpty
&& (restTimestamp + config.batteryRestDuration * 1000) < millis();
}
/**
* Set the current state of charge in 0.1% (if we don't calc it by ourselves)
*/
void Battery::setSOC(uint16_t soc) {
if (!config.batterySocCalculateInternally) {
this->soc = soc;
}
}
/**
* Get the current state of charge in 0.1%
*/
uint16_t Battery::getSOC() {
return soc;
}
/**
* Return the remaining ampere hours in the battery (in 0.1A)
*/
uint16_t Battery::getAmpereHours() {
return ampereHours;
}
void Battery::setCurrent(int16_t current) {
this->current = current;
}
int16_t Battery::getCurrent() {
return current;
}
void Battery::setVoltage(float voltage) {
this->voltage = voltage;
}
float Battery::getVoltage() {
return voltage;
}
int16_t Battery::getPower() {
return current * voltage;
}
void Battery::setVoltageSCC(float voltageSCC) {
this->voltageSCC = voltageSCC;
}
float Battery::getVoltageSCC() {
return voltageSCC;
}
Battery battery;