-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbms.cpp
269 lines (233 loc) · 5.88 KB
/
bms.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "Arduino.h"
#include "declares.h"
#include "globals.h"
#include "bq769x0.h" // Library for Texas Instruments bq76920 battery management IC
bq769x0 BMS(bq76920, BMS_I2C_ADDRESS); // battery management system object
void bmsUpdate()
{
BMS.update(); // should be called at least every 250 ms
}
int bmsBegin()
{
Serial.println("bmsBegin Start");
BMS.begin(BMS_ALERT_PIN, BMS_BOOT_PIN);
Serial.println("bmsBegin End");
}
void RebootDevice(void)
{
bmsShutdown();
delay(50);
bmsBegin();
}
void bmsShutdown(void)
{
BMS.shutdown();
}
void setBMSDefaults()
{
Serial.println("BMS Defaults Start");
BMS.setTemperatureLimits(-20, 45, 0, 45);
BMS.setShuntResistorValue(5);
BMS.setShortCircuitProtection(14000, 200); // delay in us
BMS.setOvercurrentChargeProtection(8000, 200); // delay in ms
BMS.setOvercurrentDischargeProtection(8000, 320); // delay in ms
BMS.setCellUndervoltageProtection(2600, 2); // delay in s
BMS.setCellOvervoltageProtection(3650, 2); // delay in s
BMS.setBalancingThresholds(0, 3300, 20); // minIdleTime_min, minCellV_mV, maxVoltageDiff_mV
BMS.setIdleCurrentThreshold(100);
BMS.enableAutoBalancing();
BMS.enableDischarging();
Serial.println("BMS Defaults End");
}
int checkStatus() // returns 0 if everything is OK
{
return BMS.checkStatus();
}
// charging control
bool enableCharging(void) {
return BMS.enableCharging();
}
bool disableCharging(void) {
return BMS.disableCharging();
}
bool enableDischarging(void) {
return BMS.enableDischarging();
}
bool disableDischarging(void) {
return BMS.disableDischarging();
}
// hardware settings
void setShuntResistorValue(int res_mOhm) {
BMS.setShuntResistorValue(res_mOhm);
}
void setThermistorBetaValue(int beta_K) {
BMS.setThermistorBetaValue(beta_K);
}
// limit settings (for battery protection)
void setTemperatureLimits(int minDischarge_degC, int maxDischarge_degC, int minCharge_degC, int maxCharge_degC) // °C
{
BMS.setTemperatureLimits(minDischarge_degC, maxDischarge_degC, minCharge_degC, maxCharge_degC);
}
long setShortCircuitProtection(long current_mA, int delay_us )
{
BMS.setShortCircuitProtection(current_mA, delay_us );
}
long setOvercurrentChargeProtection(long current_mA, int delay_ms )
{
BMS.setOvercurrentChargeProtection(current_mA, delay_ms );
}
long setOvercurrentDischargeProtection(long current_mA, int delay_ms )
{
BMS.setOvercurrentDischargeProtection(current_mA, delay_ms );
}
int setCellUndervoltageProtection(int voltage_mV, int delay_s )
{
BMS.setCellUndervoltageProtection(voltage_mV, delay_s );
}
int setCellOvervoltageProtection(int voltage_mV, int delay_s )
{
BMS.setCellOvervoltageProtection(voltage_mV, delay_s );
}
// balancing settings
void setBalancingThresholds(int idleTime_min, int absVoltage_mV, byte voltageDifference_mV )
{
BMS.setBalancingThresholds(idleTime_min, absVoltage_mV, voltageDifference_mV );
}
void setIdleCurrentThreshold(int current_mA)
{
BMS.setIdleCurrentThreshold(current_mA);
}
// automatic balancing when battery is within balancing thresholds
void enableAutoBalancing(void)
{
BMS.enableAutoBalancing();
}
void disableAutoBalancing(void)
{
BMS.disableAutoBalancing();
}
// battery status
int getBatteryCurrent(void)
{
return BMS.getBatteryCurrent();
}
int getBatteryVoltage(void)
{
return BMS.getBatteryVoltage();
}
int getCellVoltage(byte idCell) // from 1 to 15
{
return BMS.getCellVoltage(idCell);
}
int getMinCellVoltage(void)
{
return BMS.getMinCellVoltage();
}
int getMaxCellVoltage(void)
{
return BMS.getMaxCellVoltage();
}
int getShuntResistorValue()
{
return BMS.getShuntResistorValue();
}
int getThermistorBetaValue()
{
return BMS.getThermistorBetaValue();
}
int getTemperatureLimits(int temperatureSetting)
{
return BMS.getTemperatureLimits(temperatureSetting);
}
float getTemperatureDegC(byte channel)
{
return BMS.getTemperatureDegC(channel);
}
float getTemperatureDegF(byte channel)
{
return BMS.getTemperatureDegF(channel);
}
long getShortCircuitProtectionCurrent()
{
return BMS.getShortCircuitProtectionCurrent();
}
int getShortCircuitProtectionDelay()
{
return BMS.getShortCircuitProtectionDelay();
}
long getOvercurrentChargeProtectionCurrent()
{
return BMS.getOvercurrentChargeProtectionCurrent();
}
int getOvercurrentChargeProtectionDelay()
{
return BMS.getOvercurrentChargeProtectionDelay();
}
long getOvercurrentDischargeProtectionCurrent()
{
return BMS.getOvercurrentDischargeProtectionCurrent();
}
int getOvercurrentDischargeProtectionDelay()
{
return BMS.getOvercurrentDischargeProtectionDelay();
}
int getCellUndervoltageProtectionVoltage()
{
return BMS.getCellUndervoltageProtectionVoltage();
}
int getCellUndervoltageProtectionDelay()
{
return BMS.getCellUndervoltageProtectionDelay();
}
int getCellOvervoltageProtectionVoltage()
{
return BMS.getCellOvervoltageProtectionVoltage();
}
int getCellOvervoltageProtectionDelay()
{
return BMS.getCellOvervoltageProtectionDelay();
}
int getBalancingThresholdTime()
{
return BMS.getBalancingThresholdTime();
}
int getBalancingThresholdVoltage()
{
return BMS.getBalancingThresholdVoltage();
}
byte getBalancingThresholdDifference()
{
return BMS.getBalancingThresholdDifference();
}
int getIdleCurrentThreshold()
{
return BMS.getIdleCurrentThreshold();
}
// interrupt handling (not to be called manually!)
void setAlertInterruptFlag(void)
{
BMS.setAlertInterruptFlag();
}
//void resetSOC(int percent)
//{
// BMS.resetSOC(percent);
//}
//void updateVoltages()
//{
// BMS.updateVoltages();
//}
//void updateTemperatures()
//{
// BMS.updateTemperatures();
//}
// If ignoreCCReadFlag == true, the current is read independent of an interrupt
// indicating the availability of a new CC reading
//void updateCurrent(bool ignoreCCReadyFlag)
//{
// BMS.updateCurrent(ignoreCCReadyFlag);
//}
// sets balancing registers if balancing is allowed (sufficient idle time + voltage)
//byte updateBalancingSwitches(void)
//{
// BMS.updateBalancingSwitches();
//}