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