![](/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
Diff: main.cpp
- Revision:
- 33:54861cd567cd
- Parent:
- 32:a52605800210
- Child:
- 34:8f92a17c5be9
--- a/main.cpp Sat Oct 29 00:38:17 2016 +0000 +++ b/main.cpp Wed Nov 09 10:13:51 2016 +0000 @@ -7,11 +7,16 @@ uint8_t tx_cfg[TOTAL_IC][6]; uint16_t aux_codes[TOTAL_IC][6]; uint16_t cell_codes[TOTAL_IC][12]; + +//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}; +uint16_t temp_codes[32]; + volatile bool msgAvailable = false; volatile bool to_send = false; CANnucleo::CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name CANnucleo::CANMessage rxMsg; CANnucleo::CANMessage txMsg; +CANnucleo::CANMessage txMsg1; DigitalOut led(PA_5); /*!*********************************** @@ -117,6 +122,7 @@ } } char cells_left=0; +char temps_left=0; Ticker ticker; Ticker sender; @@ -126,6 +132,12 @@ float f[2]; } data; +typedef union can_union2 { + int i[2]; + char bytes[8]; + float f[2]; +} data2; + void message_trigger() { if(cells_left < 1) { @@ -229,6 +241,96 @@ LTC6804_wrcfg(TOTAL_IC,tx_cfg); } +AnalogIn MUXA_Read(PC_1); +DigitalOut MUXA_0(PB_10); +DigitalOut MUXA_1(PB_2); +DigitalOut MUXA_2(PB_0); +DigitalOut MUXA_3(PB_1); +AnalogIn MUXB_Read(PC_2); +DigitalOut MUXB_0(PC_7); +DigitalOut MUXB_1(PC_6); +DigitalOut MUXB_2(PB_14); +DigitalOut MUXB_3(PB_15); + +void check_temperatures() +{ + for (int i=0; i<32; i++){ + switch (i) { + case 0: //ntc 1 //from MUXA + MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=0; + case 1: //ntc 2 + MUXA_3=0; MUXA_2=0; MUXA_1=0; MUXA_0=1; + case 2: //ntc 3 + MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=0; + case 3: //ntc 4 + MUXA_3=0; MUXA_2=0; MUXA_1=1; MUXA_0=1; + case 4: //ntc 5 + MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=0; + case 5: //ntc 6 + MUXA_3=0; MUXA_2=1; MUXA_1=0; MUXA_0=1; + case 6: //ntc 7 + MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=0; + case 7: //ntc 8 + MUXA_3=0; MUXA_2=1; MUXA_1=1; MUXA_0=1; + case 8: //ntc 9 + MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=0; + case 9: //ntc 10 + MUXA_3=1; MUXA_2=0; MUXA_1=0; MUXA_0=1; + case 10: //ntc 11 + MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=0; + case 11: //ntc 12 + MUXA_3=1; MUXA_2=0; MUXA_1=1; MUXA_0=1; + case 12: //ntc 13 + MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=0; + case 13: //ntc 14 + MUXA_3=1; MUXA_2=1; MUXA_1=0; MUXA_0=1; + case 14: //ntc 15 + MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=0; + case 15: //ntc 16 + MUXA_3=1; MUXA_2=1; MUXA_1=1; MUXA_0=1; + case 16: //ntc 17 //from MUXB + MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=0; + case 17: //ntc 18 + MUXB_3=0; MUXB_2=0; MUXB_1=0; MUXB_0=1; + case 18: //ntc 19 + MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=0; + case 19: //ntc 20 + MUXB_3=0; MUXB_2=0; MUXB_1=1; MUXB_0=1; + case 20: //ntc 21 + MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=0; + case 21: //ntc 22 + MUXB_3=0; MUXB_2=1; MUXB_1=0; MUXB_0=1; + case 22: //ntc 23 + MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=0; + case 23: //ntc 24 + MUXB_3=0; MUXB_2=1; MUXB_1=1; MUXB_0=1; + case 24: //ntc 25 + MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=0; + case 25: //ntc 26 + MUXB_3=1; MUXB_2=0; MUXB_1=0; MUXB_0=1; + case 26: //ntc 27 + MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=0; + case 27: //ntc 28 + MUXB_3=1; MUXB_2=0; MUXB_1=1; MUXB_0=1; + case 28: //ntc 29 + MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=0; + case 29: //ntc 30 + MUXB_3=1; MUXB_2=1; MUXB_1=0; MUXB_0=1; + case 30: //ntc 31 + MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=0; + case 31: //ntc 32 + MUXB_3=1; MUXB_2=1; MUXB_1=1; MUXB_0=1; + } + wait_ms(10); //tempo para a tensao á saida do mux estabilisar + if(i<16){ // Converts and read the analog input value (value from 0.0 to 1.0) + temp_codes[i] = MUXA_Read.read()*3300; + }else{ + temp_codes[i] = MUXB_Read.read()*3300; + } + } + temps_left = 32; +} + void onMsgReceived() { msgAvailable = true; @@ -257,6 +359,7 @@ int main() { data data; + data2 data2; //printf("starting\n\r"); led =1; wait(1); @@ -301,17 +404,20 @@ if(charging) { charging = 0; check_charging_voltage(); + check_temperatures(); sender.attach(&message_trigger,0.1); } if(discharging) { discharging = 0; check_discharging_voltage(); + check_temperatures(); sender.attach(&message_trigger,0.1); } if(to_send) { to_send=0; + //-----------------1º send cell voltages txMsg.clear(); - txMsg.id = 13; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. + txMsg.id = 11; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. txMsg.len = 5; data.f[0] = cell_codes[0][cells_left-1]*0.0001; txMsg.data[0] = data.bytes[0]; @@ -325,6 +431,21 @@ cells_left++; //to_send=1; } + //-----------------2º send cell temperatures + txMsg1.clear(); + txMsg1.id = 21; //BMS1=>ID:11; BMS2=>ID:12; BMS3=>ID:13. + txMsg1.len = 3; + + data2.i[0] = temp_codes[temps_left-1]; + txMsg1.data[0] = data2.bytes[0]; + txMsg1.data[1] = data2.bytes[1]; + txMsg1.data[2] = temps_left; + temps_left--; + if(!(can.write(txMsg1))) { //se nao conseguiu transmitir continua a tentar transmitir a tensão dessa celula + pec_error(); + temps_left++; + //to_send=1; + } } if(msgAvailable) { int len = can.read(rxMsg);