CUER
/
charger_code
Code to run on the charger board (used to charge the car from the mains).
CANParserCharger.cpp@3:a7626dffb64a, 2017-07-27 (annotated)
- Committer:
- DasSidG
- Date:
- Thu Jul 27 21:00:52 2017 +0000
- Revision:
- 3:a7626dffb64a
- Parent:
- 1:0c77e20b4d4c
- Child:
- 5:756fae795d37
Code is now theoretically finished; completely untested however.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DasSidG | 1:0c77e20b4d4c | 1 | // Here are the functions to generate the CAN messages |
DasSidG | 1:0c77e20b4d4c | 2 | #include "CANParserCharger.h" |
DasSidG | 1:0c77e20b4d4c | 3 | #include "mbed.h" |
DasSidG | 1:0c77e20b4d4c | 4 | #include "CAN_IDs.h" |
DasSidG | 1:0c77e20b4d4c | 5 | |
DasSidG | 1:0c77e20b4d4c | 6 | |
DasSidG | 1:0c77e20b4d4c | 7 | using namespace CAN_IDs; |
DasSidG | 1:0c77e20b4d4c | 8 | |
DasSidG | 1:0c77e20b4d4c | 9 | void get_charger_control_info(CANMessage msg, float &_voltage_error, float &_temp_margin, float &_discharge_error, float &_pack_capacity) { |
DasSidG | 1:0c77e20b4d4c | 10 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 11 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 12 | |
DasSidG | 3:a7626dffb64a | 13 | _voltage_error = (float) data.get_u16(0); |
DasSidG | 1:0c77e20b4d4c | 14 | _temp_margin = data.get_u16(1) /10.0; |
DasSidG | 3:a7626dffb64a | 15 | _discharge_error = (float) data.get_u16(2); |
DasSidG | 3:a7626dffb64a | 16 | _pack_capacity = (float) data.get_u16(3); |
DasSidG | 1:0c77e20b4d4c | 17 | } |
DasSidG | 1:0c77e20b4d4c | 18 | |
DasSidG | 1:0c77e20b4d4c | 19 | void get_max_min_voltage(CANMessage msg, float &_min_cell_voltage, float &_max_cell_voltage) { |
DasSidG | 1:0c77e20b4d4c | 20 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 21 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 22 | |
DasSidG | 3:a7626dffb64a | 23 | _min_cell_voltage = (float) data.get_u16(0); |
DasSidG | 3:a7626dffb64a | 24 | _max_cell_voltage = (float) data.get_u16(1); |
DasSidG | 1:0c77e20b4d4c | 25 | } |
DasSidG | 1:0c77e20b4d4c | 26 | |
DasSidG | 1:0c77e20b4d4c | 27 | void get_battery_status(CANMessage msg, bool &error) { //note using extended battery pack status |
DasSidG | 1:0c77e20b4d4c | 28 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 29 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 30 | |
DasSidG | 1:0c77e20b4d4c | 31 | error = data.getLower_uLong() > 0; |
DasSidG | 1:0c77e20b4d4c | 32 | //ignore the rest of the status as we don't care |
DasSidG | 1:0c77e20b4d4c | 33 | } |
DasSidG | 1:0c77e20b4d4c | 34 | |
DasSidG | 1:0c77e20b4d4c | 35 | void get_charger_VI_info(CANMessage msg, float &_charger_voltage, float &_charger_current, uint8_t &_charger_status) { |
DasSidG | 1:0c77e20b4d4c | 36 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 37 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 38 | |
DasSidG | 1:0c77e20b4d4c | 39 | _charger_voltage = data.get_u16(0)*100.0; |
DasSidG | 1:0c77e20b4d4c | 40 | _charger_current = data.get_u16(1)*100.0; |
DasSidG | 1:0c77e20b4d4c | 41 | _charger_status = data.get_u8(4); |
DasSidG | 1:0c77e20b4d4c | 42 | } |
DasSidG | 1:0c77e20b4d4c | 43 | |
DasSidG | 1:0c77e20b4d4c | 44 | CANMessage generate_charger_control_msg(float _desired_voltage, float _desired_current, uint8_t _charger_control) { |
DasSidG | 1:0c77e20b4d4c | 45 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 46 | msg.format = CANExtended; //the charger uses the extended CAN frame format |
DasSidG | 1:0c77e20b4d4c | 47 | msg.len = 8; |
DasSidG | 1:0c77e20b4d4c | 48 | msg.id = CHARGER_VI_CONTROL_ID; |
DasSidG | 1:0c77e20b4d4c | 49 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 50 | data.set_u16(0, (uint16_t) (_desired_voltage/100)); |
DasSidG | 1:0c77e20b4d4c | 51 | data.set_u16(1, (uint16_t) (_desired_current/100)); |
DasSidG | 1:0c77e20b4d4c | 52 | data.set_u8(4, _charger_control); |
DasSidG | 1:0c77e20b4d4c | 53 | data.set_u8(5,0); |
DasSidG | 1:0c77e20b4d4c | 54 | data.set_u8(6,0); |
DasSidG | 1:0c77e20b4d4c | 55 | data.set_u8(7,0); |
DasSidG | 1:0c77e20b4d4c | 56 | |
DasSidG | 1:0c77e20b4d4c | 57 | for(int i=0; i<8; i++) { |
DasSidG | 1:0c77e20b4d4c | 58 | msg.data[i] = data.get_u8(i); |
DasSidG | 1:0c77e20b4d4c | 59 | } |
DasSidG | 1:0c77e20b4d4c | 60 | |
DasSidG | 1:0c77e20b4d4c | 61 | return msg; |
DasSidG | 1:0c77e20b4d4c | 62 | } |
DasSidG | 1:0c77e20b4d4c | 63 | |
DasSidG | 1:0c77e20b4d4c | 64 | CANMessage generate_charger_info_msg(float _charger_voltage, float _charger_current, uint8_t _charger_status) { |
DasSidG | 1:0c77e20b4d4c | 65 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 66 | msg.len = 8; |
DasSidG | 1:0c77e20b4d4c | 67 | msg.id = CHARGER_ID; |
DasSidG | 1:0c77e20b4d4c | 68 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 69 | data.set_u16(0, (uint16_t) (_charger_voltage/100)); |
DasSidG | 1:0c77e20b4d4c | 70 | data.set_u16(1, (uint16_t) (_charger_current/100)); |
DasSidG | 1:0c77e20b4d4c | 71 | data.set_u8(4, _charger_status); |
DasSidG | 1:0c77e20b4d4c | 72 | data.set_u8(5,0); |
DasSidG | 1:0c77e20b4d4c | 73 | data.set_u8(6,0); |
DasSidG | 1:0c77e20b4d4c | 74 | data.set_u8(7,0); |
DasSidG | 1:0c77e20b4d4c | 75 | |
DasSidG | 1:0c77e20b4d4c | 76 | for(int i=0; i<8; i++) { |
DasSidG | 1:0c77e20b4d4c | 77 | msg.data[i] = data.get_u8(i); |
DasSidG | 1:0c77e20b4d4c | 78 | } |
DasSidG | 1:0c77e20b4d4c | 79 | |
DasSidG | 1:0c77e20b4d4c | 80 | return msg; |
DasSidG | 3:a7626dffb64a | 81 | } |
DasSidG | 3:a7626dffb64a | 82 | |
DasSidG | 3:a7626dffb64a | 83 | CANMessage generate_charging_finished_msg() { |
DasSidG | 3:a7626dffb64a | 84 | CANMessage msg; |
DasSidG | 3:a7626dffb64a | 85 | msg.len = 8; |
DasSidG | 3:a7626dffb64a | 86 | msg.id = BMS_BASE_ID + EEPROM_RESET_ID; |
DasSidG | 3:a7626dffb64a | 87 | |
DasSidG | 3:a7626dffb64a | 88 | CAN_Data data; |
DasSidG | 3:a7626dffb64a | 89 | data.setLowerFloat(42); //42 is the pack capacity in Ah |
DasSidG | 3:a7626dffb64a | 90 | data.setUpperFloat(100); //100% as fully charged |
DasSidG | 3:a7626dffb64a | 91 | |
DasSidG | 3:a7626dffb64a | 92 | for(int i=0; i<8; i++) { |
DasSidG | 3:a7626dffb64a | 93 | msg.data[i] = data.get_u8(i); |
DasSidG | 3:a7626dffb64a | 94 | } |
DasSidG | 3:a7626dffb64a | 95 | return msg; |
DasSidG | 1:0c77e20b4d4c | 96 | } |