req
Dependents: BMS_BMUCore_Max_DummyData BMS_BMUCore_Max
Fork of LTC6804 by
Cell_Voltage.cpp@1:698bebac5005, 2017-06-19 (annotated)
- Committer:
- maxv008
- Date:
- Mon Jun 19 11:59:23 2017 +0000
- Revision:
- 1:698bebac5005
- Parent:
- State_Of_Charge.cpp@0:5163dda740af
- Child:
- 2:04d9634b435b
Cell Voltage structure altered for new BMS;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxv008 | 0:5163dda740af | 1 | #include "SPI_I2C_Parser.h" |
maxv008 | 0:5163dda740af | 2 | #include "mbed.h" |
maxv008 | 0:5163dda740af | 3 | #include "CAN_Data.h" |
maxv008 | 0:5163dda740af | 4 | #include "CAN_IDs.h" |
maxv008 | 0:5163dda740af | 5 | #include <stdlib.h> |
maxv008 | 1:698bebac5005 | 6 | #include "Cell_Voltage.h" |
maxv008 | 0:5163dda740af | 7 | |
maxv008 | 0:5163dda740af | 8 | //Define SoC ALCC digital in pins might be necessary as a setup, testing needed |
maxv008 | 0:5163dda740af | 9 | //DigitalIn alcc(p20); |
maxv008 | 0:5163dda740af | 10 | //Timer t; |
maxv008 | 0:5163dda740af | 11 | |
maxv008 | 0:5163dda740af | 12 | /** |
maxv008 | 0:5163dda740af | 13 | * Sets the values of the cellvoltage array with units 100 uV from all of the CMUs |
maxv008 | 0:5163dda740af | 14 | * Uses the NO_CMUS parameter for the first dimension of cellcodes |
maxv008 | 0:5163dda740af | 15 | * @param cellcodes[NO_CMUS][12] Will hold the voltage values for every cell in uV. |
maxv008 | 0:5163dda740af | 16 | */ |
maxv008 | 0:5163dda740af | 17 | void LTC6804_acquireVoltage(uint16_t cellcodes [][12]) |
maxv008 | 0:5163dda740af | 18 | { |
maxv008 | 0:5163dda740af | 19 | //spi.format(8,3); //All data transfer on LTC6804 occur in byte groups. LTC6820 set up such that POL=1 and PHA=3, this corresponds to mode 3 in mbed library. spi.frequency(spiBitrate); |
maxv008 | 0:5163dda740af | 20 | //spi.frequency(spiBitrate); |
maxv008 | 0:5163dda740af | 21 | wake_LTC6804();//ensures CMU's are in ready state and wakes it up from low power mode |
maxv008 | 0:5163dda740af | 22 | wait_us(330); |
maxv008 | 0:5163dda740af | 23 | wake_LTC6804(); //This might need to be removed |
maxv008 | 0:5163dda740af | 24 | LTC6804_acquireVoltageTx(); |
maxv008 | 0:5163dda740af | 25 | wait_us(930); |
maxv008 | 0:5163dda740af | 26 | LTC6804_acquireAllVoltageRegRx(0, NO_CMUS, cellcodes); |
maxv008 | 0:5163dda740af | 27 | } |
maxv008 | 0:5163dda740af | 28 | |
maxv008 | 0:5163dda740af | 29 | /** |
maxv008 | 0:5163dda740af | 30 | Sets the balancing transistors by adjusting the configuration states of the |
maxv008 | 0:5163dda740af | 31 | LTC6804. This version of the function writes all 12 states for a chosen |
maxv008 | 0:5163dda740af | 32 | transistor. If other forms end up being more useful I will add other overloaded |
maxv008 | 0:5163dda740af | 33 | versions. |
maxv008 | 0:5163dda740af | 34 | |
maxv008 | 0:5163dda740af | 35 | @param uint8_t ic The specific IC to write to (can be changed in favor of a |
maxv008 | 0:5163dda740af | 36 | states array of size 12*total_ic if preferred) |
maxv008 | 0:5163dda740af | 37 | |
maxv008 | 0:5163dda740af | 38 | @param uint8_t states[12] For Sn in S1-S12 set states[n-1] to 1 to enable |
maxv008 | 0:5163dda740af | 39 | balancing and 0 to disable balancing. |
maxv008 | 0:5163dda740af | 40 | */ |
maxv008 | 0:5163dda740af | 41 | void LTC6804_balance(uint8_t ic, uint8_t states[12]) |
maxv008 | 0:5163dda740af | 42 | { |
maxv008 | 0:5163dda740af | 43 | uint8_t total_ic = NO_CMUS; |
maxv008 | 0:5163dda740af | 44 | //Consider using this to define the configs: (uint8_t *)malloc(total_ic*sizeof(uint8_t)); |
maxv008 | 0:5163dda740af | 45 | uint8_t r_config[total_ic][8]; |
maxv008 | 0:5163dda740af | 46 | uint8_t w_config[total_ic][6];//Size is smaller because there aren't PEC codes |
maxv008 | 0:5163dda740af | 47 | wake_LTC6804(); |
maxv008 | 0:5163dda740af | 48 | wait_us(330); |
maxv008 | 0:5163dda740af | 49 | LTC6804_rdcfg(total_ic, r_config); |
maxv008 | 0:5163dda740af | 50 | |
maxv008 | 0:5163dda740af | 51 | /*for (int i=0; i<8; i++) { |
maxv008 | 0:5163dda740af | 52 | printf("TEST %d config \r\n", (uint8_t)r_config[0][i]); |
maxv008 | 0:5163dda740af | 53 | } */ |
maxv008 | 0:5163dda740af | 54 | |
maxv008 | 0:5163dda740af | 55 | uint8_t cfgr4 = 0; //This entire configuration is DCC states |
maxv008 | 0:5163dda740af | 56 | uint8_t cfgr5 = r_config[ic][5]; |
maxv008 | 0:5163dda740af | 57 | |
maxv008 | 0:5163dda740af | 58 | for(int i = 0; i < 8; i++) |
maxv008 | 0:5163dda740af | 59 | { |
maxv008 | 0:5163dda740af | 60 | //Note: This disgusting thing is written by someone who has not used c++ in a long time |
maxv008 | 0:5163dda740af | 61 | cfgr4 = states[i] ? cfgr4 | (1u << i) : cfgr4 & ~(1u << i); |
maxv008 | 0:5163dda740af | 62 | } |
maxv008 | 0:5163dda740af | 63 | |
maxv008 | 0:5163dda740af | 64 | for(int i = 8; i < 12; i++) |
maxv008 | 0:5163dda740af | 65 | { |
maxv008 | 0:5163dda740af | 66 | cfgr5 = states[i] ? cfgr5 | (1u << (i-8)) : cfgr5 & ~(1u << (i-8)); |
maxv008 | 0:5163dda740af | 67 | } |
maxv008 | 0:5163dda740af | 68 | |
maxv008 | 0:5163dda740af | 69 | //printf("cfgr4 %d \r\n", (uint8_t)cfgr4); |
maxv008 | 0:5163dda740af | 70 | //printf("cfgr5 %d \r\n", (uint8_t)cfgr5); |
maxv008 | 0:5163dda740af | 71 | for(int i =0 ; i < total_ic; i++) |
maxv008 | 0:5163dda740af | 72 | { |
maxv008 | 0:5163dda740af | 73 | for(int j = 0; j < 6; j++) |
maxv008 | 0:5163dda740af | 74 | { |
maxv008 | 0:5163dda740af | 75 | w_config[i][j] = r_config[i][j]; |
maxv008 | 0:5163dda740af | 76 | } |
maxv008 | 0:5163dda740af | 77 | } |
maxv008 | 0:5163dda740af | 78 | w_config[ic][4] = cfgr4; |
maxv008 | 0:5163dda740af | 79 | w_config[ic][5] = cfgr5; |
maxv008 | 0:5163dda740af | 80 | |
maxv008 | 0:5163dda740af | 81 | wake_LTC6804(); |
maxv008 | 0:5163dda740af | 82 | wait_us(330); |
maxv008 | 0:5163dda740af | 83 | LTC6804_wrcfg(total_ic,w_config); //Make sure this is written in the write order |
maxv008 | 0:5163dda740af | 84 | } |
maxv008 | 0:5163dda740af | 85 | /** |
maxv008 | 0:5163dda740af | 86 | Takes a set of voltages corresponding to cell voltage measurements and a voltage |
maxv008 | 0:5163dda740af | 87 | limit (presumably 4200 mV) and then drains all cells as needed. If current state |
maxv008 | 0:5163dda740af | 88 | of balancing is needed outside of this function, it can be modified. |
maxv008 | 0:5163dda740af | 89 | |
maxv008 | 0:5163dda740af | 90 | Current implementation of this function just uses the hard limit, so it might end |
maxv008 | 0:5163dda740af | 91 | up constantly enabling and disabling balancing as it hits the limit then goes under. |
maxv008 | 0:5163dda740af | 92 | That behavior can be improved upon if needed. One idea is to use derivative of |
maxv008 | 0:5163dda740af | 93 | voltage with time to predict when it will hit maxVoltage so there is no risk of |
maxv008 | 0:5163dda740af | 94 | going over, or allow them to drain such that all of the cells are at the same voltage. |
maxv008 | 0:5163dda740af | 95 | |
maxv008 | 0:5163dda740af | 96 | @param uint16_t voltages[][12] Measured voltages to be used for deciding what to |
maxv008 | 0:5163dda740af | 97 | balance. The dimensions must be [total_ic][12]. |
maxv008 | 0:5163dda740af | 98 | |
maxv008 | 0:5163dda740af | 99 | @param uint16_t maxVoltage voltage limit for the cells |
maxv008 | 0:5163dda740af | 100 | */ |
maxv008 | 0:5163dda740af | 101 | void LTC6804_balanceVoltage(uint16_t voltages[][12], uint16_t maxVoltage) |
maxv008 | 0:5163dda740af | 102 | { |
maxv008 | 0:5163dda740af | 103 | uint8_t total_ic = NO_CMUS; |
maxv008 | 0:5163dda740af | 104 | //Consider making states a parameter as a pointer so it can be referenced outside of the function easier. |
maxv008 | 0:5163dda740af | 105 | uint8_t states[total_ic][12]; |
maxv008 | 0:5163dda740af | 106 | for(int i = 0; i < total_ic; i++) |
maxv008 | 0:5163dda740af | 107 | { |
maxv008 | 0:5163dda740af | 108 | for(int j = 0; j < 12; j++) |
maxv008 | 0:5163dda740af | 109 | { |
maxv008 | 0:5163dda740af | 110 | states[i][j] = voltages[i][j] >= maxVoltage ? 1 : 0; //Not sure if ternary operator is needed in C. |
maxv008 | 0:5163dda740af | 111 | } |
maxv008 | 0:5163dda740af | 112 | LTC6804_balance(i, states[i]); |
maxv008 | 0:5163dda740af | 113 | } |
maxv008 | 0:5163dda740af | 114 | } |