Formula Student Electronics & Software
The code for the embedded software
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <FlexCAN_T4.h>
3#include <elapsedMillis.h>
4
5#include "can.h"
6
7FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
8
9elapsedMillis step;
10elapsedMillis temp;
11
12#define SHUTDOWN_PIN 32
13#define CH_SAFETY_PIN 36
14#define LATCHING_ERROR_PIN 34
15
16#define MAX_VOLTAGE 456000
17#define MAX_CURRENT 18000
18
20
21bool request = 0; // BMS request
22bool charge = 0; // Charger status
23bool shutdownStatus = 0; // Charging shutdown status
24bool latchingStatus = 0; // Latching error status
25
26enum status { // state machine status
30};
31
32status CH_Status; // current state machine status
33status NX_CH_Status; // next state machine status
34
35String CH_Status_Strings[] = {"idle", "charging", "shutdown"}; // print array
36
37void parseMessage(CAN_message_t message) {
38 switch (message.id) {
39 case CH_ID: {
40 switch (message.buf[0]) {
41 case 0x01: // set data response
42 {
43 switch (message.buf[1]) {
44 case 0x02: // set voltage response
45 {
46 param.setVoltage = 0;
47 uint32_t aux = message.buf[4];
48 param.setVoltage |= aux << 24;
49 aux = message.buf[5];
50 param.setVoltage |= aux << 16;
51 aux = message.buf[6];
52 param.setVoltage |= aux << 8;
53 aux = message.buf[7];
54 param.setVoltage |= aux;
55
56 Serial.print("Voltage Set= ");
57 Serial.print(param.setVoltage);
58
59 break;
60 }
61
62 case 0x03: // set current response
63 {
64 param.setCurrent = 0;
65 uint32_t aux = message.buf[4];
66 param.setCurrent |= aux << 24;
67 aux = message.buf[5];
68 param.setCurrent |= aux << 16;
69 aux = message.buf[6];
70 param.setCurrent |= aux << 8;
71 aux = message.buf[7];
72 param.setCurrent |= aux;
73
74 Serial.print("Current Set= ");
75 Serial.print(param.setCurrent);
76
77 break;
78 }
79
80 default: {
81 break;
82 }
83 }
84
85 break;
86 }
87
88 case 0x03: // read data response
89 {
90 switch (message.buf[1]) {
91 case 0x00: // current voltage response
92 {
94 uint32_t aux = message.buf[4];
95 param.currVoltage |= aux << 24;
96 aux = message.buf[5];
97 param.currVoltage |= aux << 16;
98 aux = message.buf[6];
99 param.currVoltage |= aux << 8;
100 aux = message.buf[7];
101 param.currVoltage |= aux;
102
103 Serial.print("Current voltage= ");
104 Serial.print(param.currVoltage);
105
106 break;
107 }
108
109 case 0x2f: // current current response
110 {
111 param.currCurrent = 0;
112 uint32_t aux = message.buf[4];
113 param.currCurrent |= aux << 24;
114 aux = message.buf[5];
115 param.currCurrent |= aux << 16;
116 aux = message.buf[6];
117 param.currCurrent |= aux << 8;
118 aux = message.buf[7];
119 param.currCurrent |= aux;
120
121 Serial.print("Current Current= ");
122 Serial.print(param.currCurrent);
123
124 break;
125 }
126 }
127
128 break;
129 }
130
131 break;
132 }
133
134 break;
135 }
136
137 case BMS_ID_CCL: {
138 param.ccl = message.buf[0] * 1000;
139 // Serial.print("ccl = ");
140 // Serial.println(param.ccl);
141
142 break;
143 }
144
145 case TA_ID: {
146 switch (message.buf[0]) {
147 case 0x00: {
148 for (int i = 0; i < 7; i++) // temp 0 - 6
149 {
150 param.temp[i + 0] = message.buf[i + 1];
151 }
152
153 break;
154 }
155
156 case 0x1: {
157 for (int i = 0; i < 7; i++) // temp 7 - 13
158 {
159 param.temp[i + 7] = message.buf[i + 1];
160 }
161
162 break;
163 }
164
165 case 0x2: {
166 for (int i = 0; i < 7; i++) // temp 14 - 20
167 {
168 param.temp[i + 14] = message.buf[i + 1];
169 }
170
171 break;
172 }
173
174 case 0x3: {
175 for (int i = 0; i < 7; i++) // temp 21 - 27
176 {
177 param.temp[i + 21] = message.buf[i + 1];
178 }
179
180 break;
181 }
182
183 case 0x4: {
184 for (int i = 0; i < 7; i++) // temp 28 - 34
185 {
186 param.temp[i + 28] = message.buf[i + 1];
187 }
188
189 break;
190 }
191
192 case 0x5: {
193 for (int i = 0; i < 7; i++) // temp 35 - 41
194 {
195 param.temp[i + 35] = message.buf[i + 1];
196 }
197
198 break;
199 }
200
201 case 0x6: {
202 for (int i = 0; i < 7; i++) // temp 42 - 48
203 {
204 param.temp[i + 42] = message.buf[i + 1];
205 }
206
207 break;
208 }
209
210 case 0x7: {
211 for (int i = 0; i < 7; i++) // temp 49 - 55
212 {
213 param.temp[i + 49] = message.buf[i + 1];
214 }
215
216 break;
217 }
218
219 case 0x8: {
220 for (int i = 0; i < 4; i++) // temp 56 - 59
221 {
222 param.temp[i + 56] = message.buf[i + 1];
223 }
224
225 break;
226 }
227 }
228
229 break;
230 }
231 }
232}
233
234void canint(const CAN_message_t& message) {
235 parseMessage(message);
236}
237void setup() {
238 // put your setup code here, to run once:
239 Serial.begin(115200);
240 Serial.println("startup");
241
242 pinMode(CH_SAFETY_PIN, INPUT);
243 pinMode(SHUTDOWN_PIN, INPUT);
244 pinMode(LATCHING_ERROR_PIN, INPUT);
245
246 can1.begin();
247 can1.setBaudRate(125000);
248 can1.enableFIFO();
249 can1.enableFIFOInterrupt();
250 can1.setFIFOFilter(REJECT_ALL);
251 can1.setFIFOFilter(0, CH_ID, STD);
252 can1.setFIFOFilter(1, BMS_ID_CCL, STD);
253 can1.setFIFOFilter(2, BMS_ID_ERR, STD);
254 can1.setFIFOFilter(3, TA_ID, STD);
255 can1.onReceive(canint);
256
258 charge = 0;
259}
260
262 switch (CH_Status) {
263 case idle: {
264 if (shutdownStatus) {
266 } else if (request == 1 and latchingStatus == 1 ) {
268 }
269 break;
270 }
271 case charging: {
272 if (shutdownStatus) {
274 } else if (request == 0 or latchingStatus == 0) {
276 }
277 break;
278 }
279 case shutdown:
280 break;
281
282 default: {
283 Serial.println("invalid charger state");
284 break;
285 }
286 }
287}
289 if (CH_Status != NX_CH_Status) {
290 Serial.println(CH_Status_Strings[NX_CH_Status]);
291 }
292}
293
295 request = digitalRead(CH_SAFETY_PIN);
296 shutdownStatus = digitalRead(SHUTDOWN_PIN);
297 latchingStatus = digitalRead(LATCHING_ERROR_PIN);
298}
299
300void powerOnModule(bool OnOff) {
301 CAN_message_t powerMsg;
302
303 powerMsg.id = CH_ID;
304 powerMsg.flags.extended = 1;
305 powerMsg.len = 8;
306 powerMsg.buf[0] = 0x10;
307 powerMsg.buf[1] = 0x04;
308 powerMsg.buf[2] = 0x00;
309 powerMsg.buf[3] = 0x00;
310 powerMsg.buf[4] = 0x00;
311 powerMsg.buf[5] = 0x00;
312 powerMsg.buf[6] = 0x00;
313 powerMsg.buf[7] = 0x00;
314
315 if (!OnOff)
316 powerMsg.buf[7] = 0x01; // turn off command
317
318 can1.write(powerMsg); // send message
319}
320
321void setVoltage(uint32_t voltage) {
322 CAN_message_t voltageMsg;
323
324 voltageMsg.id = CH_ID;
325 voltageMsg.flags.extended = 1;
326 voltageMsg.len = 8;
327 voltageMsg.buf[0] = 0x10;
328 voltageMsg.buf[1] = 0x02;
329 voltageMsg.buf[2] = 0x00;
330 voltageMsg.buf[3] = 0x00;
331 voltageMsg.buf[4] = voltage >> 24 & 0xff;
332 voltageMsg.buf[5] = voltage >> 16 & 0xff;
333 voltageMsg.buf[6] = voltage >> 8 & 0xff;
334 voltageMsg.buf[7] = voltage & 0xff;
335
336 can1.write(voltageMsg); // send message
337}
338
339void setCurrent(uint32_t current) {
340 CAN_message_t currentMsg;
341
342 currentMsg.id = CH_ID;
343 currentMsg.flags.extended = 1;
344 currentMsg.len = 8;
345 currentMsg.buf[0] = 0x10;
346 currentMsg.buf[1] = 0x03;
347 currentMsg.buf[2] = 0x00;
348 currentMsg.buf[3] = 0x00;
349 currentMsg.buf[4] = current >> 24 & 0xff;
350 currentMsg.buf[5] = current >> 16 & 0xff;
351 currentMsg.buf[6] = current >> 8 & 0xff;
352 currentMsg.buf[7] = current & 0xff;
353
354 can1.write(currentMsg); // send message
355}
356
357void setLOW() {
358 CAN_message_t LowMsg;
359
360 LowMsg.id = CH_ID;
361 LowMsg.flags.extended = 1;
362 LowMsg.len = 8;
363 LowMsg.buf[0] = 0x10;
364 LowMsg.buf[1] = 0x5f;
365 LowMsg.buf[2] = 0x00;
366 LowMsg.buf[3] = 0x00;
367 LowMsg.buf[4] = 0x00;
368 LowMsg.buf[5] = 0x00;
369 LowMsg.buf[6] = 0x00;
370 LowMsg.buf[7] = 0x00;
371
372 can1.write(LowMsg); // send message
373}
374
380
381void loop() {
382 // Serial.println("aa");
383
384 if (step < 1000)
385 return;
386
387 step = 0;
388
389 readInputs();
391 printStates();
393
394 if (param.ccl < MAX_CURRENT) {
396 } else {
398 }
399
400 for (int i = 0; i < 60; i++) {
401 Serial.print("temp");
402 Serial.print(i);
403 Serial.print(": ");
404 // Serial.println(param.temp[i]);
405
406 if(i == 20 or i == 21 or i== 29){
407 Serial.println(param.temp[19]);
408 continue;
409 }
410 if(param.temp[i] == 0)
411 {
412 Serial.println("Thermistor turned off");
413 }else
414 {
415 Serial.println(param.temp[i]);
416 }
417 // param.temp[i] = 0;
418 }
419
420 // Serial.printf("maxallowed: %d\n", param.allowedCurrent);
421
422
424
425 // Serial.println(CH_Status_Strings[NX_CH_Status]);
426 // Serial.printf("ccl: %d\n", param.ccl);
427
428 // Serial.println("loop");
429}
void setup()
Definition main.cpp:10
void loop()
AMI main loop: Button gets picked and lights up the corresponding LED LAST BUTTON CHECKED WINS.
Definition main.cpp:23
int voltage
Definition main.cpp:60
int current
Definition main.cpp:59
FlexCAN_T4< CAN1, RX_SIZE_256, TX_SIZE_16 > can1
Definition main.cpp:87
#define BMS_ID_CCL
Definition can.h:6
#define CH_ID
Definition can.h:8
#define TA_ID
Definition can.h:9
#define BMS_ID_ERR
Definition can.h:7
@ idle
Definition main.cpp:27
@ shutdown
Definition main.cpp:29
@ charging
Definition main.cpp:28
status CH_Status
Definition main.cpp:32
#define MAX_VOLTAGE
Definition main.cpp:16
void printStates()
Definition main.cpp:288
bool shutdownStatus
Definition main.cpp:23
void chargerMachine()
Definition main.cpp:261
status NX_CH_Status
Definition main.cpp:33
bool charge
Definition main.cpp:22
#define LATCHING_ERROR_PIN
Definition main.cpp:14
#define MAX_CURRENT
Definition main.cpp:17
PARAMETERS param
Definition main.cpp:19
void powerOnModule(bool OnOff)
Definition main.cpp:300
void setCurrent(uint32_t current)
Definition main.cpp:339
elapsedMillis step
Definition main.cpp:9
#define CH_SAFETY_PIN
Definition main.cpp:13
void canint(const CAN_message_t &message)
Definition main.cpp:234
bool latchingStatus
Definition main.cpp:24
elapsedMillis temp
Definition main.cpp:10
void updateCharger(status CH_Status)
Definition main.cpp:375
#define SHUTDOWN_PIN
Definition main.cpp:12
void parseMessage(CAN_message_t message)
Definition main.cpp:37
void setLOW()
Definition main.cpp:357
bool request
Definition main.cpp:21
void setVoltage(uint32_t voltage)
Definition main.cpp:321
void readInputs()
Definition main.cpp:294
String CH_Status_Strings[]
Definition main.cpp:35
status
uint32_t setVoltage
Definition can.h:12
uint32_t allowedCurrent
Definition can.h:14
uint32_t currCurrent
Definition can.h:16
uint32_t currVoltage
Definition can.h:13
uint32_t ccl
Definition can.h:17
int16_t temp[60]
Definition can.h:18
uint32_t setCurrent
Definition can.h:15