![](/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@48:ddb7570eb4d7, 2016-12-06 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Tue Dec 06 11:45:41 2016 +0000
- Revision:
- 48:ddb7570eb4d7
- Parent:
- 47:5c05c55d61ac
- Child:
- 49:94a58ef0edc4
changed temperature to 65
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 | 40:4144b3eec060 | 12 | float 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 | 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 | |
Crazyaboutmachines | 40:4144b3eec060 | 135 | /* |
Crazyaboutmachines | 40:4144b3eec060 | 136 | void message_trigger() |
Crazyaboutmachines | 40:4144b3eec060 | 137 | { |
Crazyaboutmachines | 40:4144b3eec060 | 138 | if(cells_left < 1) { |
Crazyaboutmachines | 40:4144b3eec060 | 139 | sender.detach(); |
Crazyaboutmachines | 40:4144b3eec060 | 140 | } else { |
Crazyaboutmachines | 40:4144b3eec060 | 141 | to_send = 1; |
Crazyaboutmachines | 40:4144b3eec060 | 142 | } |
Crazyaboutmachines | 40:4144b3eec060 | 143 | } |
Crazyaboutmachines | 40:4144b3eec060 | 144 | */ |
Crazyaboutmachines | 40:4144b3eec060 | 145 | |
ser1516 | 21:988413f53fbe | 146 | void message_trigger() |
ser1516 | 21:988413f53fbe | 147 | { |
Crazyaboutmachines | 38:48afc177a319 | 148 | if((cells_left < 1)&&(temps_left < 1)) { |
ser1516 | 21:988413f53fbe | 149 | sender.detach(); |
ser1516 | 21:988413f53fbe | 150 | } else { |
ser1516 | 21:988413f53fbe | 151 | to_send = 1; |
ser1516 | 21:988413f53fbe | 152 | } |
ser1516 | 21:988413f53fbe | 153 | } |
ser1516 | 21:988413f53fbe | 154 | |
Crazyaboutmachines | 40:4144b3eec060 | 155 | |
ser1516 | 21:988413f53fbe | 156 | void check_charging_voltage() |
ser1516 | 21:988413f53fbe | 157 | { |
ser1516 | 21:988413f53fbe | 158 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 159 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 160 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 161 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 162 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 163 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 164 | pec_error(); |
ser1516 | 21:988413f53fbe | 165 | } |
Crazyaboutmachines | 31:1fab4bb4e9ee | 166 | cells_left = 12; |
ser1516 | 21:988413f53fbe | 167 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 168 | for (int i=0; i<12; i++) { |
Crazyaboutmachines | 48:ddb7570eb4d7 | 169 | if(cell_codes[current_ic][i]*0.0001 > 4.1) { //liga balanceamento |
ser1516 | 21:988413f53fbe | 170 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 171 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 172 | tx_cfg[0][4] = tx_cfg[0][4] | 0x01 ; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 173 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 174 | tx_cfg[0][4] = tx_cfg[0][4] | 0x02 ; // 00000010 |
Crazyaboutmachines | 30:9c9f2d789153 | 175 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 176 | tx_cfg[0][4] = tx_cfg[0][4] | 0x04 ; // 00000100 |
Crazyaboutmachines | 30:9c9f2d789153 | 177 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 178 | tx_cfg[0][4] = tx_cfg[0][4] | 0x08 ; // 00001000 |
Crazyaboutmachines | 30:9c9f2d789153 | 179 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 180 | tx_cfg[0][4] = tx_cfg[0][4] | 0x10 ; // 00010000 |
Crazyaboutmachines | 30:9c9f2d789153 | 181 | case 6: //cell 7 |
Crazyaboutmachines | 30:9c9f2d789153 | 182 | tx_cfg[0][4] = tx_cfg[0][4] | 0x40 ; // 01000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 183 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 184 | tx_cfg[0][4] = tx_cfg[0][4] | 0x80 ; // 10000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 185 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 186 | tx_cfg[0][5] = tx_cfg[0][5] | 0x01; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 187 | case 9: //cell 10 |
Crazyaboutmachines | 30:9c9f2d789153 | 188 | tx_cfg[0][5] = tx_cfg[0][5] | 0x02; // 00000010 |
Crazyaboutmachines | 26:c55656391a29 | 189 | } |
Crazyaboutmachines | 26:c55656391a29 | 190 | } else { |
Crazyaboutmachines | 26:c55656391a29 | 191 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 192 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 193 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFE ; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 194 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 195 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFD ; // 11111101 |
Crazyaboutmachines | 30:9c9f2d789153 | 196 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 197 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFB ; // 11111011 |
Crazyaboutmachines | 30:9c9f2d789153 | 198 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 199 | tx_cfg[0][4] = tx_cfg[0][4] & 0xF7 ; // 11110111 |
Crazyaboutmachines | 30:9c9f2d789153 | 200 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 201 | tx_cfg[0][4] = tx_cfg[0][4] & 0xEF ; // 11101111 |
Crazyaboutmachines | 30:9c9f2d789153 | 202 | case 6: //cell 7 |
Crazyaboutmachines | 25:6f4f7510db24 | 203 | tx_cfg[0][4] = tx_cfg[0][4] & 0xBF ; // 10111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 204 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 205 | tx_cfg[0][4] = tx_cfg[0][4] & 0x7F ; // 01111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 206 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 207 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFE; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 208 | case 9: //cell 10 |
Crazyaboutmachines | 26:c55656391a29 | 209 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFD; // 11111101 |
Crazyaboutmachines | 26:c55656391a29 | 210 | } |
ser1516 | 21:988413f53fbe | 211 | } |
ser1516 | 21:988413f53fbe | 212 | } |
ser1516 | 21:988413f53fbe | 213 | } |
ser1516 | 21:988413f53fbe | 214 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 215 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 216 | } |
ser1516 | 21:988413f53fbe | 217 | |
ser1516 | 21:988413f53fbe | 218 | void check_discharging_voltage() |
ser1516 | 21:988413f53fbe | 219 | { |
ser1516 | 21:988413f53fbe | 220 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 221 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 222 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 223 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 224 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 225 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 226 | pec_error(); |
ser1516 | 21:988413f53fbe | 227 | }/* |
ser1516 | 21:988413f53fbe | 228 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 229 | for (int i=0; i<11; i++) { |
ser1516 | 21:988413f53fbe | 230 | //printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 231 | if(cell_codes[current_ic][i]*0.0001 < 2.7) { |
ser1516 | 21:988413f53fbe | 232 | txMsg.clear(); |
ser1516 | 21:988413f53fbe | 233 | txMsg.id = 1; |
ser1516 | 21:988413f53fbe | 234 | printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 235 | |
ser1516 | 21:988413f53fbe | 236 | txMsg << cell_codes[current_ic][i]*0.0001; |
ser1516 | 21:988413f53fbe | 237 | can.write(txMsg); |
ser1516 | 21:988413f53fbe | 238 | wait(0.1); |
ser1516 | 21:988413f53fbe | 239 | } |
ser1516 | 21:988413f53fbe | 240 | } |
ser1516 | 21:988413f53fbe | 241 | }*/ |
ser1516 | 21:988413f53fbe | 242 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 243 | cells_left = 12; |
Crazyaboutmachines | 32:a52605800210 | 244 | tx_cfg[0][4] = tx_cfg[0][4] & 0b00000000; //para desactivar balanceamento durante a descarga |
Crazyaboutmachines | 32:a52605800210 | 245 | tx_cfg[0][5] = tx_cfg[0][5] & 0b11110000; |
Crazyaboutmachines | 32:a52605800210 | 246 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 247 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 248 | } |
ser1516 | 21:988413f53fbe | 249 | |
Crazyaboutmachines | 33:54861cd567cd | 250 | AnalogIn MUXA_Read(PC_1); |
Crazyaboutmachines | 33:54861cd567cd | 251 | DigitalOut MUXA_0(PB_10); |
Crazyaboutmachines | 33:54861cd567cd | 252 | DigitalOut MUXA_1(PB_2); |
Crazyaboutmachines | 33:54861cd567cd | 253 | DigitalOut MUXA_2(PB_0); |
Crazyaboutmachines | 33:54861cd567cd | 254 | DigitalOut MUXA_3(PB_1); |
Crazyaboutmachines | 33:54861cd567cd | 255 | AnalogIn MUXB_Read(PC_2); |
Crazyaboutmachines | 33:54861cd567cd | 256 | DigitalOut MUXB_0(PC_7); |
Crazyaboutmachines | 33:54861cd567cd | 257 | DigitalOut MUXB_1(PC_6); |
Crazyaboutmachines | 33:54861cd567cd | 258 | DigitalOut MUXB_2(PB_14); |
Crazyaboutmachines | 33:54861cd567cd | 259 | DigitalOut MUXB_3(PB_15); |
Crazyaboutmachines | 33:54861cd567cd | 260 | |
Crazyaboutmachines | 41:0b05e0109f2e | 261 | /* |
Crazyaboutmachines | 34:8f92a17c5be9 | 262 | void check_temperatures() |
Crazyaboutmachines | 34:8f92a17c5be9 | 263 | { |
Crazyaboutmachines | 34:8f92a17c5be9 | 264 | int i=14; |
Crazyaboutmachines | 40:4144b3eec060 | 265 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=1; //(escolha do NTC on board) |
Crazyaboutmachines | 36:e70020bee11f | 266 | temp_codes[i]= MUXA_Read.read()*3300; |
Crazyaboutmachines | 38:48afc177a319 | 267 | temps_left = 32; |
Crazyaboutmachines | 34:8f92a17c5be9 | 268 | } |
Crazyaboutmachines | 41:0b05e0109f2e | 269 | */ |
Crazyaboutmachines | 34:8f92a17c5be9 | 270 | |
Crazyaboutmachines | 45:9764758d3433 | 271 | void alarm(char alarm_code){ |
Crazyaboutmachines | 44:51fadd7414b1 | 272 | txMsg.clear(); |
Crazyaboutmachines | 44:51fadd7414b1 | 273 | txMsg.id = 8; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
Crazyaboutmachines | 44:51fadd7414b1 | 274 | txMsg.len = 1; |
Crazyaboutmachines | 45:9764758d3433 | 275 | txMsg.data[0] = alarm_code; //alarm_code |
Crazyaboutmachines | 44:51fadd7414b1 | 276 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
Crazyaboutmachines | 44:51fadd7414b1 | 277 | pec_error(); |
Crazyaboutmachines | 44:51fadd7414b1 | 278 | } |
Crazyaboutmachines | 44:51fadd7414b1 | 279 | } |
Crazyaboutmachines | 44:51fadd7414b1 | 280 | |
Crazyaboutmachines | 44:51fadd7414b1 | 281 | |
Crazyaboutmachines | 43:cf574cc8b74f | 282 | float temp; |
Crazyaboutmachines | 33:54861cd567cd | 283 | void check_temperatures() |
Crazyaboutmachines | 33:54861cd567cd | 284 | { |
Crazyaboutmachines | 33:54861cd567cd | 285 | for (int i=0; i<32; i++){ |
Crazyaboutmachines | 33:54861cd567cd | 286 | switch (i) { |
Crazyaboutmachines | 33:54861cd567cd | 287 | case 0: //ntc 1 //from MUXA |
Crazyaboutmachines | 41:0b05e0109f2e | 288 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 289 | case 1: //ntc 2 |
Crazyaboutmachines | 41:0b05e0109f2e | 290 | MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 291 | case 2: //ntc 3 |
Crazyaboutmachines | 41:0b05e0109f2e | 292 | MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 293 | case 3: //ntc 4 |
Crazyaboutmachines | 41:0b05e0109f2e | 294 | MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 295 | case 4: //ntc 5 |
Crazyaboutmachines | 41:0b05e0109f2e | 296 | MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 297 | case 5: //ntc 6 |
Crazyaboutmachines | 41:0b05e0109f2e | 298 | MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 299 | case 6: //ntc 7 |
Crazyaboutmachines | 41:0b05e0109f2e | 300 | MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 301 | case 7: //ntc 8 |
Crazyaboutmachines | 41:0b05e0109f2e | 302 | MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 303 | case 8: //ntc 9 |
Crazyaboutmachines | 41:0b05e0109f2e | 304 | MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 305 | case 9: //ntc 10 |
Crazyaboutmachines | 41:0b05e0109f2e | 306 | MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 307 | case 10: //ntc 11 |
Crazyaboutmachines | 41:0b05e0109f2e | 308 | MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 309 | case 11: //ntc 12 |
Crazyaboutmachines | 41:0b05e0109f2e | 310 | MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 311 | case 12: //ntc 13 |
Crazyaboutmachines | 41:0b05e0109f2e | 312 | MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 313 | case 13: //ntc 14 |
Crazyaboutmachines | 41:0b05e0109f2e | 314 | MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 315 | case 14: //ntc 15 |
Crazyaboutmachines | 41:0b05e0109f2e | 316 | MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 317 | case 15: //ntc 16 |
Crazyaboutmachines | 41:0b05e0109f2e | 318 | MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 319 | case 16: //ntc 17 //from MUXB |
Crazyaboutmachines | 41:0b05e0109f2e | 320 | MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 321 | case 17: //ntc 18 |
Crazyaboutmachines | 41:0b05e0109f2e | 322 | MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 323 | case 18: //ntc 19 |
Crazyaboutmachines | 41:0b05e0109f2e | 324 | MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 325 | case 19: //ntc 20 |
Crazyaboutmachines | 41:0b05e0109f2e | 326 | MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 327 | case 20: //ntc 21 |
Crazyaboutmachines | 41:0b05e0109f2e | 328 | MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 329 | case 21: //ntc 22 |
Crazyaboutmachines | 41:0b05e0109f2e | 330 | MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 331 | case 22: //ntc 23 |
Crazyaboutmachines | 41:0b05e0109f2e | 332 | MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 333 | case 23: //ntc 24 |
Crazyaboutmachines | 41:0b05e0109f2e | 334 | MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 335 | case 24: //ntc 25 |
Crazyaboutmachines | 41:0b05e0109f2e | 336 | MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 337 | case 25: //ntc 26 |
Crazyaboutmachines | 41:0b05e0109f2e | 338 | MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 339 | case 26: //ntc 27 |
Crazyaboutmachines | 41:0b05e0109f2e | 340 | MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 341 | case 27: //ntc 28 |
Crazyaboutmachines | 41:0b05e0109f2e | 342 | MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 343 | case 28: //ntc 29 |
Crazyaboutmachines | 41:0b05e0109f2e | 344 | MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 345 | case 29: //ntc 30 |
Crazyaboutmachines | 41:0b05e0109f2e | 346 | MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 347 | case 30: //ntc 31 |
Crazyaboutmachines | 41:0b05e0109f2e | 348 | MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=0; break; |
Crazyaboutmachines | 33:54861cd567cd | 349 | case 31: //ntc 32 |
Crazyaboutmachines | 41:0b05e0109f2e | 350 | MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=1; break; |
Crazyaboutmachines | 33:54861cd567cd | 351 | } |
Crazyaboutmachines | 42:ca364a2bf162 | 352 | wait_ms(100); //tempo para a tensao á saida do mux estabilisar |
Crazyaboutmachines | 40:4144b3eec060 | 353 | if(i<16){ // Converts and read the analog input value (value from 0.0 to 1.0) |
Crazyaboutmachines | 33:54861cd567cd | 354 | temp_codes[i] = MUXA_Read.read()*3300; |
Crazyaboutmachines | 33:54861cd567cd | 355 | }else{ |
Crazyaboutmachines | 41:0b05e0109f2e | 356 | temp_codes[i] = MUXB_Read.read()*3300; |
Crazyaboutmachines | 33:54861cd567cd | 357 | } |
Crazyaboutmachines | 43:cf574cc8b74f | 358 | |
Crazyaboutmachines | 43:cf574cc8b74f | 359 | temp=temp_codes[i]/1000; |
Crazyaboutmachines | 43:cf574cc8b74f | 360 | temp=(temp*10000)/(3.3-temp); |
Crazyaboutmachines | 43:cf574cc8b74f | 361 | temp = 3380/log(temp/0.119228); |
Crazyaboutmachines | 43:cf574cc8b74f | 362 | temp_codes[i] = temp-273.15; |
Crazyaboutmachines | 45:9764758d3433 | 363 | // //-------------------------- |
Crazyaboutmachines | 45:9764758d3433 | 364 | // if(i==0||i==1||i==2||i==3||i==4||i==5||i==6||i==7||i==8||i==9||i==10||i==11||i==14||i==30){ |
Crazyaboutmachines | 45:9764758d3433 | 365 | // if(temp_codes[i]>23){ |
Crazyaboutmachines | 45:9764758d3433 | 366 | // // if(temp_codes[i]>30||temp_codes[i]<-20){ |
Crazyaboutmachines | 45:9764758d3433 | 367 | // //disable interrupts |
Crazyaboutmachines | 45:9764758d3433 | 368 | // alarm();//temperature alarm |
Crazyaboutmachines | 45:9764758d3433 | 369 | // //enable interrupts |
Crazyaboutmachines | 45:9764758d3433 | 370 | // } |
Crazyaboutmachines | 45:9764758d3433 | 371 | // } |
Crazyaboutmachines | 45:9764758d3433 | 372 | // //-------------------------- |
Crazyaboutmachines | 33:54861cd567cd | 373 | } |
Crazyaboutmachines | 33:54861cd567cd | 374 | temps_left = 32; |
Crazyaboutmachines | 33:54861cd567cd | 375 | } |
Crazyaboutmachines | 41:0b05e0109f2e | 376 | |
ser1516 | 21:988413f53fbe | 377 | void onMsgReceived() |
ser1516 | 21:988413f53fbe | 378 | { |
hudakz | 16:a86f339d1c25 | 379 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 380 | } |
hudakz | 16:a86f339d1c25 | 381 | |
ser1516 | 21:988413f53fbe | 382 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 383 | bool to_charge_or_not_to_charge=false; // false = discharge |
ser1516 | 21:988413f53fbe | 384 | bool charging = false; |
Crazyaboutmachines | 29:86e963cc5ce7 | 385 | bool discharging = false; |
Crazyaboutmachines | 25:6f4f7510db24 | 386 | |
ser1516 | 21:988413f53fbe | 387 | void monitor() |
ser1516 | 21:988413f53fbe | 388 | { |
ser1516 | 21:988413f53fbe | 389 | led = !led; |
ser1516 | 21:988413f53fbe | 390 | if(to_charge_or_not_to_charge) { |
ser1516 | 21:988413f53fbe | 391 | charging = 1; |
ser1516 | 21:988413f53fbe | 392 | discharging = 0; |
ser1516 | 21:988413f53fbe | 393 | } else { |
ser1516 | 21:988413f53fbe | 394 | discharging = 1; |
ser1516 | 21:988413f53fbe | 395 | charging = 0; |
hudakz | 0:c5e5d0df6f2a | 396 | } |
hudakz | 0:c5e5d0df6f2a | 397 | } |
hudakz | 7:2dce8ed51091 | 398 | |
hudakz | 12:e91e44924194 | 399 | |
Crazyaboutmachines | 26:c55656391a29 | 400 | uint8_t motostate=0; |
Crazyaboutmachines | 26:c55656391a29 | 401 | |
ser1516 | 21:988413f53fbe | 402 | int main() |
ser1516 | 21:988413f53fbe | 403 | { |
Crazyaboutmachines | 26:c55656391a29 | 404 | data data; |
ser1516 | 21:988413f53fbe | 405 | //printf("starting\n\r"); |
ser1516 | 21:988413f53fbe | 406 | led =1; |
ser1516 | 21:988413f53fbe | 407 | wait(1); |
ser1516 | 21:988413f53fbe | 408 | pec_error(); |
Crazyaboutmachines | 31:1fab4bb4e9ee | 409 | to_charge_or_not_to_charge=0; |
Crazyaboutmachines | 31:1fab4bb4e9ee | 410 | charging = 0; |
Crazyaboutmachines | 29:86e963cc5ce7 | 411 | discharging = 1; |
Crazyaboutmachines | 40:4144b3eec060 | 412 | ticker.attach(&monitor, 10); |
ser1516 | 21:988413f53fbe | 413 | LTC6804_initialize(); |
ser1516 | 21:988413f53fbe | 414 | init_cfg(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 415 | //write configuration |
ser1516 | 21:988413f53fbe | 416 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 417 | __disable_irq(); // Disable Interrupts |
ser1516 | 21:988413f53fbe | 418 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 419 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 420 | wait(1); |
ser1516 | 21:988413f53fbe | 421 | //read configuration: may differ from written config |
ser1516 | 21:988413f53fbe | 422 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 423 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 424 | err = LTC6804_rdcfg(TOTAL_IC,rx_cfg); |
ser1516 | 21:988413f53fbe | 425 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 426 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 427 | pec_error(); |
ser1516 | 21:988413f53fbe | 428 | } |
ser1516 | 21:988413f53fbe | 429 | wait(0.5); |
ser1516 | 21:988413f53fbe | 430 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 431 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 432 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 433 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 434 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 435 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 436 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 437 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 438 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 439 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 440 | pec_error(); |
ser1516 | 21:988413f53fbe | 441 | } |
ser1516 | 21:988413f53fbe | 442 | can.frequency(1000000); // set bit rate to 1Mbps |
ser1516 | 21:988413f53fbe | 443 | can.attach(&onMsgReceived); |
ser1516 | 21:988413f53fbe | 444 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 445 | while(1) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 446 | if(charging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 447 | charging = 0; |
ser1516 | 21:988413f53fbe | 448 | check_charging_voltage(); |
Crazyaboutmachines | 33:54861cd567cd | 449 | check_temperatures(); |
Crazyaboutmachines | 40:4144b3eec060 | 450 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 451 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 452 | if(discharging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 453 | discharging = 0; |
ser1516 | 21:988413f53fbe | 454 | check_discharging_voltage(); |
Crazyaboutmachines | 33:54861cd567cd | 455 | check_temperatures(); |
Crazyaboutmachines | 40:4144b3eec060 | 456 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 457 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 458 | if(to_send) { |
ser1516 | 21:988413f53fbe | 459 | to_send=0; |
Crazyaboutmachines | 33:54861cd567cd | 460 | //-----------------1º send cell voltages |
Crazyaboutmachines | 40:4144b3eec060 | 461 | |
Crazyaboutmachines | 46:9bc58ec85bd5 | 462 | //------------------------------ |
Crazyaboutmachines | 46:9bc58ec85bd5 | 463 | if(cells_left>0){ |
Crazyaboutmachines | 46:9bc58ec85bd5 | 464 | if(cells_left==1||cells_left==2||cells_left==3||cells_left==4||cells_left==5||cells_left==7||cells_left==8||cells_left==9||cells_left==10){ |
Crazyaboutmachines | 47:5c05c55d61ac | 465 | if(cell_codes[0][cells_left-1]<30000||cell_codes[0][cells_left-1]>42000){ |
Crazyaboutmachines | 47:5c05c55d61ac | 466 | // if(cell_codes[0][cells_left-1]<0||cell_codes[0][cells_left-1]>70000){ //for debug |
Crazyaboutmachines | 46:9bc58ec85bd5 | 467 | __disable_irq(); // Disable Interrupts |
Crazyaboutmachines | 46:9bc58ec85bd5 | 468 | alarm('v'); |
Crazyaboutmachines | 46:9bc58ec85bd5 | 469 | __enable_irq(); |
Crazyaboutmachines | 46:9bc58ec85bd5 | 470 | } |
Crazyaboutmachines | 46:9bc58ec85bd5 | 471 | } |
Crazyaboutmachines | 46:9bc58ec85bd5 | 472 | //------------------------------ |
Crazyaboutmachines | 40:4144b3eec060 | 473 | |
Crazyaboutmachines | 37:1776d45825ef | 474 | txMsg.clear(); |
Crazyaboutmachines | 48:ddb7570eb4d7 | 475 | txMsg.id = 13; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
ser1516 | 21:988413f53fbe | 476 | txMsg.len = 5; |
ser1516 | 21:988413f53fbe | 477 | data.f[0] = cell_codes[0][cells_left-1]*0.0001; |
ser1516 | 21:988413f53fbe | 478 | txMsg.data[0] = data.bytes[0]; |
ser1516 | 21:988413f53fbe | 479 | txMsg.data[1] = data.bytes[1]; |
ser1516 | 21:988413f53fbe | 480 | txMsg.data[2] = data.bytes[2]; |
ser1516 | 21:988413f53fbe | 481 | txMsg.data[3] = data.bytes[3]; |
ser1516 | 21:988413f53fbe | 482 | txMsg.data[4] = cells_left; |
ser1516 | 21:988413f53fbe | 483 | cells_left--; |
Crazyaboutmachines | 29:86e963cc5ce7 | 484 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
ser1516 | 21:988413f53fbe | 485 | pec_error(); |
ser1516 | 21:988413f53fbe | 486 | cells_left++; |
ser1516 | 21:988413f53fbe | 487 | //to_send=1; |
ser1516 | 21:988413f53fbe | 488 | } |
Crazyaboutmachines | 40:4144b3eec060 | 489 | } |
Crazyaboutmachines | 37:1776d45825ef | 490 | |
Crazyaboutmachines | 37:1776d45825ef | 491 | //-----------------2º send cell temperatures |
Crazyaboutmachines | 37:1776d45825ef | 492 | // wait_ms(200); falha se houver aqui um delay deste tamanho |
Crazyaboutmachines | 40:4144b3eec060 | 493 | |
Crazyaboutmachines | 40:4144b3eec060 | 494 | if(temps_left>0){ |
Crazyaboutmachines | 45:9764758d3433 | 495 | //------------------------------ |
Crazyaboutmachines | 45:9764758d3433 | 496 | if(temps_left==1||temps_left==2||temps_left==3||temps_left==4||temps_left==5|| |
Crazyaboutmachines | 45:9764758d3433 | 497 | temps_left==6||temps_left==7||temps_left==8||temps_left==9||temps_left==10|| |
Crazyaboutmachines | 45:9764758d3433 | 498 | temps_left==11||temps_left==12||temps_left==15||temps_left==31){ |
Crazyaboutmachines | 48:ddb7570eb4d7 | 499 | if(temp_codes[temps_left-1]>65||temp_codes[temps_left-1]<4){ //4-49(ambiente) |
Crazyaboutmachines | 47:5c05c55d61ac | 500 | //if(temp_codes[temps_left-1]>25||temp_codes[temps_left-1]<-300){ //for debug |
Crazyaboutmachines | 45:9764758d3433 | 501 | // if(temp_codes[i]>30||temp_codes[i]<-20){ |
Crazyaboutmachines | 45:9764758d3433 | 502 | __disable_irq(); // Disable Interrupts |
Crazyaboutmachines | 45:9764758d3433 | 503 | alarm('t'); //temperature alarm |
Crazyaboutmachines | 45:9764758d3433 | 504 | __enable_irq(); |
Crazyaboutmachines | 45:9764758d3433 | 505 | } |
Crazyaboutmachines | 45:9764758d3433 | 506 | } |
Crazyaboutmachines | 45:9764758d3433 | 507 | //------------------------------ |
Crazyaboutmachines | 35:117a807c481a | 508 | txMsg.clear(); |
Crazyaboutmachines | 48:ddb7570eb4d7 | 509 | txMsg.id = 23; //BMS1=>ID:21; BMS2=>ID:22; BMS3=>ID:23. |
Crazyaboutmachines | 35:117a807c481a | 510 | txMsg.len = 5; |
Crazyaboutmachines | 33:54861cd567cd | 511 | |
Crazyaboutmachines | 39:1e9fdc51cd65 | 512 | data.f[0] = temp_codes[temps_left-1]; |
Crazyaboutmachines | 40:4144b3eec060 | 513 | // data.f[0] = temp_codes[14]; |
Crazyaboutmachines | 45:9764758d3433 | 514 | |
Crazyaboutmachines | 45:9764758d3433 | 515 | |
Crazyaboutmachines | 35:117a807c481a | 516 | txMsg.data[0] = data.bytes[0]; |
Crazyaboutmachines | 35:117a807c481a | 517 | txMsg.data[1] = data.bytes[1]; |
Crazyaboutmachines | 35:117a807c481a | 518 | txMsg.data[2] = data.bytes[2]; |
Crazyaboutmachines | 35:117a807c481a | 519 | txMsg.data[3] = data.bytes[3]; |
Crazyaboutmachines | 34:8f92a17c5be9 | 520 | |
Crazyaboutmachines | 35:117a807c481a | 521 | txMsg.data[4] = temps_left; |
Crazyaboutmachines | 38:48afc177a319 | 522 | temps_left--; |
Crazyaboutmachines | 35:117a807c481a | 523 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
Crazyaboutmachines | 33:54861cd567cd | 524 | pec_error(); |
Crazyaboutmachines | 38:48afc177a319 | 525 | temps_left++; |
Crazyaboutmachines | 33:54861cd567cd | 526 | //to_send=1; |
Crazyaboutmachines | 40:4144b3eec060 | 527 | } |
Crazyaboutmachines | 40:4144b3eec060 | 528 | } |
Crazyaboutmachines | 35:117a807c481a | 529 | |
ser1516 | 21:988413f53fbe | 530 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 531 | if(msgAvailable) { |
Crazyaboutmachines | 26:c55656391a29 | 532 | int len = can.read(rxMsg); |
Crazyaboutmachines | 29:86e963cc5ce7 | 533 | if(rxMsg.id==9){ |
Crazyaboutmachines | 29:86e963cc5ce7 | 534 | motostate = rxMsg.data[0]; |
Crazyaboutmachines | 26:c55656391a29 | 535 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 29:86e963cc5ce7 | 536 | to_charge_or_not_to_charge=((motostate & 0b00000010)>>1); |
Crazyaboutmachines | 26:c55656391a29 | 537 | msgAvailable = false; |
Crazyaboutmachines | 26:c55656391a29 | 538 | } |
Crazyaboutmachines | 26:c55656391a29 | 539 | } |
ser1516 | 21:988413f53fbe | 540 | } |
ser1516 | 21:988413f53fbe | 541 | } |