Added EPS faults
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of QM_BAE_review_1 by
Diff: EPS.cpp
- Revision:
- 3:07e15677a75c
- Parent:
- 2:c823d84b4cb0
- Child:
- 5:bb592f3185cc
--- a/EPS.cpp Fri Dec 25 15:25:44 2015 +0000 +++ b/EPS.cpp Tue Dec 29 05:14:39 2015 +0000 @@ -14,19 +14,35 @@ //......................................Peripheral declarations.........................................................// Serial pc_eps(USBTX,USBRX); + I2C m_I2C(PIN85,PIN84); DigitalOut TRXY(TRXY_DR_EN); //active high DigitalOut TRZ(TRZ_DR_EN); //active high DigitalOut EN3V3A(ENBL3V3A); DigitalOut EN_BTRY_HT(BATT_HEAT); //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); -AnalogIn Vbatt_ang(VBATT); +//AnalogIn Vbatt_ang(VBATT); +AnalogIn Batt_voltage(PIN20); //Battery voltage + SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK DigitalOut ssn1(PIN19); //Slave select1 DigitalOut ssn2(PIN21);//Slave select2 //DigitalOut PS(PTB0); //DigitalOut HS(PTB1); +AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54 +AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53 +AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor + +DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines +DigitalOut SelectLinea2 (PIN45); +DigitalOut SelectLinea1 (PIN44); +DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines + +DigitalOut SelectLineb3 (PIN56); // MSB of Select Lines +DigitalOut SelectLineb2 (PIN57); +DigitalOut SelectLineb1 (PIN58); +DigitalOut SelectLineb0 (PIN59); // LSB of Select Lines //*********************************************************flags********************************************************// extern char EPS_INIT_STATUS ; @@ -43,9 +59,9 @@ { printf("\n\r eps init \n"); EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag - // FLAG(); + // FLAG(); FCTN_BATTERYGAUGE_INIT(); - //FCTN_BATTTEMP_INIT(); + FCTN_BATTTEMP_INIT(); EN3V3A = 1; //enable dc dc converter A char value=alertFlags(); unsigned short value_u= (short int )value; @@ -58,7 +74,7 @@ else { actual_data.Batt_gauge_actual[1] = soc(); - actual_data.Batt_voltage_actual = Vbatt_ang.read()*3.3; + actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3; FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]); EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS } @@ -82,6 +98,119 @@ //...................................................HK...........................................// + +void FCTN_HK_MAIN() +{ + int Iteration=0; + + SelectLinea0=0; + SelectLinea1=0; + SelectLinea2=0; + SelectLinea3=0; + + SelectLineb0=0; + SelectLineb1=0; + SelectLineb2=0; + SelectLineb3=0; + + //collecting data + for(Iteration=0; Iteration<16; Iteration++){ + + actual_data.voltage_actual[Iteration]=VoltageInput.read(); + actual_data.current_actual[Iteration]=CurrentInput.read(); + + SelectLinea0=!(SelectLinea0); + if(Iteration%2==1) + SelectLinea1=!(SelectLinea1); + if(Iteration%4==3) + SelectLinea2=!(SelectLinea2); + if(Iteration%8==7) + SelectLinea3=!(SelectLinea3); + int s0,s1,s2,s3; + s0=SelectLineb0=SelectLinea0; + s1=SelectLineb1=SelectLinea2; + s2=SelectLineb2=SelectLinea2; + s3=SelectLineb3=SelectLinea3; + printf("\n\r %d %d %d %d", s0,s1,s2,s3); + + } + for(Iteration=0; Iteration<16; Iteration++){ + + if(Iteration==14) + actual_data.voltage_actual[Iteration]= (-90.7*3.3*actual_data.voltage_actual[Iteration])+190.1543; + else + actual_data.voltage_actual[Iteration]= actual_data.voltage_actual[Iteration]*3.3*5.63; + } + + for(Iteration=0;Iteration<12;Iteration++){ + if(Iteration<8) + actual_data.current_actual[Iteration]= actual_data.current_actual[Iteration]*3.3/(50*rsens); + else + actual_data.current_actual[Iteration]=actual_data.current_actual[Iteration]*3.3; + int resistance; + + resistance=24000*actual_data.current_actual[Iteration]/(3.3-actual_data.current_actual[Iteration]); + if(actual_data.current_actual[Iteration]>1.47) + { + actual_data.current_actual[Iteration]=3694/log(24.032242*resistance); + } + else{ + + actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance); + } + } + actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543; + + actual_data.Batt_voltage_actual=Batt_voltage.read()*3.3*5.63; + + //quantizing data + for(Iteration=0; Iteration<16; Iteration++){ + + if(Iteration==14) + quant_data.voltage_quant[Iteration]=quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]); + else + quant_data.voltage_quant[Iteration]=quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]); + + } + for(Iteration=0;Iteration<12;Iteration++){ + if(Iteration<8) + quant_data.current_quant[Iteration]=quantiz(cstart,cstep,actual_data.current_actual[Iteration]); + else + quant_data.current_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]); + } + for(Iteration=0;Iteration<2;Iteration++){ + + quant_data.Batt_temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]); + } + + quant_data.Batt_gauge_quant[0]=quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]); + quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]); + quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]); + quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3]; + + quant_data.BAE_temp_quant=quantiz(tstart,tstep,actual_data.BAE_temp_actual); + + for(Iteration=0;Iteration<3;Iteration++){ + quant_data.AngularSpeed_quant[Iteration]=quantiz(AngularSpeed_start,AngularSpeed_step,actual_data.AngularSpeed_actual[Iteration]); + } + + for(Iteration=0;Iteration<3;Iteration++){ + quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration]; + } + + quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual); + + + arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0]; + arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1]; + arch_data.EPS_PCB_temp=quant_data.voltage_quant[14]; + arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1]; + arch_data.power_mode=actual_data.power_mode; + arch_data.Batt_voltage=quant_data.Batt_voltage_quant; + + +} + int quantiz(float start,float step,float x) { int y=(x-start)/step; @@ -90,12 +219,99 @@ return y; } -void HK_main() -{ - - +bool firstCount=true; // goes to EPS init + + +void saveMin(char x,char y){ + if(y<x){ + x=y; + } + +} +void saveMax(char x,char y){ + if (y>x) + { + x=y; + } } + +void minMaxHkData(){ + if(firstCount==true){ + for (int i = 0; i < 16; ++i){ + bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i]; + bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i]; + } + for (int i = 0; i < 12; ++i){ + bae_HK_minmax.current_min[i] = quant_data.current_quant[i]; + bae_HK_minmax.current_max[i] = quant_data.current_quant[i]; + } + + for (int i = 0; i < 2; ++i){ + bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i]; + bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i]; + } + for (int i = 0; i < 3; ++i){ + bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i]; + bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i]; + } + for (int i = 0; i < 3; ++i){ + bae_HK_minmax.AngularSpeed_min[i] = quant_data.AngularSpeed_quant[i]; + bae_HK_minmax.AngularSpeed_max[i] = quant_data.AngularSpeed_quant[i]; + } + for (int i = 0; i < 3; ++i){ + bae_HK_minmax.Bvalue_min[i] = quant_data.Bvalue_quant[i]; + bae_HK_minmax.Bvalue_max[i] = quant_data.Bvalue_quant[i]; + } + bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant; + bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant; + bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant; + bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant; + + } + else { + for (int i = 0; i < 16; ++i) + { + saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]); + saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]); + } + for (int i = 0; i < 12; ++i) + { + saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]); + saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]); + } + + for (int i = 0; i < 2; ++i) + { + saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]); + saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]); + } + for (int i = 0; i < 3; ++i) + { + saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]); + saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]); + } + for (int i = 0; i < 3; ++i) + { + saveMin(bae_HK_minmax.AngularSpeed_min[i], quant_data.AngularSpeed_quant[i]); + saveMax(bae_HK_minmax.AngularSpeed_max[i], quant_data.AngularSpeed_quant[i]); + } + for (int i = 0; i < 3; ++i) + { + saveMin(bae_HK_minmax.Bvalue_min[i], quant_data.Bvalue_quant[i]); + saveMax(bae_HK_minmax.Bvalue_max[i], quant_data.Bvalue_quant[i]); + } + saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant); + saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant); + saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant); + saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant); + + + } + firstCount=false; +} + + //............................................BATTERY GAUGE......................................// void FCTN_BATTERYGAUGE_INIT() { @@ -112,7 +328,7 @@ { printf("\n\r battery gauge \n"); - float temp=25; //=Battery_temp (from temp sensor on battery board) //value of battery temperature in C currently given a dummy value. Should be updated everytime. + float temp=30; //=Battery_temp (from temp sensor on battery board) //value of battery temperature in C currently given a dummy value. Should be updated everytime. tempCompensation(temp); @@ -150,21 +366,9 @@ return (buff[0] << 8) | buff[1]; } - unsigned short read_soc(char reg , bool ack = true) - { - - //Create a temporary buffer - char buff[2]; + - //Select the register - m_I2C.write(m_ADDR, ®, 1, true); - - //Read the 16-bit register - ack = m_I2C.read(m_ADDR, buff, 2); - - //Return the combined 16-bit value - return (buff[0] << 8) | buff[1]; - } + void write(char reg, unsigned short data) { @@ -377,15 +581,24 @@ // Get the current state of charge measurement of the MAX17049 as a float float soc() { - //unsigned short value; + + //Create a temporary buffer char buff[2]; - bool ack; - //Read the 16-bit raw SOC value - unsigned short value = read_soc(REG_SOC, ack); + int ack = 1; + //Select the register + char reg = REG_SOC; // cannot pass the hash defined values directly + m_I2C.write(m_ADDR, ®, 1, true); + + + //Read the 16-bit register + + ack = m_I2C.read(m_ADDR, buff, 2); + + printf("\n\r acknow %d", ack); //Return SOC in percent if(ack == 0) - return value * 0.00390625; + return ((buff[0] << 8) | buff[1]) * 0.00390625; else return 200; }