![](/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@30:9c9f2d789153, 2016-10-28 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Fri Oct 28 14:43:46 2016 +0000
- Revision:
- 30:9c9f2d789153
- Parent:
- 29:86e963cc5ce7
- Child:
- 31:1fab4bb4e9ee
all cells balancing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Crazyaboutmachines | 27:db96e99e37c9 | 1 | /*notes:--------------------- |
Crazyaboutmachines | 27:db96e99e37c9 | 2 | *-organizar codigo |
Crazyaboutmachines | 29:86e963cc5ce7 | 3 | *-diminuir tempos das interrupções etc |
Crazyaboutmachines | 29:86e963cc5ce7 | 4 | *-diminuir tempos mortos durante balanceamento |
Crazyaboutmachines | 27:db96e99e37c9 | 5 | * |
Crazyaboutmachines | 27:db96e99e37c9 | 6 | * |
Crazyaboutmachines | 27:db96e99e37c9 | 7 | * |
Crazyaboutmachines | 27:db96e99e37c9 | 8 | * |
Crazyaboutmachines | 27:db96e99e37c9 | 9 | -----------------------------*/ |
Crazyaboutmachines | 27:db96e99e37c9 | 10 | |
Crazyaboutmachines | 27:db96e99e37c9 | 11 | |
Crazyaboutmachines | 27:db96e99e37c9 | 12 | |
Crazyaboutmachines | 27:db96e99e37c9 | 13 | |
hudakz | 19:872e304d7e17 | 14 | #include "CANnucleo.h" |
hudakz | 16:a86f339d1c25 | 15 | #include "mbed.h" |
ser1516 | 21:988413f53fbe | 16 | #include "LTC68041.h" |
hudakz | 16:a86f339d1c25 | 17 | |
ser1516 | 21:988413f53fbe | 18 | |
ser1516 | 21:988413f53fbe | 19 | |
ser1516 | 21:988413f53fbe | 20 | uint8_t const TOTAL_IC = 1;//!<number of ICs in the daisy chain |
ser1516 | 21:988413f53fbe | 21 | |
ser1516 | 21:988413f53fbe | 22 | uint8_t rx_cfg[TOTAL_IC][8]; |
ser1516 | 21:988413f53fbe | 23 | uint8_t tx_cfg[TOTAL_IC][6]; |
ser1516 | 21:988413f53fbe | 24 | uint16_t aux_codes[TOTAL_IC][6]; |
ser1516 | 21:988413f53fbe | 25 | uint16_t cell_codes[TOTAL_IC][12]; |
ser1516 | 21:988413f53fbe | 26 | volatile bool msgAvailable = false; |
ser1516 | 21:988413f53fbe | 27 | volatile bool to_send = false; |
hudakz | 17:18d4d0ff26a6 | 28 | CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 17:18d4d0ff26a6 | 29 | CANnucleo::CANMessage rxMsg; |
hudakz | 17:18d4d0ff26a6 | 30 | CANnucleo::CANMessage txMsg; |
ser1516 | 21:988413f53fbe | 31 | DigitalOut led(PA_5); |
ser1516 | 21:988413f53fbe | 32 | /*!*********************************** |
ser1516 | 21:988413f53fbe | 33 | \brief Initializes the configuration array |
ser1516 | 21:988413f53fbe | 34 | **************************************/ |
ser1516 | 21:988413f53fbe | 35 | void init_cfg() |
ser1516 | 21:988413f53fbe | 36 | { |
ser1516 | 21:988413f53fbe | 37 | for (int i = 0; i<TOTAL_IC; i++) { |
ser1516 | 21:988413f53fbe | 38 | tx_cfg[i][0] = 0xFE; |
ser1516 | 21:988413f53fbe | 39 | tx_cfg[i][1] = 0x00 ; |
ser1516 | 21:988413f53fbe | 40 | tx_cfg[i][2] = 0x00 ; |
ser1516 | 21:988413f53fbe | 41 | tx_cfg[i][3] = 0x00 ; |
ser1516 | 21:988413f53fbe | 42 | tx_cfg[i][4] = 0x00 ; |
ser1516 | 21:988413f53fbe | 43 | tx_cfg[i][5] = 0x00 ; |
ser1516 | 21:988413f53fbe | 44 | } |
ser1516 | 21:988413f53fbe | 45 | } |
ser1516 | 21:988413f53fbe | 46 | void serial_print_hex(uint8_t data) |
ser1516 | 21:988413f53fbe | 47 | { |
ser1516 | 21:988413f53fbe | 48 | if (data< 16) { |
ser1516 | 21:988413f53fbe | 49 | printf("0"); |
ser1516 | 21:988413f53fbe | 50 | printf("%x",(uint8_t)data); |
ser1516 | 21:988413f53fbe | 51 | } else |
ser1516 | 21:988413f53fbe | 52 | printf("%x",(uint8_t)data); |
ser1516 | 21:988413f53fbe | 53 | } |
ser1516 | 21:988413f53fbe | 54 | void print_config() |
ser1516 | 21:988413f53fbe | 55 | { |
ser1516 | 21:988413f53fbe | 56 | int cfg_pec; |
ser1516 | 21:988413f53fbe | 57 | printf("Written Configuration:\n\r "); |
ser1516 | 21:988413f53fbe | 58 | for (int current_ic = 0; current_ic<TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 59 | printf(" IC "); |
ser1516 | 21:988413f53fbe | 60 | printf("%d", current_ic+1); |
ser1516 | 21:988413f53fbe | 61 | printf(": "); |
ser1516 | 21:988413f53fbe | 62 | printf("0x"); |
ser1516 | 21:988413f53fbe | 63 | serial_print_hex(tx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 64 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 65 | serial_print_hex(tx_cfg[current_ic][1]); |
ser1516 | 21:988413f53fbe | 66 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 67 | serial_print_hex(tx_cfg[current_ic][2]); |
ser1516 | 21:988413f53fbe | 68 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 69 | serial_print_hex(tx_cfg[current_ic][3]); |
ser1516 | 21:988413f53fbe | 70 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 71 | serial_print_hex(tx_cfg[current_ic][4]); |
ser1516 | 21:988413f53fbe | 72 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 73 | serial_print_hex(tx_cfg[current_ic][5]); |
ser1516 | 21:988413f53fbe | 74 | printf(", Calculated PEC: 0x"); |
ser1516 | 21:988413f53fbe | 75 | cfg_pec = pec15_calc(6,&tx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 76 | serial_print_hex((uint8_t)(cfg_pec>>8)); |
ser1516 | 21:988413f53fbe | 77 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 78 | serial_print_hex((uint8_t)(cfg_pec)); |
ser1516 | 21:988413f53fbe | 79 | } |
ser1516 | 21:988413f53fbe | 80 | printf("\n\r"); |
ser1516 | 21:988413f53fbe | 81 | } |
ser1516 | 21:988413f53fbe | 82 | |
ser1516 | 21:988413f53fbe | 83 | void print_rxconfig() |
ser1516 | 21:988413f53fbe | 84 | { |
ser1516 | 21:988413f53fbe | 85 | printf("Received Configuration "); |
ser1516 | 21:988413f53fbe | 86 | for (int current_ic=0; current_ic<TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 87 | printf(" IC "); |
ser1516 | 21:988413f53fbe | 88 | printf("%d " ,current_ic+1); |
ser1516 | 21:988413f53fbe | 89 | printf(": 0x"); |
ser1516 | 21:988413f53fbe | 90 | serial_print_hex(rx_cfg[current_ic][0]); |
ser1516 | 21:988413f53fbe | 91 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 92 | serial_print_hex(rx_cfg[current_ic][1]); |
ser1516 | 21:988413f53fbe | 93 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 94 | serial_print_hex(rx_cfg[current_ic][2]); |
ser1516 | 21:988413f53fbe | 95 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 96 | serial_print_hex(rx_cfg[current_ic][3]); |
ser1516 | 21:988413f53fbe | 97 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 98 | serial_print_hex(rx_cfg[current_ic][4]); |
ser1516 | 21:988413f53fbe | 99 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 100 | serial_print_hex(rx_cfg[current_ic][5]); |
ser1516 | 21:988413f53fbe | 101 | printf(", Received PEC: 0x"); |
ser1516 | 21:988413f53fbe | 102 | serial_print_hex(rx_cfg[current_ic][6]); |
ser1516 | 21:988413f53fbe | 103 | printf(", 0x"); |
ser1516 | 21:988413f53fbe | 104 | serial_print_hex(rx_cfg[current_ic][7]); |
ser1516 | 21:988413f53fbe | 105 | } |
ser1516 | 21:988413f53fbe | 106 | } |
Crazyaboutmachines | 25:6f4f7510db24 | 107 | |
ser1516 | 21:988413f53fbe | 108 | int err; |
ser1516 | 21:988413f53fbe | 109 | void print_cells2() |
ser1516 | 21:988413f53fbe | 110 | { |
ser1516 | 21:988413f53fbe | 111 | |
ser1516 | 21:988413f53fbe | 112 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 113 | |
ser1516 | 21:988413f53fbe | 114 | for (int i=0; i<12; i++) { |
ser1516 | 21:988413f53fbe | 115 | printf(" C"); |
ser1516 | 21:988413f53fbe | 116 | printf("%d",i+1); |
ser1516 | 21:988413f53fbe | 117 | printf(":"); |
ser1516 | 21:988413f53fbe | 118 | printf("%f", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 119 | printf(","); |
ser1516 | 21:988413f53fbe | 120 | } |
hudakz | 0:c5e5d0df6f2a | 121 | |
ser1516 | 21:988413f53fbe | 122 | } |
ser1516 | 21:988413f53fbe | 123 | } |
Crazyaboutmachines | 25:6f4f7510db24 | 124 | |
ser1516 | 21:988413f53fbe | 125 | void pec_error() |
ser1516 | 21:988413f53fbe | 126 | { |
ser1516 | 21:988413f53fbe | 127 | for(int i = 0; i<5; i++) { |
ser1516 | 21:988413f53fbe | 128 | led = 1; |
ser1516 | 21:988413f53fbe | 129 | wait(0.2); |
ser1516 | 21:988413f53fbe | 130 | led=0; |
ser1516 | 21:988413f53fbe | 131 | wait(0.2); |
ser1516 | 21:988413f53fbe | 132 | } |
ser1516 | 21:988413f53fbe | 133 | } |
Crazyaboutmachines | 25:6f4f7510db24 | 134 | |
ser1516 | 21:988413f53fbe | 135 | char cells_left=0; |
ser1516 | 21:988413f53fbe | 136 | Ticker ticker; |
ser1516 | 21:988413f53fbe | 137 | Ticker sender; |
ser1516 | 21:988413f53fbe | 138 | |
ser1516 | 21:988413f53fbe | 139 | typedef union can_union { |
ser1516 | 21:988413f53fbe | 140 | int i[2]; |
ser1516 | 21:988413f53fbe | 141 | char bytes[8]; |
ser1516 | 21:988413f53fbe | 142 | float f[2]; |
ser1516 | 21:988413f53fbe | 143 | } data; |
ser1516 | 21:988413f53fbe | 144 | |
ser1516 | 21:988413f53fbe | 145 | void message_trigger() |
ser1516 | 21:988413f53fbe | 146 | { |
ser1516 | 21:988413f53fbe | 147 | if(cells_left < 1) { |
ser1516 | 21:988413f53fbe | 148 | sender.detach(); |
ser1516 | 21:988413f53fbe | 149 | } else { |
ser1516 | 21:988413f53fbe | 150 | to_send = 1; |
ser1516 | 21:988413f53fbe | 151 | } |
ser1516 | 21:988413f53fbe | 152 | } |
ser1516 | 21:988413f53fbe | 153 | |
ser1516 | 21:988413f53fbe | 154 | void check_charging_voltage() |
ser1516 | 21:988413f53fbe | 155 | { |
ser1516 | 21:988413f53fbe | 156 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 157 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 158 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 159 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 160 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 161 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 162 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 163 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 164 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 165 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 166 | pec_error(); |
ser1516 | 21:988413f53fbe | 167 | } |
ser1516 | 21:988413f53fbe | 168 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 169 | for (int i=0; i<12; i++) { |
Crazyaboutmachines | 25:6f4f7510db24 | 170 | if(cell_codes[current_ic][i]*0.0001 > 3.6) { |
ser1516 | 21:988413f53fbe | 171 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 172 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 173 | tx_cfg[0][4] = tx_cfg[0][4] | 0x01 ; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 174 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 175 | tx_cfg[0][4] = tx_cfg[0][4] | 0x02 ; // 00000010 |
Crazyaboutmachines | 30:9c9f2d789153 | 176 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 177 | tx_cfg[0][4] = tx_cfg[0][4] | 0x04 ; // 00000100 |
Crazyaboutmachines | 30:9c9f2d789153 | 178 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 179 | tx_cfg[0][4] = tx_cfg[0][4] | 0x08 ; // 00001000 |
Crazyaboutmachines | 30:9c9f2d789153 | 180 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 181 | tx_cfg[0][4] = tx_cfg[0][4] | 0x10 ; // 00010000 |
Crazyaboutmachines | 30:9c9f2d789153 | 182 | case 6: //cell 7 |
Crazyaboutmachines | 30:9c9f2d789153 | 183 | tx_cfg[0][4] = tx_cfg[0][4] | 0x40 ; // 01000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 184 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 185 | tx_cfg[0][4] = tx_cfg[0][4] | 0x80 ; // 10000000 |
Crazyaboutmachines | 30:9c9f2d789153 | 186 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 187 | tx_cfg[0][5] = tx_cfg[0][5] | 0x01; // 00000001 |
Crazyaboutmachines | 30:9c9f2d789153 | 188 | case 9: //cell 10 |
Crazyaboutmachines | 30:9c9f2d789153 | 189 | tx_cfg[0][5] = tx_cfg[0][5] | 0x02; // 00000010 |
Crazyaboutmachines | 26:c55656391a29 | 190 | } |
Crazyaboutmachines | 26:c55656391a29 | 191 | } else { |
Crazyaboutmachines | 26:c55656391a29 | 192 | switch (i) { |
Crazyaboutmachines | 30:9c9f2d789153 | 193 | case 0: //cell 1 |
Crazyaboutmachines | 25:6f4f7510db24 | 194 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFE ; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 195 | case 1: //cell 2 |
Crazyaboutmachines | 25:6f4f7510db24 | 196 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFD ; // 11111101 |
Crazyaboutmachines | 30:9c9f2d789153 | 197 | case 2: //cell 3 |
Crazyaboutmachines | 25:6f4f7510db24 | 198 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFB ; // 11111011 |
Crazyaboutmachines | 30:9c9f2d789153 | 199 | case 3: //cell 4 |
Crazyaboutmachines | 25:6f4f7510db24 | 200 | tx_cfg[0][4] = tx_cfg[0][4] & 0xF7 ; // 11110111 |
Crazyaboutmachines | 30:9c9f2d789153 | 201 | case 4: //cell 5 |
Crazyaboutmachines | 25:6f4f7510db24 | 202 | tx_cfg[0][4] = tx_cfg[0][4] & 0xEF ; // 11101111 |
Crazyaboutmachines | 30:9c9f2d789153 | 203 | case 6: //cell 7 |
Crazyaboutmachines | 25:6f4f7510db24 | 204 | tx_cfg[0][4] = tx_cfg[0][4] & 0xBF ; // 10111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 205 | case 7: //cell 8 |
Crazyaboutmachines | 30:9c9f2d789153 | 206 | tx_cfg[0][4] = tx_cfg[0][4] & 0x7F ; // 01111111 |
Crazyaboutmachines | 30:9c9f2d789153 | 207 | case 8: //cell 9 |
Crazyaboutmachines | 25:6f4f7510db24 | 208 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFE; // 11111110 |
Crazyaboutmachines | 30:9c9f2d789153 | 209 | case 9: //cell 10 |
Crazyaboutmachines | 26:c55656391a29 | 210 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFD; // 11111101 |
Crazyaboutmachines | 26:c55656391a29 | 211 | } |
ser1516 | 21:988413f53fbe | 212 | } |
ser1516 | 21:988413f53fbe | 213 | } |
ser1516 | 21:988413f53fbe | 214 | } |
ser1516 | 21:988413f53fbe | 215 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 216 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 217 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 218 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 219 | } |
ser1516 | 21:988413f53fbe | 220 | |
ser1516 | 21:988413f53fbe | 221 | |
ser1516 | 21:988413f53fbe | 222 | |
ser1516 | 21:988413f53fbe | 223 | void check_discharging_voltage() |
ser1516 | 21:988413f53fbe | 224 | { |
ser1516 | 21:988413f53fbe | 225 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 226 | LTC6804_adcv(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 227 | |
ser1516 | 21:988413f53fbe | 228 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 229 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 230 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 231 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 232 | pec_error(); |
ser1516 | 21:988413f53fbe | 233 | }/* |
ser1516 | 21:988413f53fbe | 234 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 235 | for (int i=0; i<11; i++) { |
ser1516 | 21:988413f53fbe | 236 | //printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 237 | if(cell_codes[current_ic][i]*0.0001 < 2.7) { |
ser1516 | 21:988413f53fbe | 238 | txMsg.clear(); |
ser1516 | 21:988413f53fbe | 239 | txMsg.id = 1; |
ser1516 | 21:988413f53fbe | 240 | printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 241 | |
ser1516 | 21:988413f53fbe | 242 | txMsg << cell_codes[current_ic][i]*0.0001; |
ser1516 | 21:988413f53fbe | 243 | can.write(txMsg); |
ser1516 | 21:988413f53fbe | 244 | wait(0.1); |
ser1516 | 21:988413f53fbe | 245 | } |
ser1516 | 21:988413f53fbe | 246 | } |
ser1516 | 21:988413f53fbe | 247 | }*/ |
ser1516 | 21:988413f53fbe | 248 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 249 | cells_left = 12; |
ser1516 | 21:988413f53fbe | 250 | |
Crazyaboutmachines | 26:c55656391a29 | 251 | tx_cfg[0][4] = tx_cfg[0][4] & 0b00000000; //para desactivar balanceamento durante a descarga |
Crazyaboutmachines | 26:c55656391a29 | 252 | tx_cfg[0][5] = tx_cfg[0][5] & 0b11110000; |
Crazyaboutmachines | 26:c55656391a29 | 253 | |
ser1516 | 21:988413f53fbe | 254 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 255 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 256 | } |
ser1516 | 21:988413f53fbe | 257 | |
ser1516 | 21:988413f53fbe | 258 | void onMsgReceived() |
ser1516 | 21:988413f53fbe | 259 | { |
hudakz | 16:a86f339d1c25 | 260 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 261 | } |
hudakz | 16:a86f339d1c25 | 262 | |
ser1516 | 21:988413f53fbe | 263 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 264 | bool to_charge_or_not_to_charge=false; // false = discharge |
ser1516 | 21:988413f53fbe | 265 | bool charging = false; |
Crazyaboutmachines | 29:86e963cc5ce7 | 266 | bool discharging = false; |
Crazyaboutmachines | 25:6f4f7510db24 | 267 | |
ser1516 | 21:988413f53fbe | 268 | void monitor() |
ser1516 | 21:988413f53fbe | 269 | { |
ser1516 | 21:988413f53fbe | 270 | led = !led; |
ser1516 | 21:988413f53fbe | 271 | if(to_charge_or_not_to_charge) { |
ser1516 | 21:988413f53fbe | 272 | charging = 1; |
ser1516 | 21:988413f53fbe | 273 | discharging = 0; |
ser1516 | 21:988413f53fbe | 274 | } else { |
ser1516 | 21:988413f53fbe | 275 | discharging = 1; |
ser1516 | 21:988413f53fbe | 276 | charging = 0; |
hudakz | 0:c5e5d0df6f2a | 277 | } |
hudakz | 0:c5e5d0df6f2a | 278 | } |
hudakz | 7:2dce8ed51091 | 279 | |
hudakz | 12:e91e44924194 | 280 | |
Crazyaboutmachines | 26:c55656391a29 | 281 | uint8_t motostate=0; |
Crazyaboutmachines | 26:c55656391a29 | 282 | |
ser1516 | 21:988413f53fbe | 283 | int main() |
ser1516 | 21:988413f53fbe | 284 | { |
Crazyaboutmachines | 26:c55656391a29 | 285 | data data; |
hudakz | 17:18d4d0ff26a6 | 286 | |
ser1516 | 21:988413f53fbe | 287 | //printf("starting\n\r"); |
ser1516 | 21:988413f53fbe | 288 | led =1; |
ser1516 | 21:988413f53fbe | 289 | wait(1); |
ser1516 | 21:988413f53fbe | 290 | pec_error(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 291 | discharging = 1; |
Crazyaboutmachines | 29:86e963cc5ce7 | 292 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 293 | ticker.attach(&monitor, 5); |
ser1516 | 21:988413f53fbe | 294 | LTC6804_initialize(); |
ser1516 | 21:988413f53fbe | 295 | init_cfg(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 296 | //write configuration |
ser1516 | 21:988413f53fbe | 297 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 298 | __disable_irq(); // Disable Interrupts |
ser1516 | 21:988413f53fbe | 299 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 300 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 301 | wait(1); |
ser1516 | 21:988413f53fbe | 302 | //read configuration: may differ from written config |
ser1516 | 21:988413f53fbe | 303 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 304 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 305 | err = LTC6804_rdcfg(TOTAL_IC,rx_cfg); |
ser1516 | 21:988413f53fbe | 306 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 307 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 308 | pec_error(); |
ser1516 | 21:988413f53fbe | 309 | } |
ser1516 | 21:988413f53fbe | 310 | wait(0.5); |
ser1516 | 21:988413f53fbe | 311 | |
ser1516 | 21:988413f53fbe | 312 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 313 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 314 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 315 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 316 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 317 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 318 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 319 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 320 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 321 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 322 | pec_error(); |
ser1516 | 21:988413f53fbe | 323 | } |
ser1516 | 21:988413f53fbe | 324 | can.frequency(1000000); // set bit rate to 1Mbps |
ser1516 | 21:988413f53fbe | 325 | can.attach(&onMsgReceived); |
ser1516 | 21:988413f53fbe | 326 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 327 | while(1) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 328 | if(charging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 329 | charging = 0; |
ser1516 | 21:988413f53fbe | 330 | check_charging_voltage(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 331 | |
ser1516 | 21:988413f53fbe | 332 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 333 | if(discharging) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 334 | discharging = 0; |
ser1516 | 21:988413f53fbe | 335 | check_discharging_voltage(); |
Crazyaboutmachines | 29:86e963cc5ce7 | 336 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 337 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 338 | if(to_send) { |
Crazyaboutmachines | 29:86e963cc5ce7 | 339 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 340 | |
ser1516 | 21:988413f53fbe | 341 | to_send=0; |
ser1516 | 21:988413f53fbe | 342 | txMsg.clear(); |
Crazyaboutmachines | 30:9c9f2d789153 | 343 | txMsg.id = 11; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
ser1516 | 21:988413f53fbe | 344 | txMsg.len = 5; |
ser1516 | 21:988413f53fbe | 345 | data.f[0] = cell_codes[0][cells_left-1]*0.0001; |
ser1516 | 21:988413f53fbe | 346 | txMsg.data[0] = data.bytes[0]; |
ser1516 | 21:988413f53fbe | 347 | txMsg.data[1] = data.bytes[1]; |
ser1516 | 21:988413f53fbe | 348 | txMsg.data[2] = data.bytes[2]; |
ser1516 | 21:988413f53fbe | 349 | txMsg.data[3] = data.bytes[3]; |
ser1516 | 21:988413f53fbe | 350 | txMsg.data[4] = cells_left; |
ser1516 | 21:988413f53fbe | 351 | cells_left--; |
Crazyaboutmachines | 29:86e963cc5ce7 | 352 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
ser1516 | 21:988413f53fbe | 353 | pec_error(); |
ser1516 | 21:988413f53fbe | 354 | cells_left++; |
ser1516 | 21:988413f53fbe | 355 | //to_send=1; |
ser1516 | 21:988413f53fbe | 356 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 357 | |
ser1516 | 21:988413f53fbe | 358 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 359 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 360 | if(msgAvailable) { |
Crazyaboutmachines | 26:c55656391a29 | 361 | int len = can.read(rxMsg); |
Crazyaboutmachines | 29:86e963cc5ce7 | 362 | if(rxMsg.id==9){ |
Crazyaboutmachines | 29:86e963cc5ce7 | 363 | |
Crazyaboutmachines | 29:86e963cc5ce7 | 364 | motostate = rxMsg.data[0]; |
Crazyaboutmachines | 29:86e963cc5ce7 | 365 | |
Crazyaboutmachines | 26:c55656391a29 | 366 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 29:86e963cc5ce7 | 367 | to_charge_or_not_to_charge=((motostate & 0b00000010)>>1); |
Crazyaboutmachines | 26:c55656391a29 | 368 | msgAvailable = false; |
Crazyaboutmachines | 26:c55656391a29 | 369 | } |
Crazyaboutmachines | 26:c55656391a29 | 370 | } |
ser1516 | 21:988413f53fbe | 371 | } |
Crazyaboutmachines | 29:86e963cc5ce7 | 372 | |
ser1516 | 21:988413f53fbe | 373 | } |