latest BAE code 10 dec

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of RAJANGAM_REVIEW_BAE_CODE by Team Fox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EPS.cpp Source File

EPS.cpp

00001 #include "EPS.h"
00002 #include "pin_config.h"
00003 #include "iostream"
00004 
00005 Serial eps_pc(USBTX,USBRX);
00006 
00007 Timer timer_alertFlags;
00008 Timer timer_soc;
00009 Timer timer_FCTN_BATTERYGAUGE_INIT;
00010 
00011 //FOR APPEDING HK DATA===================================
00012 
00013 extern uint16_t crc_hk_data();
00014 
00015 //acs
00016 extern uint8_t ACS_DETUMBLING_ALGO_TYPE;
00017 extern uint8_t ACS_STATE;
00018 extern uint8_t ACS_MAG_TIME_DELAY;
00019 extern uint8_t ACS_DEMAG_TIME_DELAY;
00020 extern uint8_t ACS_Z_FIXED_MOMENT;
00021 extern uint16_t ACS_MM_X_COMSN;
00022 extern uint16_t ACS_MM_Y_COMSN;
00023 extern uint16_t ACS_MG_X_COMSN;
00024 extern uint16_t ACS_MG_Y_COMSN;
00025 extern uint16_t ACS_MM_Z_COMSN;
00026 extern uint16_t ACS_MG_Z_COMSN;
00027 extern uint8_t ACS_MAIN_STATUS;
00028 extern uint8_t ACS_STATUS;
00029 extern uint8_t ATS1_EVENT_STATUS_RGTR;
00030 extern uint8_t ATS1_SENTRAL_STATUS_RGTR;
00031 extern uint8_t ATS1_ERROR_RGTR;
00032 extern uint8_t ATS2_EVENT_STATUS_RGTR;
00033 extern uint8_t ATS2_SENTRAL_STATUS_RGTR;
00034 extern uint8_t ATS2_ERROR_RGTR;
00035 extern uint8_t ACS_DATA_ACQ_STATUS;
00036 extern uint8_t ACS_TR_X_PWM;
00037 extern uint8_t ACS_TR_Y_PWM;
00038 extern uint8_t ACS_TR_Z_PWM;
00039 extern uint8_t alarmmode;
00040 extern uint8_t controlmode_mms;
00041 extern uint8_t invjm_mms[9];
00042 extern uint8_t jm_mms[9];
00043 extern uint8_t bb_mms[3];
00044 extern uint8_t singularity_flag_mms;
00045 extern uint8_t B_SCZ_ANGLE;
00046 
00047 
00048 //bcn
00049 extern uint8_t BCN_SPND_TX;
00050 extern uint8_t BCN_FEN;
00051 extern uint8_t BCN_LONG_MSG_TYPE;
00052 extern uint8_t BCN_TX_MAIN_STATUS; 
00053 extern uint8_t BCN_TX_STATUS;
00054 extern uint8_t BCN_INIT_STATUS;
00055 extern uint8_t BCN_FAIL_COUNT;
00056 extern uint16_t BCN_TX_MAIN_COUNTER;
00057 extern uint8_t BCN_TX_MAIN_STATUS;
00058 
00059 //bae
00060 extern Timer BAE_uptime;
00061 extern Timer I2C_last;
00062 extern uint8_t BAE_RESET_COUNTER;
00063 extern uint8_t BAE_INIT_STATUS;
00064 extern uint8_t BAE_STANDBY;
00065 extern uint16_t BAE_I2C_COUNTER;
00066 extern void RETURN_UPTIME(float time, uint8_t *day,uint8_t *hour,uint8_t *min);
00067 
00068 //eps
00069 extern uint8_t ACS_INIT_STATUS;
00070 extern uint16_t EPS_MAIN_COUNTER;
00071 extern uint8_t EPS_BTRY_TMP_STATUS;
00072 
00073 
00074 //main
00075 extern uint8_t HTR_CYCLE_COUNTER;
00076 extern uint16_t ACS_MAIN_COUNTER;
00077 extern uint8_t float_to_uint8(float min,float max,float val);
00078 
00079 //=======================================================
00080 
00081 
00082 /***********************************************global variable declaration***************************************************************/
00083 extern uint32_t BAE_STATUS;
00084 extern uint32_t BAE_ENABLE;
00085 extern uint8_t LONG_HK_data[2][134];
00086 //extern uint8_t BAE_HK_data[134];
00087 //extern char BAE_chardata[74]; 
00088 
00089 //implement them assign them default values  
00090 uint8_t EPS_BATT_TEMP_LOW;
00091 uint8_t EPS_BATT_TEMP_HIGH;
00092 uint8_t EPS_BATT_TEMP_DEFAULT;
00093 uint8_t EPS_SOC_LEVEL_12 = 70;
00094 uint8_t EPS_SOC_LEVEL_23 = 90;
00095 uint8_t EPS_BAT_TEMP_LOW;
00096 uint8_t EPS_BAT_TEMP_HIGH;
00097 uint8_t EPS_BAT_TEMP_DEFAULT;
00098 DigitalIn EPS_CHARGER_FAULT(PIN42);
00099 DigitalIn EPS_CHARGER_STATUS(PIN31);
00100 DigitalIn EPS_BATTERY_GAUGE_ALERT(PIN73);
00101 
00102 //i2c.frequency(10000)
00103 extern int eps_btg_read_flag;  // flag to check I2C ack on reading from BTG: 1 indicates read failure, 0 indicates success
00104 extern int eps_btg_writ_flag;  // flag to check I2C ack on writing from BTG: 1 indicates write failure, 0 indicates success
00105 
00106 const char RCOMP0= 0x97;// don't know what it is now 
00107 BAE_HK_actual actual_data;
00108 BAE_HK_quant quant_data;
00109 BAE_HK_min_max bae_HK_minmax;
00110 BAE_HK_arch arch_data;
00111 
00112 /************************battery temperature var*********************************/
00113 //instead just return the approprate value.. test it
00114 
00115 //......................................Peripheral declarations.........................................................//
00116 Serial pc_eps(USBTX,USBRX);
00117 
00118 extern I2C i2c;
00119 
00120 //any default values or states
00121 //====================================
00122 //DigitalInOut TRXY(TRXY_DR_EN);            //active high
00123 //DigitalInOut TRZ(TRZ_DR_EN);              //active high
00124 DigitalOut EN3V3A(ENBL3V3A);
00125 //====================================
00126 
00127 DigitalOut BTRY_HTR_ENABLE(BATT_HEAT);// earlier BTRY_HTR_ENABLE
00128 //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT);
00129 //AnalogIn Vbatt_ang(VBATT);
00130 AnalogIn Batt_voltage(PIN20);   //Battery voltage
00131 
00132 SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK // battery temp something 3
00133 DigitalOut ssn1(PIN19); //Slave select1 // low line master talks
00134 DigitalOut ssn2(PIN21);//Slave select2
00135 //DigitalOut PS(PTB0);
00136 //DigitalOut HS(PTB1);
00137 
00138 AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54
00139 AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53
00140 AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor
00141 
00142 /*mux for reading value one by one*/
00143 DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines
00144 DigitalOut SelectLinea2 (PIN45);
00145 DigitalOut SelectLinea1 (PIN44);
00146 DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines
00147 
00148 DigitalOut SelectLineb0 (PIN56); // MSB of Select Lines
00149 DigitalOut SelectLineb1 (PIN57);
00150 DigitalOut SelectLineb2 (PIN58);
00151 DigitalOut SelectLineb3 (PIN59); // LSB of Select Lines
00152 
00153 //*********************************************************flags********************************************************//
00154 extern uint8_t EPS_INIT_STATUS ;
00155 extern uint8_t EPS_BATTERY_GAUGE_STATUS ;
00156 extern uint8_t EPS_MAIN_STATUS;
00157 extern uint8_t EPS_BTRY_TMP_STATUS ;
00158 extern uint8_t EPS_STATUS ;
00159 
00160 extern uint8_t EPS_BTRY_HTR ;
00161 
00162 //eps cdms fault
00163 extern uint8_t CDMS_SW_STATUS;
00164 extern DigitalIn CDMS_OC_FAULT;
00165 extern DigitalOut CDMS_RESET;               // CDMS RESET
00166 //extern bool CDMS_SW_ENABLE;                 //Temp name for CDMS_RESET pin
00167 extern int CDMS_FAULT_COUNTER;
00168 extern uint8_t EPS_BTRY_HTR_AUTO;
00169 
00170 //eps hw faults
00171 
00172 /********* EXTERN ACS VAR ********************/
00173 extern uint8_t ACS_ATS_STATUS;
00174 extern uint8_t ACS_TR_Z_SW_STATUS;
00175 extern DigitalOut ACS_TR_Z_ENABLE;
00176 extern DigitalIn ACS_TR_Z_OC_FAULT;
00177 extern DigitalIn ACS_TR_Z_FAULT;            //Driver IC fault
00178 extern int ACS_TR_Z_FAULT_COUNTER;
00179 
00180 extern uint8_t ACS_TR_XY_SW_STATUS;
00181 extern DigitalOut ACS_TR_XY_ENABLE;
00182 extern DigitalIn ACS_TR_XY_OC_FAULT;
00183 extern DigitalIn ACS_TR_XY_FAULT;            //Driver IC fault
00184 extern int ACS_TR_XY_FAULT_COUNTER;
00185 
00186 //extern uint8_t ACS_ATS1_SW_STATUS;
00187 extern DigitalOut ATS1_SW_ENABLE;
00188 extern DigitalIn ACS_ATS1_OC_FAULT;
00189 extern int ACS_ATS1_FAULT_COUNTER;
00190 
00191 //extern uint8_t ACS_ATS2_SW_STATUS;
00192 
00193 extern DigitalOut ATS2_SW_ENABLE;
00194 extern DigitalIn ACS_ATS2_OC_FAULT;
00195 extern int ACS_ATS2_FAULT_COUNTER;
00196 
00197 /********* EXTERN BCN VAR ********************/
00198 extern DigitalOut BCN_SW;       //Beacon switch
00199 extern uint8_t BCN_TX_SW_STATUS;
00200 //extern bool BCN_TX_ENABLE;    //Temp variable in place of BCN_SW
00201 extern DigitalIn BCN_TX_OC_FAULT;
00202 extern int BCN_TX_FAULT_COUNTER;
00203 extern uint8_t BCN_TMP;
00204 
00205 //........................................... FUCTIONS.................................................//
00206 
00207 void FCTN_EPS_INIT()
00208 {
00209 ////    eps_pc.printf("\n\r eps init \n");
00210     EPS_INIT_STATUS = 1 ;             //set EPS_INIT_STATUS flag
00211     // FLAG();
00212     FCTN_BATTERYGAUGE_INIT();
00213     EN3V3A = 1;                             //enable dc dc converter A  
00214     
00215     timer_alertFlags.reset();
00216     timer_alertFlags.start();
00217     char value=alertFlags(); // initialization part of battery gauge
00218     timer_alertFlags.stop();
00219     
00220     unsigned short value_u= (short int )value;
00221     //value_u &=0x0001;                     
00222     if(value_u & 0x0001 == 0x0001 )//&& eps_btg_read_flag == 0)                       // battery gauge not initialised
00223     {
00224         
00225         actual_data.power_mode = 0;                 // indicates btg not working / NA
00226         EPS_BATTERY_GAUGE_STATUS = 0;               //clear EPS_BATTERY_GAUGE_STATUS
00227         eps_pc.printf(" init BTG fail - %d\n\r", value_u);
00228     }
00229     else
00230     {
00231         timer_soc.reset();
00232         timer_soc.start();
00233         actual_data.Batt_gauge_actual[1] = soc();
00234         timer_soc.stop();
00235         
00236         actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3; //1 corresponds to 3.3 scaling factor
00237         FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);
00238         EPS_BATTERY_GAUGE_STATUS = 1;               //set EPS_BATTERY_GAUGE_STATUS
00239         eps_pc.printf("init BTG success - %d\n\r", value_u);
00240     }
00241     
00242     //if( read(REG_VERSION) == 
00243     eps_pc.printf("REG_VERSION = %d\r\n",read(REG_VERSION));
00244    
00245     FCTN_BATTTEMP_INIT();
00246     //EPS_BATTERY_GAUGE_STATUS = 1;
00247     
00248     EPS_INIT_STATUS = 0 ;             //clear EPS_INIT_STATUS flag
00249 
00250 }
00251 
00252 void FCTN_EPS_HANDLE_CDMS_FAULT()
00253 {   //Variable names from MMS structure, if not, marked as //Temp name
00254 
00255 /*now if this solves the problem then turn it back and observe if it works if doesn't then chane at other places also */
00256   
00257     if(CDMS_SW_STATUS == 0b11)/*changed to just hex representation see if this solve the purpose->earlier 0b11*/          //powered on and oc fault
00258         if(CDMS_OC_FAULT) // !CDMS_OC_FAULT 0 means badman, 1 means good man // changed   
00259            CDMS_SW_STATUS = 0b01;//chaged  from 0b01;       //powered on and working
00260     else
00261     {
00262         if(CDMS_SW_STATUS == 0b10) //changed 0b10)      //powered off and oc fault        
00263             CDMS_RESET = 1;         
00264         if(CDMS_OC_FAULT == 1) //changed
00265         {
00266             CDMS_FAULT_COUNTER = 0;     //Temp name
00267             CDMS_SW_STATUS = 0b01; //changed->0b01;      //powered on and working
00268         }
00269         else
00270         {
00271             CDMS_FAULT_COUNTER++;
00272             if(CDMS_FAULT_COUNTER == 3)
00273                 CDMS_SW_STATUS = 0b11; //changed->0b11;  //powered on and oc fault
00274             else
00275             {
00276                 CDMS_RESET = 0;    //power OFF switch
00277                 CDMS_SW_STATUS = 0b10; //chaged->0b10;      //powered off and oc fault   
00278             }
00279         }
00280     }     
00281 }
00282 
00283 void FCTN_EPS_HANDLE_HW_FAULTS()
00284 {   //Variable names from MMS structure, if not, marked as //Temp name
00285     
00286     //--------ACS_TR_Z--------//
00287     if(ACS_TR_Z_SW_STATUS != 0b11)          //!disabled
00288     {
00289         if(ACS_TR_Z_SW_STATUS == 0b10)      //oc fault and powered off
00290             ACS_TR_Z_ENABLE = 0;
00291         //printf("ACS_TR_Z_OC_FAULT  : %d  \r\n",(int)ACS_TR_Z_OC_FAULT.read());
00292         //printf("ACS_TR_Z_FAULT  : %d  \r\n",(int)ACS_TR_Z_FAULT.read());
00293         if( (ACS_TR_Z_OC_FAULT == 0) || (ACS_TR_Z_FAULT==0))
00294         {
00295             ACS_TR_Z_ENABLE = 1;
00296             ACS_TR_Z_FAULT_COUNTER++;       //Temp name
00297             if(ACS_TR_Z_FAULT_COUNTER == 3)
00298                 ACS_TR_Z_SW_STATUS = 0b11;  //disabled
00299                 //update in flash as default state at bootup
00300             else ACS_TR_Z_SW_STATUS = 0b10; //oc fault and powered off
00301         }
00302         else
00303         {
00304             ACS_TR_Z_SW_STATUS = 0b01;      //powered on and working;
00305             //update in flash also
00306             ACS_TR_Z_FAULT_COUNTER = 0;
00307         }
00308     }
00309     
00310     //--------ACS_TR_XY--------//
00311     //Same as ACS_TR_Z, just replace Z with XY
00312         if(ACS_TR_XY_SW_STATUS != 0b11)          //!disabled
00313     {
00314         if(ACS_TR_XY_SW_STATUS == 0b10)      //oc fault and powered off
00315             ACS_TR_XY_ENABLE = 0;
00316             
00317         //printf("ACS_TR_XY_OC_FAULT  : %d  \r\n",(int)ACS_TR_XY_OC_FAULT.read());
00318         //printf("ACS_TR_XY_FAULT  : %d  \r\n",(int)ACS_TR_XY_FAULT.read());
00319         if( (ACS_TR_XY_OC_FAULT==0) || (ACS_TR_XY_FAULT==0))
00320         {
00321             ACS_TR_XY_ENABLE = 1;
00322             ACS_TR_XY_FAULT_COUNTER++;       //Temp name
00323             if(ACS_TR_XY_FAULT_COUNTER == 3)
00324                 ACS_TR_XY_SW_STATUS = 0b11;  //disabled
00325                 //update in flash as default state at bootup
00326             else ACS_TR_XY_SW_STATUS = 0b10; //oc fault and powered off
00327         }
00328         else
00329         {
00330             ACS_TR_XY_SW_STATUS = 0b01;      //powered on and working;
00331             //update in flash also
00332             ACS_TR_XY_FAULT_COUNTER = 0;
00333         }
00334     }    
00335 
00336     //--------ACS_ATS1--------//
00337     //Same as ACS_ATS2
00338     //if(ACS_ATS1_SW_STATUS & 0b1100 != 0b1100)          //!disabled 
00339     if(ACS_ATS_STATUS&0xC0!=0xC0)
00340     {
00341         //if(ACS_ATS1_SW_STATUS & 0b1100 == 0b1000)      //oc fault and powered off
00342         if(ACS_ATS_STATUS&0xC0!=0x80)
00343             ATS1_SW_ENABLE = 1;                       
00344         //if(ACS_ATS1_OC_FAULT)
00345         if(0)
00346         {
00347             ATS1_SW_ENABLE = 0;
00348             ACS_ATS1_FAULT_COUNTER++;                  //Temp name
00349             if(ACS_ATS1_FAULT_COUNTER == 3)
00350                 //ACS_ATS1_SW_STATUS = ACS_ATS1_SW_STATUS | 0b1100;  //disabled
00351                 ACS_ATS_STATUS = (ACS_ATS_STATUS&0x0F)|0xC0;
00352                 //update in flash as default state at bootup
00353             else 
00354             {
00355                 //ACS_ATS1_SW_STATUS = ACS_ATS1_SW_STATUS | 0b1000; //setting to 10xx
00356                 ACS_ATS_STATUS = (ACS_ATS_STATUS&0x0F)|0x80;
00357                 //ACS_ATS1_SW_STATUS = ACS_ATS1_SW_STATUS & 0b1011; //oc fault and powered off
00358             }
00359         }
00360         else
00361         {
00362             //if(ACS_ATS1_SW_STATUS & 0b1100 == 0b1000)             //Device oc fault?
00363             if(ACS_ATS_STATUS&0xC0==0x80)
00364                 ATS1_SW_ENABLE = 0;
00365             //ACS_ATS1_SW_STATUS = ACS_ATS1_SW_STATUS & 0b0011;     //working but powered off
00366             ACS_ATS_STATUS = ACS_ATS_STATUS&0x3F;
00367             //Update in flash also
00368             ACS_ATS1_FAULT_COUNTER = 0;
00369         }
00370     }
00371     
00372     //--------ACS_ATS2--------//
00373     //if(ACS_ATS2_SW_STATUS & 0b1100 != 0b1100)          //!disabled
00374     if(ACS_ATS_STATUS&0x0C!=0x0C)
00375     {
00376         //if(ACS_ATS2_SW_STATUS & 0b1100 == 0b1000)      //oc fault and powered off
00377         if(ACS_ATS_STATUS&0x0C!=0x08)
00378             ATS2_SW_ENABLE = 0;                       
00379         //if(ACS_ATS2_OC_FAULT)
00380         if(0)
00381         {
00382             ATS2_SW_ENABLE = 1;
00383             ACS_ATS2_FAULT_COUNTER++;                  //Temp name
00384             if(ACS_ATS2_FAULT_COUNTER == 3)
00385                 //ACS_ATS2_SW_STATUS = ACS_ATS2_SW_STATUS | 0b1100;  //disabled
00386                 ACS_ATS_STATUS = (ACS_ATS_STATUS&0xF0)|0x0C;
00387                 //update in flash as default state at bootup
00388             else 
00389             {
00390                 //ACS_ATS2_SW_STATUS = ACS_ATS2_SW_STATUS | 0b1000; //setting to 10xx
00391                 //ACS_ATS2_SW_STATUS = ACS_ATS2_SW_STATUS & 0b1011; //oc fault and powered off
00392                 ACS_ATS_STATUS = (ACS_ATS_STATUS&0xF0)|0x08;
00393             }
00394         }
00395         else
00396         {
00397             //if(ACS_ATS2_SW_STATUS & 0b1100 == 0b1000)             //Device oc fault?
00398             if(ACS_ATS_STATUS&0x0C==0x08)
00399                 ATS2_SW_ENABLE = 1;
00400             //ACS_ATS2_SW_STATUS = ACS_ATS2_SW_STATUS & 0b0011;     //working but powered off
00401             ACS_ATS_STATUS = ACS_ATS_STATUS&0xF3;
00402             //Update in flash also
00403             ACS_ATS2_FAULT_COUNTER = 0;
00404         }
00405     }
00406     
00407     
00408     
00409     printf("");
00410     
00411     
00412     //--------BCN_TX----------//
00413     
00414     if(BCN_TX_SW_STATUS != 0b11)          //!disabled
00415     {
00416         if(BCN_TX_SW_STATUS == 0b10)      //oc fault and powered off
00417             BCN_SW = 1;           
00418         if(BCN_TX_OC_FAULT==0)
00419         {
00420             BCN_SW = 0;
00421             BCN_TX_FAULT_COUNTER++;       //Temp name
00422             if(BCN_TX_FAULT_COUNTER == 3)
00423                 BCN_TX_SW_STATUS = 0b11;  //disabled
00424                 //update in flash as default state at bootup
00425             else BCN_TX_SW_STATUS = 0b10; //oc fault and powered off
00426                 
00427         }
00428         else
00429         {
00430             BCN_TX_SW_STATUS = 0b01;      //powered on and working;
00431             //update in flash also
00432             BCN_TX_FAULT_COUNTER = 0;
00433         }
00434     }
00435     
00436 }
00437 
00438 //----------------------------------------------------Power algo code--------------------------------------------------------------------//
00439 /*update the power modes*/
00440 
00441 void FCTN_EPS_POWERMODE(float soc)              //dummy algo
00442 {
00443     if(soc >= EPS_SOC_LEVEL_23 )
00444         actual_data.power_mode = 3;
00445     else if(soc >= EPS_SOC_LEVEL_12 )
00446        actual_data.power_mode = 2;
00447     else actual_data.power_mode = 1;
00448 }
00449 
00450 //...................................................HK...........................................//
00451 /*reading values*/
00452 
00453 void FCTN_HK_MAIN()
00454 {
00455     int Iteration=0;
00456 
00457     SelectLinea0=0;
00458     SelectLinea1=0;
00459     SelectLinea2=0;
00460     SelectLinea3=0;
00461 
00462     SelectLineb0=0;
00463     SelectLineb1=0;
00464     SelectLineb2=0;
00465     SelectLineb3=0;
00466     
00467       //collecting data
00468     for(Iteration=0; Iteration<16; Iteration++)
00469     {
00470             actual_data.voltage_actual[Iteration]=VoltageInput.read();
00471             //wait_ms(100); //remove
00472            /// eps_pc.printf("\n\rthe raw voltage values VOL[%d] = %f",Iteration,actual_data.voltage_actual[Iteration]);
00473             wait_ms(2);
00474             actual_data.current_actual[Iteration]=CurrentInput.read();
00475             wait_ms(2);
00476             //eps_pc.printf("\n\rthe raw current values CURR[%d] = %f",Iteration,actual_data.current_actual[Iteration]);
00477                       
00478             SelectLinea0=!(SelectLinea0);
00479             if(Iteration%2==1)
00480                 SelectLinea1=!(SelectLinea1);
00481             if(Iteration%4==3)
00482                 SelectLinea2=!(SelectLinea2);
00483             if(Iteration%8==7)
00484                 SelectLinea3=!(SelectLinea3);
00485                 int s0,s1,s2,s3;
00486             s0=SelectLineb0=SelectLinea0;
00487             s1=SelectLineb1=SelectLinea2;
00488             s2=SelectLineb2=SelectLinea2;
00489             s3=SelectLineb3=SelectLinea3;
00490 ////            printf("\n\r  %d %d %d %d", s0,s1,s2,s3);
00491 
00492     wait_ms(1);
00493     /*$ wait of 3 milli sec $*/
00494     }
00495       for(Iteration=0; Iteration<16; Iteration++)
00496     {
00497         if(Iteration==1) //EPS 3.0 in EPS 2.1 it was 14th element
00498         actual_data.voltage_actual[Iteration]= (-90.7*(3.3*actual_data.voltage_actual[Iteration]))+190.1543;
00499         else
00500          actual_data.voltage_actual[Iteration] = (actual_data.voltage_actual[Iteration]*3.3*62)/11.0;
00501               
00502       //  eps_pc.printf("VVVoltage[%d] %f\r\n",Iteration,actual_data.voltage_actual[Iteration]);
00503     }
00504         
00505     for(Iteration=0;Iteration<16;Iteration++){
00506         if(Iteration<8)// thermistors before in version 2.1 now from12th element but 8-11 are null values only
00507         {
00508             actual_data.current_actual[Iteration]= 1000*(actual_data.current_actual[Iteration]*3.3)/(50*rsens); 
00509             // if it gives the better resolution //actual_data.current_actual[Iteration]= 100*(actual_data.current_actual[Iteration]*3.3)/(50*rsens); 
00510         
00511         }
00512         else if(Iteration==8)
00513         {
00514             actual_data.current_actual[Iteration] = (actual_data.current_actual[Iteration]*3.3*62)/11.0;//Load BUS voltage
00515         }
00516         else if(Iteration==9)
00517         {
00518             actual_data.current_actual[Iteration]= (-90.7*(3.3*actual_data.current_actual[Iteration]))+190.1543;//Temperature
00519         }
00520         /*1oth and 11th data are NC*/
00521         else if(Iteration<12)
00522         {
00523             actual_data.current_actual[Iteration]= 0;
00524         }
00525         else{ /*earlier loop missing*/
00526             actual_data.current_actual[Iteration] = (actual_data.current_actual[Iteration]*3.3);
00527             int resistance;       
00528              
00529             resistance = (24300*actual_data.current_actual[Iteration])/(3.3-actual_data.current_actual[Iteration]);
00530             
00531             if(actual_data.current_actual[Iteration]<1.38) 
00532             {
00533                 actual_data.current_actual[Iteration]=3694/log(24.032242*resistance);
00534             }
00535             else{
00536                 
00537                 actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance);
00538             }
00539             
00540             actual_data.current_actual[Iteration] = actual_data.current_actual[Iteration] - 273.15; /*converting from C to kelvin*/ 
00541             
00542             }
00543           //  printf("Current[%d]  %f\r\n",Iteration,actual_data.current_actual[Iteration]);
00544     }
00545     
00546     //actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543;
00547     actual_data.BAE_temp_actual=(-90.7*3.3*BAE_temp_sensor.read())+190.1543;
00548     
00549     actual_data.Batt_voltage_actual = (Batt_voltage.read()*3.3*5.63) * 10;
00550 
00551     //quantizing data //changing the algo 
00552     for(Iteration=0; Iteration<16; Iteration++){
00553 
00554         if(Iteration==1)
00555             quant_data.voltage_quant[Iteration] = float_to_uint8(-50,150,actual_data.voltage_actual[Iteration]);//quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]); /*for keeping the record of the temp'*/
00556         else
00557             quant_data.voltage_quant[Iteration] = 10*actual_data.voltage_actual[Iteration];
00558         //quant_data.voltage_quant[Iteration] = float_to_uint8(0.0,25,actual_data.voltage_actual[Iteration]);//quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]);
00559         //eps_pc.printf("\n\r quan vol data[%d],\t %d",Iteration,quant_data.voltage_quant[Iteration]);
00560         
00561     }
00562     for(Iteration=0;Iteration<16;Iteration++){
00563         if(Iteration<8)/*consedering 200 mili amp as limit here*/
00564             quant_data.current_quant[Iteration] = float_to_uint8(0.0,2640,actual_data.current_actual[Iteration]);//quantiz(cstart,cstep,actual_data.current_actual[Iteration]);
00565         else if(Iteration==8)
00566             quant_data.current_quant[Iteration] = 10*actual_data.current_actual[Iteration];
00567             //quant_data.current_quant[Iteration] = float_to_uint8(0.0,25,actual_data.current_actual[Iteration]);//quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]); //the bus voltage inside
00568         else if(Iteration==9)
00569             quant_data.current_quant[Iteration] = float_to_uint8(-50,150,actual_data.current_actual[Iteration]); /*the temp of board*/
00570         else if(Iteration>9)/*changed from 25 to 300 what value do we expectt?*/
00571             quant_data.current_quant[Iteration] = float_to_uint8(-50,100,actual_data.current_actual[Iteration]);    
00572     
00573          //  eps_pc.printf("\n\r quan curr data[%d],\t %d",Iteration,quant_data.current_quant[Iteration]);
00574     }       
00575     for(Iteration=0;Iteration<2;Iteration++){
00576         
00577         quant_data.Batt_temp_quant[Iteration] = float_to_uint8(-50,150,actual_data.Batt_temp_actual[Iteration]);//quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]);
00578     }
00579     
00580     //to be changed no need to quantized
00581     
00582     /*no need*/// ->> quant_data.Batt_gauge_quant[0] = quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]);
00583     /*no need*/// ->> quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]);
00584     /*no need*/// ->> quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]);
00585     /*no need*/// ->> quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3];
00586     
00587     quant_data.BAE_temp_quant= float_to_uint8(-50,150,actual_data.BAE_temp_actual);
00588     
00589 ////    for(Iteration=0;Iteration<3;Iteration++){
00590 ////        quant_data.AngularSpeed_quant[Iteration]=actual_data.AngularSpeed_actual[Iteration];
00591 ////        printf("\n\r w value %f",quant_data.AngularSpeed_quant[Iteration]);
00592 ////    }
00593     
00594 ////    for(Iteration=0;Iteration<3;Iteration++){
00595 ////        quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration];
00596 ////        printf("\n\r b value %f",quant_data.Bvalue_quant[Iteration]);
00597 ////    }
00598     
00599     //changed
00600     quant_data.Batt_voltage_quant = 10*(actual_data.Batt_voltage_actual);
00601     /*quant_data.Batt_voltage_quant= float_to_uint8(0,25.0,actual_data.Batt_voltage_actual);*/
00602     /* 
00603     for(Iteration=0; Iteration<16; Iteration++)
00604     eps_pc.printf("Current[%d]  %d\r\n",Iteration, quant_data.current_quant[Iteration]);
00605     for(Iteration=0; Iteration<16; Iteration++)
00606     eps_pc.printf("vvol[%d]  %d\r\n",Iteration,quant_data.voltage_quant[Iteration]);*/
00607     
00608     //?? WHAT DO here
00609     arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0];
00610     arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1];
00611     arch_data.EPS_PCB_temp=quant_data.voltage_quant[1];
00612     //arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1];
00613     arch_data.power_mode=actual_data.power_mode;
00614     arch_data.faultPoll_status=actual_data.faultPoll_status;
00615     arch_data.faultIr_status=actual_data.faultIr_status;
00616     arch_data.Batt_voltage=quant_data.Batt_voltage_quant ;   
00617 ////    printf("\n\r in hk");
00618     
00619 }
00620 
00621 void FCTN_APPEND_HKDATA()
00622 {
00623     LONG_HK_data[1][0] = 0x28;
00624     LONG_HK_data[1][1] = 0x00;
00625     LONG_HK_data[1][2] = 0x00;
00626     LONG_HK_data[1][3] = 0x00;
00627     LONG_HK_data[1][4] = ACS_ATS_STATUS;
00628     LONG_HK_data[1][5] = ACS_TR_XY_SW_STATUS;
00629     LONG_HK_data[1][5] = (LONG_HK_data[1][5]<<2)| ACS_TR_Z_SW_STATUS;
00630     LONG_HK_data[1][5] = (LONG_HK_data[1][5]<<4) | ACS_STATE;
00631     
00632     LONG_HK_data[1][6] = ACS_DETUMBLING_ALGO_TYPE;
00633     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<2) | BCN_TX_SW_STATUS;
00634     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<1) | BCN_SPND_TX;
00635     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<1) | BCN_FEN;
00636     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<1) | BCN_LONG_MSG_TYPE;
00637     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<1) | EPS_BTRY_HTR_AUTO;//EPS_BATTERY_HEATER_ENABLE
00638     LONG_HK_data[1][6] = (LONG_HK_data[1][6]<<1);
00639     //now one spares in BAE_HK_data[5]
00640     LONG_HK_data[1][7] = BAE_RESET_COUNTER;
00641     LONG_HK_data[1][8] = EPS_SOC_LEVEL_12; 
00642     LONG_HK_data[1][9] = EPS_SOC_LEVEL_23;
00643     LONG_HK_data[1][10] = ACS_MAG_TIME_DELAY;
00644     LONG_HK_data[1][11] = ACS_DEMAG_TIME_DELAY;
00645     LONG_HK_data[1][12] = EPS_BAT_TEMP_LOW;
00646     LONG_HK_data[1][13] = EPS_BAT_TEMP_HIGH;
00647     LONG_HK_data[1][14] = EPS_BAT_TEMP_DEFAULT;
00648     LONG_HK_data[1][15] = ACS_MM_X_COMSN >> 8;
00649     LONG_HK_data[1][16] = ACS_MM_X_COMSN;
00650     
00651     LONG_HK_data[1][17] = ACS_MM_Y_COMSN >> 8;
00652     LONG_HK_data[1][18] = ACS_MM_Y_COMSN;
00653                                                                 
00654     LONG_HK_data[1][19] = ACS_MG_X_COMSN >> 8;
00655     LONG_HK_data[1][20] = ACS_MG_X_COMSN;
00656                                                                 
00657     LONG_HK_data[1][21] = ACS_MG_Y_COMSN >> 8;
00658     LONG_HK_data[1][22] = ACS_MG_Y_COMSN;
00659                                                             
00660     LONG_HK_data[1][23] = ACS_MM_Z_COMSN >> 8;
00661     LONG_HK_data[1][24] = ACS_MM_Z_COMSN;
00662                                                                 
00663     LONG_HK_data[1][25] = ACS_MG_Z_COMSN >> 8;
00664     LONG_HK_data[1][26] = ACS_MG_Z_COMSN;
00665                                                                 
00666     LONG_HK_data[1][27] = ACS_Z_FIXED_MOMENT >> 8;
00667     LONG_HK_data[1][28] = ACS_Z_FIXED_MOMENT; 
00668                                                              
00669     //BAE RAM PARAMETER
00670     LONG_HK_data[1][29] = BAE_INIT_STATUS;
00671     LONG_HK_data[1][29] = (LONG_HK_data[1][29]<<1) | 0;//change it================================
00672     LONG_HK_data[1][29] = (LONG_HK_data[1][29]<<1) | BCN_INIT_STATUS; 
00673     LONG_HK_data[1][29] = (LONG_HK_data[1][29]<<1) | BCN_TX_MAIN_STATUS;
00674     LONG_HK_data[1][29] = (LONG_HK_data[1][29]<<3) | BCN_TX_STATUS;
00675     LONG_HK_data[1][29] = (LONG_HK_data[1][29]<<3) | ACS_INIT_STATUS;
00676                                                                 
00677     LONG_HK_data[1][30] = ACS_DATA_ACQ_STATUS;
00678     LONG_HK_data[1][30] = (LONG_HK_data[1][30]<<1) | ACS_MAIN_STATUS;
00679     LONG_HK_data[1][30] = (LONG_HK_data[1][30]<<4) | ACS_STATUS;
00680     LONG_HK_data[1][30] = (LONG_HK_data[1][30]<<1) | EPS_INIT_STATUS;
00681     
00682     LONG_HK_data[1][31] = EPS_BATTERY_GAUGE_STATUS;
00683     LONG_HK_data[1][31] = (LONG_HK_data[1][31]<<1) | EPS_MAIN_STATUS;
00684     LONG_HK_data[1][31] = (LONG_HK_data[1][31]<<1) | EPS_BTRY_TMP_STATUS;;
00685     LONG_HK_data[1][31] = (LONG_HK_data[1][31]<<3) | EPS_STATUS;
00686     LONG_HK_data[1][31] = (LONG_HK_data[1][31]<<2) | CDMS_SW_STATUS;
00687     ////BAE_HK_data[31] = (BAE_HK_data[31]<<1) | EPS_BTRY_HTR_STATUS;//new to : implement===========
00688     
00689     LONG_HK_data[1][32] = EPS_BTRY_HTR;// to be disscussed
00690     //spare 4
00691     LONG_HK_data[1][32] = (LONG_HK_data[1][32]<<7) | BAE_STANDBY;
00692     
00693     // 6 next telemetries value to be given by registers
00694     LONG_HK_data[1][33] = ATS1_EVENT_STATUS_RGTR;
00695     LONG_HK_data[1][34] = ATS1_SENTRAL_STATUS_RGTR;
00696     LONG_HK_data[1][35] = ATS1_ERROR_RGTR;
00697     LONG_HK_data[1][36] = ATS2_EVENT_STATUS_RGTR;
00698     LONG_HK_data[1][37] = ATS2_SENTRAL_STATUS_RGTR;
00699     LONG_HK_data[1][38] = ATS2_ERROR_RGTR;
00700                                                                 
00701     LONG_HK_data[1][39] = BCN_FAIL_COUNT;
00702     LONG_HK_data[1][40] = actual_data.power_mode;
00703     LONG_HK_data[1][41] = HTR_CYCLE_COUNTER;//new to : implement
00704                                                                 
00705     LONG_HK_data[1][42] = BAE_I2C_COUNTER;
00706     LONG_HK_data[1][43] = BAE_I2C_COUNTER>>8;
00707     LONG_HK_data[1][44] = ACS_MAIN_COUNTER;
00708     LONG_HK_data[1][45] = ACS_MAIN_COUNTER>>8;
00709     LONG_HK_data[1][46] = BCN_TX_MAIN_COUNTER;
00710     LONG_HK_data[1][47] = BCN_TX_MAIN_COUNTER>>8;
00711     LONG_HK_data[1][48] = EPS_MAIN_COUNTER;
00712     LONG_HK_data[1][49] = EPS_MAIN_COUNTER>>8;
00713     
00714     uint8_t days,hours,mins;
00715     RETURN_UPTIME(BAE_uptime.read(),&days,&hours,&mins);
00716     LONG_HK_data[1][50] = days;
00717     RETURN_UPTIME(I2C_last.read(),&days,&hours,&mins);
00718     LONG_HK_data[1][50] = (LONG_HK_data[1][50]) | (hours>>2);
00719     LONG_HK_data[1][51] = hours;
00720     LONG_HK_data[1][51] = (LONG_HK_data[1][51]<<6) | mins;
00721     
00722     
00723     LONG_HK_data[1][52] = actual_data.bit_data_acs_mm[0];
00724     LONG_HK_data[1][53] = actual_data.bit_data_acs_mm[0]>>8;
00725     LONG_HK_data[1][54] = actual_data.bit_data_acs_mm[1];
00726     LONG_HK_data[1][55] = actual_data.bit_data_acs_mm[1]>>8;
00727     LONG_HK_data[1][56] = actual_data.bit_data_acs_mm[2];
00728     LONG_HK_data[1][57] = actual_data.bit_data_acs_mm[2]>>8;
00729                                                                 
00730     LONG_HK_data[1][58] = actual_data.bit_data_acs_mg[0];
00731     LONG_HK_data[1][59] = actual_data.bit_data_acs_mg[0]>>8;
00732     LONG_HK_data[1][60] = actual_data.bit_data_acs_mg[1];
00733     LONG_HK_data[1][61] = actual_data.bit_data_acs_mg[1]>>8;
00734     LONG_HK_data[1][62] = actual_data.bit_data_acs_mg[2];
00735     LONG_HK_data[1][63] = actual_data.bit_data_acs_mg[2]>>8;
00736                                                                 
00737     LONG_HK_data[1][64] = (~BCN_TX_OC_FAULT);
00738     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | ACS_TR_XY_ENABLE;
00739     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | ACS_TR_Z_ENABLE;
00740     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | (~ACS_TR_XY_OC_FAULT);
00741     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | (~ACS_TR_Z_OC_FAULT);
00742     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | (~ACS_TR_XY_FAULT);
00743     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | (~EPS_CHARGER_FAULT);
00744     LONG_HK_data[1][64] = (LONG_HK_data[1][64]<<1) | EPS_CHARGER_STATUS;
00745                                                                 
00746     LONG_HK_data[1][65] = EPS_BATTERY_GAUGE_ALERT;
00747     LONG_HK_data[1][65] = (LONG_HK_data[1][65]<<1) | (~CDMS_OC_FAULT);
00748     LONG_HK_data[1][65] = (LONG_HK_data[1][65]<<1) | (~ACS_ATS1_OC_FAULT);
00749     LONG_HK_data[1][65] = (LONG_HK_data[1][65]<<1) | (~ACS_ATS2_OC_FAULT);
00750     LONG_HK_data[1][65] = (LONG_HK_data[1][65]<<1) | (~ACS_TR_Z_FAULT);
00751     LONG_HK_data[1][65] = (LONG_HK_data[1][65]<<3);
00752     //3 spare
00753                                                                 
00754     LONG_HK_data[1][66] = ACS_TR_X_PWM;
00755     LONG_HK_data[1][67] = ACS_TR_Y_PWM;
00756     LONG_HK_data[1][68] = ACS_TR_Z_PWM;
00757     //spare byte
00758     //assigned it to counter HTR_CYCLE_COUNTER
00759                                                                 
00760     //assign it b_scz_angle
00761     LONG_HK_data[1][69] = B_SCZ_ANGLE>>4; ; 
00762     LONG_HK_data[1][69] = (LONG_HK_data[1][69]<<1) | alarmmode;
00763     LONG_HK_data[1][69] = (LONG_HK_data[1][69]<<1) | controlmode_mms;
00764     LONG_HK_data[1][69] = (LONG_HK_data[1][69]<<1) | singularity_flag_mms;
00765     LONG_HK_data[1][69] = (LONG_HK_data[1][69]<<1);
00766     //1 bit spare
00767                                                                 
00768     for(int i=0;i<9;i++)
00769         {
00770             LONG_HK_data[1][70+i] =  invjm_mms[i];
00771             LONG_HK_data[1][81+i] =  jm_mms[i];
00772         }
00773                                                                 
00774     for(int i=0;i<2;i++)
00775         LONG_HK_data[1][79+i] = bb_mms[i];
00776                                                                 
00777     for(int i=0;i<16;i++)
00778         {
00779             LONG_HK_data[1][90+i] = quant_data.voltage_quant[i];
00780             LONG_HK_data[1][106+i] = quant_data.current_quant[i];
00781         }
00782                                                                 
00783     LONG_HK_data[1][122] = quant_data.Batt_voltage_quant;
00784     LONG_HK_data[1][123] = quant_data.BAE_temp_quant;
00785     LONG_HK_data[1][124] = (uint8_t)(actual_data.Batt_gauge_actual[1]);
00786     LONG_HK_data[1][125] = quant_data.Batt_temp_quant[0];
00787     LONG_HK_data[1][126] = quant_data.Batt_temp_quant[1];
00788     LONG_HK_data[1][127] = BCN_TMP;
00789     LONG_HK_data[1][128] = 0x00;
00790     LONG_HK_data[1][129] = 0x00;
00791     LONG_HK_data[1][130] = 0x00;
00792     LONG_HK_data[1][131] = 0x00;
00793     uint16_t crc = crc_hk_data();
00794     LONG_HK_data[1][132] = (uint8_t)(crc >> 8);
00795     LONG_HK_data[1][133] = crc;
00796 
00797 //===================================================
00798 /* can be retrived from the earlier code (function)*/   
00799 }
00800 
00801 uint8_t quantiz(float start,float step,float x)
00802 {
00803     int y=(x-start)/step;
00804     if(y<=0)y=0;
00805     if(y>=255)y=255;
00806     return y;
00807 }
00808 
00809 bool firstCount=true;  // goes to EPS init
00810 
00811 
00812 uint8_t saveMin(uint8_t x,uint8_t y){
00813     if(y<x)
00814     {
00815         x=y;
00816     }
00817     return x;
00818 }
00819 uint8_t saveMax(uint8_t x,uint8_t y){
00820     if (y>x)
00821     {
00822        x=y;
00823     }
00824     return x;
00825 }
00826 
00827 
00828 void minMaxHkData(){
00829     if(firstCount==true){
00830         for (int i = 0; i < 16; ++i){    
00831         bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i];
00832         bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i];
00833         }
00834         for (int i = 0; i < 16; ++i){    
00835         bae_HK_minmax.current_min[i] = quant_data.current_quant[i];
00836         bae_HK_minmax.current_max[i] = quant_data.current_quant[i];   
00837         }
00838 
00839         for (int i = 0; i < 2; ++i){    
00840         bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i];
00841         bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i];
00842         }    
00843         /*
00844         for (int i = 0; i < 3; ++i){    
00845         bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i];
00846         bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i];
00847         }
00848         */
00849         bae_HK_minmax.Batt_SOC_min = (uint8_t)(actual_data.Batt_gauge_actual[1]);
00850         bae_HK_minmax.Batt_SOC_max = (uint8_t)(actual_data.Batt_gauge_actual[1]);
00851         
00852         bae_HK_minmax.BCN_TEMP_min = BCN_TMP;
00853         bae_HK_minmax.BCN_TEMP_max = BCN_TMP;
00854         
00855         for (int i = 0; i < 3; ++i){    
00856         bae_HK_minmax.bit_data_acs_mg_min[i] = actual_data.bit_data_acs_mg[i];
00857         bae_HK_minmax.bit_data_acs_mg_max[i] = actual_data.bit_data_acs_mg[i];
00858         }
00859         for (int i = 0; i < 3; ++i){    
00860         bae_HK_minmax.bit_data_acs_mm_min[i] = actual_data.bit_data_acs_mm[i];//Bvalue_quant earlier
00861         bae_HK_minmax.bit_data_acs_mm_max[i] = actual_data.bit_data_acs_mm[i];
00862         }
00863         bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant;
00864         bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant;
00865         bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant;
00866         bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant;
00867           
00868     firstCount=false;//just as a precaution see if correct the problem 
00869     //printf("/n/r the status is %d",firstCount);/*else during runtime write a printf and see the changes whether it changes or not*/
00870     
00871     } 
00872     else {
00873         for (int i = 0; i < 16; ++i)
00874         {
00875             bae_HK_minmax.voltage_min[i] = saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]);
00876             bae_HK_minmax.voltage_max[i] = saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]);
00877         }
00878         for (int i = 0; i < 16; ++i)
00879         {
00880             bae_HK_minmax.current_min[i] = saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]);
00881             bae_HK_minmax.current_max[i] = saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]);
00882         }
00883         
00884         for (int i = 0; i < 2; ++i)
00885         {
00886             bae_HK_minmax.Batt_temp_min[i] = saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]);
00887             bae_HK_minmax.Batt_temp_max[i] = saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]);
00888         }
00889         /*
00890         for (int i = 0; i < 3; ++i)
00891         {
00892             saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]);
00893             saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]);
00894         }
00895         */
00896         bae_HK_minmax.Batt_SOC_min = saveMin(bae_HK_minmax.Batt_SOC_min, ((uint8_t)(actual_data.Batt_gauge_actual[1])) );
00897         bae_HK_minmax.Batt_SOC_max = saveMax(bae_HK_minmax.Batt_SOC_max, ((uint8_t)(actual_data.Batt_gauge_actual[1])) );
00898         
00899         bae_HK_minmax.BCN_TEMP_min = saveMin(bae_HK_minmax.BCN_TEMP_min, BCN_TMP);
00900         bae_HK_minmax.BCN_TEMP_max = saveMin(bae_HK_minmax.BCN_TEMP_max, BCN_TMP);
00901         
00902         for (int i = 0; i < 3; ++i)
00903         {
00904             bae_HK_minmax.bit_data_acs_mg_min[i] = saveMin(bae_HK_minmax.bit_data_acs_mg_min[i], actual_data.bit_data_acs_mg[i]);
00905             bae_HK_minmax.bit_data_acs_mg_max[i] = saveMax(bae_HK_minmax.bit_data_acs_mg_max[i], actual_data.bit_data_acs_mg[i]);
00906         }
00907         for (int i = 0; i < 3; ++i)
00908         {
00909             bae_HK_minmax.bit_data_acs_mm_min[i] = saveMin(bae_HK_minmax.bit_data_acs_mm_min[i], actual_data.bit_data_acs_mm[i]);
00910             bae_HK_minmax.bit_data_acs_mm_max[i] = saveMax(bae_HK_minmax.bit_data_acs_mm_max[i], actual_data.bit_data_acs_mm[i]);
00911         }
00912         bae_HK_minmax.BAE_temp_min = saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant);
00913         bae_HK_minmax.BAE_temp_max = saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant);
00914         bae_HK_minmax.Batt_voltage_min = saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant);
00915         bae_HK_minmax.Batt_voltage_max = saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant);        
00916           
00917         
00918     }   
00919     firstCount=false;
00920     
00921     //printing the min_max data shouldn't give the tc to get new min max data shouldn't call the case 0x41 now to reset the min_max data
00922     for (int i = 0; i < 16; ++i)    
00923        // eps_pc.printf("\n\rthe min volatages are %d = %d",i,bae_HK_minmax.voltage_min[i]);
00924     for (int i = 0; i < 16; ++i)    
00925         //eps_pc.printf("\n\rthe min current are %d = %d",i,bae_HK_minmax.current_min[i]);
00926     for (int i = 0; i < 2; ++i)    
00927        // eps_pc.printf("\n\rthe min bae_temp are %d = %d",i,bae_HK_minmax.Batt_temp_min[i]);
00928        
00929        //eps_pc.printf("\n\rthe min batt_SOC_min are  %d",bae_HK_minmax.Batt_SOC_min);
00930       // eps_pc.printf("\n\rthe min BCN_TEMP_min are  %d",bae_HK_minmax.BCN_TEMP_min);
00931       
00932      for (int i = 0; i < 3; ++i)
00933         {
00934            // eps_pc.printf("\n\rthe min bit_data_acs_mg_min are %d = %d",i,bae_HK_minmax.bit_data_acs_mg_min[i]);
00935         }
00936         for (int i = 0; i < 3; ++i)
00937         {
00938           //  eps_pc.printf("\n\rthe min bit_data_acs_mm_min are %d = %d",i,bae_HK_minmax.bit_data_acs_mm_min[i]);
00939         }
00940         
00941         //eps_pc.printf("\n\rthe min BAE_temp_min are %d",bae_HK_minmax.BAE_temp_min);
00942         //eps_pc.printf("\n\rthe min Batt_voltage_min %d",bae_HK_minmax.Batt_voltage_min);
00943         
00944     //max data now
00945     for (int i = 0; i < 16; ++i)    
00946        // eps_pc.printf("\n\rthe max volatages are %d = %d",i,bae_HK_minmax.voltage_max[i]);
00947     for (int i = 0; i < 16; ++i)    
00948         //eps_pc.printf("\n\rthe max current are %d = %d",i,bae_HK_minmax.current_max[i]);
00949     for (int i = 0; i < 2; ++i)    
00950         //eps_pc.printf("\n\rthe max bae_temp are %d = %d",i,bae_HK_minmax.Batt_temp_max[i]);
00951        
00952        //eps_pc.printf("\n\rthe max batt_SOC_max are %d",bae_HK_minmax.Batt_SOC_max);
00953      //  eps_pc.printf("\n\rthe max BCN_TEMP_max are %d",bae_HK_minmax.BCN_TEMP_max);
00954       
00955      for (int i = 0; i < 3; ++i)
00956         {
00957           //  eps_pc.printf("\n\rthe max bit_data_acs_mg_max are %d = %d",i,bae_HK_minmax.bit_data_acs_mg_max[i]);
00958         }
00959         for (int i = 0; i < 3; ++i)
00960         {
00961             //eps_pc.printf("\n\rthe max bit_data_acs_mm_max are %d = %d",i,bae_HK_minmax.bit_data_acs_mm_max[i]);
00962         }
00963         
00964         //eps_pc.printf("\n\rthe max BAE_temp_max are %d ",bae_HK_minmax.BAE_temp_max);
00965        // eps_pc.printf("\n\rthe max Batt_voltage_max %d ",bae_HK_minmax.Batt_voltage_max);
00966         
00967     
00968 }
00969  
00970 
00971 //............................................BATTERY GAUGE......................................//
00972 void FCTN_BATTERYGAUGE_INIT()
00973 {
00974         disable_sleep();
00975         disable_hibernate();
00976         socChangeAlertEnabled(true); //enabling alert on soc changing by 1%
00977         emptyAlertThreshold(32);//setting empty alert threshold to 32% soc
00978         vAlertMinMaxThreshold();//set min, max value of Valrt register
00979         vResetThresholdSet();//set threshold voltage for reset
00980         vResetAlertEnabled(true);//enable alert on reset for V < Vreset
00981         int ack = write(REG_STATUS, read(REG_STATUS) & 0xFEFF);   //Clearing Reset Indicator bit
00982         if( ack == 0 ) eps_pc.printf("\n\rBTG init success\n\r");
00983         else eps_pc.printf("\n\rBTG init fail ack = %d\n\r", ack);
00984         write(REG_STATUS, read(REG_STATUS) & 0xFEFF);   //Clearing Reset Indicator bit
00985 }
00986 
00987 int FCTN_BATTERYGAUGE_MAIN(float Battery_parameters[4], float temp)
00988 {
00989 ////        eps_pc.printf("\n\r battery gauge \n");
00990 
00991         //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.
00992         int flag = tempCompensation(temp);
00993         //tempCompensation(temp);
00994         
00995          if( flag == 0 )
00996         {
00997             Battery_parameters[0]=vcell();
00998             
00999             timer_soc.reset();
01000             timer_soc.start();
01001             Battery_parameters[1]=soc();
01002             timer_soc.stop();
01003             
01004             Battery_parameters[2]=crate();
01005            
01006             eps_pc.printf("\nVcell=%f",vcell());       //remove this for final code
01007             eps_pc.printf("\nSOC=%f",soc());           //remove this for final code
01008             eps_pc.printf("\nC_rate=%f",crate());      //remove this for final code
01009     
01010            
01011             if (alerting()== true)       //alert is on
01012             {   timer_alertFlags.reset();
01013                 timer_alertFlags.start();
01014                 Battery_parameters[3]=alertFlags();
01015                 timer_alertFlags.stop();
01016             
01017                 clearAlert();//clear alert
01018                 clearAlertFlags();//clear all alert flags
01019             }
01020         }
01021        
01022         if( soc() == 200 || flag != 0) return 1;
01023         else return 0; 
01024         
01025 }
01026 int tempCompensation(float temp)
01027 {
01028     //Calculate the new RCOMP value
01029     char rcomp;
01030     if (temp > 20.0) {
01031         rcomp = RCOMP0 + (temp - 20.0) * -0.5;
01032     } else {
01033         rcomp = RCOMP0 + (temp - 20.0) * -5.0;
01034     }
01035  
01036     //Update the RCOMP value
01037     return compensation(rcomp);
01038 }
01039 
01040 int compensation(char rcomp)
01041 {
01042     //Read the current 16-bit register value
01043     unsigned short value = read(REG_CONFIG);
01044  
01045     //Update the register value
01046     value &= 0x00FF;
01047     value |= rcomp << 8;
01048  
01049     //Write the value back out
01050     return (write(REG_CONFIG, value));
01051 }
01052 
01053 int write(char reg, unsigned short data)
01054     {
01055         eps_btg_writ_flag = -1;
01056         //Create a temporary buffer
01057         char buff[3];
01058    
01059         //Load the register address and 16-bit data
01060         buff[0] = reg;
01061         buff[1] = data >> 8;
01062         buff[2] = data;
01063         
01064         int flag = i2c.write(m_ADDR, buff, 3);    //Write the data and return ack
01065         
01066         if( flag != 0 )
01067         {
01068             flag = i2c.write(m_ADDR, buff, 3);    //Write the data and return ack
01069             if( data != read(reg) )
01070                 eps_btg_writ_flag = 1;
01071                 //EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS 
01072             else eps_btg_writ_flag = 0;
01073                  //Verify written data
01074                 //EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS 
01075         }
01076         
01077         return flag;
01078     }
01079    
01080 unsigned short read(char reg)
01081     {
01082         eps_btg_read_flag = -1;
01083         int flag = 1;
01084         char buff[2];       //Create a temporary buffer
01085  
01086         //Select the register
01087         i2c.write(m_ADDR, &reg, 1, true);
01088         //Read the 16-bit register
01089         flag = i2c.read(m_ADDR, buff, 2);
01090         
01091         if( flag )
01092         {
01093             i2c.write(m_ADDR, &reg, 1, true);
01094             flag = i2c.read(m_ADDR, buff, 2);
01095             //if( flag )
01096             //    EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS 
01097         }
01098  
01099         eps_btg_read_flag = flag;
01100         //Return the combined 16-bit value
01101         return (buff[0] << 8) | buff[1];
01102     } 
01103 /*
01104 
01105 unsigned short read(char reg)
01106     {
01107          
01108          //Create a temporary buffer
01109         char buff[2];
01110  
01111         //Select the register
01112         i2c.write(m_ADDR, &reg, 1, true);
01113  
01114         //Read the 16-bit register
01115         i2c.read(m_ADDR, buff, 2);
01116  
01117         //Return the combined 16-bit value
01118         return (buff[0] << 8) | buff[1];
01119     }
01120  
01121 
01122  
01123 
01124     
01125     void write(char reg, unsigned short data)
01126     {
01127         //Create a temporary buffer
01128         char buff[3];
01129  
01130         //Load the register address and 16-bit data
01131         buff[0] = reg;
01132         buff[1] = data >> 8;
01133         buff[2] = data;
01134  
01135         //Write the data
01136         i2c.write(m_ADDR, buff, 3);
01137     }
01138    
01139  
01140 */ 
01141     // Command the MAX17049 to perform a power-on reset
01142     void reset()
01143     {
01144         //Write the POR command
01145         write(REG_CMD, 0x5400);
01146         //Re-initialise gauge
01147         
01148         timer_FCTN_BATTERYGAUGE_INIT.reset();
01149         timer_FCTN_BATTERYGAUGE_INIT.start();
01150         FCTN_BATTERYGAUGE_INIT();
01151         timer_FCTN_BATTERYGAUGE_INIT.stop();
01152     }
01153     
01154     // Command the MAX17049 to perform a QuickStart
01155      void quickStart()
01156     {
01157         //Read the current 16-bit register value
01158         unsigned short value = read(REG_MODE);
01159   
01160         //Set the QuickStart bit
01161         value |= (1 << 14);
01162  
01163         //Write the value back out
01164         write(REG_MODE, value);
01165     }
01166     
01167     
01168    //disable sleep
01169    void disable_sleep()
01170     {
01171         unsigned short value = read(REG_MODE);
01172         value &= ~(1 << 13);
01173         write(REG_MODE, value);
01174     }
01175   
01176     //disable the hibernate  of the MAX17049
01177     void disable_hibernate()
01178     {
01179         write(REG_HIBRT, 0x0000);
01180     }
01181   
01182     
01183     // Enable or disable the SOC 1% change alert on the MAX17049
01184     void socChangeAlertEnabled(bool enabled)
01185     {
01186         //Read the current 16-bit register value
01187         unsigned short value = read(REG_CONFIG);
01188  
01189         //Set or clear the ALSC bit
01190         if (enabled)
01191             value |= (1 << 6);
01192         else
01193             value &= ~(1 << 6);
01194  
01195         //Write the value back out
01196         write(REG_CONFIG, value);
01197 } 
01198 
01199 /*
01200 void compensation(char rcomp)
01201 {
01202     //Read the current 16-bit register value
01203     unsigned short value = read(REG_CONFIG);
01204  
01205     //Update the register value
01206     value &= 0x00FF;
01207     value |= rcomp << 8;
01208  
01209     //Write the value back out
01210     write(REG_CONFIG, value);
01211 }
01212 
01213 
01214 void tempCompensation(float temp)
01215 {
01216     //Calculate the new RCOMP value
01217     char rcomp;
01218     if (temp > 20.0) {
01219         rcomp = RCOMP0 + (temp - 20.0) * -0.5;
01220     } else {
01221         rcomp = RCOMP0 + (temp - 20.0) * -5.0;
01222     }
01223  
01224     //Update the RCOMP value
01225     compensation(rcomp);
01226 }
01227 
01228 */
01229   // Command the MAX17049 to de-assert the ALRT pin
01230     void clearAlert()
01231     {
01232         //Read the current 16-bit register value
01233         unsigned short value = read(REG_CONFIG);
01234  
01235         //Clear the ALRT bit
01236         value &= ~(1 << 5);
01237  
01238         //Write the value back out
01239         write(REG_CONFIG, value);
01240     }
01241   
01242  
01243     //Set the SOC empty alert threshold of the MAX17049
01244     void emptyAlertThreshold(char threshold)
01245     {
01246         //Read the current 16-bit register value
01247         unsigned short value = read(REG_CONFIG);
01248  
01249         //Update the register value
01250         value &= 0xFFE0;
01251         value |= 32 - threshold;
01252  
01253         //Write the 16-bit register
01254         write(REG_CONFIG, value);
01255     }
01256     
01257     // Set the low and high voltage alert threshold of the MAX17049
01258     void vAlertMinMaxThreshold()
01259     {
01260         //Read the current 16-bit register value
01261         unsigned short value = read(REG_VALRT);
01262  
01263         //Mask off the old value
01264     
01265                 value = 0x96D2;
01266      
01267         //Write the 16-bit register
01268         write(REG_VALRT, value);
01269     }
01270 
01271     
01272     // Set the reset voltage threshold of the MAX17049
01273     void vResetThresholdSet()
01274     {
01275         //Read the current 16-bit register value
01276         unsigned short value = read(REG_VRESET_ID);
01277  
01278         //Mask off the old //value
01279         value &= 0x00FF;//Dis=0
01280  
01281         value |= 0x9400;//corresponding to 2.5 V
01282     
01283  
01284         //Write the 16-bit register
01285         write(REG_VRESET_ID, value);
01286     }
01287     
01288     
01289     // Enable or disable the voltage reset alert on the MAX17049
01290      void vResetAlertEnabled(bool enabled)
01291     {
01292         //Read the current 16-bit register value
01293         unsigned short value = read(REG_STATUS);
01294     
01295         //Set or clear the EnVR bit
01296         if (enabled)
01297             value |= (1 << 14);
01298         else
01299             value &= ~(1 << 14);
01300  
01301         //Write the value back out
01302         write(REG_STATUS, value);
01303     }
01304  
01305     //Get the current alert flags on the MAX17049
01306     //refer datasheet-status registers section to decode it.
01307     char alertFlags()
01308     {
01309         //Read the 16-bit register value
01310         unsigned short value = read(REG_STATUS);
01311  
01312         //Return only the flag bits
01313         return (value >> 8) & 0x3F;
01314     }
01315     
01316     // Clear all the alert flags on the MAX17049
01317     void clearAlertFlags()
01318     {
01319         //Read the current 16-bit register value
01320         unsigned short value = read(REG_STATUS);
01321  
01322         //Clear the specified flag bits
01323         value &= ~( 0x3F<< 8);
01324  
01325         //Write the value back out
01326         write(REG_STATUS, value);
01327     }
01328     
01329     // Get the current cell voltage measurement of the MAX17049
01330     float vcell()
01331     {
01332         
01333         //Read the 16-bit raw Vcell value
01334         unsigned short value = read(REG_VCELL);
01335  
01336         //Return Vcell in volts
01337         return value * 0.000078125*2;
01338     }
01339     
01340     // Get the current state of charge measurement of the MAX17049 as a float
01341     float soc()
01342     {
01343       
01344          //Create a temporary buffer
01345         char buff[2];
01346          int ack = 1;
01347         //Select the register
01348         char reg = REG_SOC;         // cannot pass the hash defined values directly
01349         i2c.write(m_ADDR, &reg, 1, true);
01350         
01351  
01352         //Read the 16-bit register
01353        
01354         ack = i2c.read(m_ADDR, buff, 2);
01355             
01356 ////        eps_pc.printf("\n\r acknow %d", ack);
01357  
01358         //Return SOC in percent
01359         if(ack == 0)
01360         return ((buff[0] << 8) | buff[1]) * 0.00390625;
01361         else 
01362         return 200;     //SOC not available
01363     }
01364     
01365    
01366  
01367     // Get the current C rate measurement of the MAX17049
01368     float crate()
01369     {
01370         //Read the 16-bit raw C/Rate value
01371         short value = read(REG_CRATE);
01372  
01373         //Return C/Rate in %/hr
01374         return value * 0.208;
01375     }
01376     
01377     // Determine whether or not the MAX17049 is asserting the ALRT pin
01378     bool alerting()
01379     {
01380         //Read the 16-bit register value
01381         unsigned short value = read(REG_CONFIG);
01382  
01383         //Return the status of the ALRT bit
01384         if (value & (1 << 5))
01385             return true;
01386         else
01387             return false;
01388     }
01389     
01390 //.............................Battery board Temp sensor........................//
01391 void FCTN_BATTTEMP_INIT()
01392 {
01393     ssn1=1;ssn2=1;
01394     //PS=0;
01395     //HS=0;
01396     spi_bt.format(8,3);
01397     spi_bt.frequency(1000000);
01398     EPS_BTRY_TMP_STATUS = 1;
01399 } 
01400 
01401 void FCTN_BATT_TEMP_SENSOR_MAIN(float temp[2])
01402 {
01403     uint8_t MSB, LSB;
01404     int16_t bit_data;
01405     float sensitivity=0.0078125;   //1 LSB = sensitivity degree celcius
01406     wait_ms(320);
01407     //can we reduce it further ??? azad.......!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
01408     ssn1=0;
01409 
01410     spi_bt.write(0x80);//Reading digital data from Sensor 1
01411     LSB = spi_bt.write(0x00);//LSB first
01412     wait_ms(10);
01413     MSB = spi_bt.write(0x00);
01414     wait_ms(10);
01415 ////    pc_eps.eps_pc.printf("%d %d\n",MSB,LSB);
01416     bit_data= ((uint16_t)MSB<<8)|LSB;
01417     wait_ms(10);
01418     temp[0]=(float)bit_data*sensitivity;//Converting into decimal value 
01419     ssn1=1;
01420     wait_ms(10);
01421     ssn2=0;//Reading data from sensor 2
01422     spi_bt.write(0x80);
01423     LSB = spi_bt.write(0x00);
01424     wait_ms(10);
01425     MSB = spi_bt.write(0x00);
01426     wait_ms(10);
01427     bit_data= ((int16_t)MSB<<8)|LSB;
01428     wait_ms(10);
01429     temp[1]=(float)bit_data*sensitivity;
01430     ssn2=1;
01431     
01432 }