Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of workinQM_10thDec by
Diff: EPS.cpp
- Revision:
- 30:22b338b027de
- Parent:
- 29:bb0d64656ba1
- Child:
- 32:623747b1cecb
--- a/EPS.cpp Thu Jun 23 12:14:28 2016 +0000 +++ b/EPS.cpp Fri Jul 01 14:59:12 2016 +0000 @@ -22,7 +22,7 @@ DigitalOut TRXY(TRXY_DR_EN); //active high DigitalOut TRZ(TRZ_DR_EN); //active high DigitalOut EN3V3A(ENBL3V3A); -DigitalOut EN_BTRY_HT(BATT_HEAT); +//DigitalOut BTRY_HTR_ENABLE(BATT_HEAT); //EN_BTRY_HT //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); //AnalogIn Vbatt_ang(VBATT); AnalogIn Batt_voltage(PIN20); //Battery voltage @@ -52,10 +52,10 @@ extern uint8_t EPS_INIT_STATUS ; extern uint8_t EPS_BATTERY_GAUGE_STATUS ; extern uint8_t EPS_MAIN_STATUS; -extern uint8_t EPS_BATTERY_TEMP_STATUS ; +extern uint8_t EPS_BTRY_TMP_STATUS ; extern uint8_t EPS_STATUS ; -extern uint8_t EPS_BATTERY_HEATER_ENABLE ; +extern uint8_t EPS_BTRY_HTR ; //eps cdms fault extern uint8_t CDMS_SW_STATUS; @@ -347,12 +347,12 @@ for(Iteration=0;Iteration<3;Iteration++){ quant_data.AngularSpeed_quant[Iteration]=actual_data.AngularSpeed_actual[Iteration]; - printf("\n\r w value %f",quant_data.AngularSpeed_quant[Iteration]); + //printf("\n\r w value %f",quant_data.AngularSpeed_quant[Iteration]); } for(Iteration=0;Iteration<3;Iteration++){ quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration]; - printf("\n\r b value %f",quant_data.Bvalue_quant[Iteration]); + //printf("\n\r b value %f",quant_data.Bvalue_quant[Iteration]); } quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual); @@ -528,75 +528,115 @@ vAlertMinMaxThreshold();//set min, max value of Valrt register vResetThresholdSet();//set threshold voltage for reset vResetAlertEnabled(true);//enable alert on reset for V < Vreset + write(REG_STATUS, read(REG_STATUS) & 0xFEFF); //Clearing Reset Indicator bit } -void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4]) +int FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4], float temp) { printf("\n\r battery gauge \n"); - 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); Battery_parameters[0]=vcell(); Battery_parameters[1]=soc(); Battery_parameters[2]=crate(); - + printf("\nVcell=%f",vcell()); //remove this for final code printf("\nSOC=%f",soc()); //remove this for final code printf("\nC_rate=%f",crate()); //remove this for final code - if (alerting()== true) //alert is on { Battery_parameters[3]=alertFlags(); clearAlert();//clear alert clearAlertFlags();//clear all alert flags } - + + if( soc() == 200) return 0; //Data not received - Invalid SOC + else return 1; +} + +void tempCompensation(float temp) +{ + //Calculate the new RCOMP value + char rcomp; + if (temp > 20.0) { + rcomp = RCOMP0 + (temp - 20.0) * -0.5; + } else { + rcomp = RCOMP0 + (temp - 20.0) * -5.0; + } + + //Update the RCOMP value + compensation(rcomp); } +void compensation(char rcomp) +{ + //Read the current 16-bit register value + unsigned short value = read(REG_CONFIG); + + //Update the register value + value &= 0x00FF; + value |= rcomp << 8; + + //Write the value back out + write(REG_CONFIG, value); +} + + +int write(char reg, unsigned short data) + { + //Create a temporary buffer + char buff[3]; + + //Load the register address and 16-bit data + buff[0] = reg; + buff[1] = data >> 8; + buff[2] = data; + + int flag = m_I2C.write(m_ADDR, buff, 3); //Write the data and return ack + + if( flag != 0 ) + { + flag = m_I2C.write(m_ADDR, buff, 3); //Write the data and return ack + if( data != read(reg) ) //Verify written data + EPS_BATTERY_GAUGE_STATUS = 0; //clear EPS_BATTERY_GAUGE_STATUS + } + + return flag; + } + unsigned short read(char reg) { - - //Create a temporary buffer - char buff[2]; + int flag = 1; + char buff[2]; //Create a temporary buffer //Select the register m_I2C.write(m_ADDR, ®, 1, true); - //Read the 16-bit register - m_I2C.read(m_ADDR, buff, 2); + flag = m_I2C.read(m_ADDR, buff, 2); + + if( flag ) + { + m_I2C.write(m_ADDR, ®, 1, true); + flag = m_I2C.read(m_ADDR, buff, 2); + if( flag ) + EPS_BATTERY_GAUGE_STATUS = 0; //clear EPS_BATTERY_GAUGE_STATUS + } //Return the combined 16-bit value return (buff[0] << 8) | buff[1]; } - - - - - void write(char reg, unsigned short data) - { - //Create a temporary buffer - char buff[3]; - - //Load the register address and 16-bit data - buff[0] = reg; - buff[1] = data >> 8; - buff[2] = data; - - //Write the data - m_I2C.write(m_ADDR, buff, 3); - } - - // Command the MAX17049 to perform a power-on reset void reset() { //Write the POR command write(REG_CMD, 0x5400); + //Re-initialise gauge + FCTN_BATTERYGAUGE_INIT(); } // Command the MAX17049 to perform a QuickStart @@ -643,49 +683,6 @@ //Write the value back out write(REG_CONFIG, value); } - - -void compensation(char rcomp) -{ - //Read the current 16-bit register value - unsigned short value = read(REG_CONFIG); - - //Update the register value - value &= 0x00FF; - value |= rcomp << 8; - - //Write the value back out - write(REG_CONFIG, value); -} - - -void tempCompensation(float temp) -{ - //Calculate the new RCOMP value - char rcomp; - if (temp > 20.0) { - rcomp = RCOMP0 + (temp - 20.0) * -0.5; - } else { - rcomp = RCOMP0 + (temp - 20.0) * -5.0; - } - - //Update the RCOMP value - compensation(rcomp); -} - - // Command the MAX17049 to de-assert the ALRT pin - void clearAlert() - { - //Read the current 16-bit register value - unsigned short value = read(REG_CONFIG); - - //Clear the ALRT bit - value &= ~(1 << 5); - - //Write the value back out - write(REG_CONFIG, value); - } - //Set the SOC empty alert threshold of the MAX17049 void emptyAlertThreshold(char threshold) @@ -748,7 +745,20 @@ //Write the value back out write(REG_STATUS, value); } + + // Command the MAX17049 to de-assert the ALRT pin + void clearAlert() + { + //Read the current 16-bit register value + unsigned short value = read(REG_CONFIG); + //Clear the ALRT bit + value &= ~(1 << 5); + + //Write the value back out + write(REG_CONFIG, value); + } + //Get the current alert flags on the MAX17049 //refer datasheet-status registers section to decode it. char alertFlags() @@ -842,7 +852,7 @@ //HS=0; spi_bt.format(8,3); spi_bt.frequency(1000000); - EPS_BATTERY_TEMP_STATUS = 1; + EPS_BTRY_TMP_STATUS = 1; } void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2])