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

Dependencies:   mbed CUER_CAN

Committer:
DasSidG
Date:
Sun Sep 10 11:33:19 2017 +0000
Revision:
11:f578a372be42
Parent:
10:839e8f984d28
Child:
12:77d493b3320b
CAN functionality now tested after refactoring, and checked to be fine.; Timeouts tested and are fine

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 1:0c77e20b4d4c 8 void init();
DasSidG 7:70cf5bff23f9 9
DasSidG 7:70cf5bff23f9 10 float calculate_desired_current(float charger_current, float min_cell_voltage);
DasSidG 1:0c77e20b4d4c 11 void check_timeouts();
DasSidG 1:0c77e20b4d4c 12 void update_LEDS();
DasSidG 7:70cf5bff23f9 13
DasSidG 5:756fae795d37 14 DigitalOut green_led(p21); //charging LED
DasSidG 5:756fae795d37 15 DigitalOut yellow_led(p22); //timeout LED
DasSidG 5:756fae795d37 16 DigitalOut red_led(p23); //error LED
DasSidG 5:756fae795d37 17 DigitalIn charge_switch(p8); //switch to enable charging
drajan 0:6d930d0d13a1 18
DasSidG 5:756fae795d37 19 uint8_t charger_status; //status packet given by charger, see Elcon CAN Specification in google drive
DasSidG 5:756fae795d37 20 bool precharge_status;
DasSidG 5:756fae795d37 21 bool comms_timeout;
DasSidG 5:756fae795d37 22 bool charger_failure;
DasSidG 5:756fae795d37 23 bool charge_finished;
DasSidG 5:756fae795d37 24 bool bms_error;
DasSidG 7:70cf5bff23f9 25 bool charging;
DasSidG 5:756fae795d37 26
DasSidG 5:756fae795d37 27 float min_cell_voltage = 3600; //mV
DasSidG 5:756fae795d37 28 float max_cell_voltage = 3600;//mV
DasSidG 5:756fae795d37 29
DasSidG 5:756fae795d37 30 float charger_current; //mA
DasSidG 5:756fae795d37 31 float charger_voltage; //mV
DasSidG 5:756fae795d37 32
DasSidG 5:756fae795d37 33 float desired_current = 0; //mA
DasSidG 7:70cf5bff23f9 34 float desired_voltage = MAX_PACK_VOLTAGE; //mV
DasSidG 5:756fae795d37 35
DasSidG 10:839e8f984d28 36 //float voltage_error = 1000; //mV //This = RISING_BALANCE_THRESHOLD - max_cell_voltage
DasSidG 5:756fae795d37 37
DasSidG 5:756fae795d37 38 uint8_t charger_control = STOP_CHARGING;
drajan 0:6d930d0d13a1 39
DasSidG 1:0c77e20b4d4c 40 timeouts_t timeouts;
DasSidG 1:0c77e20b4d4c 41
DasSidG 1:0c77e20b4d4c 42 int main() {
drajan 0:6d930d0d13a1 43
DasSidG 1:0c77e20b4d4c 44 init();
drajan 0:6d930d0d13a1 45
DasSidG 1:0c77e20b4d4c 46 while(1) {
DasSidG 7:70cf5bff23f9 47
DasSidG 1:0c77e20b4d4c 48 //get the various data from the CAN packets
DasSidG 1:0c77e20b4d4c 49 get_CAN_data();
DasSidG 1:0c77e20b4d4c 50
DasSidG 1:0c77e20b4d4c 51 check_timeouts();
DasSidG 1:0c77e20b4d4c 52 update_LEDS();
DasSidG 1:0c77e20b4d4c 53
DasSidG 5:756fae795d37 54 charger_control = STOP_CHARGING;
DasSidG 5:756fae795d37 55
DasSidG 7:70cf5bff23f9 56 //if (min_cell_voltage > RISING_BALANCE_THRESHOLD || charge_finished) { //Note: if balancing is implemented, use this version instead
DasSidG 7:70cf5bff23f9 57 if ((max_cell_voltage > CHARGING_FINISHED_THRESHOLD_VOLTAGE && charger_current < CHARGING_FINISHED_THRESHOLD_CURRENT)
DasSidG 7:70cf5bff23f9 58 || charge_finished
DasSidG 7:70cf5bff23f9 59 || max_cell_voltage > MAX_CELL_VOLTAGE) {
DasSidG 3:a7626dffb64a 60 charge_finished = true;
DasSidG 7:70cf5bff23f9 61 charging = false;
DasSidG 5:756fae795d37 62 if (DEBUG) printf("Charge Finished\r\n");
DasSidG 3:a7626dffb64a 63 car_can.write(generate_charging_finished_msg());
DasSidG 5:756fae795d37 64 charger_control = STOP_CHARGING; //set charger control bit to stop charging
DasSidG 3:a7626dffb64a 65 }
DasSidG 7:70cf5bff23f9 66 else if (precharge_status && !bms_error && !comms_timeout && !charger_failure && charge_switch.read()) {
DasSidG 7:70cf5bff23f9 67 charging = true;
DasSidG 7:70cf5bff23f9 68 desired_current = calculate_desired_current(charger_current, min_cell_voltage);
DasSidG 7:70cf5bff23f9 69 desired_voltage = MAX_PACK_VOLTAGE;
DasSidG 3:a7626dffb64a 70 charge_finished = false;
DasSidG 5:756fae795d37 71 charger_control = START_CHARGING; //set charger control bit to start charging
DasSidG 3:a7626dffb64a 72 }
DasSidG 1:0c77e20b4d4c 73
DasSidG 1:0c77e20b4d4c 74 //send CAN data
DasSidG 4:f6459580c312 75
DasSidG 4:f6459580c312 76 Timer t;
DasSidG 4:f6459580c312 77 t.start();
DasSidG 4:f6459580c312 78 charger_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 79 charger_can.write(generate_charger_control_msg(desired_voltage, desired_current, charger_control)); //control message to charger
DasSidG 4:f6459580c312 80 while(!charger_CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS);
DasSidG 4:f6459580c312 81
DasSidG 4:f6459580c312 82 t.reset();
DasSidG 4:f6459580c312 83 car_CAN_data_sent = false;
DasSidG 1:0c77e20b4d4c 84 car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car
DasSidG 4:f6459580c312 85 while(!car_CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS);
DasSidG 1:0c77e20b4d4c 86
DasSidG 5:756fae795d37 87 if (DEBUG) {
DasSidG 5:756fae795d37 88 printf("Desired Voltage = %f \r\n", desired_voltage);
DasSidG 5:756fae795d37 89 printf("Desired Current = %f \r\n", desired_current);
DasSidG 5:756fae795d37 90 printf("Charger voltage = %f \r\n", charger_voltage);
DasSidG 5:756fae795d37 91 printf("Charger current = %f \r\n", charger_current);
DasSidG 5:756fae795d37 92 printf("Min cell voltage = %f \r\n", min_cell_voltage);
DasSidG 5:756fae795d37 93 printf("Max cell voltage = %f \r\n", max_cell_voltage);
DasSidG 5:756fae795d37 94 printf("Precharge status is %d \r\n", precharge_status);
DasSidG 7:70cf5bff23f9 95 printf("\r\n");
DasSidG 5:756fae795d37 96 }
DasSidG 5:756fae795d37 97
DasSidG 7:70cf5bff23f9 98 wait_ms(500);
DasSidG 1:0c77e20b4d4c 99
drajan 0:6d930d0d13a1 100 }
DasSidG 1:0c77e20b4d4c 101 }
drajan 0:6d930d0d13a1 102
DasSidG 7:70cf5bff23f9 103 float calculate_desired_current(float _charger_current, float _min_cell_voltage){
DasSidG 7:70cf5bff23f9 104 float _desired_current;
DasSidG 7:70cf5bff23f9 105 _desired_current += 500; //gradually ramp up current when charging starts
DasSidG 5:756fae795d37 106
DasSidG 7:70cf5bff23f9 107 if (_min_cell_voltage < DEEP_DISCHARGE_THRESHOLD_VOLTAGE) {
DasSidG 7:70cf5bff23f9 108 if(_desired_current > DEEP_DISCHARGE_MAX_CURRENT) {
DasSidG 7:70cf5bff23f9 109 _desired_current = DEEP_DISCHARGE_MAX_CURRENT;
DasSidG 7:70cf5bff23f9 110 }
DasSidG 7:70cf5bff23f9 111 }
DasSidG 7:70cf5bff23f9 112
DasSidG 7:70cf5bff23f9 113 else if(_desired_current > MAX_CURRENT) {
DasSidG 7:70cf5bff23f9 114 _desired_current = MAX_CURRENT;
DasSidG 7:70cf5bff23f9 115 }
DasSidG 7:70cf5bff23f9 116 if(_desired_current < 0) {
DasSidG 7:70cf5bff23f9 117 _desired_current = 0;
DasSidG 3:a7626dffb64a 118 }
DasSidG 5:756fae795d37 119
DasSidG 7:70cf5bff23f9 120 return _desired_current;
drajan 0:6d930d0d13a1 121 }
drajan 0:6d930d0d13a1 122
DasSidG 1:0c77e20b4d4c 123 void init()
DasSidG 1:0c77e20b4d4c 124 {
DasSidG 8:e5c07b9b8593 125 CAN_Init();
DasSidG 1:0c77e20b4d4c 126 //Start comms timeout timers
DasSidG 1:0c77e20b4d4c 127 timeouts.BMS_timeout.start();
DasSidG 1:0c77e20b4d4c 128 timeouts.charger_timeout.start();
DasSidG 5:756fae795d37 129 precharge_status = false;
DasSidG 7:70cf5bff23f9 130 //Reset error indicators
DasSidG 7:70cf5bff23f9 131 comms_timeout = false;
DasSidG 7:70cf5bff23f9 132 charger_failure = false;
DasSidG 7:70cf5bff23f9 133 bms_error = false;
DasSidG 7:70cf5bff23f9 134 charging = false;
DasSidG 1:0c77e20b4d4c 135 }
DasSidG 1:0c77e20b4d4c 136
DasSidG 1:0c77e20b4d4c 137 void check_timeouts() //Check if it's been too long since any of the other devices in the car have communicated
DasSidG 1:0c77e20b4d4c 138 {
DasSidG 1:0c77e20b4d4c 139
DasSidG 1:0c77e20b4d4c 140 if (timeouts.BMS_timeout.read_ms() > BMS_MSG_TIMEOUT_MS)
DasSidG 1:0c77e20b4d4c 141 {
DasSidG 11:f578a372be42 142 if (DEBUG) printf("Error: BMS comms timeout \r\n");
DasSidG 1:0c77e20b4d4c 143 comms_timeout = true;
DasSidG 1:0c77e20b4d4c 144 }
DasSidG 1:0c77e20b4d4c 145
DasSidG 1:0c77e20b4d4c 146 if (timeouts.charger_timeout.read_ms() > CHARGER_MSG_TIMEOUT_MS)
DasSidG 1:0c77e20b4d4c 147 {
DasSidG 11:f578a372be42 148 if (DEBUG) printf("Error: Charger comms timeout \r\n");
DasSidG 1:0c77e20b4d4c 149 comms_timeout = true;
DasSidG 1:0c77e20b4d4c 150 }
DasSidG 1:0c77e20b4d4c 151 }
DasSidG 1:0c77e20b4d4c 152
DasSidG 1:0c77e20b4d4c 153 void update_LEDS() {
DasSidG 1:0c77e20b4d4c 154 if (charger_failure || bms_error) {
DasSidG 1:0c77e20b4d4c 155 desired_current = 0;
DasSidG 1:0c77e20b4d4c 156 red_led = 1;
DasSidG 1:0c77e20b4d4c 157 }
drajan 0:6d930d0d13a1 158
DasSidG 1:0c77e20b4d4c 159 else if (comms_timeout) {
DasSidG 1:0c77e20b4d4c 160 desired_current = 0;
DasSidG 1:0c77e20b4d4c 161 yellow_led = 1;
DasSidG 1:0c77e20b4d4c 162 }
DasSidG 1:0c77e20b4d4c 163
DasSidG 1:0c77e20b4d4c 164 else if (!charge_finished) {
DasSidG 1:0c77e20b4d4c 165 green_led = 1;
DasSidG 1:0c77e20b4d4c 166 }
DasSidG 1:0c77e20b4d4c 167
DasSidG 1:0c77e20b4d4c 168 else {
DasSidG 1:0c77e20b4d4c 169 red_led = 0;
DasSidG 1:0c77e20b4d4c 170 yellow_led = 0;
DasSidG 1:0c77e20b4d4c 171 green_led = 0;
DasSidG 4:f6459580c312 172 }
DasSidG 3:a7626dffb64a 173
DasSidG 1:0c77e20b4d4c 174 }
DasSidG 1:0c77e20b4d4c 175