CUER
/
charger_code
Code to run on the charger board (used to charge the car from the mains).
CANParserCharger.cpp@5:756fae795d37, 2017-07-30 (annotated)
- Committer:
- DasSidG
- Date:
- Sun Jul 30 22:59:37 2017 +0000
- Revision:
- 5:756fae795d37
- Parent:
- 3:a7626dffb64a
- Child:
- 7:70cf5bff23f9
Made it calculate the voltage error itself.; Made it reset the relevant part of the CAN buffer each time it reads it so that it uses fresh data each time; ; Ready for testing.
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_max_min_voltage(CANMessage msg, float &_min_cell_voltage, float &_max_cell_voltage) { |
DasSidG | 1:0c77e20b4d4c | 10 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 11 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 12 | |
DasSidG | 3:a7626dffb64a | 13 | _min_cell_voltage = (float) data.get_u16(0); |
DasSidG | 3:a7626dffb64a | 14 | _max_cell_voltage = (float) data.get_u16(1); |
DasSidG | 1:0c77e20b4d4c | 15 | } |
DasSidG | 1:0c77e20b4d4c | 16 | |
DasSidG | 1:0c77e20b4d4c | 17 | void get_battery_status(CANMessage msg, bool &error) { //note using extended battery pack status |
DasSidG | 1:0c77e20b4d4c | 18 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 19 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 20 | |
DasSidG | 1:0c77e20b4d4c | 21 | error = data.getLower_uLong() > 0; |
DasSidG | 1:0c77e20b4d4c | 22 | //ignore the rest of the status as we don't care |
DasSidG | 1:0c77e20b4d4c | 23 | } |
DasSidG | 1:0c77e20b4d4c | 24 | |
DasSidG | 1:0c77e20b4d4c | 25 | void get_charger_VI_info(CANMessage msg, float &_charger_voltage, float &_charger_current, uint8_t &_charger_status) { |
DasSidG | 1:0c77e20b4d4c | 26 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 27 | data.importCANData(msg); |
DasSidG | 1:0c77e20b4d4c | 28 | |
DasSidG | 1:0c77e20b4d4c | 29 | _charger_voltage = data.get_u16(0)*100.0; |
DasSidG | 1:0c77e20b4d4c | 30 | _charger_current = data.get_u16(1)*100.0; |
DasSidG | 1:0c77e20b4d4c | 31 | _charger_status = data.get_u8(4); |
DasSidG | 1:0c77e20b4d4c | 32 | } |
DasSidG | 1:0c77e20b4d4c | 33 | |
DasSidG | 5:756fae795d37 | 34 | void check_precharge_status (CANMessage msg, bool &_precharge_ready) { |
DasSidG | 5:756fae795d37 | 35 | CAN_Data data; |
DasSidG | 5:756fae795d37 | 36 | data.importCANData(msg); |
DasSidG | 5:756fae795d37 | 37 | |
DasSidG | 5:756fae795d37 | 38 | if(data.get_u8(1) == 4) _precharge_ready = true; |
DasSidG | 5:756fae795d37 | 39 | else _precharge_ready = false; |
DasSidG | 5:756fae795d37 | 40 | } |
DasSidG | 5:756fae795d37 | 41 | |
DasSidG | 1:0c77e20b4d4c | 42 | CANMessage generate_charger_control_msg(float _desired_voltage, float _desired_current, uint8_t _charger_control) { |
DasSidG | 1:0c77e20b4d4c | 43 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 44 | msg.format = CANExtended; //the charger uses the extended CAN frame format |
DasSidG | 1:0c77e20b4d4c | 45 | msg.len = 8; |
DasSidG | 1:0c77e20b4d4c | 46 | msg.id = CHARGER_VI_CONTROL_ID; |
DasSidG | 1:0c77e20b4d4c | 47 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 48 | data.set_u16(0, (uint16_t) (_desired_voltage/100)); |
DasSidG | 1:0c77e20b4d4c | 49 | data.set_u16(1, (uint16_t) (_desired_current/100)); |
DasSidG | 1:0c77e20b4d4c | 50 | data.set_u8(4, _charger_control); |
DasSidG | 1:0c77e20b4d4c | 51 | data.set_u8(5,0); |
DasSidG | 1:0c77e20b4d4c | 52 | data.set_u8(6,0); |
DasSidG | 1:0c77e20b4d4c | 53 | data.set_u8(7,0); |
DasSidG | 1:0c77e20b4d4c | 54 | |
DasSidG | 1:0c77e20b4d4c | 55 | for(int i=0; i<8; i++) { |
DasSidG | 1:0c77e20b4d4c | 56 | msg.data[i] = data.get_u8(i); |
DasSidG | 1:0c77e20b4d4c | 57 | } |
DasSidG | 1:0c77e20b4d4c | 58 | |
DasSidG | 1:0c77e20b4d4c | 59 | return msg; |
DasSidG | 1:0c77e20b4d4c | 60 | } |
DasSidG | 1:0c77e20b4d4c | 61 | |
DasSidG | 1:0c77e20b4d4c | 62 | CANMessage generate_charger_info_msg(float _charger_voltage, float _charger_current, uint8_t _charger_status) { |
DasSidG | 1:0c77e20b4d4c | 63 | CANMessage msg; |
DasSidG | 1:0c77e20b4d4c | 64 | msg.len = 8; |
DasSidG | 1:0c77e20b4d4c | 65 | msg.id = CHARGER_ID; |
DasSidG | 1:0c77e20b4d4c | 66 | CAN_Data data; |
DasSidG | 1:0c77e20b4d4c | 67 | data.set_u16(0, (uint16_t) (_charger_voltage/100)); |
DasSidG | 1:0c77e20b4d4c | 68 | data.set_u16(1, (uint16_t) (_charger_current/100)); |
DasSidG | 1:0c77e20b4d4c | 69 | data.set_u8(4, _charger_status); |
DasSidG | 1:0c77e20b4d4c | 70 | data.set_u8(5,0); |
DasSidG | 1:0c77e20b4d4c | 71 | data.set_u8(6,0); |
DasSidG | 1:0c77e20b4d4c | 72 | data.set_u8(7,0); |
DasSidG | 1:0c77e20b4d4c | 73 | |
DasSidG | 1:0c77e20b4d4c | 74 | for(int i=0; i<8; i++) { |
DasSidG | 1:0c77e20b4d4c | 75 | msg.data[i] = data.get_u8(i); |
DasSidG | 1:0c77e20b4d4c | 76 | } |
DasSidG | 1:0c77e20b4d4c | 77 | |
DasSidG | 1:0c77e20b4d4c | 78 | return msg; |
DasSidG | 3:a7626dffb64a | 79 | } |
DasSidG | 3:a7626dffb64a | 80 | |
DasSidG | 3:a7626dffb64a | 81 | CANMessage generate_charging_finished_msg() { |
DasSidG | 3:a7626dffb64a | 82 | CANMessage msg; |
DasSidG | 3:a7626dffb64a | 83 | msg.len = 8; |
DasSidG | 3:a7626dffb64a | 84 | msg.id = BMS_BASE_ID + EEPROM_RESET_ID; |
DasSidG | 3:a7626dffb64a | 85 | |
DasSidG | 3:a7626dffb64a | 86 | CAN_Data data; |
DasSidG | 5:756fae795d37 | 87 | data.setLowerFloat(PACK_CAPACITY); //42 is the pack capacity in Ah |
DasSidG | 3:a7626dffb64a | 88 | data.setUpperFloat(100); //100% as fully charged |
DasSidG | 3:a7626dffb64a | 89 | |
DasSidG | 3:a7626dffb64a | 90 | for(int i=0; i<8; i++) { |
DasSidG | 3:a7626dffb64a | 91 | msg.data[i] = data.get_u8(i); |
DasSidG | 3:a7626dffb64a | 92 | } |
DasSidG | 3:a7626dffb64a | 93 | return msg; |
DasSidG | 1:0c77e20b4d4c | 94 | } |