CUER
/
charger_code
Code to run on the charger board (used to charge the car from the mains).
charger.cpp@2:da91931184de, 2017-07-23 (annotated)
- Committer:
- DasSidG
- Date:
- Sun Jul 23 21:57:34 2017 +0000
- Revision:
- 2:da91931184de
- Parent:
- 1:0c77e20b4d4c
- Child:
- 3:a7626dffb64a
test to see if i can publish anything
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:0c77e20b4d4c | 20 | void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage); |
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 | |
DasSidG | 1:0c77e20b4d4c | 57 | update_LEDS(); |
DasSidG | 1:0c77e20b4d4c | 58 | |
DasSidG | 1:0c77e20b4d4c | 59 | //TODO: add logic here to calculate current and then decide if charging should go ahead |
DasSidG | 1:0c77e20b4d4c | 60 | |
DasSidG | 1:0c77e20b4d4c | 61 | |
DasSidG | 1:0c77e20b4d4c | 62 | //send CAN data |
DasSidG | 1:0c77e20b4d4c | 63 | charger_can.write(generate_charger_control_msg(desired_voltage, desired_current, charger_control)); //control message to charger |
DasSidG | 1:0c77e20b4d4c | 64 | car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car |
DasSidG | 1:0c77e20b4d4c | 65 | |
DasSidG | 1:0c77e20b4d4c | 66 | //send charge_finished value to control bit. |
DasSidG | 1:0c77e20b4d4c | 67 | |
DasSidG | 1:0c77e20b4d4c | 68 | printf("Voltage Error = %f\n", voltage_error); |
DasSidG | 1:0c77e20b4d4c | 69 | printf("Temperature Margin = %f\n", temp_margin); |
DasSidG | 1:0c77e20b4d4c | 70 | printf("Desired Voltage = %f\n", desired_voltage); |
DasSidG | 1:0c77e20b4d4c | 71 | printf("Desired Current = %f\n", desired_current); |
DasSidG | 1:0c77e20b4d4c | 72 | printf("Voltage = %f\n", charger_voltage); |
DasSidG | 1:0c77e20b4d4c | 73 | printf("Current = %f\n", charger_current); |
DasSidG | 1:0c77e20b4d4c | 74 | printf("Min cell voltage = %f\n", min_cell_voltage); |
DasSidG | 1:0c77e20b4d4c | 75 | printf("Max cell voltage = %f\n", max_cell_voltage); |
DasSidG | 1:0c77e20b4d4c | 76 | |
drajan | 0:6d930d0d13a1 | 77 | } |
DasSidG | 1:0c77e20b4d4c | 78 | } |
drajan | 0:6d930d0d13a1 | 79 | |
drajan | 0:6d930d0d13a1 | 80 | void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage){ |
drajan | 0:6d930d0d13a1 | 81 | |
drajan | 0:6d930d0d13a1 | 82 | float Idot, I; |
drajan | 0:6d930d0d13a1 | 83 | static bool balancing = false; |
drajan | 0:6d930d0d13a1 | 84 | I = *current; |
drajan | 0:6d930d0d13a1 | 85 | if (I < 0.8 && voltage_error < 0.1 || balancing) { |
drajan | 0:6d930d0d13a1 | 86 | balancing = true; |
drajan | 0:6d930d0d13a1 | 87 | printf("balancing\r\n"); |
drajan | 0:6d930d0d13a1 | 88 | Idot = voltage_error*KI_BALANCE; |
drajan | 0:6d930d0d13a1 | 89 | } else { |
drajan | 0:6d930d0d13a1 | 90 | Idot = voltage_error*KI_CHARGE; |
drajan | 0:6d930d0d13a1 | 91 | } |
drajan | 0:6d930d0d13a1 | 92 | I += Idot*TIME_STEP/1000.0; |
drajan | 0:6d930d0d13a1 | 93 | |
drajan | 0:6d930d0d13a1 | 94 | if(I > MAX_CURRENT) { |
drajan | 0:6d930d0d13a1 | 95 | I = MAX_CURRENT; |
drajan | 0:6d930d0d13a1 | 96 | } |
drajan | 0:6d930d0d13a1 | 97 | if(I < 0) { |
drajan | 0:6d930d0d13a1 | 98 | I = 0; |
drajan | 0:6d930d0d13a1 | 99 | } |
drajan | 0:6d930d0d13a1 | 100 | |
drajan | 0:6d930d0d13a1 | 101 | //Reduce current if temperature is too high |
drajan | 0:6d930d0d13a1 | 102 | if (temp_margin > TEMP_RAMP_START) { |
drajan | 0:6d930d0d13a1 | 103 | I = 1 - ((temp_margin - TEMP_RAMP_START) / (TEMP_RAMP_FINISH - TEMP_RAMP_START)); |
drajan | 0:6d930d0d13a1 | 104 | } |
drajan | 0:6d930d0d13a1 | 105 | if (temp_margin > TEMP_RAMP_FINISH) { |
drajan | 0:6d930d0d13a1 | 106 | I *= 0; |
drajan | 0:6d930d0d13a1 | 107 | } |
drajan | 0:6d930d0d13a1 | 108 | |
drajan | 0:6d930d0d13a1 | 109 | *current = I; |
drajan | 0:6d930d0d13a1 | 110 | *voltage = MAX_VOLTAGE; |
drajan | 0:6d930d0d13a1 | 111 | } |
drajan | 0:6d930d0d13a1 | 112 | |
DasSidG | 1:0c77e20b4d4c | 113 | void init() |
DasSidG | 1:0c77e20b4d4c | 114 | { |
DasSidG | 1:0c77e20b4d4c | 115 | for(int i=0; i<CAN_BUFFER_SIZE; i++) |
DasSidG | 1:0c77e20b4d4c | 116 | { |
DasSidG | 1:0c77e20b4d4c | 117 | car_buffer[i].id = BLANK_ID; |
DasSidG | 1:0c77e20b4d4c | 118 | //("%d",buffer[i].id); |
DasSidG | 1:0c77e20b4d4c | 119 | car_safe_to_write[i]= true; |
drajan | 0:6d930d0d13a1 | 120 | |
DasSidG | 1:0c77e20b4d4c | 121 | charger_buffer[i].id = BLANK_ID; |
DasSidG | 1:0c77e20b4d4c | 122 | //("%d",buffer[i].id); |
DasSidG | 1:0c77e20b4d4c | 123 | charger_safe_to_write[i]= true; |
DasSidG | 1:0c77e20b4d4c | 124 | } |
DasSidG | 1:0c77e20b4d4c | 125 | |
DasSidG | 1:0c77e20b4d4c | 126 | //Initialise CAN stuff, attach CAN interrupt handlers |
DasSidG | 1:0c77e20b4d4c | 127 | car_can.frequency(CAN_BIT_RATE); //set transmission rate to agreed bit rate |
DasSidG | 1:0c77e20b4d4c | 128 | car_can.reset(); |
DasSidG | 1:0c77e20b4d4c | 129 | car_can.attach(&car_interruptHandler, CAN::RxIrq); //receive interrupt handler |
DasSidG | 1:0c77e20b4d4c | 130 | car_can.attach(&car_CANDataSentCallback, CAN::TxIrq); //send interrupt handler |
DasSidG | 1:0c77e20b4d4c | 131 | |
DasSidG | 1:0c77e20b4d4c | 132 | charger_can.frequency(CHARGER_CAN_BIT_RATE); //set transmission rate to agreed bit rate |
DasSidG | 1:0c77e20b4d4c | 133 | charger_can.reset(); |
DasSidG | 1:0c77e20b4d4c | 134 | charger_can.attach(&charger_interruptHandler, CAN::RxIrq); //receive interrupt handler |
DasSidG | 1:0c77e20b4d4c | 135 | charger_can.attach(&charger_CANDataSentCallback, CAN::TxIrq); //send interrupt handler |
DasSidG | 1:0c77e20b4d4c | 136 | |
DasSidG | 1:0c77e20b4d4c | 137 | //Start comms timeout timers |
DasSidG | 1:0c77e20b4d4c | 138 | |
DasSidG | 1:0c77e20b4d4c | 139 | timeouts.BMS_timeout.start(); |
DasSidG | 1:0c77e20b4d4c | 140 | timeouts.charger_timeout.start(); |
DasSidG | 1:0c77e20b4d4c | 141 | |
DasSidG | 1:0c77e20b4d4c | 142 | } |
DasSidG | 1:0c77e20b4d4c | 143 | |
DasSidG | 1:0c77e20b4d4c | 144 | void car_CANDataSentCallback(void) { |
DasSidG | 1:0c77e20b4d4c | 145 | car_CAN_data_sent = true; |
DasSidG | 1:0c77e20b4d4c | 146 | } |
DasSidG | 1:0c77e20b4d4c | 147 | |
DasSidG | 1:0c77e20b4d4c | 148 | void charger_CANDataSentCallback(void) { |
DasSidG | 1:0c77e20b4d4c | 149 | charger_CAN_data_sent = true; |
DasSidG | 1:0c77e20b4d4c | 150 | } |
DasSidG | 1:0c77e20b4d4c | 151 | |
DasSidG | 1:0c77e20b4d4c | 152 | void car_interruptHandler() |
DasSidG | 1:0c77e20b4d4c | 153 | { |
DasSidG | 1:0c77e20b4d4c | 154 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 155 | car_can.read(msg); |
DasSidG | 1:0c77e20b4d4c | 156 | //if(DEBUG) printf("id %d incoming \r\n", msg.id); |
DasSidG | 1:0c77e20b4d4c | 157 | if(idAccepted(msg.id)) { |
DasSidG | 1:0c77e20b4d4c | 158 | for(int i=0; i<CAN_BUFFER_SIZE; i++) { |
DasSidG | 1:0c77e20b4d4c | 159 | if((car_buffer[i].id == msg.id || car_buffer[i].id==BLANK_ID) && car_safe_to_write[i]) { |
DasSidG | 1:0c77e20b4d4c | 160 | //("id %d added to buffer \r\n", msg.id); |
DasSidG | 1:0c77e20b4d4c | 161 | car_buffer[i] = msg; |
DasSidG | 1:0c77e20b4d4c | 162 | //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 | 163 | return; |
DasSidG | 1:0c77e20b4d4c | 164 | } |
DasSidG | 1:0c77e20b4d4c | 165 | } |
DasSidG | 1:0c77e20b4d4c | 166 | } |
DasSidG | 1:0c77e20b4d4c | 167 | } |
DasSidG | 1:0c77e20b4d4c | 168 | |
DasSidG | 1:0c77e20b4d4c | 169 | void charger_interruptHandler() |
DasSidG | 1:0c77e20b4d4c | 170 | { |
DasSidG | 1:0c77e20b4d4c | 171 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 172 | charger_can.read(msg); |
DasSidG | 1:0c77e20b4d4c | 173 | //if(DEBUG) printf("id %d incoming \r\n", msg.id); |
DasSidG | 1:0c77e20b4d4c | 174 | if(idAccepted(msg.id)) { |
DasSidG | 1:0c77e20b4d4c | 175 | for(int i=0; i<CAN_BUFFER_SIZE; i++) { |
DasSidG | 1:0c77e20b4d4c | 176 | if((charger_buffer[i].id == msg.id || charger_buffer[i].id==BLANK_ID) && charger_safe_to_write[i]) { |
DasSidG | 1:0c77e20b4d4c | 177 | //("id %d added to buffer \r\n", msg.id); |
DasSidG | 1:0c77e20b4d4c | 178 | charger_buffer[i] = msg; |
DasSidG | 1:0c77e20b4d4c | 179 | //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 | 180 | return; |
DasSidG | 1:0c77e20b4d4c | 181 | } |
DasSidG | 1:0c77e20b4d4c | 182 | } |
DasSidG | 1:0c77e20b4d4c | 183 | } |
DasSidG | 1:0c77e20b4d4c | 184 | } |
DasSidG | 1:0c77e20b4d4c | 185 | |
DasSidG | 1:0c77e20b4d4c | 186 | bool idAccepted(int id) |
DasSidG | 1:0c77e20b4d4c | 187 | { |
DasSidG | 1:0c77e20b4d4c | 188 | switch(id) { |
DasSidG | 1:0c77e20b4d4c | 189 | case BMS_BASE_ID: |
DasSidG | 1:0c77e20b4d4c | 190 | timeouts.BMS_timeout.reset(); |
DasSidG | 1:0c77e20b4d4c | 191 | return true; |
DasSidG | 1:0c77e20b4d4c | 192 | |
DasSidG | 1:0c77e20b4d4c | 193 | case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: |
DasSidG | 1:0c77e20b4d4c | 194 | return true; |
DasSidG | 1:0c77e20b4d4c | 195 | |
DasSidG | 1:0c77e20b4d4c | 196 | case BMS_BASE_ID + MAX_MIN_VOLTAGE: |
DasSidG | 1:0c77e20b4d4c | 197 | return true; |
DasSidG | 1:0c77e20b4d4c | 198 | |
DasSidG | 1:0c77e20b4d4c | 199 | case BMS_BASE_ID + BATTERY_STATUS_ID: |
DasSidG | 1:0c77e20b4d4c | 200 | return true; |
DasSidG | 1:0c77e20b4d4c | 201 | |
DasSidG | 1:0c77e20b4d4c | 202 | case CHARGER_VI_INFO_ID: |
DasSidG | 1:0c77e20b4d4c | 203 | timeouts.charger_timeout.reset(); |
DasSidG | 1:0c77e20b4d4c | 204 | return true; |
DasSidG | 1:0c77e20b4d4c | 205 | default: |
DasSidG | 1:0c77e20b4d4c | 206 | return false; |
DasSidG | 1:0c77e20b4d4c | 207 | } |
DasSidG | 1:0c77e20b4d4c | 208 | } |
drajan | 0:6d930d0d13a1 | 209 | |
drajan | 0:6d930d0d13a1 | 210 | |
drajan | 0:6d930d0d13a1 | 211 | |
drajan | 0:6d930d0d13a1 | 212 | |
drajan | 0:6d930d0d13a1 | 213 | |
DasSidG | 1:0c77e20b4d4c | 214 | void check_timeouts() //Check if it's been too long since any of the other devices in the car have communicated |
DasSidG | 1:0c77e20b4d4c | 215 | { |
DasSidG | 1:0c77e20b4d4c | 216 | |
DasSidG | 1:0c77e20b4d4c | 217 | if (timeouts.BMS_timeout.read_ms() > BMS_MSG_TIMEOUT_MS) |
DasSidG | 1:0c77e20b4d4c | 218 | { |
DasSidG | 1:0c77e20b4d4c | 219 | printf("Error: BMS comms timeout"); |
DasSidG | 1:0c77e20b4d4c | 220 | comms_timeout = true; |
DasSidG | 1:0c77e20b4d4c | 221 | } |
DasSidG | 1:0c77e20b4d4c | 222 | |
DasSidG | 1:0c77e20b4d4c | 223 | if (timeouts.charger_timeout.read_ms() > CHARGER_MSG_TIMEOUT_MS) |
DasSidG | 1:0c77e20b4d4c | 224 | { |
DasSidG | 1:0c77e20b4d4c | 225 | printf("Error: BMS comms timeout"); |
DasSidG | 1:0c77e20b4d4c | 226 | comms_timeout = true; |
DasSidG | 1:0c77e20b4d4c | 227 | } |
DasSidG | 1:0c77e20b4d4c | 228 | } |
DasSidG | 1:0c77e20b4d4c | 229 | |
DasSidG | 1:0c77e20b4d4c | 230 | void update_LEDS() { |
DasSidG | 1:0c77e20b4d4c | 231 | if (charger_failure || bms_error) { |
DasSidG | 1:0c77e20b4d4c | 232 | desired_current = 0; |
DasSidG | 1:0c77e20b4d4c | 233 | red_led = 1; |
DasSidG | 1:0c77e20b4d4c | 234 | } |
drajan | 0:6d930d0d13a1 | 235 | |
DasSidG | 1:0c77e20b4d4c | 236 | else if (comms_timeout) { |
DasSidG | 1:0c77e20b4d4c | 237 | desired_current = 0; |
DasSidG | 1:0c77e20b4d4c | 238 | yellow_led = 1; |
DasSidG | 1:0c77e20b4d4c | 239 | } |
DasSidG | 1:0c77e20b4d4c | 240 | |
DasSidG | 1:0c77e20b4d4c | 241 | else if (!charge_finished) { |
DasSidG | 1:0c77e20b4d4c | 242 | green_led = 1; |
DasSidG | 1:0c77e20b4d4c | 243 | } |
DasSidG | 1:0c77e20b4d4c | 244 | |
DasSidG | 1:0c77e20b4d4c | 245 | else { |
DasSidG | 1:0c77e20b4d4c | 246 | red_led = 0; |
DasSidG | 1:0c77e20b4d4c | 247 | yellow_led = 0; |
DasSidG | 1:0c77e20b4d4c | 248 | green_led = 0; |
DasSidG | 1:0c77e20b4d4c | 249 | } |
DasSidG | 1:0c77e20b4d4c | 250 | |
DasSidG | 1:0c77e20b4d4c | 251 | |
DasSidG | 1:0c77e20b4d4c | 252 | if (min_cell_voltage > RISING_BALANCE_THRESHOLD) { |
DasSidG | 1:0c77e20b4d4c | 253 | charge_finished = true; |
DasSidG | 1:0c77e20b4d4c | 254 | printf("Charge Finished\r\n"); |
DasSidG | 1:0c77e20b4d4c | 255 | //set charger control bit to finished charging |
DasSidG | 1:0c77e20b4d4c | 256 | } |
DasSidG | 1:0c77e20b4d4c | 257 | else { |
DasSidG | 1:0c77e20b4d4c | 258 | calculate_current(voltage_error, temp_margin, &desired_current, &desired_voltage); |
DasSidG | 1:0c77e20b4d4c | 259 | charge_finished = false; |
DasSidG | 1:0c77e20b4d4c | 260 | } |
DasSidG | 1:0c77e20b4d4c | 261 | } |
DasSidG | 1:0c77e20b4d4c | 262 | |
DasSidG | 1:0c77e20b4d4c | 263 | void get_CAN_data() { |
drajan | 0:6d930d0d13a1 | 264 | |
DasSidG | 1:0c77e20b4d4c | 265 | //Import the data from the buffer into a non-volatile, more usable format |
DasSidG | 1:0c77e20b4d4c | 266 | CAN_Data car_can_data[CAN_BUFFER_SIZE]; //container for all of the raw data |
DasSidG | 1:0c77e20b4d4c | 267 | CANMessage car_msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter |
DasSidG | 1:0c77e20b4d4c | 268 | int car_received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far |
DasSidG | 1:0c77e20b4d4c | 269 | for (int i = 0; i<CAN_BUFFER_SIZE; ++i) |
DasSidG | 1:0c77e20b4d4c | 270 | { |
DasSidG | 1:0c77e20b4d4c | 271 | car_safe_to_write[i] = false; |
DasSidG | 1:0c77e20b4d4c | 272 | car_can_data[i].importCANData(car_buffer[i]); |
DasSidG | 1:0c77e20b4d4c | 273 | car_received_CAN_IDs[i] = car_buffer[i].id; |
DasSidG | 1:0c77e20b4d4c | 274 | car_msgArray[i] = car_buffer[i]; |
DasSidG | 1:0c77e20b4d4c | 275 | car_safe_to_write[i] = true; |
DasSidG | 1:0c77e20b4d4c | 276 | |
DasSidG | 1:0c77e20b4d4c | 277 | //Now actually import the data from the CAN packets into the global variables |
DasSidG | 1:0c77e20b4d4c | 278 | switch (car_received_CAN_IDs[i]) { |
drajan | 0:6d930d0d13a1 | 279 | |
DasSidG | 1:0c77e20b4d4c | 280 | case BMS_BASE_ID: |
DasSidG | 1:0c77e20b4d4c | 281 | break; |
DasSidG | 1:0c77e20b4d4c | 282 | |
DasSidG | 1:0c77e20b4d4c | 283 | case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: |
DasSidG | 1:0c77e20b4d4c | 284 | get_charger_control_info(car_msgArray[i], voltage_error, temp_margin, discharge_error, pack_capacity); |
DasSidG | 1:0c77e20b4d4c | 285 | break; |
DasSidG | 1:0c77e20b4d4c | 286 | |
DasSidG | 1:0c77e20b4d4c | 287 | case BMS_BASE_ID + MAX_MIN_VOLTAGE: |
DasSidG | 1:0c77e20b4d4c | 288 | get_max_min_voltage(car_msgArray[i], min_cell_voltage, max_cell_voltage); |
DasSidG | 1:0c77e20b4d4c | 289 | break; |
DasSidG | 1:0c77e20b4d4c | 290 | |
DasSidG | 1:0c77e20b4d4c | 291 | case BMS_BASE_ID + BATTERY_STATUS_ID: |
DasSidG | 1:0c77e20b4d4c | 292 | get_battery_status(car_msgArray[i], bms_error); |
DasSidG | 1:0c77e20b4d4c | 293 | break; |
DasSidG | 1:0c77e20b4d4c | 294 | case BLANK_ID: //This means we haven't received this type of message yet, so do nothing |
DasSidG | 1:0c77e20b4d4c | 295 | break; |
DasSidG | 1:0c77e20b4d4c | 296 | default: |
DasSidG | 1:0c77e20b4d4c | 297 | break; |
DasSidG | 1:0c77e20b4d4c | 298 | } |
DasSidG | 1:0c77e20b4d4c | 299 | } |
drajan | 0:6d930d0d13a1 | 300 | |
DasSidG | 1:0c77e20b4d4c | 301 | //Import the data from the buffer into a non-volatile, more usable format |
DasSidG | 1:0c77e20b4d4c | 302 | CAN_Data charger_can_data[CAN_BUFFER_SIZE]; //container for all of the raw data |
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_can_data[i].importCANData(charger_buffer[i]); |
DasSidG | 1:0c77e20b4d4c | 309 | charger_received_CAN_IDs[i] = charger_buffer[i].id; |
DasSidG | 1:0c77e20b4d4c | 310 | charger_msgArray[i] = charger_buffer[i]; |
DasSidG | 1:0c77e20b4d4c | 311 | charger_safe_to_write[i] = true; |
DasSidG | 1:0c77e20b4d4c | 312 | |
DasSidG | 1:0c77e20b4d4c | 313 | //Now actually import the data from the CAN packets into the global variables |
DasSidG | 1:0c77e20b4d4c | 314 | switch (charger_received_CAN_IDs[i]) { |
DasSidG | 1:0c77e20b4d4c | 315 | |
DasSidG | 1:0c77e20b4d4c | 316 | case CHARGER_VI_INFO_ID: |
DasSidG | 1:0c77e20b4d4c | 317 | get_charger_VI_info(charger_msgArray[i], charger_voltage, charger_current, charger_status); |
DasSidG | 1:0c77e20b4d4c | 318 | if(charger_status bitand 1 == 1) { |
DasSidG | 1:0c77e20b4d4c | 319 | printf("Charger status: Hardware Failure\r\n"); |
DasSidG | 1:0c77e20b4d4c | 320 | charger_failure = true; |
DasSidG | 1:0c77e20b4d4c | 321 | } |
DasSidG | 1:0c77e20b4d4c | 322 | |
DasSidG | 1:0c77e20b4d4c | 323 | if(charger_status bitand 2 == 2) { |
DasSidG | 1:0c77e20b4d4c | 324 | printf("Charger status: Over Temperature\r\n"); |
DasSidG | 1:0c77e20b4d4c | 325 | charger_failure = true; |
DasSidG | 1:0c77e20b4d4c | 326 | } |
DasSidG | 1:0c77e20b4d4c | 327 | |
DasSidG | 1:0c77e20b4d4c | 328 | if(charger_status bitand 4 == 4) { |
DasSidG | 1:0c77e20b4d4c | 329 | printf("Charger status: Input Voltage Wrong\r\n"); |
DasSidG | 1:0c77e20b4d4c | 330 | charger_failure = true; |
DasSidG | 1:0c77e20b4d4c | 331 | } |
DasSidG | 1:0c77e20b4d4c | 332 | |
DasSidG | 1:0c77e20b4d4c | 333 | if(charger_status bitand 8 == 8) { |
DasSidG | 1:0c77e20b4d4c | 334 | printf("Charger status: Reverse Polarity\r\n"); |
DasSidG | 1:0c77e20b4d4c | 335 | charger_failure = true; |
DasSidG | 1:0c77e20b4d4c | 336 | } |
DasSidG | 1:0c77e20b4d4c | 337 | |
DasSidG | 1:0c77e20b4d4c | 338 | if(charger_status bitand 16 == 16) { |
DasSidG | 1:0c77e20b4d4c | 339 | printf("Charger status: communication timeout\r\n"); |
DasSidG | 1:0c77e20b4d4c | 340 | charger_failure = true; |
DasSidG | 1:0c77e20b4d4c | 341 | } |
DasSidG | 1:0c77e20b4d4c | 342 | break; |
DasSidG | 1:0c77e20b4d4c | 343 | case BLANK_ID: //This means we haven't received this type of message yet, so do nothing |
DasSidG | 1:0c77e20b4d4c | 344 | break; |
DasSidG | 1:0c77e20b4d4c | 345 | default: |
DasSidG | 1:0c77e20b4d4c | 346 | break; |
DasSidG | 1:0c77e20b4d4c | 347 | } |
drajan | 0:6d930d0d13a1 | 348 | } |
drajan | 0:6d930d0d13a1 | 349 | } |