forked from collin80/GEVCU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThinkBatteryManager.cpp
195 lines (169 loc) · 5.97 KB
/
ThinkBatteryManager.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
/*
* ThinkBatteryManager.cpp
*
* Interface to the BMS which is within the Think City battery packs
*
Copyright (c) 2013 Collin Kidder, Michael Neuweiler, Charles Galpin
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ThinkBatteryManager.h"
ThinkBatteryManager::ThinkBatteryManager() : BatteryManager() {
prefsHandler = new PrefHandler(THINKBMS);
allowCharge = false;
allowDischarge = false;
commonName = "Think City BMS";
}
void ThinkBatteryManager::setup() {
TickHandler::getInstance()->detach(this);
Logger::info("add device: Th!nk City BMS (id: %X, %X)", THINKBMS, this);
BatteryManager::setup(); // run the parent class version of this function
//Relevant BMS messages are 0x300 - 0x30F
CanHandler::getInstanceEV()->attach(this, 0x300, 0x7f0, false);
TickHandler::getInstance()->attach(this, CFG_TICK_INTERVAL_BMS_THINK);
}
/*For all multibyte integers the format is MSB first, LSB last
*/
void ThinkBatteryManager::handleCanFrame(CAN_FRAME *frame) {
int temp;
switch (frame->id) {
case 0x300: //Start up message
//we're not really interested in much here except whether init worked.
if ((frame->data.bytes[6] & 1) == 0) //there was an initialization error!
{
faultHandler.raiseFault(THINKBMS, FAULT_BMS_INIT, true);
allowCharge = false;
allowDischarge = false;
}
else
{
faultHandler.cancelOngoingFault(THINKBMS, FAULT_BMS_INIT);
}
break;
case 0x301: //System Data 0
//first two bytes = current, next two voltage, next two DOD, last two avg. temp
//readings in tenths
packVoltage = (frame->data.bytes[0] * 256 + frame->data.bytes[1]);
packCurrent = (frame->data.bytes[2] * 256 + frame->data.bytes[3]);
break;
case 0x302: //System Data 1
if ((frame->data.bytes[0] & 1) == 1) //Byte 0 bit 0 = general error
{
faultHandler.raiseFault(THINKBMS, FAULT_BMS_MISC, true);
allowDischarge = false;
allowCharge = false;
}
else
{
faultHandler.cancelOngoingFault(THINKBMS, FAULT_BMS_MISC);
}
if ((frame->data.bytes[2] & 1) == 1) //Byte 2 bit 0 = general isolation error
{
faultHandler.raiseFault(THINKBMS, FAULT_HV_BATT_ISOLATION, true);
allowDischarge = false;
allowCharge = false;
}
else
{
faultHandler.cancelOngoingFault(THINKBMS, FAULT_HV_BATT_ISOLATION);
}
//Min discharge voltage = bytes 4-5 - tenths of a volt
//Max discharge current = bytes 6-7 - tenths of an amp
temp = (S16)(frame->data.bytes[6] * 256 + frame->data.bytes[7]);
if (temp > 0) allowDischarge = true;
break;
case 0x303: //System Data 2
//bytes 0-1 = max charge voltage (tenths of volt)
//bytes 2-3 = max charge current (tenths of amp)
temp = (S16)(frame->data.bytes[2] * 256 + frame->data.bytes[3]);
if (temp > 0) allowCharge = true;
//byte 4 bit 1 = regen braking OK, bit 2 = Discharging OK
//byte 6 bit 3 = EPO (emergency power off) happened, bit 5 = battery pack fan is on
break;
case 0x304: //System Data 3
//Byte 2 lower 4 bits = highest error category
//categories: 0 = no faults, 1 = Reserved, 2 = Warning, 3 = Delayed switch off, 4 = immediate switch off
//bytes 4-5 = Pack max temperature (tenths of degree C) - Signed
//byte 6-7 = Pack min temperature (tenths of a degree C) - Signed
lowestCellTemp = (S16)(frame->data.bytes[4] * 256 + frame->data.bytes[5]);
highestCellTemp = (S16)(frame->data.bytes[6] * 256 + frame->data.bytes[7]);
break;
case 0x305: //System Data 4
//byte 2 bits 0-3 = BMS state
//0 = idle state, 1 = discharge state (contactor closed), 15 = fault state
//byte 2 bit 4 = Internal HV isolation fault
//byte 2 bit 5 = External HV isolation fault
break;
case 0x306: //System Data 5
//bytes 0-1 = Equiv. internal resistance in milliohms
//not recommended to rely on so probably just ignore it
break;
//technically there is a specification for frames 0x307 - 0x30A but I have never seen these frames
//sent on the canbus system so I doubt that they are used.
/*
case 0x307: //System Data 6
case 0x308: //System Data 7
case 0x309: //System Data 8
case 0x30A: //System Data 9
//do we care about the serial #? Probably not.
case 0x30E: //Serial # part 1
case 0x30B: //Serial # part 2
*/
}
}
void ThinkBatteryManager::handleTick() {
BatteryManager::handleTick(); //kick the ball up to papa
sendKeepAlive();
}
//Contactors in pack will close if we sent these two frames with all zeros.
void ThinkBatteryManager::sendKeepAlive()
{
CAN_FRAME output;
output.length = 3;
output.id = 0x310;
output.extended = 0; //standard frame
output.rtr = 0;
for (int i = 0; i < 8; i++) output.data.bytes[i] = 0;
CanHandler::getInstanceEV()->sendFrame(output);
output.id = 0x311;
output.length = 2;
CanHandler::getInstanceEV()->sendFrame(output);
}
DeviceId ThinkBatteryManager::getId()
{
return (THINKBMS);
}
bool ThinkBatteryManager::hasPackVoltage()
{
return true;
}
bool ThinkBatteryManager::hasPackCurrent()
{
return true;
}
bool ThinkBatteryManager::hasTemperatures()
{
return true;
}
bool ThinkBatteryManager::isChargeOK()
{
return allowCharge;
}
bool ThinkBatteryManager::isDischargeOK()
{
return allowDischarge;
}