CUER
/
charger_code
Code to run on the charger board (used to charge the car from the mains).
Diff: charger.cpp
- Revision:
- 5:756fae795d37
- Parent:
- 4:f6459580c312
- Child:
- 7:70cf5bff23f9
--- a/charger.cpp Thu Jul 27 21:21:32 2017 +0000 +++ b/charger.cpp Sun Jul 30 22:59:37 2017 +0000 @@ -5,7 +5,6 @@ #include "Data_types.h" #include "CANParserCharger.h" -//TEST PUBLISH using namespace CAN_IDs; void car_interruptHandler(); @@ -17,12 +16,39 @@ void init(); void get_CAN_data(); -void calculate_current(float voltage_error, float temp_margin, float ¤t); +void calculate_current(float ¤t); void check_timeouts(); void update_LEDS(); bool idAccepted(int id); +DigitalOut green_led(p21); //charging LED +DigitalOut yellow_led(p22); //timeout LED +DigitalOut red_led(p23); //error LED +DigitalIn charge_switch(p8); //switch to enable charging +void calculate_current(float voltage_error, float temp_margin, float *current, float *voltage); +uint8_t charger_status; //status packet given by charger, see Elcon CAN Specification in google drive +bool precharge_status; +bool comms_timeout; +bool charger_failure; +bool charge_finished; +bool bms_error; + + +//float temp_margin = 0; //degrees C, this is the max cell temperature minus max cell temperature limit, need to avoid this hitting 0 + +float min_cell_voltage = 3600; //mV +float max_cell_voltage = 3600;//mV + +float charger_current; //mA +float charger_voltage; //mV + +float desired_current = 0; //mA +float desired_voltage = MAX_VOLTAGE; //mV + +float voltage_error = 1000; //mV //This = RISING_BALANCE_THRESHOLD - max_cell_voltage + +uint8_t charger_control = STOP_CHARGING; timeouts_t timeouts; @@ -55,17 +81,19 @@ check_timeouts(); update_LEDS(); + charger_control = STOP_CHARGING; + if (min_cell_voltage > RISING_BALANCE_THRESHOLD || charge_finished) { charge_finished = true; - printf("Charge Finished\r\n"); + if (DEBUG) printf("Charge Finished\r\n"); car_can.write(generate_charging_finished_msg()); - charger_control = 0; //set charger control bit to stop charging + charger_control = STOP_CHARGING; //set charger control bit to stop charging } - else { - calculate_current(voltage_error, temp_margin, desired_current); + else if (precharge_status && charge_switch.read()) { + calculate_current(desired_current); desired_voltage = MAX_VOLTAGE; charge_finished = false; - charger_control = 1; //set charger control bit to start charging + charger_control = START_CHARGING; //set charger control bit to start charging } //send CAN data @@ -81,38 +109,42 @@ car_can.write(generate_charger_info_msg(charger_voltage, charger_current, charger_status)); //charger info message for rest of car while(!car_CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS); - printf("Voltage Error = %f\n", voltage_error); - printf("Temperature Margin = %f\n", temp_margin); - printf("Desired Voltage = %f\n", desired_voltage); - printf("Desired Current = %f\n", desired_current); - printf("Charger voltage = %f\n", charger_voltage); - printf("Charger current = %f\n", charger_current); - printf("Min cell voltage = %f\n", min_cell_voltage); - printf("Max cell voltage = %f\n", max_cell_voltage); + if (DEBUG) { + printf("Desired Voltage = %f \r\n", desired_voltage); + printf("Desired Current = %f \r\n", desired_current); + printf("Charger voltage = %f \r\n", charger_voltage); + printf("Charger current = %f \r\n", charger_current); + printf("Min cell voltage = %f \r\n", min_cell_voltage); + printf("Max cell voltage = %f \r\n", max_cell_voltage); + printf("Precharge status is %d \r\n", precharge_status); + } + + wait(TIME_STEP); } } -void calculate_current(float voltage_error, float temp_margin, float ¤t){ +void calculate_current(float ¤t){ - float Idot, I; + float Idot; + static bool balancing = false; - I = current; - if (I < 800 && voltage_error < 100 || balancing) { + if (current < 800 && voltage_error < 100 || balancing) { balancing = true; - printf("balancing\r\n"); - Idot = voltage_error*KI_BALANCE; - } + if (DEBUG) printf("balancing\r\n"); + Idot = voltage_error *KI_BALANCE; + } else { Idot = voltage_error*KI_CHARGE; } - I += Idot*TIME_STEP/1000.0; + + current += Idot*TIME_STEP/1000.0; - if(I > MAX_CURRENT) { - I = MAX_CURRENT; + if(current > MAX_CURRENT) { + current = MAX_CURRENT; } - if(I < 0) { - I = 0; + if(current < 0) { + current = 0; } /* //Reduce current if temperature is too high @@ -123,7 +155,6 @@ I *= 0; } */ - current = I; } void init() @@ -154,7 +185,8 @@ timeouts.BMS_timeout.start(); timeouts.charger_timeout.start(); - + + precharge_status = false; } void car_CANDataSentCallback(void) { @@ -206,15 +238,10 @@ timeouts.BMS_timeout.reset(); return true; - case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: - return true; - case BMS_BASE_ID + MAX_MIN_VOLTAGE: + case BMS_BASE_ID + BATTERY_STATUS_ID: + case BMS_BASE_ID + BATTERY_PRECHARGE_ID: return true; - - case BMS_BASE_ID + BATTERY_STATUS_ID: - return true; - case CHARGER_VI_INFO_ID: timeouts.charger_timeout.reset(); return true; @@ -229,13 +256,13 @@ if (timeouts.BMS_timeout.read_ms() > BMS_MSG_TIMEOUT_MS) { - printf("Error: BMS comms timeout"); + if (DEBUG) printf("Error: BMS comms timeout"); comms_timeout = true; } if (timeouts.charger_timeout.read_ms() > CHARGER_MSG_TIMEOUT_MS) { - printf("Error: Charger comms timeout"); + if (DEBUG) printf("Error: Charger comms timeout"); comms_timeout = true; } } @@ -273,6 +300,7 @@ car_safe_to_write[i] = false; car_received_CAN_IDs[i] = car_buffer[i].id; car_msgArray[i] = car_buffer[i]; + car_buffer[i].id = BLANK_ID; //this effectively clears the buffer, so that we never reuse old values car_safe_to_write[i] = true; //Now actually import the data from the CAN packets into the global variables @@ -281,17 +309,19 @@ case BMS_BASE_ID: break; - case BMS_BASE_ID + CHARGER_CONTROL_INFO_ID: - get_charger_control_info(car_msgArray[i], voltage_error, temp_margin, discharge_error, pack_capacity); - break; - case BMS_BASE_ID + MAX_MIN_VOLTAGE: get_max_min_voltage(car_msgArray[i], min_cell_voltage, max_cell_voltage); + voltage_error = RISING_BALANCE_THRESHOLD - max_cell_voltage; break; case BMS_BASE_ID + BATTERY_STATUS_ID: get_battery_status(car_msgArray[i], bms_error); break; + + case BMS_BASE_ID + BATTERY_PRECHARGE_ID: + check_precharge_status(car_msgArray[i], precharge_status); + break; + case BLANK_ID: //This means we haven't received this type of message yet, so do nothing break; default: @@ -307,6 +337,7 @@ charger_safe_to_write[i] = false; charger_received_CAN_IDs[i] = charger_buffer[i].id; charger_msgArray[i] = charger_buffer[i]; + charger_buffer[i].id = BLANK_ID; charger_safe_to_write[i] = true; //Now actually import the data from the CAN packets into the global variables @@ -315,27 +346,27 @@ case CHARGER_VI_INFO_ID: get_charger_VI_info(charger_msgArray[i], charger_voltage, charger_current, charger_status); if(charger_status bitand 1 == 1) { - printf("Charger status: Hardware Failure\r\n"); + if (DEBUG) printf("Charger status: Hardware Failure\r\n"); charger_failure = true; } if(charger_status bitand 2 == 2) { - printf("Charger status: Over Temperature\r\n"); + if (DEBUG) printf("Charger status: Over Temperature\r\n"); charger_failure = true; } if(charger_status bitand 4 == 4) { - printf("Charger status: Input Voltage Wrong\r\n"); + if (DEBUG) printf("Charger status: Input Voltage Wrong\r\n"); charger_failure = true; } if(charger_status bitand 8 == 8) { - printf("Charger status: Reverse Polarity\r\n"); + if (DEBUG) printf("Charger status: Reverse Polarity\r\n"); charger_failure = true; } if(charger_status bitand 16 == 16) { - printf("Charger status: communication timeout\r\n"); + if (DEBUG) printf("Charger status: communication timeout\r\n"); charger_failure = true; } break;