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