-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbat.h
193 lines (183 loc) · 5 KB
/
bat.h
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
#ifndef _BAT_H_
#define _BAT_H_
/*
basically just a data item representing a battery
based on the basic information required for Pylon CAN protocol
*/
class batCell {
public:
batCell(void) {
voltage = -1.0f;
temperature = -1.0f;
}
float voltage;
float temperature;
};
class batBat {
public:
// number of cells
batBat(const char * name_, int num) : name(name_) {
if(num < 1) num = 1; // sanity check
numCells = num;
cells = new batCell[num];
nomVoltage = -1.0f;
nomAH = -1.0f;
nomChargeCurrent = -1.0f;
nomChargeVoltage = -1.0f;
nomDischargeCurrent = -1.0f;
chargeCurrent = -1.0f;
chargeVoltage = -1.0f;
dischargeCurrent = -1.0f;
soh = -1.0f;
soc = -1.0f;
voltage = -1.0f;
current = -1.0f;
temperature = -1.0f;
minCellVoltage = -1.0f;
maxCellVoltage = -1.0f;
minCellVoltageNumber = -1;
maxCellVoltageNumber = -1;
updateMillis = 0UL;
balancing = false;
}
~batBat(void) {
delete cells;
}
// return a unique identifier string for logging
String &myId(void) { return name; }
// id/name
String name;
// cell data - not used in pylontech can protocol, but collect for now anyway
int numCells;
batCell * cells;
// nominal charge/discharge parameters
// populate these if we are not manually derating, so we can detect when the battery derates itself
float nomVoltage;
float nomAH;
float nomChargeCurrent;
float nomChargeVoltage;
float nomDischargeCurrent;
// reported charge/discharge parameters
// if these are not set, call a derate function to populate
float chargeCurrent;
float chargeVoltage;
float dischargeCurrent;
// current state
float soh;
float soc;
float voltage;
float current; // daly - discharge is positive!
float temperature;
// need these for derating
float minCellVoltage;
float maxCellVoltage;
// not strictly required, but useful info - cell numbers
int minCellVoltageNumber;
int maxCellVoltageNumber;
// so we can keep track of changes...
unsigned long updateMillis;
// we need to know if this pack is balancing, as current will be unreliable...
bool balancing;
// debug
// l0 = only dynamic parameters
// l1 = include cell voltages
// l2 = include cell temperatures
// l3 = include nominals
void dump(int level = 0) {
if(level > 2) {
Serial.print(name);
Serial.print("_nomVoltage = ");
Serial.println(nomVoltage);
Serial.print(name);
Serial.print("_nomAH = ");
Serial.println(nomAH);
Serial.print(name);
Serial.print("_nomChargeCurrent = ");
Serial.println(nomChargeCurrent);
Serial.print(name);
Serial.print("_nomChargeVoltage = ");
Serial.println(nomChargeVoltage);
Serial.print(name);
Serial.print("_nomDischargeCurrent = ");
Serial.println(nomDischargeCurrent);
}
if(chargeCurrent != -1.0f) {
Serial.print(name);
Serial.print("_chargeCurrent = ");
Serial.println(chargeCurrent);
}
if(chargeVoltage != -1.0f) {
Serial.print(name);
Serial.print("_chargeVoltage = ");
Serial.println(chargeVoltage);
}
if(dischargeCurrent != -1.0f) {
Serial.print(name);
Serial.print("_dischargeCurrent = ");
Serial.println(dischargeCurrent);
}
if(soh != -1.0f) {
Serial.print(name);
Serial.print("_soh = ");
Serial.println(soh);
}
if(soc != -1.0f) {
Serial.print(name);
Serial.print("_soc = ");
Serial.println(soc);
}
if(voltage != -1.0f) {
Serial.print(name);
Serial.print("_voltage = ");
Serial.println(voltage, 3);
}
if(current != -1.0f) {
Serial.print(name);
Serial.print("_current = ");
Serial.println(current, 3);
}
if(temperature != -1.0f) {
Serial.print(name);
Serial.print("_temperature = ");
Serial.println(temperature);
}
Serial.print(name);
Serial.print("_balancing = ");
Serial.println(balancing);
Serial.print(name);
Serial.print("_minCellVoltageNumber = ");
Serial.println(minCellVoltageNumber);
if(minCellVoltage != -1.0f) {
Serial.print(name);
Serial.print("_minCellVoltage = ");
Serial.println(minCellVoltage, 3);
}
Serial.print(name);
Serial.print("_maxCellVoltageNumber = ");
Serial.println(maxCellVoltageNumber);
if(maxCellVoltage != -1.0f) {
Serial.print(name);
Serial.print("_maxCellVoltage = ");
Serial.println(maxCellVoltage, 3);
}
for(int i = 0; level > 0 && i < numCells; i++) {
if(cells[i].voltage != -1.0f) {
Serial.print(name);
Serial.print("_CellV_");
Serial.print(i);
Serial.print(" = ");
Serial.println(cells[i].voltage, 3);
}
}
for(int i = 0; level > 1 && i < numCells; i++) {
if(cells[i].temperature != -1.0f) {
Serial.print(name);
Serial.print("_CellT_");
Serial.print(i);
Serial.print(" = ");
Serial.println(cells[i].temperature);
}
}
}
};
#endif