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