-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathchademo.cpp
261 lines (223 loc) · 7.56 KB
/
chademo.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
/*
* This file is part of the tumanako_vc project.
*
* Copyright (C) 2018 Johannes Huebner <dev@johanneshuebner.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "chademo.h"
bool FCChademo::chargeEnabled = false;
bool FCChademo::parkingPosition = false;
bool FCChademo::fault = false;
bool FCChademo::contactorOpen = false;
bool chargeMode = false;
uint8_t FCChademo::chargerMaxCurrent;
uint16_t FCChademo::chargerMaxVoltage;
uint8_t FCChademo::chargeCurrentRequest;
uint32_t FCChademo::rampedCurReq;
uint16_t FCChademo::targetBatteryVoltage;
uint16_t FCChademo::chargerOutputVoltage = 0;
uint8_t FCChademo::chargerOutputCurrent = 0;
uint8_t FCChademo::chargerStatus = 0;
uint8_t FCChademo::soc;
uint32_t FCChademo::vtgTimeout = 0;
uint32_t FCChademo::curTimeout = 0;
static uint32_t chademoStartTime = 0;
uCAN_MSG txMessage;
#define FLASH_DELAY 8000
static void delay(void)
{
int i;
for (i = 0; i < FLASH_DELAY; i++) /* Wait a bit. */
__asm__("nop");
}
void FCChademo::DecodeCAN(int id, uint32_t data[2])
{
if (id == 0x108)
{
chargerMaxCurrent = data[0] >> 24;
chargerMaxVoltage = data[0] >> 8;
}
if (id == 0x109)
{
chargerOutputVoltage = data[0] >> 8;
chargerOutputCurrent = data[0] >> 24;
chargerStatus = (data[1] >> 8) & 0x3F;
}
}
void FCChademo::SetEnabled(bool enabled)
{
chargeEnabled = enabled;
if (!chargeEnabled)
{
rampedCurReq = 0;
vtgTimeout = 0;
curTimeout = 0;
}
}
void FCChademo::SetChargeCurrent(uint8_t current)
{
chargeCurrentRequest = MIN(current, chargerMaxCurrent);
if (chargeCurrentRequest > rampedCurReq)
rampedCurReq++;
else if (chargeCurrentRequest < rampedCurReq)
rampedCurReq--;
}
void FCChademo::CheckSensorDeviation(uint16_t internalVoltage)
{
int vtgDev = (int)internalVoltage - (int)chargerOutputVoltage;
vtgDev = ABS(vtgDev);
if (vtgDev > 10 && chargerOutputVoltage > 50)
{
vtgTimeout++;
}
else
{
vtgTimeout = 0;
}
if (chargerOutputCurrent > (rampedCurReq + 12))
{
curTimeout++;
}
else
{
curTimeout = 0;
}
}
void FCChademo::Task100Ms()//sends chademo messages every 100ms
{
uint32_t data[2];
bool curSensFault = curTimeout > 10;
bool vtgSensFault = vtgTimeout > 50;
//Capacity fixed to 200 - so SoC resolution is 0.5
data[0] = 0;
data[1] = (targetBatteryVoltage + 40) | 200 << 16;
txMessage.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B;
txMessage.frame.id = 0x100;
txMessage.frame.dlc = 8;
txMessage.frame.data0 = (data[0] & 0xFF);
txMessage.frame.data1 = (data[0]>>8 & 0xFF);
txMessage.frame.data2 = (data[0]>>16 & 0xFF);
txMessage.frame.data3 = (data[0]>>24 & 0xFF);
txMessage.frame.data4 = (data[1] & 0xFF);
txMessage.frame.data5 = (data[1]>>8 & 0xFF);
txMessage.frame.data6 = (data[1]>>16 & 0xFF);
txMessage.frame.data7 = (data[1]>>24 & 0xFF);
CANSPI_Transmit(&txMessage);
delay();
data[0] = 0x00FEFF00;
data[1] = 0;
txMessage.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B;
txMessage.frame.id = 0x101;
txMessage.frame.dlc = 8;
txMessage.frame.data0 = (data[0] & 0xFF);
txMessage.frame.data1 = (data[0]>>8 & 0xFF);
txMessage.frame.data2 = (data[0]>>16 & 0xFF);
txMessage.frame.data3 = (data[0]>>24 & 0xFF);
txMessage.frame.data4 = (data[1] & 0xFF);
txMessage.frame.data5 = (data[1]>>8 & 0xFF);
txMessage.frame.data6 = (data[1]>>16 & 0xFF);
txMessage.frame.data7 = (data[1]>>24 & 0xFF);
CANSPI_Transmit(&txMessage);
delay();
data[0] = 1 | ((uint32_t)targetBatteryVoltage << 8) | ((uint32_t)rampedCurReq << 24);
data[1] = (uint32_t)curSensFault << 2 |
(uint32_t)vtgSensFault << 4 |
(uint32_t)chargeEnabled << 8 |
(uint32_t)parkingPosition << 9 |
(uint32_t)fault << 10 |
(uint32_t)contactorOpen << 11 |
(uint32_t)soc << 16;
txMessage.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B;
txMessage.frame.id = 0x102;
txMessage.frame.dlc = 8;
txMessage.frame.data0 = (data[0] & 0xFF);
txMessage.frame.data1 = (data[0]>>8 & 0xFF);
txMessage.frame.data2 = (data[0]>>16 & 0xFF);
txMessage.frame.data3 = (data[0]>>24 & 0xFF);
txMessage.frame.data4 = (data[1] & 0xFF);
txMessage.frame.data5 = (data[1]>>8 & 0xFF);
txMessage.frame.data6 = (data[1]>>16 & 0xFF);
txMessage.frame.data7 = (data[1]>>24 & 0xFF);
CANSPI_Transmit(&txMessage);
}
void FCChademo::Task200Ms()
{
//formally the runchademo routine.
static int32_t controlledCurrent = 0;
if (chademoStartTime == 0)// && Param::GetInt(Param::opmode) != MOD_CHARGE)
{
chademoStartTime = rtc_get_counter_val();
FCChademo::SetChargeCurrent(0);
}
if ((rtc_get_counter_val() - chademoStartTime) > 4 && (rtc_get_counter_val() - chademoStartTime) < 8)
{
FCChademo::SetEnabled(true);
IOMatrix::GetPin(IOMatrix::CHADEMOALLOW)->Set();//never gets here ...
}
if (Param::GetInt(Param::opmode) == MOD_CHARGE && FCChademo::ConnectorLocked())
{
Param::SetInt(Param::chgtyp,DCFC);
chargeMode = true; //DC charge mode
}
if (chargeMode)
{
int udc = Param::GetInt(Param::udc);
int udcspnt = Param::GetInt(Param::Voltspnt);
int chargeLim = Param::GetInt(Param::CCS_ILim);
chargeLim = MIN(150, chargeLim);
chargeLim = MIN(Param::GetInt(Param::BMS_ChargeLim), chargeLim);//BMS charge current limit for chademo
//Note: No need to worry about bms type as if none selected sets to 999.
//If chargeLim==0 chademo session will end.
if (udc < udcspnt && controlledCurrent <= chargeLim)
controlledCurrent++;
if (udc > udcspnt && controlledCurrent > 0)
controlledCurrent--;
if(controlledCurrent>chargeLim)
controlledCurrent--;
FCChademo::SetChargeCurrent(controlledCurrent);
//TODO: fix this to not false trigger
//FCChademo::CheckSensorDeviation(Param::GetInt(Param::udc));
}
FCChademo::SetTargetBatteryVoltage(Param::GetInt(Param::Voltspnt)+10);
FCChademo::SetSoC(Param::GetFloat(Param::SOCFC));
Param::SetInt(Param::CCS_Ireq, FCChademo::GetRampedCurrentRequest());
if (Param::GetInt(Param::CCS_ILim) == 0)
{
FCChademo::SetChargeCurrent(0);
FCChademo::SetEnabled(false);
IOMatrix::GetPin(IOMatrix::CHADEMOALLOW)->Clear();//FCChademo charge allow off
chargeMode = false;
}
Param::SetInt(Param::CCS_V, FCChademo::GetChargerOutputVoltage());
Param::SetInt(Param::CCS_I, FCChademo::GetChargerOutputCurrent());
Param::SetInt(Param::CCS_State, FCChademo::GetChargerStatus());
Param::SetInt(Param::CCS_I_Avail, FCChademo::GetChargerMaxCurrent());
Param::SetInt(Param::CCS_V_Avail, FCChademo::GetChargerMaxVoltage());
}
bool FCChademo::DCFCRequest(bool RunCh)
{
if ((RunCh) && (IOMatrix::GetPin(IOMatrix::DCFCREQUEST)->Get()))
{
return true;
}
else
{
FCChademo::SetChargeCurrent(0);
FCChademo::SetEnabled(false);
IOMatrix::GetPin(IOMatrix::CHADEMOALLOW)->Clear();//FCChademo charge allow off
chademoStartTime=0;
return false;
}
}