![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
all bms comunicating with nucleo board and balancing
Dependencies: CANnucleo LTC68041 mbed
Fork of BMS_2 by
main.cpp@35:117a807c481a, 2016-11-09 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Wed Nov 09 12:10:48 2016 +0000
- Revision:
- 35:117a807c481a
- Parent:
- 34:8f92a17c5be9
- Child:
- 36:e70020bee11f
readed 1 ntc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 19:872e304d7e17 | 1 | #include "CANnucleo.h" |
hudakz | 16:a86f339d1c25 | 2 | #include "mbed.h" |
ser1516 | 21:988413f53fbe | 3 | #include "LTC68041.h" |
hudakz | 16:a86f339d1c25 | 4 | |
ser1516 | 21:988413f53fbe | 5 | uint8_t const TOTAL_IC = 1;//!<number of ICs in the daisy chain |
ser1516 | 21:988413f53fbe | 6 | uint8_t rx_cfg[TOTAL_IC][8]; |
ser1516 | 21:988413f53fbe | 7 | uint8_t tx_cfg[TOTAL_IC][6]; |
ser1516 | 21:988413f53fbe | 8 | uint16_t aux_codes[TOTAL_IC][6]; |
ser1516 | 21:988413f53fbe | 9 | uint16_t cell_codes[TOTAL_IC][12]; |
Crazyaboutmachines | 33:54861cd567cd | 10 | |
Crazyaboutmachines | 33:54861cd567cd | 11 | //uint16_t temp_codes[32]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}; |
Crazyaboutmachines | 34:8f92a17c5be9 | 12 | float temp_codes[32]; |
Crazyaboutmachines | 33:54861cd567cd | 13 | |
ser1516 | 21:988413f53fbe | 14 | volatile bool msgAvailable = false; |
ser1516 | 21:988413f53fbe | 15 | volatile bool to_send = false; |
hudakz | 17:18d4d0ff26a6 | 16 | CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 17:18d4d0ff26a6 | 17 | CANnucleo::CANMessage rxMsg; |
hudakz | 17:18d4d0ff26a6 | 18 | CANnucleo::CANMessage txMsg; |
ser1516 | 21:988413f53fbe | 19 | DigitalOut led(PA_5); |
Crazyaboutmachines | 32:a52605800210 | 20 | |
ser1516 | 21:988413f53fbe | 21 | /*!*********************************** |
ser1516 | 21:988413f53fbe | 22 | \brief Initializes the configuration array |
ser1516 | 21:988413f53fbe | 23 | **************************************/ |
ser1516 | 21:988413f53fbe | 24 | void init_cfg() |
ser1516 | 21:988413f53fbe | 25 | { |
ser1516 | 21:988413f53fbe | 26 | for (int i = 0; i<TOTAL_IC; i++) { |
ser1516 | 21:988413f53fbe | 27 | tx_cfg[i][0] = 0xFE; |
ser1516 | 21:988413f53fbe | 28 | tx_cfg[i][1] = 0x00 ; |
ser1516 | 21:988413f53fbe | 29 | tx_cfg[i][2] = 0x00 ; |
ser1516 | 21:988413f53fbe | 30 | tx_cfg[i][3] = 0x00 ; |
ser1516 | 21:988413f53fbe | 31 | tx_cfg[i][4] = 0x00 ; |
ser1516 | 21:988413f53fbe | 32 | tx_cfg[i][5] = 0x00 ; |
ser1516 | 21:988413f53fbe | 33 | } |
ser1516 | 21:988413f53fbe | 34 | } |
ser1516 | 21:988413f53fbe | 35 | void serial_print_hex(uint8_t data) |
ser1516 | 21:988413f53fbe | 36 | { |
ser1516 | 21:988413f53fbe | 37 | if (data< 16) { |
ser1516 | 21:988413f53fbe | 38 | printf("0"); |
ser1516 | 21:988413f53fbe | 39 | printf("%x",(uint8_t)data); |
ser1516 | 21:988413f53fbe | 40 | } else |
ser1516 | 21:988413f53fbe | 41 | printf("%x",(uint8_t)data); |
ser1516 | 21:988413f53fbe | 42 | } |
ser1516 | 21:988413f53fbe | 43 | void print_config() |
ser1516 | 21:988413f53fbe | 44 | { |
ser1516 | 21:988413f53fbe | 45 | int cfg_pec; |
ser1516 | 21:988413f53fbe | 46 | printf("Written Configuration:\n\r "); |
ser1516 | 21:988413f53fbe | 47 | for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 48 | printf(" IC "); |
ser1516 | 21:988413f53fbe | 49 | printf("%d", current_ic+1); |
ser1516 | 21:988413f53fbe | 50 | printf(": "); |
ser1516 | 21:988413f53fbe | 51 | printf("0x"); |
ser1516 | 21:988413f53fbe | 52 | serial_print_hex(tx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 53 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 54 | serial_print_hex(tx_cfg[current_ic][1]); |
ser1516 | 21:988413f53fbe | 55 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 56 | serial_print_hex(tx_cfg[current_ic][2]); |
ser1516 | 21:988413f53fbe | 57 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 58 | serial_print_hex(tx_cfg[current_ic][3]); |
ser1516 | 21:988413f53fbe | 59 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 60 | serial_print_hex(tx_cfg[current_ic][4]); |
ser1516 | 21:988413f53fbe | 61 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 62 | serial_print_hex(tx_cfg[current_ic][5]); |
ser1516 | 21:988413f53fbe | 63 | printf(", Calculated PEC: 0x"); |
ser1516 | 21:988413f53fbe | 64 | cfg_pec = pec15_calc(6,&tx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 65 | serial_print_hex((uint8_t)(cfg_pec>>8)); |
ser1516 | 21:988413f53fbe | 66 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 67 | serial_print_hex((uint8_t)(cfg_pec)); |
ser1516 | 21:988413f53fbe | 68 | } |
ser1516 | 21:988413f53fbe | 69 | printf("\n\r"); |
ser1516 | 21:988413f53fbe | 70 | } |
ser1516 | 21:988413f53fbe | 71 | |
ser1516 | 21:988413f53fbe | 72 | void print_rxconfig() |
ser1516 | 21:988413f53fbe | 73 | { |
ser1516 | 21:988413f53fbe | 74 | printf("Received Configuration "); |
ser1516 | 21:988413f53fbe | 75 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 76 | printf(" IC "); |
ser1516 | 21:988413f53fbe | 77 | printf("%d " ,current_ic+1); |
ser1516 | 21:988413f53fbe | 78 | printf(": 0x"); |
ser1516 | 21:988413f53fbe | 79 | serial_print_hex(rx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 80 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 81 | serial_print_hex(rx_cfg[current_ic][1]); |
ser1516 | 21:988413f53fbe | 82 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 83 | serial_print_hex(rx_cfg[current_ic][2]); |
ser1516 | 21:988413f53fbe | 84 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 85 | serial_print_hex(rx_cfg[current_ic][3]); |
ser1516 | 21:988413f53fbe | 86 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 87 | serial_print_hex(rx_cfg[current_ic][4]); |
ser1516 | 21:988413f53fbe | 88 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 89 | serial_print_hex(rx_cfg[current_ic][5]); |
ser1516 | 21:988413f53fbe | 90 | printf(", Received PEC: 0x"); |
ser1516 | 21:988413f53fbe | 91 | serial_print_hex(rx_cfg[current_ic][6]); |
ser1516 | 21:988413f53fbe | 92 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 93 | serial_print_hex(rx_cfg[current_ic][7]); |
ser1516 | 21:988413f53fbe | 94 | } |
ser1516 | 21:988413f53fbe | 95 | } |
Crazyaboutmachines | 25:6f4f7510db24 | 96 | |
ser1516 | 21:988413f53fbe | 97 | int err; |
ser1516 | 21:988413f53fbe | 98 | void print_cells2() |
ser1516 | 21:988413f53fbe | 99 | { |
ser1516 | 21:988413f53fbe | 100 | |
ser1516 | 21:988413f53fbe | 101 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 102 | |
ser1516 | 21:988413f53fbe | 103 | for (int i=0; i<12; i++) { |
ser1516 | 21:988413f53fbe | 104 | printf(" C"); |
ser1516 | 21:988413f53fbe | 105 | printf("%d",i+1); |
ser1516 | 21:988413f53fbe | 106 | printf(":"); |
ser1516 | 21:988413f53fbe | 107 | printf("%f", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 108 | printf(","); |
ser1516 | 21:988413f53fbe | 109 | } |
hudakz | 0:c5e5d0df6f2a | 110 | |
ser1516 | 21:988413f53fbe | 111 | } |
ser1516 | 21:988413f53fbe | 112 | } |
Crazyaboutmachines | 25:6f4f7510db24 | 113 | |
ser1516 | 21:988413f53fbe | 114 | void pec_error() |
ser1516 | 21:988413f53fbe | 115 | { |
ser1516 | 21:988413f53fbe | 116 | for(int i = 0; i<5; i++) { |
ser1516 | 21:988413f53fbe | 117 | led = 1; |
ser1516 | 21:988413f53fbe | 118 | wait(0.2); |
ser1516 | 21:988413f53fbe | 119 | led=0; |
ser1516 | 21:988413f53fbe | 120 | wait(0.2); |
ser1516 | 21:988413f53fbe | 121 | } |
ser1516 | 21:988413f53fbe | 122 | } |
ser1516 | 21:988413f53fbe | 123 | char cells_left=0; |
Crazyaboutmachines | 33:54861cd567cd | 124 | char temps_left=0; |
ser1516 | 21:988413f53fbe | 125 | Ticker ticker; |
ser1516 | 21:988413f53fbe | 126 | Ticker sender; |
ser1516 | 21:988413f53fbe | 127 | |
ser1516 | 21:988413f53fbe | 128 | typedef union can_union { |
ser1516 | 21:988413f53fbe | 129 | int i[2]; |
ser1516 | 21:988413f53fbe | 130 | char bytes[8]; |
ser1516 | 21:988413f53fbe | 131 | float f[2]; |
ser1516 | 21:988413f53fbe | 132 | } data; |
ser1516 | 21:988413f53fbe | 133 | |
Crazyaboutmachines | 33:54861cd567cd | 134 | |
ser1516 | 21:988413f53fbe | 135 | void message_trigger() |
ser1516 | 21:988413f53fbe | 136 | { |
ser1516 | 21:988413f53fbe | 137 | if(cells_left < 1) { |
ser1516 | 21:988413f53fbe | 138 | sender.detach(); |
ser1516 | 21:988413f53fbe | 139 | } else { |
ser1516 | 21:988413f53fbe | 140 | to_send = 1; |
ser1516 | 21:988413f53fbe | 141 | } |
ser1516 | 21:988413f53fbe | 142 | } |
ser1516 | 21:988413f53fbe | 143 | |
ser1516 | 21:988413f53fbe | 144 | void check_charging_voltage() |
ser1516 | 21:988413f53fbe | 145 | { |
ser1516 | 21:988413f53fbe | 146 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 147 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 148 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 149 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 150 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 151 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 152 | pec_error(); |
ser1516 | 21:988413f53fbe | 153 | } |
Crazyaboutmachines | 31:1fab4bb4e9ee | 154 | cells_left = 12; |
ser1516 | 21:988413f53fbe | 155 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 156 | for (int i=0; i<12; i++) { |
Crazyaboutmachines | 25:6f4f7510db24 | 157 | if(cell_codes[current_ic][i]*0.0001 > 3.6) { |
ser1516 | 21:988413f53fbe | 158 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 159 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 160 | tx_cfg[0][4] = tx_cfg[0][4] | 0x01 ; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 161 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 162 | tx_cfg[0][4] = tx_cfg[0][4] | 0x02 ; // 00000010 |
Crazyaboutmachines | 30:9c9f2d789153 | 163 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 164 | tx_cfg[0][4] = tx_cfg[0][4] | 0x04 ; // 00000100 |
Crazyaboutmachines | 30:9c9f2d789153 | 165 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 166 | tx_cfg[0][4] = tx_cfg[0][4] | 0x08 ; // 00001000 |
Crazyaboutmachines | 30:9c9f2d789153 | 167 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 168 | tx_cfg[0][4] = tx_cfg[0][4] | 0x10 ; // 00010000 |
Crazyaboutmachines | 30:9c9f2d789153 | 169 | case 6: //cell 7 |
Crazyaboutmachines | 30:9c9f2d789153 | 170 | tx_cfg[0][4] = tx_cfg[0][4] | 0x40 ; // 01000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 171 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 172 | tx_cfg[0][4] = tx_cfg[0][4] | 0x80 ; // 10000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 173 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 174 | tx_cfg[0][5] = tx_cfg[0][5] | 0x01; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 175 | case 9: //cell 10 |
Crazyaboutmachines | 30:9c9f2d789153 | 176 | tx_cfg[0][5] = tx_cfg[0][5] | 0x02; // 00000010 |
Crazyaboutmachines | 26:c55656391a29 | 177 | } |
Crazyaboutmachines | 26:c55656391a29 | 178 | } else { |
Crazyaboutmachines | 26:c55656391a29 | 179 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 180 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 181 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFE ; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 182 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 183 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFD ; // 11111101 |
Crazyaboutmachines | 30:9c9f2d789153 | 184 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 185 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFB ; // 11111011 |
Crazyaboutmachines | 30:9c9f2d789153 | 186 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 187 | tx_cfg[0][4] = tx_cfg[0][4] & 0xF7 ; // 11110111 |
Crazyaboutmachines | 30:9c9f2d789153 | 188 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 189 | tx_cfg[0][4] = tx_cfg[0][4] & 0xEF ; // 11101111 |
Crazyaboutmachines | 30:9c9f2d789153 | 190 | case 6: //cell 7 |
Crazyaboutmachines | 25:6f4f7510db24 | 191 | tx_cfg[0][4] = tx_cfg[0][4] & 0xBF ; // 10111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 192 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 193 | tx_cfg[0][4] = tx_cfg[0][4] & 0x7F ; // 01111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 194 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 195 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFE; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 196 | case 9: //cell 10 |
Crazyaboutmachines | 26:c55656391a29 | 197 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFD; // 11111101 |
Crazyaboutmachines | 26:c55656391a29 | 198 | } |
ser1516 | 21:988413f53fbe | 199 | } |
ser1516 | 21:988413f53fbe | 200 | } |
ser1516 | 21:988413f53fbe | 201 | } |
ser1516 | 21:988413f53fbe | 202 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 203 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 204 | } |
ser1516 | 21:988413f53fbe | 205 | |
ser1516 | 21:988413f53fbe | 206 | void check_discharging_voltage() |
ser1516 | 21:988413f53fbe | 207 | { |
ser1516 | 21:988413f53fbe | 208 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 209 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 210 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 211 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 212 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 213 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 214 | pec_error(); |
ser1516 | 21:988413f53fbe | 215 | }/* |
ser1516 | 21:988413f53fbe | 216 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 217 | for (int i=0; i<11; i++) { |
ser1516 | 21:988413f53fbe | 218 | //printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 219 | if(cell_codes[current_ic][i]*0.0001 < 2.7) { |
ser1516 | 21:988413f53fbe | 220 | txMsg.clear(); |
ser1516 | 21:988413f53fbe | 221 | txMsg.id = 1; |
ser1516 | 21:988413f53fbe | 222 | printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 223 | |
ser1516 | 21:988413f53fbe | 224 | txMsg << cell_codes[current_ic][i]*0.0001; |
ser1516 | 21:988413f53fbe | 225 | can.write(txMsg); |
ser1516 | 21:988413f53fbe | 226 | wait(0.1); |
ser1516 | 21:988413f53fbe | 227 | } |
ser1516 | 21:988413f53fbe | 228 | } |
ser1516 | 21:988413f53fbe | 229 | }*/ |
ser1516 | 21:988413f53fbe | 230 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 231 | cells_left = 12; |
Crazyaboutmachines | 32:a52605800210 | 232 | tx_cfg[0][4] = tx_cfg[0][4] & 0b00000000; //para desactivar balanceamento durante a descarga |
Crazyaboutmachines | 32:a52605800210 | 233 | tx_cfg[0][5] = tx_cfg[0][5] & 0b11110000; |
Crazyaboutmachines | 32:a52605800210 | 234 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 235 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 236 | } |
ser1516 | 21:988413f53fbe | 237 | |
Crazyaboutmachines | 33:54861cd567cd | 238 | AnalogIn MUXA_Read(PC_1); |
Crazyaboutmachines | 33:54861cd567cd | 239 | DigitalOut MUXA_0(PB_10); |
Crazyaboutmachines | 33:54861cd567cd | 240 | DigitalOut MUXA_1(PB_2); |
Crazyaboutmachines | 33:54861cd567cd | 241 | DigitalOut MUXA_2(PB_0); |
Crazyaboutmachines | 33:54861cd567cd | 242 | DigitalOut MUXA_3(PB_1); |
Crazyaboutmachines | 33:54861cd567cd | 243 | AnalogIn MUXB_Read(PC_2); |
Crazyaboutmachines | 33:54861cd567cd | 244 | DigitalOut MUXB_0(PC_7); |
Crazyaboutmachines | 33:54861cd567cd | 245 | DigitalOut MUXB_1(PC_6); |
Crazyaboutmachines | 33:54861cd567cd | 246 | DigitalOut MUXB_2(PB_14); |
Crazyaboutmachines | 33:54861cd567cd | 247 | DigitalOut MUXB_3(PB_15); |
Crazyaboutmachines | 33:54861cd567cd | 248 | |
Crazyaboutmachines | 34:8f92a17c5be9 | 249 | |
Crazyaboutmachines | 34:8f92a17c5be9 | 250 | void check_temperatures() |
Crazyaboutmachines | 34:8f92a17c5be9 | 251 | { |
Crazyaboutmachines | 34:8f92a17c5be9 | 252 | float meas; |
Crazyaboutmachines | 34:8f92a17c5be9 | 253 | int i=14; |
Crazyaboutmachines | 35:117a807c481a | 254 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=0; //(escolha do NTC on board) |
Crazyaboutmachines | 34:8f92a17c5be9 | 255 | meas = MUXA_Read.read(); |
Crazyaboutmachines | 34:8f92a17c5be9 | 256 | temp_codes[i]=meas; |
Crazyaboutmachines | 34:8f92a17c5be9 | 257 | temps_left = 14; |
Crazyaboutmachines | 34:8f92a17c5be9 | 258 | } |
Crazyaboutmachines | 34:8f92a17c5be9 | 259 | |
Crazyaboutmachines | 34:8f92a17c5be9 | 260 | |
Crazyaboutmachines | 34:8f92a17c5be9 | 261 | /* |
Crazyaboutmachines | 33:54861cd567cd | 262 | void check_temperatures() |
Crazyaboutmachines | 33:54861cd567cd | 263 | { |
Crazyaboutmachines | 33:54861cd567cd | 264 | for (int i=0; i<32; i++){ |
Crazyaboutmachines | 33:54861cd567cd | 265 | switch (i) { |
Crazyaboutmachines | 33:54861cd567cd | 266 | case 0: //ntc 1 //from MUXA |
Crazyaboutmachines | 33:54861cd567cd | 267 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 268 | case 1: //ntc 2 |
Crazyaboutmachines | 33:54861cd567cd | 269 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 270 | case 2: //ntc 3 |
Crazyaboutmachines | 33:54861cd567cd | 271 | MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 272 | case 3: //ntc 4 |
Crazyaboutmachines | 33:54861cd567cd | 273 | MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 274 | case 4: //ntc 5 |
Crazyaboutmachines | 33:54861cd567cd | 275 | MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 276 | case 5: //ntc 6 |
Crazyaboutmachines | 33:54861cd567cd | 277 | MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 278 | case 6: //ntc 7 |
Crazyaboutmachines | 33:54861cd567cd | 279 | MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 280 | case 7: //ntc 8 |
Crazyaboutmachines | 33:54861cd567cd | 281 | MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 282 | case 8: //ntc 9 |
Crazyaboutmachines | 33:54861cd567cd | 283 | MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 284 | case 9: //ntc 10 |
Crazyaboutmachines | 33:54861cd567cd | 285 | MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 286 | case 10: //ntc 11 |
Crazyaboutmachines | 33:54861cd567cd | 287 | MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 288 | case 11: //ntc 12 |
Crazyaboutmachines | 33:54861cd567cd | 289 | MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 290 | case 12: //ntc 13 |
Crazyaboutmachines | 33:54861cd567cd | 291 | MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 292 | case 13: //ntc 14 |
Crazyaboutmachines | 33:54861cd567cd | 293 | MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 294 | case 14: //ntc 15 |
Crazyaboutmachines | 33:54861cd567cd | 295 | MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 296 | case 15: //ntc 16 |
Crazyaboutmachines | 33:54861cd567cd | 297 | MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 298 | case 16: //ntc 17 //from MUXB |
Crazyaboutmachines | 33:54861cd567cd | 299 | MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 300 | case 17: //ntc 18 |
Crazyaboutmachines | 33:54861cd567cd | 301 | MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 302 | case 18: //ntc 19 |
Crazyaboutmachines | 33:54861cd567cd | 303 | MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 304 | case 19: //ntc 20 |
Crazyaboutmachines | 33:54861cd567cd | 305 | MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 306 | case 20: //ntc 21 |
Crazyaboutmachines | 33:54861cd567cd | 307 | MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 308 | case 21: //ntc 22 |
Crazyaboutmachines | 33:54861cd567cd | 309 | MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 310 | case 22: //ntc 23 |
Crazyaboutmachines | 33:54861cd567cd | 311 | MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 312 | case 23: //ntc 24 |
Crazyaboutmachines | 33:54861cd567cd | 313 | MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 314 | case 24: //ntc 25 |
Crazyaboutmachines | 33:54861cd567cd | 315 | MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 316 | case 25: //ntc 26 |
Crazyaboutmachines | 33:54861cd567cd | 317 | MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 318 | case 26: //ntc 27 |
Crazyaboutmachines | 33:54861cd567cd | 319 | MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 320 | case 27: //ntc 28 |
Crazyaboutmachines | 33:54861cd567cd | 321 | MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 322 | case 28: //ntc 29 |
Crazyaboutmachines | 33:54861cd567cd | 323 | MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 324 | case 29: //ntc 30 |
Crazyaboutmachines | 33:54861cd567cd | 325 | MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 326 | case 30: //ntc 31 |
Crazyaboutmachines | 33:54861cd567cd | 327 | MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=0; |
Crazyaboutmachines | 33:54861cd567cd | 328 | case 31: //ntc 32 |
Crazyaboutmachines | 33:54861cd567cd | 329 | MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=1; |
Crazyaboutmachines | 33:54861cd567cd | 330 | } |
Crazyaboutmachines | 33:54861cd567cd | 331 | wait_ms(10); //tempo para a tensao á saida do mux estabilisar |
Crazyaboutmachines | 33:54861cd567cd | 332 | if(i<16){ // Converts and read the analog input value (value from 0.0 to 1.0) |
Crazyaboutmachines | 33:54861cd567cd | 333 | temp_codes[i] = MUXA_Read.read()*3300; |
Crazyaboutmachines | 33:54861cd567cd | 334 | }else{ |
Crazyaboutmachines | 33:54861cd567cd | 335 | temp_codes[i] = MUXB_Read.read()*3300; |
Crazyaboutmachines | 33:54861cd567cd | 336 | } |
Crazyaboutmachines | 33:54861cd567cd | 337 | } |
Crazyaboutmachines | 33:54861cd567cd | 338 | temps_left = 32; |
Crazyaboutmachines | 33:54861cd567cd | 339 | } |
Crazyaboutmachines | 34:8f92a17c5be9 | 340 | */ |
ser1516 | 21:988413f53fbe | 341 | void onMsgReceived() |
ser1516 | 21:988413f53fbe | 342 | { |
hudakz | 16:a86f339d1c25 | 343 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 344 | } |
hudakz | 16:a86f339d1c25 | 345 | |
ser1516 | 21:988413f53fbe | 346 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 347 | bool to_charge_or_not_to_charge=false; // false = discharge |
ser1516 | 21:988413f53fbe | 348 | bool charging = false; |
Crazyaboutmachines | 29:86e963cc5ce7 | 349 | bool discharging = false; |
Crazyaboutmachines | 25:6f4f7510db24 | 350 | |
ser1516 | 21:988413f53fbe | 351 | void monitor() |
ser1516 | 21:988413f53fbe | 352 | { |
ser1516 | 21:988413f53fbe | 353 | led = !led; |
ser1516 | 21:988413f53fbe | 354 | if(to_charge_or_not_to_charge) { |
ser1516 | 21:988413f53fbe | 355 | charging = 1; |
ser1516 | 21:988413f53fbe | 356 | discharging = 0; |
ser1516 | 21:988413f53fbe | 357 | } else { |
ser1516 | 21:988413f53fbe | 358 | discharging = 1; |
ser1516 | 21:988413f53fbe | 359 | charging = 0; |
hudakz | 0:c5e5d0df6f2a | 360 | } |
hudakz | 0:c5e5d0df6f2a | 361 | } |
hudakz | 7:2dce8ed51091 | 362 | |
hudakz | 12:e91e44924194 | 363 | |
Crazyaboutmachines | 26:c55656391a29 | 364 | uint8_t motostate=0; |
Crazyaboutmachines | 26:c55656391a29 | 365 | |
ser1516 | 21:988413f53fbe | 366 | int main() |
ser1516 | 21:988413f53fbe | 367 | { |
Crazyaboutmachines | 26:c55656391a29 | 368 | data data; |
ser1516 | 21:988413f53fbe | 369 | //printf("starting\n\r"); |
ser1516 | 21:988413f53fbe | 370 | led =1; |
ser1516 | 21:988413f53fbe | 371 | wait(1); |
ser1516 | 21:988413f53fbe | 372 | pec_error(); |
Crazyaboutmachines | 31:1fab4bb4e9ee | 373 | to_charge_or_not_to_charge=0; |
Crazyaboutmachines | 31:1fab4bb4e9ee | 374 | charging = 0; |
Crazyaboutmachines | 29:86e963cc5ce7 | 375 | discharging = 1; |
Crazyaboutmachines | 32:a52605800210 | 376 | ticker.attach(&monitor, 2); |
ser1516 | 21:988413f53fbe | 377 | LTC6804_initialize(); |
ser1516 | 21:988413f53fbe | 378 | init_cfg(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 379 | //write configuration |
ser1516 | 21:988413f53fbe | 380 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 381 | __disable_irq(); // Disable Interrupts |
ser1516 | 21:988413f53fbe | 382 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 383 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 384 | wait(1); |
ser1516 | 21:988413f53fbe | 385 | //read configuration: may differ from written config |
ser1516 | 21:988413f53fbe | 386 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 387 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 388 | err = LTC6804_rdcfg(TOTAL_IC,rx_cfg); |
ser1516 | 21:988413f53fbe | 389 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 390 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 391 | pec_error(); |
ser1516 | 21:988413f53fbe | 392 | } |
ser1516 | 21:988413f53fbe | 393 | wait(0.5); |
ser1516 | 21:988413f53fbe | 394 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 395 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 396 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 397 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 398 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 399 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 400 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 401 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 402 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 403 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 404 | pec_error(); |
ser1516 | 21:988413f53fbe | 405 | } |
ser1516 | 21:988413f53fbe | 406 | can.frequency(1000000); // set bit rate to 1Mbps |
ser1516 | 21:988413f53fbe | 407 | can.attach(&onMsgReceived); |
ser1516 | 21:988413f53fbe | 408 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 409 | while(1) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 410 | if(charging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 411 | charging = 0; |
ser1516 | 21:988413f53fbe | 412 | check_charging_voltage(); |
Crazyaboutmachines | 33:54861cd567cd | 413 | check_temperatures(); |
Crazyaboutmachines | 31:1fab4bb4e9ee | 414 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 415 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 416 | if(discharging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 417 | discharging = 0; |
ser1516 | 21:988413f53fbe | 418 | check_discharging_voltage(); |
Crazyaboutmachines | 33:54861cd567cd | 419 | check_temperatures(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 420 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 421 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 422 | if(to_send) { |
ser1516 | 21:988413f53fbe | 423 | to_send=0; |
Crazyaboutmachines | 33:54861cd567cd | 424 | //-----------------1º send cell voltages |
Crazyaboutmachines | 35:117a807c481a | 425 | /* txMsg.clear(); |
Crazyaboutmachines | 33:54861cd567cd | 426 | txMsg.id = 11; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
ser1516 | 21:988413f53fbe | 427 | txMsg.len = 5; |
ser1516 | 21:988413f53fbe | 428 | data.f[0] = cell_codes[0][cells_left-1]*0.0001; |
ser1516 | 21:988413f53fbe | 429 | txMsg.data[0] = data.bytes[0]; |
ser1516 | 21:988413f53fbe | 430 | txMsg.data[1] = data.bytes[1]; |
ser1516 | 21:988413f53fbe | 431 | txMsg.data[2] = data.bytes[2]; |
ser1516 | 21:988413f53fbe | 432 | txMsg.data[3] = data.bytes[3]; |
ser1516 | 21:988413f53fbe | 433 | txMsg.data[4] = cells_left; |
ser1516 | 21:988413f53fbe | 434 | cells_left--; |
Crazyaboutmachines | 29:86e963cc5ce7 | 435 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
ser1516 | 21:988413f53fbe | 436 | pec_error(); |
ser1516 | 21:988413f53fbe | 437 | cells_left++; |
ser1516 | 21:988413f53fbe | 438 | //to_send=1; |
ser1516 | 21:988413f53fbe | 439 | } |
Crazyaboutmachines | 35:117a807c481a | 440 | */ |
Crazyaboutmachines | 33:54861cd567cd | 441 | //-----------------2º send cell temperatures |
Crazyaboutmachines | 35:117a807c481a | 442 | txMsg.clear(); |
Crazyaboutmachines | 35:117a807c481a | 443 | txMsg.id = 21; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
Crazyaboutmachines | 35:117a807c481a | 444 | txMsg.len = 5; |
Crazyaboutmachines | 33:54861cd567cd | 445 | |
Crazyaboutmachines | 35:117a807c481a | 446 | // data.f[0] = temp_codes[temps_left-1]; |
Crazyaboutmachines | 35:117a807c481a | 447 | data.f[0] = temp_codes[14]; |
Crazyaboutmachines | 34:8f92a17c5be9 | 448 | |
Crazyaboutmachines | 35:117a807c481a | 449 | txMsg.data[0] = data.bytes[0]; |
Crazyaboutmachines | 35:117a807c481a | 450 | txMsg.data[1] = data.bytes[1]; |
Crazyaboutmachines | 35:117a807c481a | 451 | txMsg.data[2] = data.bytes[2]; |
Crazyaboutmachines | 35:117a807c481a | 452 | txMsg.data[3] = data.bytes[3]; |
Crazyaboutmachines | 34:8f92a17c5be9 | 453 | |
Crazyaboutmachines | 35:117a807c481a | 454 | txMsg.data[4] = temps_left; |
Crazyaboutmachines | 34:8f92a17c5be9 | 455 | //temps_left--; |
Crazyaboutmachines | 35:117a807c481a | 456 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
Crazyaboutmachines | 33:54861cd567cd | 457 | pec_error(); |
Crazyaboutmachines | 34:8f92a17c5be9 | 458 | //temps_left++; |
Crazyaboutmachines | 33:54861cd567cd | 459 | //to_send=1; |
Crazyaboutmachines | 33:54861cd567cd | 460 | } |
Crazyaboutmachines | 35:117a807c481a | 461 | |
ser1516 | 21:988413f53fbe | 462 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 463 | if(msgAvailable) { |
Crazyaboutmachines | 26:c55656391a29 | 464 | int len = can.read(rxMsg); |
Crazyaboutmachines | 29:86e963cc5ce7 | 465 | if(rxMsg.id==9){ |
Crazyaboutmachines | 29:86e963cc5ce7 | 466 | motostate = rxMsg.data[0]; |
Crazyaboutmachines | 26:c55656391a29 | 467 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 29:86e963cc5ce7 | 468 | to_charge_or_not_to_charge=((motostate & 0b00000010)>>1); |
Crazyaboutmachines | 26:c55656391a29 | 469 | msgAvailable = false; |
Crazyaboutmachines | 26:c55656391a29 | 470 | } |
Crazyaboutmachines | 26:c55656391a29 | 471 | } |
ser1516 | 21:988413f53fbe | 472 | } |
ser1516 | 21:988413f53fbe | 473 | } |