![](/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@27:db96e99e37c9, 2016-10-25 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Tue Oct 25 08:24:58 2016 +0000
- Revision:
- 27:db96e99e37c9
- Parent:
- 26:c55656391a29
- Child:
- 28:b7d1c8d8a2aa
charger coments
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 | 27:db96e99e37c9 | 3 | *-diminuir tempos das interrupções etc |
Crazyaboutmachines | 27:db96e99e37c9 | 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) { |
ser1516 | 21:988413f53fbe | 172 | case 0: |
Crazyaboutmachines | 25:6f4f7510db24 | 173 | tx_cfg[0][4] = tx_cfg[0][4] | 0x01 ; // 00000001 |
ser1516 | 21:988413f53fbe | 174 | case 1: |
Crazyaboutmachines | 25:6f4f7510db24 | 175 | tx_cfg[0][4] = tx_cfg[0][4] | 0x02 ; // 00000010 |
ser1516 | 21:988413f53fbe | 176 | case 2: |
Crazyaboutmachines | 25:6f4f7510db24 | 177 | tx_cfg[0][4] = tx_cfg[0][4] | 0x04 ; // 00000100 |
ser1516 | 21:988413f53fbe | 178 | case 3: |
Crazyaboutmachines | 25:6f4f7510db24 | 179 | tx_cfg[0][4] = tx_cfg[0][4] | 0x08 ; // 00001000 |
ser1516 | 21:988413f53fbe | 180 | case 4: |
Crazyaboutmachines | 25:6f4f7510db24 | 181 | tx_cfg[0][4] = tx_cfg[0][4] | 0x10 ; // 00010000 |
ser1516 | 21:988413f53fbe | 182 | case 5: |
Crazyaboutmachines | 25:6f4f7510db24 | 183 | tx_cfg[0][4] = tx_cfg[0][4] | 0x20 ; // 00100001 |
ser1516 | 21:988413f53fbe | 184 | case 6: |
Crazyaboutmachines | 25:6f4f7510db24 | 185 | tx_cfg[0][4] = tx_cfg[0][4] | 0x40 ; // 01000001 |
ser1516 | 21:988413f53fbe | 186 | case 7: |
Crazyaboutmachines | 25:6f4f7510db24 | 187 | tx_cfg[0][4] = tx_cfg[0][4] | 0x80; // 10000001 |
ser1516 | 21:988413f53fbe | 188 | case 8: |
Crazyaboutmachines | 25:6f4f7510db24 | 189 | tx_cfg[0][5] = tx_cfg[0][5] | 0x01; // 00000001 |
ser1516 | 21:988413f53fbe | 190 | case 9: |
Crazyaboutmachines | 25:6f4f7510db24 | 191 | tx_cfg[0][5] = tx_cfg[0][5] | 0x10; // 00000010 |
Crazyaboutmachines | 26:c55656391a29 | 192 | } |
Crazyaboutmachines | 26:c55656391a29 | 193 | } else { |
Crazyaboutmachines | 26:c55656391a29 | 194 | switch (i) { |
Crazyaboutmachines | 25:6f4f7510db24 | 195 | case 0: |
Crazyaboutmachines | 25:6f4f7510db24 | 196 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFE ; // 11111110 |
Crazyaboutmachines | 25:6f4f7510db24 | 197 | case 1: |
Crazyaboutmachines | 25:6f4f7510db24 | 198 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFD ; // 11111101 |
Crazyaboutmachines | 25:6f4f7510db24 | 199 | case 2: |
Crazyaboutmachines | 25:6f4f7510db24 | 200 | tx_cfg[0][4] = tx_cfg[0][4] & 0xFB ; // 11111011 |
Crazyaboutmachines | 25:6f4f7510db24 | 201 | case 3: |
Crazyaboutmachines | 25:6f4f7510db24 | 202 | tx_cfg[0][4] = tx_cfg[0][4] & 0xF7 ; // 11110111 |
Crazyaboutmachines | 25:6f4f7510db24 | 203 | case 4: |
Crazyaboutmachines | 25:6f4f7510db24 | 204 | tx_cfg[0][4] = tx_cfg[0][4] & 0xEF ; // 11101111 |
Crazyaboutmachines | 25:6f4f7510db24 | 205 | case 5: |
Crazyaboutmachines | 25:6f4f7510db24 | 206 | tx_cfg[0][4] = tx_cfg[0][4] & 0xDF ; // 11011111 |
Crazyaboutmachines | 25:6f4f7510db24 | 207 | case 6: |
Crazyaboutmachines | 25:6f4f7510db24 | 208 | tx_cfg[0][4] = tx_cfg[0][4] & 0xBF ; // 10111111 |
Crazyaboutmachines | 25:6f4f7510db24 | 209 | case 7: |
Crazyaboutmachines | 25:6f4f7510db24 | 210 | tx_cfg[0][4] = tx_cfg[0][4] & 0x7F; // 01111111 |
Crazyaboutmachines | 25:6f4f7510db24 | 211 | case 8: |
Crazyaboutmachines | 25:6f4f7510db24 | 212 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFE; // 11111110 |
Crazyaboutmachines | 25:6f4f7510db24 | 213 | case 9: |
Crazyaboutmachines | 26:c55656391a29 | 214 | tx_cfg[0][5] = tx_cfg[0][5] & 0xFD; // 11111101 |
Crazyaboutmachines | 26:c55656391a29 | 215 | } |
ser1516 | 21:988413f53fbe | 216 | } |
ser1516 | 21:988413f53fbe | 217 | } |
ser1516 | 21:988413f53fbe | 218 | } |
ser1516 | 21:988413f53fbe | 219 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 220 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 221 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 222 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 223 | } |
ser1516 | 21:988413f53fbe | 224 | |
ser1516 | 21:988413f53fbe | 225 | |
ser1516 | 21:988413f53fbe | 226 | |
ser1516 | 21:988413f53fbe | 227 | void check_discharging_voltage() |
ser1516 | 21:988413f53fbe | 228 | { |
ser1516 | 21:988413f53fbe | 229 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 230 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 231 | |
ser1516 | 21:988413f53fbe | 232 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 233 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 234 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 235 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 236 | pec_error(); |
ser1516 | 21:988413f53fbe | 237 | }/* |
ser1516 | 21:988413f53fbe | 238 | for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++) { |
ser1516 | 21:988413f53fbe | 239 | for (int i=0; i<11; i++) { |
ser1516 | 21:988413f53fbe | 240 | //printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 241 | if(cell_codes[current_ic][i]*0.0001 < 2.7) { |
ser1516 | 21:988413f53fbe | 242 | txMsg.clear(); |
ser1516 | 21:988413f53fbe | 243 | txMsg.id = 1; |
ser1516 | 21:988413f53fbe | 244 | printf("%f\t", cell_codes[current_ic][i]*0.0001); |
ser1516 | 21:988413f53fbe | 245 | |
ser1516 | 21:988413f53fbe | 246 | txMsg << cell_codes[current_ic][i]*0.0001; |
ser1516 | 21:988413f53fbe | 247 | can.write(txMsg); |
ser1516 | 21:988413f53fbe | 248 | wait(0.1); |
ser1516 | 21:988413f53fbe | 249 | } |
ser1516 | 21:988413f53fbe | 250 | } |
ser1516 | 21:988413f53fbe | 251 | }*/ |
ser1516 | 21:988413f53fbe | 252 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 253 | cells_left = 12; |
ser1516 | 21:988413f53fbe | 254 | |
Crazyaboutmachines | 26:c55656391a29 | 255 | tx_cfg[0][4] = tx_cfg[0][4] & 0b00000000; //para desactivar balanceamento durante a descarga |
Crazyaboutmachines | 26:c55656391a29 | 256 | tx_cfg[0][5] = tx_cfg[0][5] & 0b11110000; |
Crazyaboutmachines | 26:c55656391a29 | 257 | |
ser1516 | 21:988413f53fbe | 258 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 259 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 260 | } |
ser1516 | 21:988413f53fbe | 261 | |
ser1516 | 21:988413f53fbe | 262 | void onMsgReceived() |
ser1516 | 21:988413f53fbe | 263 | { |
hudakz | 16:a86f339d1c25 | 264 | msgAvailable = true; |
hudakz | 16:a86f339d1c25 | 265 | } |
hudakz | 16:a86f339d1c25 | 266 | |
ser1516 | 21:988413f53fbe | 267 | |
Crazyaboutmachines | 26:c55656391a29 | 268 | bool to_charge_or_not_to_charge=false; // false = discharge |
ser1516 | 21:988413f53fbe | 269 | bool charging = false; |
ser1516 | 21:988413f53fbe | 270 | bool discharging = false; |
Crazyaboutmachines | 25:6f4f7510db24 | 271 | |
ser1516 | 21:988413f53fbe | 272 | void monitor() |
ser1516 | 21:988413f53fbe | 273 | { |
ser1516 | 21:988413f53fbe | 274 | led = !led; |
ser1516 | 21:988413f53fbe | 275 | if(to_charge_or_not_to_charge) { |
ser1516 | 21:988413f53fbe | 276 | charging = 1; |
ser1516 | 21:988413f53fbe | 277 | discharging = 0; |
ser1516 | 21:988413f53fbe | 278 | } else { |
ser1516 | 21:988413f53fbe | 279 | discharging = 1; |
ser1516 | 21:988413f53fbe | 280 | charging = 0; |
hudakz | 0:c5e5d0df6f2a | 281 | } |
hudakz | 0:c5e5d0df6f2a | 282 | } |
hudakz | 7:2dce8ed51091 | 283 | |
hudakz | 12:e91e44924194 | 284 | |
Crazyaboutmachines | 26:c55656391a29 | 285 | uint8_t motostate=0; |
Crazyaboutmachines | 26:c55656391a29 | 286 | |
ser1516 | 21:988413f53fbe | 287 | int main() |
ser1516 | 21:988413f53fbe | 288 | { |
Crazyaboutmachines | 26:c55656391a29 | 289 | data data; |
hudakz | 17:18d4d0ff26a6 | 290 | |
ser1516 | 21:988413f53fbe | 291 | //printf("starting\n\r"); |
ser1516 | 21:988413f53fbe | 292 | led =1; |
ser1516 | 21:988413f53fbe | 293 | wait(1); |
ser1516 | 21:988413f53fbe | 294 | pec_error(); |
ser1516 | 21:988413f53fbe | 295 | discharging = 1; |
ser1516 | 21:988413f53fbe | 296 | |
ser1516 | 21:988413f53fbe | 297 | ticker.attach(&monitor, 5); |
ser1516 | 21:988413f53fbe | 298 | LTC6804_initialize(); |
ser1516 | 21:988413f53fbe | 299 | init_cfg(); |
ser1516 | 21:988413f53fbe | 300 | //write configuration |
ser1516 | 21:988413f53fbe | 301 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 302 | __disable_irq(); // Disable Interrupts |
ser1516 | 21:988413f53fbe | 303 | LTC6804_wrcfg(TOTAL_IC,tx_cfg); |
ser1516 | 21:988413f53fbe | 304 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 305 | wait(1); |
ser1516 | 21:988413f53fbe | 306 | //read configuration: may differ from written config |
ser1516 | 21:988413f53fbe | 307 | wakeup_sleep(); |
ser1516 | 21:988413f53fbe | 308 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 309 | err = LTC6804_rdcfg(TOTAL_IC,rx_cfg); |
ser1516 | 21:988413f53fbe | 310 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 311 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 312 | pec_error(); |
ser1516 | 21:988413f53fbe | 313 | } |
ser1516 | 21:988413f53fbe | 314 | wait(0.5); |
ser1516 | 21:988413f53fbe | 315 | |
ser1516 | 21:988413f53fbe | 316 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 317 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 318 | LTC6804_adcv(); |
ser1516 | 21:988413f53fbe | 319 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 320 | wait_ms(10); |
ser1516 | 21:988413f53fbe | 321 | wakeup_idle(); |
ser1516 | 21:988413f53fbe | 322 | __disable_irq(); |
ser1516 | 21:988413f53fbe | 323 | err = LTC6804_rdcv(0, TOTAL_IC,cell_codes); |
ser1516 | 21:988413f53fbe | 324 | __enable_irq(); |
ser1516 | 21:988413f53fbe | 325 | if (err == -1) { |
ser1516 | 21:988413f53fbe | 326 | pec_error(); |
ser1516 | 21:988413f53fbe | 327 | } |
ser1516 | 21:988413f53fbe | 328 | can.frequency(1000000); // set bit rate to 1Mbps |
ser1516 | 21:988413f53fbe | 329 | can.attach(&onMsgReceived); |
ser1516 | 21:988413f53fbe | 330 | //print_cells2(); |
ser1516 | 21:988413f53fbe | 331 | while(1) { |
ser1516 | 21:988413f53fbe | 332 | if(charging) { |
ser1516 | 21:988413f53fbe | 333 | charging = 0; |
ser1516 | 21:988413f53fbe | 334 | check_charging_voltage(); |
ser1516 | 21:988413f53fbe | 335 | |
ser1516 | 21:988413f53fbe | 336 | } |
ser1516 | 21:988413f53fbe | 337 | if(discharging) { |
ser1516 | 21:988413f53fbe | 338 | discharging = 0; |
ser1516 | 21:988413f53fbe | 339 | check_discharging_voltage(); |
ser1516 | 21:988413f53fbe | 340 | sender.attach(&message_trigger,0.1); |
ser1516 | 21:988413f53fbe | 341 | } |
ser1516 | 21:988413f53fbe | 342 | if(to_send) { |
Crazyaboutmachines | 26:c55656391a29 | 343 | |
Crazyaboutmachines | 26:c55656391a29 | 344 | |
ser1516 | 21:988413f53fbe | 345 | to_send=0; |
ser1516 | 21:988413f53fbe | 346 | txMsg.clear(); |
Crazyaboutmachines | 25:6f4f7510db24 | 347 | txMsg.id = 11; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. |
ser1516 | 21:988413f53fbe | 348 | txMsg.len = 5; |
ser1516 | 21:988413f53fbe | 349 | data.f[0] = cell_codes[0][cells_left-1]*0.0001; |
ser1516 | 21:988413f53fbe | 350 | txMsg.data[0] = data.bytes[0]; |
ser1516 | 21:988413f53fbe | 351 | txMsg.data[1] = data.bytes[1]; |
ser1516 | 21:988413f53fbe | 352 | txMsg.data[2] = data.bytes[2]; |
ser1516 | 21:988413f53fbe | 353 | txMsg.data[3] = data.bytes[3]; |
ser1516 | 21:988413f53fbe | 354 | txMsg.data[4] = cells_left; |
ser1516 | 21:988413f53fbe | 355 | cells_left--; |
Crazyaboutmachines | 23:bde0179eee92 | 356 | if(!(can.write(txMsg))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula |
ser1516 | 21:988413f53fbe | 357 | pec_error(); |
ser1516 | 21:988413f53fbe | 358 | cells_left++; |
ser1516 | 21:988413f53fbe | 359 | //to_send=1; |
ser1516 | 21:988413f53fbe | 360 | } |
ser1516 | 21:988413f53fbe | 361 | |
ser1516 | 21:988413f53fbe | 362 | } |
Crazyaboutmachines | 26:c55656391a29 | 363 | |
Crazyaboutmachines | 26:c55656391a29 | 364 | if(msgAvailable) { |
Crazyaboutmachines | 26:c55656391a29 | 365 | int len = can.read(rxMsg); |
Crazyaboutmachines | 26:c55656391a29 | 366 | if(rxMsg.id==9){ |
Crazyaboutmachines | 26:c55656391a29 | 367 | |
Crazyaboutmachines | 26:c55656391a29 | 368 | motostate = rxMsg.data[0]; |
Crazyaboutmachines | 26:c55656391a29 | 369 | |
Crazyaboutmachines | 26:c55656391a29 | 370 | //motostate: (0|0|0|0|0|0|to_charge_or_not_to_charge|key_switch) |
Crazyaboutmachines | 26:c55656391a29 | 371 | to_charge_or_not_to_charge=((motostate & 0b00000010)>>1); |
Crazyaboutmachines | 26:c55656391a29 | 372 | msgAvailable = false; |
Crazyaboutmachines | 26:c55656391a29 | 373 | } |
Crazyaboutmachines | 26:c55656391a29 | 374 | } |
ser1516 | 21:988413f53fbe | 375 | } |
ser1516 | 21:988413f53fbe | 376 | |
ser1516 | 21:988413f53fbe | 377 | } |