Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
charger.cpp@1:0c77e20b4d4c, 2017-07-06 (annotated)
- Committer:
- DasSidG
- Date:
- Thu Jul 06 21:27:26 2017 +0000
- Revision:
- 1:0c77e20b4d4c
- Parent:
- 0:6d930d0d13a1
- Child:
- 2:da91931184de
Code majorly changed:; ; Structure changed significantly; All of the CAN-related functions, structures etc. added (but not yet tested); Still need to add some of the main program logic in terms of deciding when/when not to charge etc
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 | 1:0c77e20b4d4c | 8 | using namespace CAN_IDs; |
| DasSidG | 1:0c77e20b4d4c | 9 | |
| DasSidG | 1:0c77e20b4d4c | 10 | void car_interruptHandler(); |
| DasSidG | 1:0c77e20b4d4c | 11 | void car_CANDataSentCallback(); |
| DasSidG | 1:0c77e20b4d4c | 12 | |
| DasSidG | 1:0c77e20b4d4c | 13 | void charger_interruptHandler(); |
| DasSidG | 1:0c77e20b4d4c | 14 | void charger_CANDataSentCallback(); |
| DasSidG | 1:0c77e20b4d4c | 15 | |
| DasSidG | 1:0c77e20b4d4c | 16 | void init(); |
| DasSidG | 1:0c77e20b4d4c | 17 | void get_CAN_data(); |
| DasSidG | 1:0c77e20b4d4c | 18 | |
| DasSidG | 1:0c77e20b4d4c | 19 | void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage); |
| DasSidG | 1:0c77e20b4d4c | 20 | void check_timeouts(); |
| DasSidG | 1:0c77e20b4d4c | 21 | void update_LEDS(); |
| DasSidG | 1:0c77e20b4d4c | 22 | bool idAccepted(int id); |
| DasSidG | 1:0c77e20b4d4c | 23 | |
| drajan | 0:6d930d0d13a1 | 24 | |
| drajan | 0:6d930d0d13a1 | 25 | |
| DasSidG | 1:0c77e20b4d4c | 26 | timeouts_t timeouts; |
| DasSidG | 1:0c77e20b4d4c | 27 | |
| DasSidG | 1:0c77e20b4d4c | 28 | //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 | 29 | //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 | 30 | CAN car_can(CAR_CAN_READ_PIN, CAR_CAN_WRITE_PIN); //Create a CAN object to handle CAN comms |
| DasSidG | 1:0c77e20b4d4c | 31 | CANMessage car_buffer[CAN_BUFFER_SIZE]; //CAN receive buffer |
| DasSidG | 1:0c77e20b4d4c | 32 | bool car_safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer |
| DasSidG | 1:0c77e20b4d4c | 33 | bool car_CAN_data_sent = false; |
| DasSidG | 1:0c77e20b4d4c | 34 | |
| DasSidG | 1:0c77e20b4d4c | 35 | CAN charger_can(CHARGER_CAN_READ_PIN, CHARGER_CAN_WRITE_PIN); //Create a CAN object to handle CAN comms |
| DasSidG | 1:0c77e20b4d4c | 36 | CANMessage charger_buffer[CAN_BUFFER_SIZE]; //CAN receive buffer |
| DasSidG | 1:0c77e20b4d4c | 37 | bool charger_safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer |
| DasSidG | 1:0c77e20b4d4c | 38 | bool charger_CAN_data_sent = false; |
| DasSidG | 1:0c77e20b4d4c | 39 | |
| DasSidG | 1:0c77e20b4d4c | 40 | int main() { |
| drajan | 0:6d930d0d13a1 | 41 | |
| DasSidG | 1:0c77e20b4d4c | 42 | init(); |
| drajan | 0:6d930d0d13a1 | 43 | |
| DasSidG | 1:0c77e20b4d4c | 44 | while(1) { |
| DasSidG | 1:0c77e20b4d4c | 45 | |
| DasSidG | 1:0c77e20b4d4c | 46 | //Reset error indicators |
| DasSidG | 1:0c77e20b4d4c | 47 | comms_timeout = false; |
| DasSidG | 1:0c77e20b4d4c | 48 | charger_failure = false; |
| DasSidG | 1:0c77e20b4d4c | 49 | bms_error = false; |
| DasSidG | 1:0c77e20b4d4c | 50 | |
| DasSidG | 1:0c77e20b4d4c | 51 | //get the various data from the CAN packets |
| DasSidG | 1:0c77e20b4d4c | 52 | get_CAN_data(); |
| DasSidG | 1:0c77e20b4d4c | 53 | |
| DasSidG | 1:0c77e20b4d4c | 54 | check_timeouts(); |
| DasSidG | 1:0c77e20b4d4c | 55 | |
| DasSidG | 1:0c77e20b4d4c | 56 | update_LEDS(); |
| DasSidG | 1:0c77e20b4d4c | 57 | |
| DasSidG | 1:0c77e20b4d4c | 58 | //TODO: add logic here to calculate current and then decide if charging should go ahead |
| DasSidG | 1:0c77e20b4d4c | 59 | |
| DasSidG | 1:0c77e20b4d4c | 60 | |
| DasSidG | 1:0c77e20b4d4c | 61 | //send CAN data |
| DasSidG | 1:0c77e20b4d4c | 62 | charger_can.write(generate_charger_control_msg(desired_voltage, desired_current, charger_control)); //control message to charger |
| DasSidG | 1:0c77e20b4d4c | 63 | car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car |
| DasSidG | 1:0c77e20b4d4c | 64 | |
| DasSidG | 1:0c77e20b4d4c | 65 | //send charge_finished value to control bit. |
| DasSidG | 1:0c77e20b4d4c | 66 | |
| DasSidG | 1:0c77e20b4d4c | 67 | printf("Voltage Error = %f\n", voltage_error); |
| DasSidG | 1:0c77e20b4d4c | 68 | printf("Temperature Margin = %f\n", temp_margin); |
| DasSidG | 1:0c77e20b4d4c | 69 | printf("Desired Voltage = %f\n", desired_voltage); |
| DasSidG | 1:0c77e20b4d4c | 70 | printf("Desired Current = %f\n", desired_current); |
| DasSidG | 1:0c77e20b4d4c | 71 | printf("Voltage = %f\n", charger_voltage); |
| DasSidG | 1:0c77e20b4d4c | 72 | printf("Current = %f\n", charger_current); |
| DasSidG | 1:0c77e20b4d4c | 73 | printf("Min cell voltage = %f\n", min_cell_voltage); |
| DasSidG | 1:0c77e20b4d4c | 74 | printf("Max cell voltage = %f\n", max_cell_voltage); |
| DasSidG | 1:0c77e20b4d4c | 75 | |
| drajan | 0:6d930d0d13a1 | 76 | } |
| DasSidG | 1:0c77e20b4d4c | 77 | } |
| drajan | 0:6d930d0d13a1 | 78 | |
| drajan | 0:6d930d0d13a1 | 79 | void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage){ |
| drajan | 0:6d930d0d13a1 | 80 | |
| drajan | 0:6d930d0d13a1 | 81 | float Idot, I; |
| drajan | 0:6d930d0d13a1 | 82 | static bool balancing = false; |
| drajan | 0:6d930d0d13a1 | 83 | I = *current; |
| drajan | 0:6d930d0d13a1 | 84 | if (I < 0.8 && voltage_error < 0.1 || balancing) { |
| drajan | 0:6d930d0d13a1 | 85 | balancing = true; |
| drajan | 0:6d930d0d13a1 | 86 | printf("balancing\r\n"); |
| drajan | 0:6d930d0d13a1 | 87 | Idot = voltage_error*KI_BALANCE; |
| drajan | 0:6d930d0d13a1 | 88 | } else { |
| drajan | 0:6d930d0d13a1 | 89 | Idot = voltage_error*KI_CHARGE; |
| drajan | 0:6d930d0d13a1 | 90 | } |
| drajan | 0:6d930d0d13a1 | 91 | I += Idot*TIME_STEP/1000.0; |
| drajan | 0:6d930d0d13a1 | 92 | |
| drajan | 0:6d930d0d13a1 | 93 | if(I > MAX_CURRENT) { |
| drajan | 0:6d930d0d13a1 | 94 | I = MAX_CURRENT; |
| drajan | 0:6d930d0d13a1 | 95 | } |
| drajan | 0:6d930d0d13a1 | 96 | if(I < 0) { |
| drajan | 0:6d930d0d13a1 | 97 | I = 0; |
| drajan | 0:6d930d0d13a1 | 98 | } |
| drajan | 0:6d930d0d13a1 | 99 | |
| drajan | 0:6d930d0d13a1 | 100 | //Reduce current if temperature is too high |
| drajan | 0:6d930d0d13a1 | 101 | if (temp_margin > TEMP_RAMP_START) { |
| drajan | 0:6d930d0d13a1 | 102 | I = 1 - ((temp_margin - TEMP_RAMP_START) / (TEMP_RAMP_FINISH - TEMP_RAMP_START)); |
| drajan | 0:6d930d0d13a1 | 103 | } |
| drajan | 0:6d930d0d13a1 | 104 | if (temp_margin > TEMP_RAMP_FINISH) { |
| drajan | 0:6d930d0d13a1 | 105 | I *= 0; |
| drajan | 0:6d930d0d13a1 | 106 | } |
| drajan | 0:6d930d0d13a1 | 107 | |
| drajan | 0:6d930d0d13a1 | 108 | *current = I; |
| drajan | 0:6d930d0d13a1 | 109 | *voltage = MAX_VOLTAGE; |
| drajan | 0:6d930d0d13a1 | 110 | } |
| drajan | 0:6d930d0d13a1 | 111 | |
| DasSidG | 1:0c77e20b4d4c | 112 | void init() |
| DasSidG | 1:0c77e20b4d4c | 113 | { |
| DasSidG | 1:0c77e20b4d4c | 114 | for(int i=0; i<CAN_BUFFER_SIZE; i++) |
| DasSidG | 1:0c77e20b4d4c | 115 | { |
| DasSidG | 1:0c77e20b4d4c | 116 | car_buffer[i].id = BLANK_ID; |
| DasSidG | 1:0c77e20b4d4c | 117 | //("%d",buffer[i].id); |
| DasSidG | 1:0c77e20b4d4c | 118 | car_safe_to_write[i]= true; |
| drajan | 0:6d930d0d13a1 | 119 | |
| DasSidG | 1:0c77e20b4d4c | 120 | charger_buffer[i].id = BLANK_ID; |
| DasSidG | 1:0c77e20b4d4c | 121 | //("%d",buffer[i].id); |
| DasSidG | 1:0c77e20b4d4c | 122 | charger_safe_to_write[i]= true; |
| DasSidG | 1:0c77e20b4d4c | 123 | } |
| DasSidG | 1:0c77e20b4d4c | 124 | |
| DasSidG | 1:0c77e20b4d4c | 125 | //Initialise CAN stuff, attach CAN interrupt handlers |
| DasSidG | 1:0c77e20b4d4c | 126 | car_can.frequency(CAN_BIT_RATE); //set transmission rate to agreed bit rate |
| DasSidG | 1:0c77e20b4d4c | 127 | car_can.reset(); |
| DasSidG | 1:0c77e20b4d4c | 128 | car_can.attach(&car_interruptHandler, CAN::RxIrq); //receive interrupt handler |
| DasSidG | 1:0c77e20b4d4c | 129 | car_can.attach(&car_CANDataSentCallback, CAN::TxIrq); //send interrupt handler |
| DasSidG | 1:0c77e20b4d4c | 130 | |
| DasSidG | 1:0c77e20b4d4c | 131 | charger_can.frequency(CHARGER_CAN_BIT_RATE); //set transmission rate to agreed bit rate |
| DasSidG | 1:0c77e20b4d4c | 132 | charger_can.reset(); |
| DasSidG | 1:0c77e20b4d4c | 133 | charger_can.attach(&charger_interruptHandler, CAN::RxIrq); //receive interrupt handler |
| DasSidG | 1:0c77e20b4d4c | 134 | charger_can.attach(&charger_CANDataSentCallback, CAN::TxIrq); //send interrupt handler |
| DasSidG | 1:0c77e20b4d4c | 135 | |
| DasSidG | 1:0c77e20b4d4c | 136 | //Start comms timeout timers |
| DasSidG | 1:0c77e20b4d4c | 137 | |
| DasSidG | 1:0c77e20b4d4c | 138 | timeouts.BMS_timeout.start(); |
| DasSidG | 1:0c77e20b4d4c | 139 | timeouts.charger_timeout.start(); |
| DasSidG | 1:0c77e20b4d4c | 140 | |
| DasSidG | 1:0c77e20b4d4c | 141 | } |
| DasSidG | 1:0c77e20b4d4c | 142 | |
| DasSidG | 1:0c77e20b4d4c | 143 | void car_CANDataSentCallback(void) { |
| DasSidG | 1:0c77e20b4d4c | 144 | car_CAN_data_sent = true; |
| DasSidG | 1:0c77e20b4d4c | 145 | } |
| DasSidG | 1:0c77e20b4d4c | 146 | |
| DasSidG | 1:0c77e20b4d4c | 147 | void charger_CANDataSentCallback(void) { |
| DasSidG | 1:0c77e20b4d4c | 148 | charger_CAN_data_sent = true; |
| DasSidG | 1:0c77e20b4d4c | 149 | } |
| DasSidG | 1:0c77e20b4d4c | 150 | |
| DasSidG | 1:0c77e20b4d4c | 151 | void car_interruptHandler() |
| DasSidG | 1:0c77e20b4d4c | 152 | { |
| DasSidG | 1:0c77e20b4d4c | 153 | CANMessage msg; |
| DasSidG | 1:0c77e20b4d4c | 154 | car_can.read(msg); |
| DasSidG | 1:0c77e20b4d4c | 155 | //if(DEBUG) printf("id %d incoming \r\n", msg.id); |
| DasSidG | 1:0c77e20b4d4c | 156 | if(idAccepted(msg.id)) { |
| DasSidG | 1:0c77e20b4d4c | 157 | for(int i=0; i<CAN_BUFFER_SIZE; i++) { |
| DasSidG | 1:0c77e20b4d4c | 158 | if((car_buffer[i].id == msg.id || car_buffer[i].id==BLANK_ID) && car_safe_to_write[i]) { |
| DasSidG | 1:0c77e20b4d4c | 159 | //("id %d added to buffer \r\n", msg.id); |
| DasSidG | 1:0c77e20b4d4c | 160 | car_buffer[i] = msg; |
| DasSidG | 1:0c77e20b4d4c | 161 | //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 | 162 | return; |
| DasSidG | 1:0c77e20b4d4c | 163 | } |
| DasSidG | 1:0c77e20b4d4c | 164 | } |
| DasSidG | 1:0c77e20b4d4c | 165 | } |
| DasSidG | 1:0c77e20b4d4c | 166 | } |
| DasSidG | 1:0c77e20b4d4c | 167 | |
| DasSidG | 1:0c77e20b4d4c | 168 | void charger_interruptHandler() |
| DasSidG | 1:0c77e20b4d4c | 169 | { |
| DasSidG | 1:0c77e20b4d4c | 170 | CANMessage msg; |
| DasSidG | 1:0c77e20b4d4c | 171 | charger_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((charger_buffer[i].id == msg.id || charger_buffer[i].id==BLANK_ID) && charger_safe_to_write[i]) { |
| DasSidG | 1:0c77e20b4d4c | 176 | //("id %d added to buffer \r\n", msg.id); |
| DasSidG | 1:0c77e20b4d4c | 177 | charger_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 | bool idAccepted(int id) |
| DasSidG | 1:0c77e20b4d4c | 186 | { |
| DasSidG | 1:0c77e20b4d4c | 187 | switch(id) { |
| DasSidG | 1:0c77e20b4d4c | 188 | case BMS_BASE_ID: |
| DasSidG | 1:0c77e20b4d4c | 189 | timeouts.BMS_timeout.reset(); |
| DasSidG | 1:0c77e20b4d4c | 190 | return true; |
| DasSidG | 1:0c77e20b4d4c | 191 | |
| DasSidG | 1:0c77e20b4d4c | 192 | case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: |
| DasSidG | 1:0c77e20b4d4c | 193 | return true; |
| DasSidG | 1:0c77e20b4d4c | 194 | |
| DasSidG | 1:0c77e20b4d4c | 195 | case BMS_BASE_ID + MAX_MIN_VOLTAGE: |
| DasSidG | 1:0c77e20b4d4c | 196 | return true; |
| DasSidG | 1:0c77e20b4d4c | 197 | |
| DasSidG | 1:0c77e20b4d4c | 198 | case BMS_BASE_ID + BATTERY_STATUS_ID: |
| DasSidG | 1:0c77e20b4d4c | 199 | return true; |
| DasSidG | 1:0c77e20b4d4c | 200 | |
| DasSidG | 1:0c77e20b4d4c | 201 | case CHARGER_VI_INFO_ID: |
| DasSidG | 1:0c77e20b4d4c | 202 | timeouts.charger_timeout.reset(); |
| DasSidG | 1:0c77e20b4d4c | 203 | return true; |
| DasSidG | 1:0c77e20b4d4c | 204 | default: |
| DasSidG | 1:0c77e20b4d4c | 205 | return false; |
| DasSidG | 1:0c77e20b4d4c | 206 | } |
| DasSidG | 1:0c77e20b4d4c | 207 | } |
| drajan | 0:6d930d0d13a1 | 208 | |
| drajan | 0:6d930d0d13a1 | 209 | |
| drajan | 0:6d930d0d13a1 | 210 | |
| drajan | 0:6d930d0d13a1 | 211 | |
| drajan | 0:6d930d0d13a1 | 212 | |
| DasSidG | 1:0c77e20b4d4c | 213 | void check_timeouts() //Check if it's been too long since any of the other devices in the car have communicated |
| DasSidG | 1:0c77e20b4d4c | 214 | { |
| DasSidG | 1:0c77e20b4d4c | 215 | |
| DasSidG | 1:0c77e20b4d4c | 216 | if (timeouts.BMS_timeout.read_ms() > BMS_MSG_TIMEOUT_MS) |
| DasSidG | 1:0c77e20b4d4c | 217 | { |
| DasSidG | 1:0c77e20b4d4c | 218 | printf("Error: BMS comms timeout"); |
| DasSidG | 1:0c77e20b4d4c | 219 | comms_timeout = true; |
| DasSidG | 1:0c77e20b4d4c | 220 | } |
| DasSidG | 1:0c77e20b4d4c | 221 | |
| DasSidG | 1:0c77e20b4d4c | 222 | if (timeouts.charger_timeout.read_ms() > CHARGER_MSG_TIMEOUT_MS) |
| DasSidG | 1:0c77e20b4d4c | 223 | { |
| DasSidG | 1:0c77e20b4d4c | 224 | printf("Error: BMS comms timeout"); |
| DasSidG | 1:0c77e20b4d4c | 225 | comms_timeout = true; |
| DasSidG | 1:0c77e20b4d4c | 226 | } |
| DasSidG | 1:0c77e20b4d4c | 227 | } |
| DasSidG | 1:0c77e20b4d4c | 228 | |
| DasSidG | 1:0c77e20b4d4c | 229 | void update_LEDS() { |
| DasSidG | 1:0c77e20b4d4c | 230 | if (charger_failure || bms_error) { |
| DasSidG | 1:0c77e20b4d4c | 231 | desired_current = 0; |
| DasSidG | 1:0c77e20b4d4c | 232 | red_led = 1; |
| DasSidG | 1:0c77e20b4d4c | 233 | } |
| drajan | 0:6d930d0d13a1 | 234 | |
| DasSidG | 1:0c77e20b4d4c | 235 | else if (comms_timeout) { |
| DasSidG | 1:0c77e20b4d4c | 236 | desired_current = 0; |
| DasSidG | 1:0c77e20b4d4c | 237 | yellow_led = 1; |
| DasSidG | 1:0c77e20b4d4c | 238 | } |
| DasSidG | 1:0c77e20b4d4c | 239 | |
| DasSidG | 1:0c77e20b4d4c | 240 | else if (!charge_finished) { |
| DasSidG | 1:0c77e20b4d4c | 241 | green_led = 1; |
| DasSidG | 1:0c77e20b4d4c | 242 | } |
| DasSidG | 1:0c77e20b4d4c | 243 | |
| DasSidG | 1:0c77e20b4d4c | 244 | else { |
| DasSidG | 1:0c77e20b4d4c | 245 | red_led = 0; |
| DasSidG | 1:0c77e20b4d4c | 246 | yellow_led = 0; |
| DasSidG | 1:0c77e20b4d4c | 247 | green_led = 0; |
| DasSidG | 1:0c77e20b4d4c | 248 | } |
| DasSidG | 1:0c77e20b4d4c | 249 | |
| DasSidG | 1:0c77e20b4d4c | 250 | |
| DasSidG | 1:0c77e20b4d4c | 251 | if (min_cell_voltage > RISING_BALANCE_THRESHOLD) { |
| DasSidG | 1:0c77e20b4d4c | 252 | charge_finished = true; |
| DasSidG | 1:0c77e20b4d4c | 253 | printf("Charge Finished\r\n"); |
| DasSidG | 1:0c77e20b4d4c | 254 | //set charger control bit to finished charging |
| DasSidG | 1:0c77e20b4d4c | 255 | } |
| DasSidG | 1:0c77e20b4d4c | 256 | else { |
| DasSidG | 1:0c77e20b4d4c | 257 | calculate_current(voltage_error, temp_margin, &desired_current, &desired_voltage); |
| DasSidG | 1:0c77e20b4d4c | 258 | charge_finished = false; |
| DasSidG | 1:0c77e20b4d4c | 259 | } |
| DasSidG | 1:0c77e20b4d4c | 260 | } |
| DasSidG | 1:0c77e20b4d4c | 261 | |
| DasSidG | 1:0c77e20b4d4c | 262 | void get_CAN_data() { |
| drajan | 0:6d930d0d13a1 | 263 | |
| DasSidG | 1:0c77e20b4d4c | 264 | //Import the data from the buffer into a non-volatile, more usable format |
| DasSidG | 1:0c77e20b4d4c | 265 | CAN_Data car_can_data[CAN_BUFFER_SIZE]; //container for all of the raw data |
| DasSidG | 1:0c77e20b4d4c | 266 | CANMessage car_msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter |
| DasSidG | 1:0c77e20b4d4c | 267 | int car_received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far |
| DasSidG | 1:0c77e20b4d4c | 268 | for (int i = 0; i<CAN_BUFFER_SIZE; ++i) |
| DasSidG | 1:0c77e20b4d4c | 269 | { |
| DasSidG | 1:0c77e20b4d4c | 270 | car_safe_to_write[i] = false; |
| DasSidG | 1:0c77e20b4d4c | 271 | car_can_data[i].importCANData(car_buffer[i]); |
| DasSidG | 1:0c77e20b4d4c | 272 | car_received_CAN_IDs[i] = car_buffer[i].id; |
| DasSidG | 1:0c77e20b4d4c | 273 | car_msgArray[i] = car_buffer[i]; |
| DasSidG | 1:0c77e20b4d4c | 274 | car_safe_to_write[i] = true; |
| DasSidG | 1:0c77e20b4d4c | 275 | |
| DasSidG | 1:0c77e20b4d4c | 276 | //Now actually import the data from the CAN packets into the global variables |
| DasSidG | 1:0c77e20b4d4c | 277 | switch (car_received_CAN_IDs[i]) { |
| drajan | 0:6d930d0d13a1 | 278 | |
| DasSidG | 1:0c77e20b4d4c | 279 | case BMS_BASE_ID: |
| DasSidG | 1:0c77e20b4d4c | 280 | break; |
| DasSidG | 1:0c77e20b4d4c | 281 | |
| DasSidG | 1:0c77e20b4d4c | 282 | case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: |
| DasSidG | 1:0c77e20b4d4c | 283 | get_charger_control_info(car_msgArray[i], voltage_error, temp_margin, discharge_error, pack_capacity); |
| DasSidG | 1:0c77e20b4d4c | 284 | break; |
| DasSidG | 1:0c77e20b4d4c | 285 | |
| DasSidG | 1:0c77e20b4d4c | 286 | case BMS_BASE_ID + MAX_MIN_VOLTAGE: |
| DasSidG | 1:0c77e20b4d4c | 287 | get_max_min_voltage(car_msgArray[i], min_cell_voltage, max_cell_voltage); |
| DasSidG | 1:0c77e20b4d4c | 288 | break; |
| DasSidG | 1:0c77e20b4d4c | 289 | |
| DasSidG | 1:0c77e20b4d4c | 290 | case BMS_BASE_ID + BATTERY_STATUS_ID: |
| DasSidG | 1:0c77e20b4d4c | 291 | get_battery_status(car_msgArray[i], bms_error); |
| DasSidG | 1:0c77e20b4d4c | 292 | break; |
| DasSidG | 1:0c77e20b4d4c | 293 | case BLANK_ID: //This means we haven't received this type of message yet, so do nothing |
| DasSidG | 1:0c77e20b4d4c | 294 | break; |
| DasSidG | 1:0c77e20b4d4c | 295 | default: |
| DasSidG | 1:0c77e20b4d4c | 296 | break; |
| DasSidG | 1:0c77e20b4d4c | 297 | } |
| DasSidG | 1:0c77e20b4d4c | 298 | } |
| drajan | 0:6d930d0d13a1 | 299 | |
| DasSidG | 1:0c77e20b4d4c | 300 | //Import the data from the buffer into a non-volatile, more usable format |
| DasSidG | 1:0c77e20b4d4c | 301 | CAN_Data charger_can_data[CAN_BUFFER_SIZE]; //container for all of the raw data |
| DasSidG | 1:0c77e20b4d4c | 302 | CANMessage charger_msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter |
| DasSidG | 1:0c77e20b4d4c | 303 | int charger_received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far |
| DasSidG | 1:0c77e20b4d4c | 304 | for (int i = 0; i<CAN_BUFFER_SIZE; ++i) |
| DasSidG | 1:0c77e20b4d4c | 305 | { |
| DasSidG | 1:0c77e20b4d4c | 306 | charger_safe_to_write[i] = false; |
| DasSidG | 1:0c77e20b4d4c | 307 | charger_can_data[i].importCANData(charger_buffer[i]); |
| 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 | } |