SHENG-HEN HSIEH
/
VDU_2021
Just a regular publish
Diff: main.cpp
- Revision:
- 2:c7a3a8c1aeed
- Parent:
- 1:8a9ac822aab7
- Child:
- 4:f05bef255292
diff -r 8a9ac822aab7 -r c7a3a8c1aeed main.cpp --- a/main.cpp Mon Jun 18 06:50:24 2018 +0000 +++ b/main.cpp Sat Jul 14 20:02:18 2018 +0000 @@ -1,20 +1,73 @@ #include "mbed.h" +#include "SDFileSystem.h" +#define dt 0.01f +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + +DigitalOut Cool(PA_15,0); +DigitalOut LED(D13, 0); //Internal LED output, general purpose +CAN can1(PA_11, PA_12, 1000000); //(Inverter, PadalBox, DashBoard) 1Mbps, contain critical torque command message +CAN can2(PB_5, PB_13, 500000); //(BMS1, BMS2) 500kbps, contain battery status +Serial pc(USBTX, USBRX, 115200); +Ticker ticker1; //General + +Ticker ticker2; //Logger +SDFileSystem sd(PC_12, PC_11, PC_10, PD_2, "sd"); // mosi, miso, sck, cs -DigitalOut Cool(PA_15,0); -//DigitalOut Led(D13,0); -CAN can1(PA_11, PA_12, 1000000); //(Inverter, PadalBox, DashBoard) 1Mbps, contain critical torque command message -CAN can2(PB_5, PB_13, 500000); //(BMS1, BMS2) 500kbps, contain battery status -Serial pc(USBTX, USBRX, 115200); +CANMessage can_msg_1; +CANMessage can_msg_2; +CANMessage can_msg_send; +char data_msg[8] = {0,0,0,0,0,0,0,0}; -CANMessage can_msg_1; -CANMessage can_msg_2; +int16_t IGBT_Temp_A = 0; // *0.1 to get float value +int16_t IGBT_Temp_B = 0; // *0.1 to get float value +int16_t IGBT_Temp_C = 0; // *0.1 to get float value +int16_t IGBT_Temp = 0; // *0.1 to get float value +int16_t Motor_Temp = 0; // *0.1 to get float value +int16_t DC_Current = 0; // *0.1 to get float value +int16_t DC_Voltage = 0; // *0.1 to get float value +int16_t Motor_Speed = 0; + uint8_t CAN_flag_1 = 0; uint8_t CAN_flag_2 = 0; -uint8_t RTD_State = 0; // use as bool +uint8_t timer_flag = 0; +uint8_t RTD_State = 0; //use as bool +uint8_t SD_OK = 0; //SD OK flag +uint8_t Logging = 0; //Logging flag + +int16_t Max_temp_within_BMS1 = 0; //*0.01 to degC +int16_t Max_temp_within_BMS2 = 0; //*0.01 to degC +int16_t Max_temp = 0; //*0.01 to degC +uint16_t Module_Total_BMS1 = 0; //*0.01 to Voltage ~ 120 V +uint16_t Module_Total_BMS2 = 0; //*0.01 to Voltage ~ 120 V +uint16_t Module_Total = 0; //*0.01 to Voltage ~ 240 V + +uint16_t Module_Min_BMS1 = 0; //*0.0001 to Voltage ~ 3.0 V +uint16_t Module_Min_BMS2 = 0; //*0.0001 to Voltage ~ 3.0 V +uint16_t Module_Min = 0; //*0.0001 to Voltage ~ 3.0 V + +uint16_t Module_Max_BMS1 = 0; //*0.0001 to Voltage ~ 3.0 V +uint16_t Module_Max_BMS2 = 0; //*0.0001 to Voltage ~ 3.0 V +uint16_t Module_Max = 0; //*0.0001 to Voltage ~ 3.0 V + +void send(char ID); void CAN_RX1(void); void CAN_RX2(void); +void Send2CAN1(void); + +FILE *fp; //General file pointer +void start_log(void); +void stop_log(void); +void autoname(void); +char fileName[] = "/sd/NTHU_RACING/00.CSV"; +int16_t max_val(int16_t i1, int16_t i2, int16_t i3); + +void timer1_interrupt(void) +{ + timer_flag = 1; +} +void timer2_interrupt(void); int main() { @@ -23,24 +76,165 @@ can1.attach(&CAN_RX1, CAN::RxIrq); //CAN1 Recieve Irq can2.attach(&CAN_RX2, CAN::RxIrq); //CAN2 Recieve Irq + ticker1.attach(&timer1_interrupt, 1); //1sec + //Logger header + mkdir("/sd/NTHU_RACING", 0777); + + //General task while(1) { - if(CAN_flag_1) { -// Led = RTD_State; - Cool = RTD_State; - pc.printf("%d\r", RTD_State); - CAN_flag_1 = 0; + if(timer_flag == 1) { + + if(CAN_flag_1) { + if(RTD_State) { + Cool = 1; + if(!Logging) { + autoname(); //GENERATE NEW FILE EACH call fp ready or stuck + if(SD_OK) { + Logging = 1; + start_log(); + } + } + } else { + Cool = 0; + if(Logging) { + Logging = 0; + stop_log(); + } + } + CAN_flag_1 = 0; + } + + if(CAN_flag_2) { + //Total + Module_Total = Module_Total_BMS1 + Module_Total_BMS2; + + //Min + if(Module_Min_BMS1 < Module_Min_BMS2) { + Module_Min = Module_Min_BMS1; + } else { + Module_Min = Module_Min_BMS2; + } + + //Max + if(Module_Max_BMS1 < Module_Max_BMS2) { + Module_Max = Module_Max_BMS2; + } else { + Module_Max = Module_Max_BMS1; + } + + //Temp + if(Max_temp_within_BMS1 < Max_temp_within_BMS2) { + Max_temp = Max_temp_within_BMS2; + } else { + Max_temp = Max_temp_within_BMS1; + } + + CAN_flag_2 = 0; + Send2CAN1(); + } + +// pc.printf("SOC: %.2f\n", Module_Total*0.01f); +// pc.printf("Min: %.3f\n", Module_Min*0.0001f); +// pc.printf("Max: %.3f\n", Module_Max*0.0001f); +// pc.printf("Max1: %.3f\n", Module_Max_BMS1*0.0001f); +// pc.printf("Max2: %.3f\n", Module_Max_BMS2*0.0001f); +// pc.printf("Temp: %.1f\n", Max_temp*0.01f); + timer_flag = 0; } } } +void timer2_interrupt(void) +{ + fprintf(fp, "%.0f,%.0f,%d\n", DC_Voltage*0.1f, DC_Current*0.1f, Motor_Speed); +} + +void start_log(void) +{ + fprintf(fp, "NTHU_RACING DataLogger_V1.00\n"); + fprintf(fp, "Sample period: %.3f\n", dt); + fprintf(fp, "POOPOOphysic\n\n"); + fprintf(fp, "Volt,Amp,RPM\n"); + ticker2.attach(&timer2_interrupt, dt); +} + +void stop_log(void) +{ + ticker2.detach(); + fprintf(fp, "End of logging\n"); + fclose(fp); +} + +void autoname(void) +{ + for(uint8_t i=0; i<100; i++) { + fileName[16] = i/10 + '0'; + fileName[17] = i%10 + '0'; + + pc.printf("Openning: "); + pc.printf(fileName); + pc.printf("\n"); + + fp = fopen(fileName, "r" ); + + if(fp == NULL) { + fp = fopen(fileName, "w" ); + if(fp != NULL) { //indicates a valid file + printf("File opened: %s\n", fileName); + SD_OK = 1; + break; //out of for loop + + } else { + printf("Failed to open %s for read or write access. Check card is inserted.\n", fileName); + SD_OK = 0; + } + } else { + fclose(fp); + SD_OK = 0; + } + } + +} + void CAN_RX1(void) { if(can1.read(can_msg_1)) { switch(can_msg_1.id) { //Filtered the input message and do corresponding action - case 0xAA: + case 0xF0: //Internal state address - RTD_State = can_msg_1.data[6] & 0x01; //get bit "0", result ( 1 = RTD, 0 = OFF ) + RTD_State = can_msg_1.data[7] & 0x01; //get bit "0", result ( 1 = RTD, 0 = OFF ) + CAN_flag_1 = 1; + break; + + case 0xA0 : + //Temperature infrom address 1 + IGBT_Temp_A = can_msg_1.data[1] << 8 | can_msg_1.data[0]; + IGBT_Temp_B = can_msg_1.data[3] << 8 | can_msg_1.data[2]; + IGBT_Temp_C = can_msg_1.data[5] << 8 | can_msg_1.data[4]; + IGBT_Temp = max_val(IGBT_Temp_A,IGBT_Temp_B,IGBT_Temp_C); + CAN_flag_1 = 1; + break; + + case 0xA2 : + //Temperature infrom address 3 + Motor_Temp = can_msg_1.data[5] << 8 | can_msg_1.data[4]; + CAN_flag_1 = 1; + break; + + case 0xA5: + Motor_Speed = can_msg_1.data[3] << 8 | can_msg_1.data[2]; + break; + + case 0xA6 : + //Current infrom address + DC_Current = can_msg_1.data[7] << 8 | can_msg_1.data[6]; + CAN_flag_1 = 1; + break; + + case 0xA7 : + //Voltage infrom address + DC_Voltage = can_msg_1.data[1] << 8 | can_msg_1.data[0]; CAN_flag_1 = 1; break; } @@ -50,5 +244,79 @@ void CAN_RX2(void) { if(can2.read(can_msg_2)) { + switch(can_msg_2.id) { + case 0xD1: + //BMS1 soc + Module_Total_BMS1 = can_msg_2.data[1] << 8 | can_msg_2.data[0]; + Module_Total_BMS1 += can_msg_2.data[3] << 8 | can_msg_2.data[2]; + Module_Total_BMS1 += can_msg_2.data[5] << 8 | can_msg_2.data[4]; + CAN_flag_2 = 1; + break; + case 0xD2: + //BMS1 min Volt + Module_Min_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + case 0xD3: + //BMS1 max Volt + Module_Max_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + case 0xD4: + //BMS1 max temperature + Max_temp_within_BMS1 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + + case 0xE1: + //BMS2 soc + Module_Total_BMS2 = can_msg_2.data[1] << 8 | can_msg_2.data[0]; + Module_Total_BMS2 += can_msg_2.data[3] << 8 | can_msg_2.data[2]; + Module_Total_BMS2 += can_msg_2.data[5] << 8 | can_msg_2.data[4]; + CAN_flag_2 = 1; + break; + case 0xE2: + //BMS2 min Volt + Module_Min_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + case 0xE3: + //BMS2 max Volt + Module_Max_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + case 0xE4: + //BMS2 max temperature + Max_temp_within_BMS2 = can_msg_2.data[7] << 8 | can_msg_2.data[6]; + CAN_flag_2 = 1; + break; + } } +} + +void Send2CAN1(void) +{ + data_msg[0] = Module_Total; + data_msg[1] = Module_Total >> 8; + data_msg[2] = Module_Min; + data_msg[3] = Module_Min >> 8; + data_msg[4] = Module_Max; + data_msg[5] = Module_Max >> 8; + data_msg[6] = Max_temp; + data_msg[7] = Max_temp >> 8; + send(0xF1); +} + +void send(char ID) +{ + can_msg_send = CANMessage(ID,data_msg,8,CANData,CANStandard); + can1.write(can_msg_send); +} + +int16_t max_val(int16_t i1, int16_t i2, int16_t i3) +{ + int16_t max = i1; + if(i2 > max) max = i2; + if(i3 > max) max = i3; + return max; } \ No newline at end of file