Code to run on the charger board (used to charge the car from the mains).

Dependencies:   mbed CUER_CAN

Committer:
DasSidG
Date:
Thu Jul 27 21:21:32 2017 +0000
Revision:
4:f6459580c312
Parent:
3:a7626dffb64a
Child:
5:756fae795d37
Tidied up code a bit, replaced 'waits' for CAN sending with callbacks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
drajan 0:6d930d0d13a1 1 #include "charger.h"
DasSidG 1:0c77e20b4d4c 2 #include "mbed.h"
DasSidG 1:0c77e20b4d4c 3 #include "CAN_Data.h"
DasSidG 1:0c77e20b4d4c 4 #include "CAN_IDs.h"
DasSidG 1:0c77e20b4d4c 5 #include "Data_types.h"
DasSidG 1:0c77e20b4d4c 6 #include "CANParserCharger.h"
DasSidG 1:0c77e20b4d4c 7
DasSidG 2:da91931184de 8 //TEST PUBLISH
DasSidG 1:0c77e20b4d4c 9 using namespace CAN_IDs;
DasSidG 1:0c77e20b4d4c 10
DasSidG 1:0c77e20b4d4c 11 void car_interruptHandler();
DasSidG 1:0c77e20b4d4c 12 void car_CANDataSentCallback();
DasSidG 1:0c77e20b4d4c 13
DasSidG 1:0c77e20b4d4c 14 void charger_interruptHandler();
DasSidG 1:0c77e20b4d4c 15 void charger_CANDataSentCallback();
DasSidG 1:0c77e20b4d4c 16
DasSidG 1:0c77e20b4d4c 17 void init();
DasSidG 1:0c77e20b4d4c 18 void get_CAN_data();
DasSidG 1:0c77e20b4d4c 19
DasSidG 3:a7626dffb64a 20 void calculate_current(float voltage_error, float temp_margin, float &current);
DasSidG 1:0c77e20b4d4c 21 void check_timeouts();
DasSidG 1:0c77e20b4d4c 22 void update_LEDS();
DasSidG 1:0c77e20b4d4c 23 bool idAccepted(int id);
DasSidG 1:0c77e20b4d4c 24
drajan 0:6d930d0d13a1 25
drajan 0:6d930d0d13a1 26
DasSidG 1:0c77e20b4d4c 27 timeouts_t timeouts;
DasSidG 1:0c77e20b4d4c 28
DasSidG 1:0c77e20b4d4c 29 //CAN stuff. Not that there are two sets because there are two separate CAN buses here; one to communicate with the charger, and one to communicate with the rest of the car (importantly the BMS).
DasSidG 1:0c77e20b4d4c 30 //The reason that there are two separate CAN buses is because the charger uses the extended CAN frame format, with a 29 bit ID, whereas the car uses the default 11 bit ID.
DasSidG 1:0c77e20b4d4c 31 CAN car_can(CAR_CAN_READ_PIN, CAR_CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
DasSidG 1:0c77e20b4d4c 32 CANMessage car_buffer[CAN_BUFFER_SIZE]; //CAN receive buffer
DasSidG 1:0c77e20b4d4c 33 bool car_safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer
DasSidG 1:0c77e20b4d4c 34 bool car_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 35
DasSidG 1:0c77e20b4d4c 36 CAN charger_can(CHARGER_CAN_READ_PIN, CHARGER_CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
DasSidG 1:0c77e20b4d4c 37 CANMessage charger_buffer[CAN_BUFFER_SIZE]; //CAN receive buffer
DasSidG 1:0c77e20b4d4c 38 bool charger_safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer
DasSidG 1:0c77e20b4d4c 39 bool charger_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 40
DasSidG 1:0c77e20b4d4c 41 int main() {
drajan 0:6d930d0d13a1 42
DasSidG 1:0c77e20b4d4c 43 init();
drajan 0:6d930d0d13a1 44
DasSidG 1:0c77e20b4d4c 45 while(1) {
DasSidG 1:0c77e20b4d4c 46
DasSidG 1:0c77e20b4d4c 47 //Reset error indicators
DasSidG 1:0c77e20b4d4c 48 comms_timeout = false;
DasSidG 1:0c77e20b4d4c 49 charger_failure = false;
DasSidG 1:0c77e20b4d4c 50 bms_error = false;
DasSidG 1:0c77e20b4d4c 51
DasSidG 1:0c77e20b4d4c 52 //get the various data from the CAN packets
DasSidG 1:0c77e20b4d4c 53 get_CAN_data();
DasSidG 1:0c77e20b4d4c 54
DasSidG 1:0c77e20b4d4c 55 check_timeouts();
DasSidG 1:0c77e20b4d4c 56 update_LEDS();
DasSidG 1:0c77e20b4d4c 57
DasSidG 3:a7626dffb64a 58 if (min_cell_voltage > RISING_BALANCE_THRESHOLD || charge_finished) {
DasSidG 3:a7626dffb64a 59 charge_finished = true;
DasSidG 3:a7626dffb64a 60 printf("Charge Finished\r\n");
DasSidG 3:a7626dffb64a 61 car_can.write(generate_charging_finished_msg());
DasSidG 3:a7626dffb64a 62 charger_control = 0; //set charger control bit to stop charging
DasSidG 3:a7626dffb64a 63 }
DasSidG 3:a7626dffb64a 64 else {
DasSidG 3:a7626dffb64a 65 calculate_current(voltage_error, temp_margin, desired_current);
DasSidG 3:a7626dffb64a 66 desired_voltage = MAX_VOLTAGE;
DasSidG 3:a7626dffb64a 67 charge_finished = false;
DasSidG 3:a7626dffb64a 68 charger_control = 1; //set charger control bit to start charging
DasSidG 3:a7626dffb64a 69 }
DasSidG 1:0c77e20b4d4c 70
DasSidG 1:0c77e20b4d4c 71 //send CAN data
DasSidG 4:f6459580c312 72
DasSidG 4:f6459580c312 73 Timer t;
DasSidG 4:f6459580c312 74 t.start();
DasSidG 4:f6459580c312 75 charger_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 76 charger_can.write(generate_charger_control_msg(desired_voltage, desired_current, charger_control)); //control message to charger
DasSidG 4:f6459580c312 77 while(!charger_CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS);
DasSidG 4:f6459580c312 78
DasSidG 4:f6459580c312 79 t.reset();
DasSidG 4:f6459580c312 80 car_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 81 car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car
DasSidG 4:f6459580c312 82 while(!car_CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS);
DasSidG 1:0c77e20b4d4c 83
DasSidG 1:0c77e20b4d4c 84 printf("Voltage Error = %f\n", voltage_error);
DasSidG 1:0c77e20b4d4c 85 printf("Temperature Margin = %f\n", temp_margin);
DasSidG 1:0c77e20b4d4c 86 printf("Desired Voltage = %f\n", desired_voltage);
DasSidG 1:0c77e20b4d4c 87 printf("Desired Current = %f\n", desired_current);
DasSidG 4:f6459580c312 88 printf("Charger voltage = %f\n", charger_voltage);
DasSidG 4:f6459580c312 89 printf("Charger current = %f\n", charger_current);
DasSidG 1:0c77e20b4d4c 90 printf("Min cell voltage = %f\n", min_cell_voltage);
DasSidG 1:0c77e20b4d4c 91 printf("Max cell voltage = %f\n", max_cell_voltage);
DasSidG 1:0c77e20b4d4c 92
drajan 0:6d930d0d13a1 93 }
DasSidG 1:0c77e20b4d4c 94 }
drajan 0:6d930d0d13a1 95
DasSidG 3:a7626dffb64a 96 void calculate_current(float voltage_error, float temp_margin, float &current){
drajan 0:6d930d0d13a1 97
DasSidG 3:a7626dffb64a 98 float Idot, I;
DasSidG 3:a7626dffb64a 99 static bool balancing = false;
DasSidG 3:a7626dffb64a 100 I = current;
DasSidG 3:a7626dffb64a 101 if (I < 800 && voltage_error < 100 || balancing) {
DasSidG 3:a7626dffb64a 102 balancing = true;
DasSidG 3:a7626dffb64a 103 printf("balancing\r\n");
DasSidG 3:a7626dffb64a 104 Idot = voltage_error*KI_BALANCE;
DasSidG 3:a7626dffb64a 105 }
DasSidG 3:a7626dffb64a 106 else {
DasSidG 3:a7626dffb64a 107 Idot = voltage_error*KI_CHARGE;
DasSidG 3:a7626dffb64a 108 }
drajan 0:6d930d0d13a1 109 I += Idot*TIME_STEP/1000.0;
drajan 0:6d930d0d13a1 110
drajan 0:6d930d0d13a1 111 if(I > MAX_CURRENT) {
drajan 0:6d930d0d13a1 112 I = MAX_CURRENT;
drajan 0:6d930d0d13a1 113 }
drajan 0:6d930d0d13a1 114 if(I < 0) {
drajan 0:6d930d0d13a1 115 I = 0;
drajan 0:6d930d0d13a1 116 }
DasSidG 3:a7626dffb64a 117 /*
drajan 0:6d930d0d13a1 118 //Reduce current if temperature is too high
drajan 0:6d930d0d13a1 119 if (temp_margin > TEMP_RAMP_START) {
drajan 0:6d930d0d13a1 120 I = 1 - ((temp_margin - TEMP_RAMP_START) / (TEMP_RAMP_FINISH - TEMP_RAMP_START));
drajan 0:6d930d0d13a1 121 }
drajan 0:6d930d0d13a1 122 if (temp_margin > TEMP_RAMP_FINISH) {
drajan 0:6d930d0d13a1 123 I *= 0;
drajan 0:6d930d0d13a1 124 }
DasSidG 3:a7626dffb64a 125 */
DasSidG 3:a7626dffb64a 126 current = I;
drajan 0:6d930d0d13a1 127 }
drajan 0:6d930d0d13a1 128
DasSidG 1:0c77e20b4d4c 129 void init()
DasSidG 1:0c77e20b4d4c 130 {
DasSidG 1:0c77e20b4d4c 131 for(int i=0; i<CAN_BUFFER_SIZE; i++)
DasSidG 1:0c77e20b4d4c 132 {
DasSidG 1:0c77e20b4d4c 133 car_buffer[i].id = BLANK_ID;
DasSidG 1:0c77e20b4d4c 134 //("%d",buffer[i].id);
DasSidG 1:0c77e20b4d4c 135 car_safe_to_write[i]= true;
drajan 0:6d930d0d13a1 136
DasSidG 1:0c77e20b4d4c 137 charger_buffer[i].id = BLANK_ID;
DasSidG 1:0c77e20b4d4c 138 //("%d",buffer[i].id);
DasSidG 1:0c77e20b4d4c 139 charger_safe_to_write[i]= true;
DasSidG 1:0c77e20b4d4c 140 }
DasSidG 1:0c77e20b4d4c 141
DasSidG 1:0c77e20b4d4c 142 //Initialise CAN stuff, attach CAN interrupt handlers
DasSidG 1:0c77e20b4d4c 143 car_can.frequency(CAN_BIT_RATE); //set transmission rate to agreed bit rate
DasSidG 1:0c77e20b4d4c 144 car_can.reset();
DasSidG 1:0c77e20b4d4c 145 car_can.attach(&car_interruptHandler, CAN::RxIrq); //receive interrupt handler
DasSidG 1:0c77e20b4d4c 146 car_can.attach(&car_CANDataSentCallback, CAN::TxIrq); //send interrupt handler
DasSidG 1:0c77e20b4d4c 147
DasSidG 1:0c77e20b4d4c 148 charger_can.frequency(CHARGER_CAN_BIT_RATE); //set transmission rate to agreed bit rate
DasSidG 1:0c77e20b4d4c 149 charger_can.reset();
DasSidG 1:0c77e20b4d4c 150 charger_can.attach(&charger_interruptHandler, CAN::RxIrq); //receive interrupt handler
DasSidG 1:0c77e20b4d4c 151 charger_can.attach(&charger_CANDataSentCallback, CAN::TxIrq); //send interrupt handler
DasSidG 1:0c77e20b4d4c 152
DasSidG 1:0c77e20b4d4c 153 //Start comms timeout timers
DasSidG 1:0c77e20b4d4c 154
DasSidG 1:0c77e20b4d4c 155 timeouts.BMS_timeout.start();
DasSidG 1:0c77e20b4d4c 156 timeouts.charger_timeout.start();
DasSidG 1:0c77e20b4d4c 157
DasSidG 1:0c77e20b4d4c 158 }
DasSidG 1:0c77e20b4d4c 159
DasSidG 1:0c77e20b4d4c 160 void car_CANDataSentCallback(void) {
DasSidG 1:0c77e20b4d4c 161 car_CAN_data_sent = true;
DasSidG 1:0c77e20b4d4c 162 }
DasSidG 1:0c77e20b4d4c 163
DasSidG 1:0c77e20b4d4c 164 void charger_CANDataSentCallback(void) {
DasSidG 1:0c77e20b4d4c 165 charger_CAN_data_sent = true;
DasSidG 1:0c77e20b4d4c 166 }
DasSidG 1:0c77e20b4d4c 167
DasSidG 1:0c77e20b4d4c 168 void car_interruptHandler()
DasSidG 1:0c77e20b4d4c 169 {
DasSidG 1:0c77e20b4d4c 170 CANMessage msg;
DasSidG 1:0c77e20b4d4c 171 car_can.read(msg);
DasSidG 1:0c77e20b4d4c 172 //if(DEBUG) printf("id %d incoming \r\n", msg.id);
DasSidG 1:0c77e20b4d4c 173 if(idAccepted(msg.id)) {
DasSidG 1:0c77e20b4d4c 174 for(int i=0; i<CAN_BUFFER_SIZE; i++) {
DasSidG 1:0c77e20b4d4c 175 if((car_buffer[i].id == msg.id || car_buffer[i].id==BLANK_ID) && car_safe_to_write[i]) {
DasSidG 1:0c77e20b4d4c 176 //("id %d added to buffer \r\n", msg.id);
DasSidG 1:0c77e20b4d4c 177 car_buffer[i] = msg;
DasSidG 1:0c77e20b4d4c 178 //return required so that only first blank buffer entry is converted to incoming message ID each time new message ID is encountered
DasSidG 1:0c77e20b4d4c 179 return;
DasSidG 1:0c77e20b4d4c 180 }
DasSidG 1:0c77e20b4d4c 181 }
DasSidG 1:0c77e20b4d4c 182 }
DasSidG 1:0c77e20b4d4c 183 }
DasSidG 1:0c77e20b4d4c 184
DasSidG 1:0c77e20b4d4c 185 void charger_interruptHandler()
DasSidG 1:0c77e20b4d4c 186 {
DasSidG 1:0c77e20b4d4c 187 CANMessage msg;
DasSidG 1:0c77e20b4d4c 188 charger_can.read(msg);
DasSidG 1:0c77e20b4d4c 189 //if(DEBUG) printf("id %d incoming \r\n", msg.id);
DasSidG 1:0c77e20b4d4c 190 if(idAccepted(msg.id)) {
DasSidG 1:0c77e20b4d4c 191 for(int i=0; i<CAN_BUFFER_SIZE; i++) {
DasSidG 1:0c77e20b4d4c 192 if((charger_buffer[i].id == msg.id || charger_buffer[i].id==BLANK_ID) && charger_safe_to_write[i]) {
DasSidG 1:0c77e20b4d4c 193 //("id %d added to buffer \r\n", msg.id);
DasSidG 1:0c77e20b4d4c 194 charger_buffer[i] = msg;
DasSidG 1:0c77e20b4d4c 195 //return required so that only first blank buffer entry is converted to incoming message ID each time new message ID is encountered
DasSidG 1:0c77e20b4d4c 196 return;
DasSidG 1:0c77e20b4d4c 197 }
DasSidG 1:0c77e20b4d4c 198 }
DasSidG 1:0c77e20b4d4c 199 }
DasSidG 1:0c77e20b4d4c 200 }
DasSidG 1:0c77e20b4d4c 201
DasSidG 1:0c77e20b4d4c 202 bool idAccepted(int id)
DasSidG 1:0c77e20b4d4c 203 {
DasSidG 1:0c77e20b4d4c 204 switch(id) {
DasSidG 1:0c77e20b4d4c 205 case BMS_BASE_ID:
DasSidG 1:0c77e20b4d4c 206 timeouts.BMS_timeout.reset();
DasSidG 1:0c77e20b4d4c 207 return true;
DasSidG 1:0c77e20b4d4c 208
DasSidG 1:0c77e20b4d4c 209 case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID:
DasSidG 1:0c77e20b4d4c 210 return true;
DasSidG 1:0c77e20b4d4c 211
DasSidG 1:0c77e20b4d4c 212 case BMS_BASE_ID + MAX_MIN_VOLTAGE:
DasSidG 1:0c77e20b4d4c 213 return true;
DasSidG 1:0c77e20b4d4c 214
DasSidG 1:0c77e20b4d4c 215 case BMS_BASE_ID + BATTERY_STATUS_ID:
DasSidG 1:0c77e20b4d4c 216 return true;
DasSidG 1:0c77e20b4d4c 217
DasSidG 1:0c77e20b4d4c 218 case CHARGER_VI_INFO_ID:
DasSidG 1:0c77e20b4d4c 219 timeouts.charger_timeout.reset();
DasSidG 1:0c77e20b4d4c 220 return true;
DasSidG 1:0c77e20b4d4c 221 default:
DasSidG 1:0c77e20b4d4c 222 return false;
DasSidG 1:0c77e20b4d4c 223 }
DasSidG 1:0c77e20b4d4c 224 }
drajan 0:6d930d0d13a1 225
drajan 0:6d930d0d13a1 226
DasSidG 1:0c77e20b4d4c 227 void check_timeouts() //Check if it's been too long since any of the other devices in the car have communicated
DasSidG 1:0c77e20b4d4c 228 {
DasSidG 1:0c77e20b4d4c 229
DasSidG 1:0c77e20b4d4c 230 if (timeouts.BMS_timeout.read_ms() > BMS_MSG_TIMEOUT_MS)
DasSidG 1:0c77e20b4d4c 231 {
DasSidG 1:0c77e20b4d4c 232 printf("Error: BMS comms timeout");
DasSidG 1:0c77e20b4d4c 233 comms_timeout = true;
DasSidG 1:0c77e20b4d4c 234 }
DasSidG 1:0c77e20b4d4c 235
DasSidG 1:0c77e20b4d4c 236 if (timeouts.charger_timeout.read_ms() > CHARGER_MSG_TIMEOUT_MS)
DasSidG 1:0c77e20b4d4c 237 {
DasSidG 4:f6459580c312 238 printf("Error: Charger comms timeout");
DasSidG 1:0c77e20b4d4c 239 comms_timeout = true;
DasSidG 1:0c77e20b4d4c 240 }
DasSidG 1:0c77e20b4d4c 241 }
DasSidG 1:0c77e20b4d4c 242
DasSidG 1:0c77e20b4d4c 243 void update_LEDS() {
DasSidG 1:0c77e20b4d4c 244 if (charger_failure || bms_error) {
DasSidG 1:0c77e20b4d4c 245 desired_current = 0;
DasSidG 1:0c77e20b4d4c 246 red_led = 1;
DasSidG 1:0c77e20b4d4c 247 }
drajan 0:6d930d0d13a1 248
DasSidG 1:0c77e20b4d4c 249 else if (comms_timeout) {
DasSidG 1:0c77e20b4d4c 250 desired_current = 0;
DasSidG 1:0c77e20b4d4c 251 yellow_led = 1;
DasSidG 1:0c77e20b4d4c 252 }
DasSidG 1:0c77e20b4d4c 253
DasSidG 1:0c77e20b4d4c 254 else if (!charge_finished) {
DasSidG 1:0c77e20b4d4c 255 green_led = 1;
DasSidG 1:0c77e20b4d4c 256 }
DasSidG 1:0c77e20b4d4c 257
DasSidG 1:0c77e20b4d4c 258 else {
DasSidG 1:0c77e20b4d4c 259 red_led = 0;
DasSidG 1:0c77e20b4d4c 260 yellow_led = 0;
DasSidG 1:0c77e20b4d4c 261 green_led = 0;
DasSidG 4:f6459580c312 262 }
DasSidG 3:a7626dffb64a 263
DasSidG 1:0c77e20b4d4c 264 }
DasSidG 1:0c77e20b4d4c 265
DasSidG 1:0c77e20b4d4c 266 void get_CAN_data() {
drajan 0:6d930d0d13a1 267
DasSidG 1:0c77e20b4d4c 268 //Import the data from the buffer into a non-volatile, more usable format
DasSidG 1:0c77e20b4d4c 269 CANMessage car_msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter
DasSidG 1:0c77e20b4d4c 270 int car_received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
DasSidG 1:0c77e20b4d4c 271 for (int i = 0; i<CAN_BUFFER_SIZE; ++i)
DasSidG 1:0c77e20b4d4c 272 {
DasSidG 1:0c77e20b4d4c 273 car_safe_to_write[i] = false;
DasSidG 1:0c77e20b4d4c 274 car_received_CAN_IDs[i] = car_buffer[i].id;
DasSidG 1:0c77e20b4d4c 275 car_msgArray[i] = car_buffer[i];
DasSidG 1:0c77e20b4d4c 276 car_safe_to_write[i] = true;
DasSidG 1:0c77e20b4d4c 277
DasSidG 1:0c77e20b4d4c 278 //Now actually import the data from the CAN packets into the global variables
DasSidG 1:0c77e20b4d4c 279 switch (car_received_CAN_IDs[i]) {
drajan 0:6d930d0d13a1 280
DasSidG 1:0c77e20b4d4c 281 case BMS_BASE_ID:
DasSidG 1:0c77e20b4d4c 282 break;
DasSidG 1:0c77e20b4d4c 283
DasSidG 1:0c77e20b4d4c 284 case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID:
DasSidG 1:0c77e20b4d4c 285 get_charger_control_info(car_msgArray[i], voltage_error, temp_margin, discharge_error, pack_capacity);
DasSidG 1:0c77e20b4d4c 286 break;
DasSidG 1:0c77e20b4d4c 287
DasSidG 1:0c77e20b4d4c 288 case BMS_BASE_ID + MAX_MIN_VOLTAGE:
DasSidG 1:0c77e20b4d4c 289 get_max_min_voltage(car_msgArray[i], min_cell_voltage, max_cell_voltage);
DasSidG 1:0c77e20b4d4c 290 break;
DasSidG 1:0c77e20b4d4c 291
DasSidG 1:0c77e20b4d4c 292 case BMS_BASE_ID + BATTERY_STATUS_ID:
DasSidG 1:0c77e20b4d4c 293 get_battery_status(car_msgArray[i], bms_error);
DasSidG 1:0c77e20b4d4c 294 break;
DasSidG 1:0c77e20b4d4c 295 case BLANK_ID: //This means we haven't received this type of message yet, so do nothing
DasSidG 1:0c77e20b4d4c 296 break;
DasSidG 1:0c77e20b4d4c 297 default:
DasSidG 1:0c77e20b4d4c 298 break;
DasSidG 1:0c77e20b4d4c 299 }
DasSidG 1:0c77e20b4d4c 300 }
drajan 0:6d930d0d13a1 301
DasSidG 1:0c77e20b4d4c 302 //Import the data from the buffer into a non-volatile, more usable format
DasSidG 1:0c77e20b4d4c 303 CANMessage charger_msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter
DasSidG 1:0c77e20b4d4c 304 int charger_received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
DasSidG 1:0c77e20b4d4c 305 for (int i = 0; i<CAN_BUFFER_SIZE; ++i)
DasSidG 1:0c77e20b4d4c 306 {
DasSidG 1:0c77e20b4d4c 307 charger_safe_to_write[i] = false;
DasSidG 1:0c77e20b4d4c 308 charger_received_CAN_IDs[i] = charger_buffer[i].id;
DasSidG 1:0c77e20b4d4c 309 charger_msgArray[i] = charger_buffer[i];
DasSidG 1:0c77e20b4d4c 310 charger_safe_to_write[i] = true;
DasSidG 1:0c77e20b4d4c 311
DasSidG 1:0c77e20b4d4c 312 //Now actually import the data from the CAN packets into the global variables
DasSidG 1:0c77e20b4d4c 313 switch (charger_received_CAN_IDs[i]) {
DasSidG 1:0c77e20b4d4c 314
DasSidG 1:0c77e20b4d4c 315 case CHARGER_VI_INFO_ID:
DasSidG 1:0c77e20b4d4c 316 get_charger_VI_info(charger_msgArray[i], charger_voltage, charger_current, charger_status);
DasSidG 1:0c77e20b4d4c 317 if(charger_status bitand 1 == 1) {
DasSidG 1:0c77e20b4d4c 318 printf("Charger status: Hardware Failure\r\n");
DasSidG 1:0c77e20b4d4c 319 charger_failure = true;
DasSidG 1:0c77e20b4d4c 320 }
DasSidG 1:0c77e20b4d4c 321
DasSidG 1:0c77e20b4d4c 322 if(charger_status bitand 2 == 2) {
DasSidG 1:0c77e20b4d4c 323 printf("Charger status: Over Temperature\r\n");
DasSidG 1:0c77e20b4d4c 324 charger_failure = true;
DasSidG 1:0c77e20b4d4c 325 }
DasSidG 1:0c77e20b4d4c 326
DasSidG 1:0c77e20b4d4c 327 if(charger_status bitand 4 == 4) {
DasSidG 1:0c77e20b4d4c 328 printf("Charger status: Input Voltage Wrong\r\n");
DasSidG 1:0c77e20b4d4c 329 charger_failure = true;
DasSidG 1:0c77e20b4d4c 330 }
DasSidG 1:0c77e20b4d4c 331
DasSidG 1:0c77e20b4d4c 332 if(charger_status bitand 8 == 8) {
DasSidG 1:0c77e20b4d4c 333 printf("Charger status: Reverse Polarity\r\n");
DasSidG 1:0c77e20b4d4c 334 charger_failure = true;
DasSidG 1:0c77e20b4d4c 335 }
DasSidG 1:0c77e20b4d4c 336
DasSidG 1:0c77e20b4d4c 337 if(charger_status bitand 16 == 16) {
DasSidG 1:0c77e20b4d4c 338 printf("Charger status: communication timeout\r\n");
DasSidG 1:0c77e20b4d4c 339 charger_failure = true;
DasSidG 1:0c77e20b4d4c 340 }
DasSidG 1:0c77e20b4d4c 341 break;
DasSidG 1:0c77e20b4d4c 342 case BLANK_ID: //This means we haven't received this type of message yet, so do nothing
DasSidG 1:0c77e20b4d4c 343 break;
DasSidG 1:0c77e20b4d4c 344 default:
DasSidG 1:0c77e20b4d4c 345 break;
DasSidG 1:0c77e20b4d4c 346 }
drajan 0:6d930d0d13a1 347 }
drajan 0:6d930d0d13a1 348 }