Added EPS faults
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of QM_BAE_review_1 by
EPS.cpp@3:07e15677a75c, 2015-12-29 (annotated)
- Committer:
- sakthipriya
- Date:
- Tue Dec 29 05:14:39 2015 +0000
- Revision:
- 3:07e15677a75c
- Parent:
- 2:c823d84b4cb0
- Child:
- 5:bb592f3185cc
BAE with acs + eps + bcn (conops) + tmtc (without calling actual funtions).; status : working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:7b4c00e3912f | 1 | #include "EPS.h" |
sakthipriya | 0:7b4c00e3912f | 2 | #include "pin_config.h" |
sakthipriya | 0:7b4c00e3912f | 3 | /***********************************************global variable declaration***************************************************************/ |
sakthipriya | 0:7b4c00e3912f | 4 | extern uint32_t BAE_STATUS; |
sakthipriya | 0:7b4c00e3912f | 5 | extern uint32_t BAE_ENABLE; |
sakthipriya | 0:7b4c00e3912f | 6 | |
sakthipriya | 0:7b4c00e3912f | 7 | |
sakthipriya | 0:7b4c00e3912f | 8 | //m_I2C.frequency(10000) |
sakthipriya | 0:7b4c00e3912f | 9 | const char RCOMP0= 0x97; |
sakthipriya | 0:7b4c00e3912f | 10 | BAE_HK_actual actual_data; |
sakthipriya | 0:7b4c00e3912f | 11 | BAE_HK_quant quant_data; |
sakthipriya | 0:7b4c00e3912f | 12 | BAE_HK_min_max bae_HK_minmax; |
sakthipriya | 0:7b4c00e3912f | 13 | BAE_HK_arch arch_data; |
sakthipriya | 0:7b4c00e3912f | 14 | |
sakthipriya | 0:7b4c00e3912f | 15 | //......................................Peripheral declarations.........................................................// |
sakthipriya | 0:7b4c00e3912f | 16 | Serial pc_eps(USBTX,USBRX); |
sakthipriya | 3:07e15677a75c | 17 | |
sakthipriya | 0:7b4c00e3912f | 18 | I2C m_I2C(PIN85,PIN84); |
sakthipriya | 0:7b4c00e3912f | 19 | DigitalOut TRXY(TRXY_DR_EN); //active high |
sakthipriya | 0:7b4c00e3912f | 20 | DigitalOut TRZ(TRZ_DR_EN); //active high |
sakthipriya | 0:7b4c00e3912f | 21 | DigitalOut EN3V3A(ENBL3V3A); |
sakthipriya | 0:7b4c00e3912f | 22 | DigitalOut EN_BTRY_HT(BATT_HEAT); |
sakthipriya | 0:7b4c00e3912f | 23 | //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); |
sakthipriya | 3:07e15677a75c | 24 | //AnalogIn Vbatt_ang(VBATT); |
sakthipriya | 3:07e15677a75c | 25 | AnalogIn Batt_voltage(PIN20); //Battery voltage |
sakthipriya | 3:07e15677a75c | 26 | |
sakthipriya | 2:c823d84b4cb0 | 27 | SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK |
sakthipriya | 2:c823d84b4cb0 | 28 | DigitalOut ssn1(PIN19); //Slave select1 |
sakthipriya | 2:c823d84b4cb0 | 29 | DigitalOut ssn2(PIN21);//Slave select2 |
sakthipriya | 2:c823d84b4cb0 | 30 | //DigitalOut PS(PTB0); |
sakthipriya | 2:c823d84b4cb0 | 31 | //DigitalOut HS(PTB1); |
sakthipriya | 2:c823d84b4cb0 | 32 | |
sakthipriya | 3:07e15677a75c | 33 | AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54 |
sakthipriya | 3:07e15677a75c | 34 | AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53 |
sakthipriya | 3:07e15677a75c | 35 | AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor |
sakthipriya | 3:07e15677a75c | 36 | |
sakthipriya | 3:07e15677a75c | 37 | DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines |
sakthipriya | 3:07e15677a75c | 38 | DigitalOut SelectLinea2 (PIN45); |
sakthipriya | 3:07e15677a75c | 39 | DigitalOut SelectLinea1 (PIN44); |
sakthipriya | 3:07e15677a75c | 40 | DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines |
sakthipriya | 3:07e15677a75c | 41 | |
sakthipriya | 3:07e15677a75c | 42 | DigitalOut SelectLineb3 (PIN56); // MSB of Select Lines |
sakthipriya | 3:07e15677a75c | 43 | DigitalOut SelectLineb2 (PIN57); |
sakthipriya | 3:07e15677a75c | 44 | DigitalOut SelectLineb1 (PIN58); |
sakthipriya | 3:07e15677a75c | 45 | DigitalOut SelectLineb0 (PIN59); // LSB of Select Lines |
sakthipriya | 0:7b4c00e3912f | 46 | |
sakthipriya | 0:7b4c00e3912f | 47 | //*********************************************************flags********************************************************// |
sakthipriya | 0:7b4c00e3912f | 48 | extern char EPS_INIT_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 49 | extern char EPS_BATTERY_GAUGE_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 50 | extern char EPS_MAIN_STATUS; |
sakthipriya | 0:7b4c00e3912f | 51 | extern char EPS_BATTERY_TEMP_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 52 | extern char EPS_STATUS ; |
sakthipriya | 0:7b4c00e3912f | 53 | |
sakthipriya | 0:7b4c00e3912f | 54 | extern char EPS_BATTERY_HEAT_ENABLE ; |
sakthipriya | 0:7b4c00e3912f | 55 | |
sakthipriya | 0:7b4c00e3912f | 56 | //........................................... FUCTIONS.................................................// |
sakthipriya | 0:7b4c00e3912f | 57 | |
sakthipriya | 0:7b4c00e3912f | 58 | void FCTN_EPS_INIT() |
sakthipriya | 0:7b4c00e3912f | 59 | { |
sakthipriya | 0:7b4c00e3912f | 60 | printf("\n\r eps init \n"); |
sakthipriya | 0:7b4c00e3912f | 61 | EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag |
sakthipriya | 3:07e15677a75c | 62 | // FLAG(); |
sakthipriya | 0:7b4c00e3912f | 63 | FCTN_BATTERYGAUGE_INIT(); |
sakthipriya | 3:07e15677a75c | 64 | FCTN_BATTTEMP_INIT(); |
sakthipriya | 0:7b4c00e3912f | 65 | EN3V3A = 1; //enable dc dc converter A |
sakthipriya | 0:7b4c00e3912f | 66 | char value=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 67 | unsigned short value_u= (short int )value; |
sakthipriya | 0:7b4c00e3912f | 68 | value_u &=0x0001; |
sakthipriya | 0:7b4c00e3912f | 69 | if(value_u ==0x0001) // battery gauge not initialised |
sakthipriya | 0:7b4c00e3912f | 70 | { |
sakthipriya | 0:7b4c00e3912f | 71 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 72 | EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 73 | } |
sakthipriya | 0:7b4c00e3912f | 74 | else |
sakthipriya | 0:7b4c00e3912f | 75 | { |
sakthipriya | 0:7b4c00e3912f | 76 | actual_data.Batt_gauge_actual[1] = soc(); |
sakthipriya | 3:07e15677a75c | 77 | actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3; |
sakthipriya | 0:7b4c00e3912f | 78 | FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]); |
sakthipriya | 0:7b4c00e3912f | 79 | EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS |
sakthipriya | 0:7b4c00e3912f | 80 | } |
sakthipriya | 0:7b4c00e3912f | 81 | |
sakthipriya | 0:7b4c00e3912f | 82 | EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag |
sakthipriya | 0:7b4c00e3912f | 83 | |
sakthipriya | 0:7b4c00e3912f | 84 | } |
sakthipriya | 0:7b4c00e3912f | 85 | |
sakthipriya | 0:7b4c00e3912f | 86 | //----------------------------------------------------Power algo code--------------------------------------------------------------------// |
sakthipriya | 0:7b4c00e3912f | 87 | void FCTN_EPS_POWERMODE(float soc) //dummy algo |
sakthipriya | 0:7b4c00e3912f | 88 | { |
sakthipriya | 0:7b4c00e3912f | 89 | if(soc >= 80) |
sakthipriya | 0:7b4c00e3912f | 90 | actual_data.power_mode = 4; |
sakthipriya | 0:7b4c00e3912f | 91 | else if(soc >= 70 & soc < 80) |
sakthipriya | 0:7b4c00e3912f | 92 | actual_data.power_mode = 3; |
sakthipriya | 0:7b4c00e3912f | 93 | else if(soc >= 60 & soc < 70) |
sakthipriya | 0:7b4c00e3912f | 94 | actual_data.power_mode = 2; |
sakthipriya | 0:7b4c00e3912f | 95 | else if(soc < 60) |
sakthipriya | 0:7b4c00e3912f | 96 | actual_data.power_mode = 1; |
sakthipriya | 0:7b4c00e3912f | 97 | } |
sakthipriya | 0:7b4c00e3912f | 98 | |
sakthipriya | 0:7b4c00e3912f | 99 | //...................................................HK...........................................// |
sakthipriya | 0:7b4c00e3912f | 100 | |
sakthipriya | 3:07e15677a75c | 101 | |
sakthipriya | 3:07e15677a75c | 102 | void FCTN_HK_MAIN() |
sakthipriya | 3:07e15677a75c | 103 | { |
sakthipriya | 3:07e15677a75c | 104 | int Iteration=0; |
sakthipriya | 3:07e15677a75c | 105 | |
sakthipriya | 3:07e15677a75c | 106 | SelectLinea0=0; |
sakthipriya | 3:07e15677a75c | 107 | SelectLinea1=0; |
sakthipriya | 3:07e15677a75c | 108 | SelectLinea2=0; |
sakthipriya | 3:07e15677a75c | 109 | SelectLinea3=0; |
sakthipriya | 3:07e15677a75c | 110 | |
sakthipriya | 3:07e15677a75c | 111 | SelectLineb0=0; |
sakthipriya | 3:07e15677a75c | 112 | SelectLineb1=0; |
sakthipriya | 3:07e15677a75c | 113 | SelectLineb2=0; |
sakthipriya | 3:07e15677a75c | 114 | SelectLineb3=0; |
sakthipriya | 3:07e15677a75c | 115 | |
sakthipriya | 3:07e15677a75c | 116 | //collecting data |
sakthipriya | 3:07e15677a75c | 117 | for(Iteration=0; Iteration<16; Iteration++){ |
sakthipriya | 3:07e15677a75c | 118 | |
sakthipriya | 3:07e15677a75c | 119 | actual_data.voltage_actual[Iteration]=VoltageInput.read(); |
sakthipriya | 3:07e15677a75c | 120 | actual_data.current_actual[Iteration]=CurrentInput.read(); |
sakthipriya | 3:07e15677a75c | 121 | |
sakthipriya | 3:07e15677a75c | 122 | SelectLinea0=!(SelectLinea0); |
sakthipriya | 3:07e15677a75c | 123 | if(Iteration%2==1) |
sakthipriya | 3:07e15677a75c | 124 | SelectLinea1=!(SelectLinea1); |
sakthipriya | 3:07e15677a75c | 125 | if(Iteration%4==3) |
sakthipriya | 3:07e15677a75c | 126 | SelectLinea2=!(SelectLinea2); |
sakthipriya | 3:07e15677a75c | 127 | if(Iteration%8==7) |
sakthipriya | 3:07e15677a75c | 128 | SelectLinea3=!(SelectLinea3); |
sakthipriya | 3:07e15677a75c | 129 | int s0,s1,s2,s3; |
sakthipriya | 3:07e15677a75c | 130 | s0=SelectLineb0=SelectLinea0; |
sakthipriya | 3:07e15677a75c | 131 | s1=SelectLineb1=SelectLinea2; |
sakthipriya | 3:07e15677a75c | 132 | s2=SelectLineb2=SelectLinea2; |
sakthipriya | 3:07e15677a75c | 133 | s3=SelectLineb3=SelectLinea3; |
sakthipriya | 3:07e15677a75c | 134 | printf("\n\r %d %d %d %d", s0,s1,s2,s3); |
sakthipriya | 3:07e15677a75c | 135 | |
sakthipriya | 3:07e15677a75c | 136 | } |
sakthipriya | 3:07e15677a75c | 137 | for(Iteration=0; Iteration<16; Iteration++){ |
sakthipriya | 3:07e15677a75c | 138 | |
sakthipriya | 3:07e15677a75c | 139 | if(Iteration==14) |
sakthipriya | 3:07e15677a75c | 140 | actual_data.voltage_actual[Iteration]= (-90.7*3.3*actual_data.voltage_actual[Iteration])+190.1543; |
sakthipriya | 3:07e15677a75c | 141 | else |
sakthipriya | 3:07e15677a75c | 142 | actual_data.voltage_actual[Iteration]= actual_data.voltage_actual[Iteration]*3.3*5.63; |
sakthipriya | 3:07e15677a75c | 143 | } |
sakthipriya | 3:07e15677a75c | 144 | |
sakthipriya | 3:07e15677a75c | 145 | for(Iteration=0;Iteration<12;Iteration++){ |
sakthipriya | 3:07e15677a75c | 146 | if(Iteration<8) |
sakthipriya | 3:07e15677a75c | 147 | actual_data.current_actual[Iteration]= actual_data.current_actual[Iteration]*3.3/(50*rsens); |
sakthipriya | 3:07e15677a75c | 148 | else |
sakthipriya | 3:07e15677a75c | 149 | actual_data.current_actual[Iteration]=actual_data.current_actual[Iteration]*3.3; |
sakthipriya | 3:07e15677a75c | 150 | int resistance; |
sakthipriya | 3:07e15677a75c | 151 | |
sakthipriya | 3:07e15677a75c | 152 | resistance=24000*actual_data.current_actual[Iteration]/(3.3-actual_data.current_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 153 | if(actual_data.current_actual[Iteration]>1.47) |
sakthipriya | 3:07e15677a75c | 154 | { |
sakthipriya | 3:07e15677a75c | 155 | actual_data.current_actual[Iteration]=3694/log(24.032242*resistance); |
sakthipriya | 3:07e15677a75c | 156 | } |
sakthipriya | 3:07e15677a75c | 157 | else{ |
sakthipriya | 3:07e15677a75c | 158 | |
sakthipriya | 3:07e15677a75c | 159 | actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance); |
sakthipriya | 3:07e15677a75c | 160 | } |
sakthipriya | 3:07e15677a75c | 161 | } |
sakthipriya | 3:07e15677a75c | 162 | actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543; |
sakthipriya | 3:07e15677a75c | 163 | |
sakthipriya | 3:07e15677a75c | 164 | actual_data.Batt_voltage_actual=Batt_voltage.read()*3.3*5.63; |
sakthipriya | 3:07e15677a75c | 165 | |
sakthipriya | 3:07e15677a75c | 166 | //quantizing data |
sakthipriya | 3:07e15677a75c | 167 | for(Iteration=0; Iteration<16; Iteration++){ |
sakthipriya | 3:07e15677a75c | 168 | |
sakthipriya | 3:07e15677a75c | 169 | if(Iteration==14) |
sakthipriya | 3:07e15677a75c | 170 | quant_data.voltage_quant[Iteration]=quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 171 | else |
sakthipriya | 3:07e15677a75c | 172 | quant_data.voltage_quant[Iteration]=quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 173 | |
sakthipriya | 3:07e15677a75c | 174 | } |
sakthipriya | 3:07e15677a75c | 175 | for(Iteration=0;Iteration<12;Iteration++){ |
sakthipriya | 3:07e15677a75c | 176 | if(Iteration<8) |
sakthipriya | 3:07e15677a75c | 177 | quant_data.current_quant[Iteration]=quantiz(cstart,cstep,actual_data.current_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 178 | else |
sakthipriya | 3:07e15677a75c | 179 | quant_data.current_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 180 | } |
sakthipriya | 3:07e15677a75c | 181 | for(Iteration=0;Iteration<2;Iteration++){ |
sakthipriya | 3:07e15677a75c | 182 | |
sakthipriya | 3:07e15677a75c | 183 | quant_data.Batt_temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 184 | } |
sakthipriya | 3:07e15677a75c | 185 | |
sakthipriya | 3:07e15677a75c | 186 | quant_data.Batt_gauge_quant[0]=quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]); |
sakthipriya | 3:07e15677a75c | 187 | quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]); |
sakthipriya | 3:07e15677a75c | 188 | quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]); |
sakthipriya | 3:07e15677a75c | 189 | quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3]; |
sakthipriya | 3:07e15677a75c | 190 | |
sakthipriya | 3:07e15677a75c | 191 | quant_data.BAE_temp_quant=quantiz(tstart,tstep,actual_data.BAE_temp_actual); |
sakthipriya | 3:07e15677a75c | 192 | |
sakthipriya | 3:07e15677a75c | 193 | for(Iteration=0;Iteration<3;Iteration++){ |
sakthipriya | 3:07e15677a75c | 194 | quant_data.AngularSpeed_quant[Iteration]=quantiz(AngularSpeed_start,AngularSpeed_step,actual_data.AngularSpeed_actual[Iteration]); |
sakthipriya | 3:07e15677a75c | 195 | } |
sakthipriya | 3:07e15677a75c | 196 | |
sakthipriya | 3:07e15677a75c | 197 | for(Iteration=0;Iteration<3;Iteration++){ |
sakthipriya | 3:07e15677a75c | 198 | quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration]; |
sakthipriya | 3:07e15677a75c | 199 | } |
sakthipriya | 3:07e15677a75c | 200 | |
sakthipriya | 3:07e15677a75c | 201 | quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual); |
sakthipriya | 3:07e15677a75c | 202 | |
sakthipriya | 3:07e15677a75c | 203 | |
sakthipriya | 3:07e15677a75c | 204 | arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0]; |
sakthipriya | 3:07e15677a75c | 205 | arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1]; |
sakthipriya | 3:07e15677a75c | 206 | arch_data.EPS_PCB_temp=quant_data.voltage_quant[14]; |
sakthipriya | 3:07e15677a75c | 207 | arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1]; |
sakthipriya | 3:07e15677a75c | 208 | arch_data.power_mode=actual_data.power_mode; |
sakthipriya | 3:07e15677a75c | 209 | arch_data.Batt_voltage=quant_data.Batt_voltage_quant; |
sakthipriya | 3:07e15677a75c | 210 | |
sakthipriya | 3:07e15677a75c | 211 | |
sakthipriya | 3:07e15677a75c | 212 | } |
sakthipriya | 3:07e15677a75c | 213 | |
sakthipriya | 0:7b4c00e3912f | 214 | int quantiz(float start,float step,float x) |
sakthipriya | 0:7b4c00e3912f | 215 | { |
sakthipriya | 0:7b4c00e3912f | 216 | int y=(x-start)/step; |
sakthipriya | 0:7b4c00e3912f | 217 | if(y<=0)y=0; |
sakthipriya | 0:7b4c00e3912f | 218 | if(y>=255)y=255; |
sakthipriya | 0:7b4c00e3912f | 219 | return y; |
sakthipriya | 0:7b4c00e3912f | 220 | } |
sakthipriya | 0:7b4c00e3912f | 221 | |
sakthipriya | 3:07e15677a75c | 222 | bool firstCount=true; // goes to EPS init |
sakthipriya | 3:07e15677a75c | 223 | |
sakthipriya | 3:07e15677a75c | 224 | |
sakthipriya | 3:07e15677a75c | 225 | void saveMin(char x,char y){ |
sakthipriya | 3:07e15677a75c | 226 | if(y<x){ |
sakthipriya | 3:07e15677a75c | 227 | x=y; |
sakthipriya | 3:07e15677a75c | 228 | } |
sakthipriya | 3:07e15677a75c | 229 | |
sakthipriya | 3:07e15677a75c | 230 | } |
sakthipriya | 3:07e15677a75c | 231 | void saveMax(char x,char y){ |
sakthipriya | 3:07e15677a75c | 232 | if (y>x) |
sakthipriya | 3:07e15677a75c | 233 | { |
sakthipriya | 3:07e15677a75c | 234 | x=y; |
sakthipriya | 3:07e15677a75c | 235 | } |
sakthipriya | 0:7b4c00e3912f | 236 | } |
sakthipriya | 0:7b4c00e3912f | 237 | |
sakthipriya | 3:07e15677a75c | 238 | |
sakthipriya | 3:07e15677a75c | 239 | void minMaxHkData(){ |
sakthipriya | 3:07e15677a75c | 240 | if(firstCount==true){ |
sakthipriya | 3:07e15677a75c | 241 | for (int i = 0; i < 16; ++i){ |
sakthipriya | 3:07e15677a75c | 242 | bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i]; |
sakthipriya | 3:07e15677a75c | 243 | bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i]; |
sakthipriya | 3:07e15677a75c | 244 | } |
sakthipriya | 3:07e15677a75c | 245 | for (int i = 0; i < 12; ++i){ |
sakthipriya | 3:07e15677a75c | 246 | bae_HK_minmax.current_min[i] = quant_data.current_quant[i]; |
sakthipriya | 3:07e15677a75c | 247 | bae_HK_minmax.current_max[i] = quant_data.current_quant[i]; |
sakthipriya | 3:07e15677a75c | 248 | } |
sakthipriya | 3:07e15677a75c | 249 | |
sakthipriya | 3:07e15677a75c | 250 | for (int i = 0; i < 2; ++i){ |
sakthipriya | 3:07e15677a75c | 251 | bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i]; |
sakthipriya | 3:07e15677a75c | 252 | bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i]; |
sakthipriya | 3:07e15677a75c | 253 | } |
sakthipriya | 3:07e15677a75c | 254 | for (int i = 0; i < 3; ++i){ |
sakthipriya | 3:07e15677a75c | 255 | bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i]; |
sakthipriya | 3:07e15677a75c | 256 | bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i]; |
sakthipriya | 3:07e15677a75c | 257 | } |
sakthipriya | 3:07e15677a75c | 258 | for (int i = 0; i < 3; ++i){ |
sakthipriya | 3:07e15677a75c | 259 | bae_HK_minmax.AngularSpeed_min[i] = quant_data.AngularSpeed_quant[i]; |
sakthipriya | 3:07e15677a75c | 260 | bae_HK_minmax.AngularSpeed_max[i] = quant_data.AngularSpeed_quant[i]; |
sakthipriya | 3:07e15677a75c | 261 | } |
sakthipriya | 3:07e15677a75c | 262 | for (int i = 0; i < 3; ++i){ |
sakthipriya | 3:07e15677a75c | 263 | bae_HK_minmax.Bvalue_min[i] = quant_data.Bvalue_quant[i]; |
sakthipriya | 3:07e15677a75c | 264 | bae_HK_minmax.Bvalue_max[i] = quant_data.Bvalue_quant[i]; |
sakthipriya | 3:07e15677a75c | 265 | } |
sakthipriya | 3:07e15677a75c | 266 | bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant; |
sakthipriya | 3:07e15677a75c | 267 | bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant; |
sakthipriya | 3:07e15677a75c | 268 | bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant; |
sakthipriya | 3:07e15677a75c | 269 | bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant; |
sakthipriya | 3:07e15677a75c | 270 | |
sakthipriya | 3:07e15677a75c | 271 | } |
sakthipriya | 3:07e15677a75c | 272 | else { |
sakthipriya | 3:07e15677a75c | 273 | for (int i = 0; i < 16; ++i) |
sakthipriya | 3:07e15677a75c | 274 | { |
sakthipriya | 3:07e15677a75c | 275 | saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]); |
sakthipriya | 3:07e15677a75c | 276 | saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]); |
sakthipriya | 3:07e15677a75c | 277 | } |
sakthipriya | 3:07e15677a75c | 278 | for (int i = 0; i < 12; ++i) |
sakthipriya | 3:07e15677a75c | 279 | { |
sakthipriya | 3:07e15677a75c | 280 | saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]); |
sakthipriya | 3:07e15677a75c | 281 | saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]); |
sakthipriya | 3:07e15677a75c | 282 | } |
sakthipriya | 3:07e15677a75c | 283 | |
sakthipriya | 3:07e15677a75c | 284 | for (int i = 0; i < 2; ++i) |
sakthipriya | 3:07e15677a75c | 285 | { |
sakthipriya | 3:07e15677a75c | 286 | saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]); |
sakthipriya | 3:07e15677a75c | 287 | saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]); |
sakthipriya | 3:07e15677a75c | 288 | } |
sakthipriya | 3:07e15677a75c | 289 | for (int i = 0; i < 3; ++i) |
sakthipriya | 3:07e15677a75c | 290 | { |
sakthipriya | 3:07e15677a75c | 291 | saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]); |
sakthipriya | 3:07e15677a75c | 292 | saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]); |
sakthipriya | 3:07e15677a75c | 293 | } |
sakthipriya | 3:07e15677a75c | 294 | for (int i = 0; i < 3; ++i) |
sakthipriya | 3:07e15677a75c | 295 | { |
sakthipriya | 3:07e15677a75c | 296 | saveMin(bae_HK_minmax.AngularSpeed_min[i], quant_data.AngularSpeed_quant[i]); |
sakthipriya | 3:07e15677a75c | 297 | saveMax(bae_HK_minmax.AngularSpeed_max[i], quant_data.AngularSpeed_quant[i]); |
sakthipriya | 3:07e15677a75c | 298 | } |
sakthipriya | 3:07e15677a75c | 299 | for (int i = 0; i < 3; ++i) |
sakthipriya | 3:07e15677a75c | 300 | { |
sakthipriya | 3:07e15677a75c | 301 | saveMin(bae_HK_minmax.Bvalue_min[i], quant_data.Bvalue_quant[i]); |
sakthipriya | 3:07e15677a75c | 302 | saveMax(bae_HK_minmax.Bvalue_max[i], quant_data.Bvalue_quant[i]); |
sakthipriya | 3:07e15677a75c | 303 | } |
sakthipriya | 3:07e15677a75c | 304 | saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant); |
sakthipriya | 3:07e15677a75c | 305 | saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant); |
sakthipriya | 3:07e15677a75c | 306 | saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant); |
sakthipriya | 3:07e15677a75c | 307 | saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant); |
sakthipriya | 3:07e15677a75c | 308 | |
sakthipriya | 3:07e15677a75c | 309 | |
sakthipriya | 3:07e15677a75c | 310 | } |
sakthipriya | 3:07e15677a75c | 311 | firstCount=false; |
sakthipriya | 3:07e15677a75c | 312 | } |
sakthipriya | 3:07e15677a75c | 313 | |
sakthipriya | 3:07e15677a75c | 314 | |
sakthipriya | 0:7b4c00e3912f | 315 | //............................................BATTERY GAUGE......................................// |
sakthipriya | 0:7b4c00e3912f | 316 | void FCTN_BATTERYGAUGE_INIT() |
sakthipriya | 0:7b4c00e3912f | 317 | { |
sakthipriya | 0:7b4c00e3912f | 318 | disable_sleep(); |
sakthipriya | 0:7b4c00e3912f | 319 | disable_hibernate(); |
sakthipriya | 0:7b4c00e3912f | 320 | socChangeAlertEnabled(true); //enabling alert on soc changing by 1% |
sakthipriya | 0:7b4c00e3912f | 321 | emptyAlertThreshold(32);//setting empty alert threshold to 32% soc |
sakthipriya | 0:7b4c00e3912f | 322 | vAlertMinMaxThreshold();//set min, max value of Valrt register |
sakthipriya | 0:7b4c00e3912f | 323 | vResetThresholdSet();//set threshold voltage for reset |
sakthipriya | 0:7b4c00e3912f | 324 | vResetAlertEnabled(true);//enable alert on reset for V < Vreset |
sakthipriya | 0:7b4c00e3912f | 325 | } |
sakthipriya | 0:7b4c00e3912f | 326 | |
sakthipriya | 0:7b4c00e3912f | 327 | void FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4]) |
sakthipriya | 0:7b4c00e3912f | 328 | { |
sakthipriya | 1:446a959e36ce | 329 | printf("\n\r battery gauge \n"); |
sakthipriya | 0:7b4c00e3912f | 330 | |
sakthipriya | 3:07e15677a75c | 331 | 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. |
sakthipriya | 0:7b4c00e3912f | 332 | tempCompensation(temp); |
sakthipriya | 0:7b4c00e3912f | 333 | |
sakthipriya | 0:7b4c00e3912f | 334 | |
sakthipriya | 0:7b4c00e3912f | 335 | Battery_parameters[0]=vcell(); |
sakthipriya | 0:7b4c00e3912f | 336 | Battery_parameters[1]=soc(); |
sakthipriya | 0:7b4c00e3912f | 337 | Battery_parameters[2]=crate(); |
sakthipriya | 0:7b4c00e3912f | 338 | |
sakthipriya | 0:7b4c00e3912f | 339 | printf("\nVcell=%f",vcell()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 340 | printf("\nSOC=%f",soc()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 341 | printf("\nC_rate=%f",crate()); //remove this for final code |
sakthipriya | 0:7b4c00e3912f | 342 | |
sakthipriya | 0:7b4c00e3912f | 343 | |
sakthipriya | 0:7b4c00e3912f | 344 | if (alerting()== true) //alert is on |
sakthipriya | 0:7b4c00e3912f | 345 | { |
sakthipriya | 0:7b4c00e3912f | 346 | Battery_parameters[3]=alertFlags(); |
sakthipriya | 0:7b4c00e3912f | 347 | clearAlert();//clear alert |
sakthipriya | 0:7b4c00e3912f | 348 | clearAlertFlags();//clear all alert flags |
sakthipriya | 0:7b4c00e3912f | 349 | } |
sakthipriya | 0:7b4c00e3912f | 350 | |
sakthipriya | 0:7b4c00e3912f | 351 | } |
sakthipriya | 0:7b4c00e3912f | 352 | |
sakthipriya | 0:7b4c00e3912f | 353 | unsigned short read(char reg) |
sakthipriya | 0:7b4c00e3912f | 354 | { |
sakthipriya | 0:7b4c00e3912f | 355 | |
sakthipriya | 0:7b4c00e3912f | 356 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 357 | char buff[2]; |
sakthipriya | 0:7b4c00e3912f | 358 | |
sakthipriya | 0:7b4c00e3912f | 359 | //Select the register |
sakthipriya | 0:7b4c00e3912f | 360 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 0:7b4c00e3912f | 361 | |
sakthipriya | 0:7b4c00e3912f | 362 | //Read the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 363 | m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 0:7b4c00e3912f | 364 | |
sakthipriya | 0:7b4c00e3912f | 365 | //Return the combined 16-bit value |
sakthipriya | 0:7b4c00e3912f | 366 | return (buff[0] << 8) | buff[1]; |
sakthipriya | 0:7b4c00e3912f | 367 | } |
sakthipriya | 0:7b4c00e3912f | 368 | |
sakthipriya | 3:07e15677a75c | 369 | |
sakthipriya | 1:446a959e36ce | 370 | |
sakthipriya | 3:07e15677a75c | 371 | |
sakthipriya | 0:7b4c00e3912f | 372 | |
sakthipriya | 0:7b4c00e3912f | 373 | void write(char reg, unsigned short data) |
sakthipriya | 0:7b4c00e3912f | 374 | { |
sakthipriya | 0:7b4c00e3912f | 375 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 376 | char buff[3]; |
sakthipriya | 0:7b4c00e3912f | 377 | |
sakthipriya | 0:7b4c00e3912f | 378 | //Load the register address and 16-bit data |
sakthipriya | 0:7b4c00e3912f | 379 | buff[0] = reg; |
sakthipriya | 0:7b4c00e3912f | 380 | buff[1] = data >> 8; |
sakthipriya | 0:7b4c00e3912f | 381 | buff[2] = data; |
sakthipriya | 0:7b4c00e3912f | 382 | |
sakthipriya | 0:7b4c00e3912f | 383 | //Write the data |
sakthipriya | 0:7b4c00e3912f | 384 | m_I2C.write(m_ADDR, buff, 3); |
sakthipriya | 0:7b4c00e3912f | 385 | } |
sakthipriya | 0:7b4c00e3912f | 386 | |
sakthipriya | 0:7b4c00e3912f | 387 | |
sakthipriya | 0:7b4c00e3912f | 388 | |
sakthipriya | 0:7b4c00e3912f | 389 | // Command the MAX17049 to perform a power-on reset |
sakthipriya | 0:7b4c00e3912f | 390 | void reset() |
sakthipriya | 0:7b4c00e3912f | 391 | { |
sakthipriya | 0:7b4c00e3912f | 392 | //Write the POR command |
sakthipriya | 0:7b4c00e3912f | 393 | write(REG_CMD, 0x5400); |
sakthipriya | 0:7b4c00e3912f | 394 | } |
sakthipriya | 0:7b4c00e3912f | 395 | |
sakthipriya | 0:7b4c00e3912f | 396 | // Command the MAX17049 to perform a QuickStart |
sakthipriya | 0:7b4c00e3912f | 397 | void quickStart() |
sakthipriya | 0:7b4c00e3912f | 398 | { |
sakthipriya | 0:7b4c00e3912f | 399 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 400 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 401 | |
sakthipriya | 0:7b4c00e3912f | 402 | //Set the QuickStart bit |
sakthipriya | 0:7b4c00e3912f | 403 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 404 | |
sakthipriya | 0:7b4c00e3912f | 405 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 406 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 407 | } |
sakthipriya | 0:7b4c00e3912f | 408 | |
sakthipriya | 0:7b4c00e3912f | 409 | |
sakthipriya | 0:7b4c00e3912f | 410 | //disable sleep |
sakthipriya | 0:7b4c00e3912f | 411 | void disable_sleep() |
sakthipriya | 0:7b4c00e3912f | 412 | { |
sakthipriya | 0:7b4c00e3912f | 413 | unsigned short value = read(REG_MODE); |
sakthipriya | 0:7b4c00e3912f | 414 | value &= ~(1 << 13); |
sakthipriya | 0:7b4c00e3912f | 415 | write(REG_MODE, value); |
sakthipriya | 0:7b4c00e3912f | 416 | } |
sakthipriya | 0:7b4c00e3912f | 417 | |
sakthipriya | 0:7b4c00e3912f | 418 | //disable the hibernate of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 419 | void disable_hibernate() |
sakthipriya | 0:7b4c00e3912f | 420 | { |
sakthipriya | 0:7b4c00e3912f | 421 | write(REG_HIBRT, 0x0000); |
sakthipriya | 0:7b4c00e3912f | 422 | } |
sakthipriya | 0:7b4c00e3912f | 423 | |
sakthipriya | 0:7b4c00e3912f | 424 | |
sakthipriya | 0:7b4c00e3912f | 425 | // Enable or disable the SOC 1% change alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 426 | void socChangeAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 427 | { |
sakthipriya | 0:7b4c00e3912f | 428 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 429 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 430 | |
sakthipriya | 0:7b4c00e3912f | 431 | //Set or clear the ALSC bit |
sakthipriya | 0:7b4c00e3912f | 432 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 433 | value |= (1 << 6); |
sakthipriya | 0:7b4c00e3912f | 434 | else |
sakthipriya | 0:7b4c00e3912f | 435 | value &= ~(1 << 6); |
sakthipriya | 0:7b4c00e3912f | 436 | |
sakthipriya | 0:7b4c00e3912f | 437 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 438 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 439 | } |
sakthipriya | 0:7b4c00e3912f | 440 | |
sakthipriya | 0:7b4c00e3912f | 441 | |
sakthipriya | 0:7b4c00e3912f | 442 | void compensation(char rcomp) |
sakthipriya | 0:7b4c00e3912f | 443 | { |
sakthipriya | 0:7b4c00e3912f | 444 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 445 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 446 | |
sakthipriya | 0:7b4c00e3912f | 447 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 448 | value &= 0x00FF; |
sakthipriya | 0:7b4c00e3912f | 449 | value |= rcomp << 8; |
sakthipriya | 0:7b4c00e3912f | 450 | |
sakthipriya | 0:7b4c00e3912f | 451 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 452 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 453 | } |
sakthipriya | 0:7b4c00e3912f | 454 | |
sakthipriya | 0:7b4c00e3912f | 455 | |
sakthipriya | 0:7b4c00e3912f | 456 | void tempCompensation(float temp) |
sakthipriya | 0:7b4c00e3912f | 457 | { |
sakthipriya | 0:7b4c00e3912f | 458 | //Calculate the new RCOMP value |
sakthipriya | 0:7b4c00e3912f | 459 | char rcomp; |
sakthipriya | 0:7b4c00e3912f | 460 | if (temp > 20.0) { |
sakthipriya | 0:7b4c00e3912f | 461 | rcomp = RCOMP0 + (temp - 20.0) * -0.5; |
sakthipriya | 0:7b4c00e3912f | 462 | } else { |
sakthipriya | 0:7b4c00e3912f | 463 | rcomp = RCOMP0 + (temp - 20.0) * -5.0; |
sakthipriya | 0:7b4c00e3912f | 464 | } |
sakthipriya | 0:7b4c00e3912f | 465 | |
sakthipriya | 0:7b4c00e3912f | 466 | //Update the RCOMP value |
sakthipriya | 0:7b4c00e3912f | 467 | compensation(rcomp); |
sakthipriya | 0:7b4c00e3912f | 468 | } |
sakthipriya | 0:7b4c00e3912f | 469 | |
sakthipriya | 0:7b4c00e3912f | 470 | // Command the MAX17049 to de-assert the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 471 | void clearAlert() |
sakthipriya | 0:7b4c00e3912f | 472 | { |
sakthipriya | 0:7b4c00e3912f | 473 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 474 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 475 | |
sakthipriya | 0:7b4c00e3912f | 476 | //Clear the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 477 | value &= ~(1 << 5); |
sakthipriya | 0:7b4c00e3912f | 478 | |
sakthipriya | 0:7b4c00e3912f | 479 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 480 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 481 | } |
sakthipriya | 0:7b4c00e3912f | 482 | |
sakthipriya | 0:7b4c00e3912f | 483 | |
sakthipriya | 0:7b4c00e3912f | 484 | //Set the SOC empty alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 485 | void emptyAlertThreshold(char threshold) |
sakthipriya | 0:7b4c00e3912f | 486 | { |
sakthipriya | 0:7b4c00e3912f | 487 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 488 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 489 | |
sakthipriya | 0:7b4c00e3912f | 490 | //Update the register value |
sakthipriya | 0:7b4c00e3912f | 491 | value &= 0xFFE0; |
sakthipriya | 0:7b4c00e3912f | 492 | value |= 32 - threshold; |
sakthipriya | 0:7b4c00e3912f | 493 | |
sakthipriya | 0:7b4c00e3912f | 494 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 495 | write(REG_CONFIG, value); |
sakthipriya | 0:7b4c00e3912f | 496 | } |
sakthipriya | 0:7b4c00e3912f | 497 | |
sakthipriya | 0:7b4c00e3912f | 498 | // Set the low and high voltage alert threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 499 | void vAlertMinMaxThreshold() |
sakthipriya | 0:7b4c00e3912f | 500 | { |
sakthipriya | 0:7b4c00e3912f | 501 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 502 | unsigned short value = read(REG_VALRT); |
sakthipriya | 0:7b4c00e3912f | 503 | |
sakthipriya | 0:7b4c00e3912f | 504 | //Mask off the old value |
sakthipriya | 0:7b4c00e3912f | 505 | |
sakthipriya | 0:7b4c00e3912f | 506 | value = 0x96D2; |
sakthipriya | 0:7b4c00e3912f | 507 | |
sakthipriya | 0:7b4c00e3912f | 508 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 509 | write(REG_VALRT, value); |
sakthipriya | 0:7b4c00e3912f | 510 | } |
sakthipriya | 0:7b4c00e3912f | 511 | |
sakthipriya | 0:7b4c00e3912f | 512 | |
sakthipriya | 0:7b4c00e3912f | 513 | // Set the reset voltage threshold of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 514 | void vResetThresholdSet() |
sakthipriya | 0:7b4c00e3912f | 515 | { |
sakthipriya | 0:7b4c00e3912f | 516 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 517 | unsigned short value = read(REG_VRESET_ID); |
sakthipriya | 0:7b4c00e3912f | 518 | |
sakthipriya | 0:7b4c00e3912f | 519 | //Mask off the old //value |
sakthipriya | 0:7b4c00e3912f | 520 | value &= 0x00FF;//Dis=0 |
sakthipriya | 0:7b4c00e3912f | 521 | |
sakthipriya | 0:7b4c00e3912f | 522 | value |= 0x9400;//corresponding to 2.5 V |
sakthipriya | 0:7b4c00e3912f | 523 | |
sakthipriya | 0:7b4c00e3912f | 524 | |
sakthipriya | 0:7b4c00e3912f | 525 | //Write the 16-bit register |
sakthipriya | 0:7b4c00e3912f | 526 | write(REG_VRESET_ID, value); |
sakthipriya | 0:7b4c00e3912f | 527 | } |
sakthipriya | 0:7b4c00e3912f | 528 | |
sakthipriya | 0:7b4c00e3912f | 529 | |
sakthipriya | 0:7b4c00e3912f | 530 | // Enable or disable the voltage reset alert on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 531 | void vResetAlertEnabled(bool enabled) |
sakthipriya | 0:7b4c00e3912f | 532 | { |
sakthipriya | 0:7b4c00e3912f | 533 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 534 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 535 | |
sakthipriya | 0:7b4c00e3912f | 536 | //Set or clear the EnVR bit |
sakthipriya | 0:7b4c00e3912f | 537 | if (enabled) |
sakthipriya | 0:7b4c00e3912f | 538 | value |= (1 << 14); |
sakthipriya | 0:7b4c00e3912f | 539 | else |
sakthipriya | 0:7b4c00e3912f | 540 | value &= ~(1 << 14); |
sakthipriya | 0:7b4c00e3912f | 541 | |
sakthipriya | 0:7b4c00e3912f | 542 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 543 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 544 | } |
sakthipriya | 0:7b4c00e3912f | 545 | |
sakthipriya | 0:7b4c00e3912f | 546 | //Get the current alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 547 | //refer datasheet-status registers section to decode it. |
sakthipriya | 0:7b4c00e3912f | 548 | char alertFlags() |
sakthipriya | 0:7b4c00e3912f | 549 | { |
sakthipriya | 0:7b4c00e3912f | 550 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 551 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 552 | |
sakthipriya | 0:7b4c00e3912f | 553 | //Return only the flag bits |
sakthipriya | 0:7b4c00e3912f | 554 | return (value >> 8) & 0x3F; |
sakthipriya | 0:7b4c00e3912f | 555 | } |
sakthipriya | 0:7b4c00e3912f | 556 | |
sakthipriya | 0:7b4c00e3912f | 557 | // Clear all the alert flags on the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 558 | void clearAlertFlags() |
sakthipriya | 0:7b4c00e3912f | 559 | { |
sakthipriya | 0:7b4c00e3912f | 560 | //Read the current 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 561 | unsigned short value = read(REG_STATUS); |
sakthipriya | 0:7b4c00e3912f | 562 | |
sakthipriya | 0:7b4c00e3912f | 563 | //Clear the specified flag bits |
sakthipriya | 0:7b4c00e3912f | 564 | value &= ~( 0x3F<< 8); |
sakthipriya | 0:7b4c00e3912f | 565 | |
sakthipriya | 0:7b4c00e3912f | 566 | //Write the value back out |
sakthipriya | 0:7b4c00e3912f | 567 | write(REG_STATUS, value); |
sakthipriya | 0:7b4c00e3912f | 568 | } |
sakthipriya | 0:7b4c00e3912f | 569 | |
sakthipriya | 0:7b4c00e3912f | 570 | // Get the current cell voltage measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 571 | float vcell() |
sakthipriya | 0:7b4c00e3912f | 572 | { |
sakthipriya | 1:446a959e36ce | 573 | |
sakthipriya | 0:7b4c00e3912f | 574 | //Read the 16-bit raw Vcell value |
sakthipriya | 0:7b4c00e3912f | 575 | unsigned short value = read(REG_VCELL); |
sakthipriya | 0:7b4c00e3912f | 576 | |
sakthipriya | 0:7b4c00e3912f | 577 | //Return Vcell in volts |
sakthipriya | 0:7b4c00e3912f | 578 | return value * 0.000078125*2; |
sakthipriya | 0:7b4c00e3912f | 579 | } |
sakthipriya | 0:7b4c00e3912f | 580 | |
sakthipriya | 0:7b4c00e3912f | 581 | // Get the current state of charge measurement of the MAX17049 as a float |
sakthipriya | 0:7b4c00e3912f | 582 | float soc() |
sakthipriya | 0:7b4c00e3912f | 583 | { |
sakthipriya | 3:07e15677a75c | 584 | |
sakthipriya | 3:07e15677a75c | 585 | //Create a temporary buffer |
sakthipriya | 0:7b4c00e3912f | 586 | char buff[2]; |
sakthipriya | 3:07e15677a75c | 587 | int ack = 1; |
sakthipriya | 3:07e15677a75c | 588 | //Select the register |
sakthipriya | 3:07e15677a75c | 589 | char reg = REG_SOC; // cannot pass the hash defined values directly |
sakthipriya | 3:07e15677a75c | 590 | m_I2C.write(m_ADDR, ®, 1, true); |
sakthipriya | 3:07e15677a75c | 591 | |
sakthipriya | 3:07e15677a75c | 592 | |
sakthipriya | 3:07e15677a75c | 593 | //Read the 16-bit register |
sakthipriya | 3:07e15677a75c | 594 | |
sakthipriya | 3:07e15677a75c | 595 | ack = m_I2C.read(m_ADDR, buff, 2); |
sakthipriya | 3:07e15677a75c | 596 | |
sakthipriya | 3:07e15677a75c | 597 | printf("\n\r acknow %d", ack); |
sakthipriya | 0:7b4c00e3912f | 598 | |
sakthipriya | 0:7b4c00e3912f | 599 | //Return SOC in percent |
sakthipriya | 2:c823d84b4cb0 | 600 | if(ack == 0) |
sakthipriya | 3:07e15677a75c | 601 | return ((buff[0] << 8) | buff[1]) * 0.00390625; |
sakthipriya | 2:c823d84b4cb0 | 602 | else |
sakthipriya | 2:c823d84b4cb0 | 603 | return 200; |
sakthipriya | 0:7b4c00e3912f | 604 | } |
sakthipriya | 0:7b4c00e3912f | 605 | |
sakthipriya | 0:7b4c00e3912f | 606 | |
sakthipriya | 0:7b4c00e3912f | 607 | |
sakthipriya | 0:7b4c00e3912f | 608 | // Get the current C rate measurement of the MAX17049 |
sakthipriya | 0:7b4c00e3912f | 609 | float crate() |
sakthipriya | 0:7b4c00e3912f | 610 | { |
sakthipriya | 0:7b4c00e3912f | 611 | //Read the 16-bit raw C/Rate value |
sakthipriya | 0:7b4c00e3912f | 612 | short value = read(REG_CRATE); |
sakthipriya | 0:7b4c00e3912f | 613 | |
sakthipriya | 0:7b4c00e3912f | 614 | //Return C/Rate in %/hr |
sakthipriya | 0:7b4c00e3912f | 615 | return value * 0.208; |
sakthipriya | 0:7b4c00e3912f | 616 | } |
sakthipriya | 0:7b4c00e3912f | 617 | |
sakthipriya | 0:7b4c00e3912f | 618 | // Determine whether or not the MAX17049 is asserting the ALRT pin |
sakthipriya | 0:7b4c00e3912f | 619 | bool alerting() |
sakthipriya | 0:7b4c00e3912f | 620 | { |
sakthipriya | 0:7b4c00e3912f | 621 | //Read the 16-bit register value |
sakthipriya | 0:7b4c00e3912f | 622 | unsigned short value = read(REG_CONFIG); |
sakthipriya | 0:7b4c00e3912f | 623 | |
sakthipriya | 0:7b4c00e3912f | 624 | //Return the status of the ALRT bit |
sakthipriya | 0:7b4c00e3912f | 625 | if (value & (1 << 5)) |
sakthipriya | 0:7b4c00e3912f | 626 | return true; |
sakthipriya | 0:7b4c00e3912f | 627 | else |
sakthipriya | 0:7b4c00e3912f | 628 | return false; |
sakthipriya | 2:c823d84b4cb0 | 629 | } |
sakthipriya | 2:c823d84b4cb0 | 630 | |
sakthipriya | 2:c823d84b4cb0 | 631 | //.............................Battery board Temp sensor........................// |
sakthipriya | 2:c823d84b4cb0 | 632 | void FCTN_BATTTEMP_INIT() |
sakthipriya | 2:c823d84b4cb0 | 633 | { |
sakthipriya | 2:c823d84b4cb0 | 634 | ssn1=1;ssn2=1; |
sakthipriya | 2:c823d84b4cb0 | 635 | //PS=0; |
sakthipriya | 2:c823d84b4cb0 | 636 | //HS=0; |
sakthipriya | 2:c823d84b4cb0 | 637 | spi_bt.format(8,3); |
sakthipriya | 2:c823d84b4cb0 | 638 | spi_bt.frequency(1000000); |
sakthipriya | 2:c823d84b4cb0 | 639 | } |
sakthipriya | 2:c823d84b4cb0 | 640 | |
sakthipriya | 2:c823d84b4cb0 | 641 | void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2]) |
sakthipriya | 2:c823d84b4cb0 | 642 | { |
sakthipriya | 2:c823d84b4cb0 | 643 | uint8_t MSB, LSB; |
sakthipriya | 2:c823d84b4cb0 | 644 | int16_t bit_data; |
sakthipriya | 2:c823d84b4cb0 | 645 | float sensitivity=0.0078125; //1 LSB = sensitivity degree celcius |
sakthipriya | 2:c823d84b4cb0 | 646 | wait_ms(320); |
sakthipriya | 2:c823d84b4cb0 | 647 | ssn1=0; |
sakthipriya | 2:c823d84b4cb0 | 648 | |
sakthipriya | 2:c823d84b4cb0 | 649 | spi_bt.write(0x80);//Reading digital data from Sensor 1 |
sakthipriya | 2:c823d84b4cb0 | 650 | LSB = spi_bt.write(0x00);//LSB first |
sakthipriya | 2:c823d84b4cb0 | 651 | wait_ms(0); |
sakthipriya | 2:c823d84b4cb0 | 652 | MSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 653 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 654 | pc_eps.printf("%d %d\n",MSB,LSB); |
sakthipriya | 2:c823d84b4cb0 | 655 | bit_data= ((uint16_t)MSB<<8)|LSB; |
sakthipriya | 2:c823d84b4cb0 | 656 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 657 | temp[0]=(float)bit_data*sensitivity;//Converting into decimal value |
sakthipriya | 2:c823d84b4cb0 | 658 | ssn1=1; |
sakthipriya | 2:c823d84b4cb0 | 659 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 660 | ssn2=0;//Reading data from sensor 2 |
sakthipriya | 2:c823d84b4cb0 | 661 | spi_bt.write(0x80); |
sakthipriya | 2:c823d84b4cb0 | 662 | LSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 663 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 664 | MSB = spi_bt.write(0x00); |
sakthipriya | 2:c823d84b4cb0 | 665 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 666 | bit_data= ((int16_t)MSB<<8)|LSB; |
sakthipriya | 2:c823d84b4cb0 | 667 | wait_ms(10); |
sakthipriya | 2:c823d84b4cb0 | 668 | temp[1]=(float)bit_data*sensitivity; |
sakthipriya | 2:c823d84b4cb0 | 669 | ssn2=1; |
sakthipriya | 2:c823d84b4cb0 | 670 | |
sakthipriya | 2:c823d84b4cb0 | 671 | } |