FINAL ACS TO BE USED FOR TESTING. COMMISSIONING, ACS MAIN, DATA ACQ ALL DONE.

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_FULL_Flowchart_BAE by Team Fox

main.cpp

Committer:
Bragadeesh153
Date:
2016-06-13
Revision:
18:21740620c65e
Parent:
17:1e1955f3db75
Child:
19:403cb36e22ed

File content as of revision 18:21740620c65e:

#include "mbed.h"
#include "rtos.h"
#include "pin_config.h"
#include "ACS.h"
#include "EPS.h"
#include "BCN.h"
#include "TCTM.h"

#define tm_len 135
#define tc_len 11
#define batt_heat_low 20

//***************************************************** flags *************************************************************//
uint32_t BAE_STATUS = 0x00000000;
uint32_t BAE_ENABLE = 0xFFFFFFFF;

//i2c//
char data_send_flag = 'h'; 

//.........ACS...............//


uint8_t ACS_INIT_STATUS = 0;
uint8_t ACS_DATA_ACQ_STATUS = 0;
uint8_t ACS_ATS_STATUS = 0x60;
uint8_t ACS_MAIN_STATUS = 0;
uint8_t ACS_STATUS = 0;
uint8_t ACS_DETUMBLING_ALGO_TYPE = 0;

uint8_t ACS_TR_Z_SW_STATUS=1;
uint8_t ACS_TR_XY_SW_STATUS=1;

uint8_t ACS_ATS_ENABLE = 1;
uint8_t ACS_DATA_ACQ_ENABLE = 1;
uint8_t ACS_STATE = 7;

//.....................EPS...................//
//eps init

uint8_t EPS_INIT_STATUS = 0;
uint8_t EPS_BATTERY_GAUGE_STATUS = 0;
//eps main
uint8_t EPS_MAIN_STATUS = 0;
uint8_t EPS_BATTERY_TEMP_STATUS = 0;
uint8_t EPS_STATUS = 7; //invalid status

uint8_t EPS_BATTERY_HEAT_ENABLE = 0;

//.......................global variables..................................................................// new hk structure- everything has to changed based on this
uint8_t BAE_data[74];  
char BAE_chardata[74];     
    

//*************************************Global declarations************************************************//
const int addr = 0x20;                                            //slave address 

Timer t_rfsilence;
Timer t_start;
Timer t_tc;
Timer t_tm;

Serial pc(USBTX, USBRX);
int power_flag_dummy=2;

extern float gyro_data[3];
extern float mag_data[3];

extern float moment[3];
extern float b_old[3];  // Unit: Tesla
extern float db[3];
extern uint8_t flag_firsttime;


extern uint8_t BCN_FEN;
extern BAE_HK_actual actual_data;
extern BAE_HK_quant quant_data;
extern BAE_HK_min_max bae_HK_minmax;
extern BAE_HK_arch arch_data;

int write_ack = 1;
int read_ack = 1;
char telecommand[tc_len];
extern uint8_t telemetry[135];

bool pf1check = 0;
bool pf2check = 0;
bool if1check = 0;
bool if2check = 0;

//*****************************************************Assigning pins******************************************************//
DigitalOut ATS1_SW_ENABLE(PTC0); // enable of att sens2 switch
DigitalOut ATS2_SW_ENABLE(PTC16); // enable of att sens switch

InterruptIn irpt_4m_mstr(PIN38);                                      //I2c interrupt from CDMS
DigitalOut irpt_2_mstr(PIN4);                                        //I2C interrupt to CDMS
I2CSlave slave (PIN1,PIN2);///pin1 pin2
DigitalOut batt_heat(PIN96);

//ATS1_SW_ENABLE = 0;
PwmOut PWM1(PIN93); //x                         //Functions used to generate PWM signal 
PwmOut PWM2(PIN94); //y
PwmOut PWM3(PIN95); //z                         //PWM output comes from pins p6

//........faults
//Polled Faults
DigitalIn pf1(PIN5);//Attitude Sensor 1 OC bar fault signal
DigitalIn pf2(PIN97);//Attitude Sensor 2 OC bar fault signal
DigitalIn pf3(PIN83);//Fault Bar for TRXY driver
 
//Interrupt based faults
//InterruptIn  ir1(PIN73);//Battery Gauge - Alert Bar Signal
InterruptIn  ir2(PIN72);//TRXY Driver TR switch Fault
InterruptIn  ir3(PIN89);//TRZ Driver Fault Bar
InterruptIn  ir4(PIN91);//TRZ Driver TR switch Fault
InterruptIn  ir5(PIN79);//CDMS - Switch Fault
InterruptIn  ir6(PIN80);//Beacon- Switch OC bar
InterruptIn  ir7(PIN42);//Charger IC - Fault Bar



DigitalOut TRXY_SW(PIN71);  //TR XY Switch
DigitalOut DRV_Z_EN(PIN88);    //Sleep pin of driver z
DigitalOut TRZ_SW(PIN40);  //TR Z Switch
DigitalOut CDMS_RESET(PIN7); // CDMS RESET
DigitalOut BCN_SW(PIN14);      //Beacon switch
DigitalOut DRV_XY_EN(PIN82);

/*****************************************************************Threads USed***********************************************************************************/

Thread *ptr_t_i2c;

/*********************************************************FCTN HEADERS***********************************************************************************/

void FCTN_ISR_I2C();
void FCTN_TM();
void F_ACS();
void F_EPS();
void F_BCN();

//*******************************************ACS THREAD**************************************************//
uint8_t iterP1;
uint8_t iterP2;
uint8_t iterI1;
uint8_t iterI2;

extern float max_array(float arr[3]);

void F_ACS()
{
    
    pc.printf("Entered ACS.\n\r");

    ACS_MAIN_STATUS = 1; //set ACS_MAIN_STATUS flag 
    
    PWM1 = 0;                     //clear pwm pins
    PWM2 = 0;                     //clear pwm pins
    PWM3 = 0;                     //clear pwm pins
    

    ACS_DATA_ACQ_STATUS = (uint8_t) FCTN_ATS_DATA_ACQ();
    
    //printing the angular speed and magnetic field values
    
    pc.printf("gyro values\n\r"); 
    for(int i=0; i<3; i++) 
    {
        printf("%f\n\r",actual_data.AngularSpeed_actual[i]);
    }
    
    pc.printf("mag values\n\r");
    for(int i=0; i<3; i++) 
    {
        pc.printf("%f\n\r",actual_data.Bvalue_actual[i]);
    }
    
    for(int i=0;i<3;i++)
    {
        mag_data[i] = actual_data.Bvalue_actual[i]/1000000;
        gyro_data[i] = actual_data.AngularSpeed_actual[i]*3.14159/180;
    }
    


    if(ACS_STATE == 0)                      // check ACS_STATE = ACS_CONTROL_OFF?
    {
          printf("\n\r acs control off\n");
          ACS_STATUS = 0;                // set ACS_STATUS = ACS_CONTROL_OFF
          
          ACS_MAIN_STATUS = 0;
          return;
    }
    
    else if(actual_data.power_mode<2)
    {
                printf("\n\r Low Power \n\r");
                
                DRV_Z_EN = 0;
                DRV_XY_EN = 0;
                
                ACS_STATUS = 1;                    // set ACS_STATUS = ACS_LOW_POWER
                      
                ACS_MAIN_STATUS = 0;
                return;
        
    }
    
    else if(ACS_TR_Z_SW_STATUS != 1)
    {
                DRV_Z_EN = 0;
                DRV_XY_EN = 0;
                
                ACS_STATUS = 2;                 // set ACS_STAUS = ACS_TRZ_DISABLED
                
                ACS_MAIN_STATUS = 0;
                return;       
    }
    
    else if(ACS_TR_XY_SW_STATUS != 1)
    {                 
                                
                DRV_Z_EN = 1;
                DRV_XY_EN = 0;
                
                ACS_STATUS = 3;                 // set ACS_STAUS = ACS_TRXY_DISABLED , Z axis only
                
                moment[0] = 0;
                moment[1] = 0;
                moment[2] =1.3;                 // is a dummy value 
                

                
                FCTN_ACS_GENPWM_MAIN(moment) ;
                
                ACS_MAIN_STATUS = 0;
                return;
        
    }
    
    else if(ACS_DATA_ACQ_STATUS == 1)
    {
                DRV_Z_EN = 1;
                DRV_XY_EN = 0;
                
                ACS_STATUS = 3;                 // set Set ACS_STATUS = ACS_DATA_ACQN_FAILURE , Z axis only
                
                moment[0] = 0;
                moment[1] = 0;
                moment[2] =1.3;                 // is a dummy value 
                FCTN_ACS_GENPWM_MAIN(moment) ;
                
                ACS_MAIN_STATUS = 0;
                return; 
        
    }
    
    else if(ACS_STATE == 5)
    {
                
                DRV_Z_EN = 1;
                DRV_XY_EN = 0;
                
                ACS_STATUS = 3;                 // set ACS_STAUS = ACS_TRXY_DISABLED by ACS_STATE i.e Z axis only
                
                moment[0] = 0;
                moment[1] = 0;
                moment[2] =1.3;                 // 1.3 is a dummy value 
                FCTN_ACS_GENPWM_MAIN(moment) ;
                
                ACS_MAIN_STATUS = 0;
                return;
        
    }
    
    else if(ACS_DATA_ACQ_STATUS == 2)           // MM only is available
    {
                DRV_Z_EN = 1;
                DRV_XY_EN = 1;
                              
                ACS_STATUS = 4;                 // set Set ACS_STATUS = ACS_BDOT_CONTROL
                
                float db[3];
                
                if(flag_firsttime==1)
                    {
                        for(int i=0;i<3;i++)
                        {
                            db[i]=0; // Unit: Tesla/Second
                        }
                        flag_firsttime=0;
                    }
                                    
                else
                    {
                        for(int i=0;i<3;i++)
                        {
                            db[i]= (mag_data[i]-b_old[i])/sampling_time; // Unit: Tesla/Second
                        }
                    }
                    
                    
                    
                for(int i=0;i<3;i++)
                {
                    moment[i]=-kdetumble*db[i];
                    b_old[i]= mag_data[i]; // Unit: Tesla/Second
                }
                
                printf("\n\r Moment values returned by control algo \n");
                for(int i=0; i<3; i++) 
                    {
                        printf("%f\t",moment[i]);
                    }
                    
                FCTN_ACS_GENPWM_MAIN(moment) ; 
                
                ACS_MAIN_STATUS = 0;
                return; 
        
    }
    
    else if(ACS_STATE == 7)                     // Nominal mode
    {
                
                printf("\n\r Nominal mode \n");
                DRV_Z_EN = 1;
                DRV_XY_EN = 1;               
                
                FCTN_ACS_CNTRLALGO(mag_data,gyro_data,1);
                
                printf("\n\r Moment values returned by control algo \n");
                for(int i=0; i<3; i++) 
                    {
                        printf("%f\t",moment[i]);
                    }
                FCTN_ACS_GENPWM_MAIN(moment) ;  
                
                ACS_STATUS = 5;                   // set ACS_STATUS = ACS_NOMINAL_ONLY
                
                ACS_MAIN_STATUS = 0;
                return; 
        
    }
    
    else if(ACS_STATE == 8)                     // Auto Control
    {
                
                printf("\n\r Auto control mode \n");
                DRV_Z_EN = 1;
                DRV_XY_EN = 1;              
                             
                FCTN_ACS_CNTRLALGO(mag_data,gyro_data,0);
                printf("\n\r Moment values returned by control algo \n");
                for(int i=0; i<3; i++) 
                    {
                        printf("%f\t",moment[i]);
                    }
                FCTN_ACS_GENPWM_MAIN(moment) ; 
                                     // set ACS_STATUS in function
                
                ACS_MAIN_STATUS = 0;
                return; 
    }
    
    else if(ACS_STATE == 9)                     // Detumbling
    {
                DRV_Z_EN = 1;
                DRV_XY_EN = 1;       
                
                if(flag_firsttime==1)
                    {
                        for(int i=0;i<3;i++)
                        {
                            db[i]=0; // Unit: Tesla/Second
                        }
                        flag_firsttime=0;
                    }
                                    
                else
                    {
                        for(int i=0;i<3;i++)
                        {
                            db[i]= (mag_data[i]-b_old[i])/sampling_time; // Unit: Tesla/Second
                        }
                    }
                            
                
                if (ACS_DETUMBLING_ALGO_TYPE == 0)
                {
                    
                    for(int i=0;i<3;i++)
                    {
                        moment[i]=-kdetumble*(mag_data[(i+1)%3]*gyro_data[(i+2)%3]-mag_data[(i+2)%3]*gyro_data[(i+1)%3]); // Unit: Ampere*Meter^2
                    }
                    
                    
                    ACS_STATUS = 6;                         // set ACS_STATUS = ACS_BOMEGA_CONTROL
                }
                
                else if(ACS_DETUMBLING_ALGO_TYPE == 1)
                {
                    
                    for(int i=0;i<3;i++)
                    {
                        moment[i]=-kdetumble*db[i];          // Unit: Ampere*Meter^2
                    }
                    
                    ACS_STATUS = 4;                         // set ACS_STATUS = ACS_BDOT_CONTROL
                }
                
                for(int i=0;i<3;i++)
                {
                    
                    b_old[i]= mag_data[i]; // Unit: Tesla/Second
                }
                                    
                printf("\n\r Moment values returned by control algo \n");
                for(int i=0; i<3; i++) 
                    {
                        printf("%f\t",moment[i]);
                    }
                FCTN_ACS_GENPWM_MAIN(moment) ;  
                
                ACS_MAIN_STATUS = 0;
                return; 
    }
    
    ACS_STATUS = 7;                    //INVALID_STATE
    DRV_Z_EN = 0;
    DRV_XY_EN = 0;    
    ACS_MAIN_STATUS = 0; //clear ACS_MAIN_STATUS flag 
        
}
//***************************************************EPS THREAD***********************************************//

void F_EPS()
{
    
        pc.printf("\n\rEntered EPS   %f\n",t_start.read());
        EPS_MAIN_STATUS = 1; // Set EPS main status
        FCTN_BATT_TEMP_SENSOR_MAIN(actual_data.Batt_temp_actual);
          pc.printf("\n\r Battery temperature %f %f" ,actual_data.Batt_temp_actual[0], actual_data.Batt_temp_actual[1]);
          EPS_BATTERY_TEMP_STATUS = 1;          //set EPS_BATTERY_TEMP_STATUS
          if(EPS_BATTERY_HEAT_ENABLE == 1)
          {
              if((actual_data.Batt_temp_actual[0] < batt_heat_low) && (actual_data.Batt_temp_actual[1] < batt_heat_low)) // to confirm
              {
                  batt_heat = 1;    //turn on battery heater
              }
              else
              {
                  batt_heat = 0;     //turn off battery heater
              }
              
           } 
          else if(EPS_BATTERY_HEAT_ENABLE == 0)
          {
                EPS_STATUS = 1;//EPS_STATUS = EPS_BATTERY_HEATER_DISABLED
          }
        FCTN_BATTERYGAUGE_MAIN(actual_data.Batt_gauge_actual);
        if (actual_data.Batt_gauge_actual[1] == 200)   //data not received
        {
          actual_data.power_mode = 1;
          EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS
          
        }
        else
        {
          FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);            //updating power level 
          EPS_BATTERY_GAUGE_STATUS = 1;           //set EPS_BATTERY_GAUGE_STATUS
        }
       // if( Temperature data received)
        //{
          
          
      //  }
//        else
//        {
//          Set battery temp to XX  
//          EPS_BATTERY_TEMP_STATUS = 0;          //clear EPS_BATTERY_TEMP_STATUS
//          EPS_STATUS = EPS_ERR_BATTERY_TEMP;
//        }
        FCTN_HK_MAIN();
       // printf("\n\r here");
        FCTN_APPEND_HKDATA();
        minMaxHkData();
        //printf("\n\r here");  
        EPS_MAIN_STATUS = 0; // clear EPS main status 

}

//**************************************************BCN THREAD*******************************************************************//

void F_BCN()
{
  
          pc.printf("\n\rEntered BCN   %f\n",t_start.read());
          
          FCTN_BCN_TX_MAIN();
    
}

//**************************************************TCTM THREAD*******************************************************************//

void T_TC(void const * args)
{
      while(1)
    {
        Thread::signal_wait(0x4);
        wait_us(200);  
        //printf("\n\rreached\n");                                             // can be between 38 to 15700
        if( slave.receive() == 0)        
            {slave.stop();
            //printf("\n\rnot send\n");
            }     
        else if( slave.receive() == 1)                                     // slave writes to master
        {
             if(data_send_flag == 'h')
             {
                //FCTN_APPEND_HKDATA();
                // pc.printf("\n\r here");
                write_ack=slave.write(BAE_chardata,74); 
                if(write_ack==0)                
                    {irpt_2_mstr = 0;
                    printf("\n\rgot interrupt\n");
                    }
            }
            else if(data_send_flag == 't')
            {
                write_ack=slave.write((char*)telemetry,tm_len);
                data_send_flag = 'h';
                if(write_ack==0)                
                    irpt_2_mstr = 0;
            }       
        }
        else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
        {
           read_ack=slave.read(telecommand,tc_len);
           t_tc.start();
           //pc.printf("\n\rTELECOMMAND received from CDMS is %s \n",telecommand);
           pc.printf("\n\r Executing Telecommand \n"); 
           // FCTN_TC_DECODE((uint8_t*) telecommand);
           
           FCTN_BAE_TM_TC((uint8_t*) telecommand);
           //telemetry = (char*)temp;
          
           FCTN_TM();
            t_tc.stop();
           printf("\n\r time taken %d",t_tc.read_us());
            t_tc.reset();
            // for(int i = 0; i<134; i++)
            //pc.printf("%c", telemetry[i]);
        } 
       
      
    } 
}

void FCTN_TM()
{
    //irpt_2_mstr = 0; 
    data_send_flag = 't';
    pc.printf("\n\r Telemetry Generation \n");
    irpt_2_mstr = 1;   
}


//******************************************************* I2C *******************************************************************//

void FCTN_I2C_ISR()
{
     ptr_t_i2c->signal_set(0x4);
}


//***********************************************************FAULTS***************************************************************//
/*void ir1clear()
{
    actual_data.faultIr_status |= 0x01;   // alert
 
}*/


 
void ir2clear()
{
    actual_data.faultIr_status |= 0x02;
    TRXY_SW = 0;   // Switch off TR XY
    if1check = 1;
}
 
void ir3clear()
{
    actual_data.faultIr_status |= 0x04;
    DRV_Z_EN = 0;
    wait_us(1);
    DRV_Z_EN = 1;
 
}
 
void ir4clear()
{
    if2check = 1;
    actual_data.faultIr_status |= 0x08;
    TRZ_SW = 0;
}
 
void ir5clear()
{
    actual_data.faultIr_status |= 0x10;
    CDMS_RESET = 0;
    wait_us(1);
    CDMS_RESET = 1;
}
 
void ir6clear()
{
    actual_data.faultIr_status |= 0x20;
    BCN_SW = 0;
    wait_us(1);
    BCN_SW = 1;
}
 
void ir7clear()
{
    actual_data.faultIr_status |= 0x40;
}
uint8_t iter2=0,iter4 = 0; 



void pollfault()
{   
    
    
 
    if (pf1==0)                // OC_ATS1
    { 
        pf1check=1;
        actual_data.faultPoll_status |=0x01 ;
        ATS1_SW_ENABLE = 1;  // turn off ats1  // to be turned on next cycle in ACS
    }
    else actual_data.faultPoll_status &= 0xFE;
 
    if (pf2==0)
    {   
        pf2check=1;
        actual_data.faultPoll_status |=0x02 ;
        ATS2_SW_ENABLE = 1;  // turn off ats2  // turn on in ACS
    }
    else actual_data.faultPoll_status &= 0xFD;
 
    if (pf3==0)
    {   actual_data.faultPoll_status |=0x04 ;
        DRV_XY_EN = 0;
        wait_us(1);
        DRV_XY_EN = 1;
    }
    else actual_data.faultPoll_status &= 0xFB;
 
 
 
 
    /*if (ir1==1)
    {
        actual_data.faultIr_status &=0xFE;
    }*/
   
    if (ir2==1)
    {
        actual_data.faultIr_status &=0xFD;
    }
    if (ir3==1)
    {
        actual_data.faultIr_status &=0xFB;
    }
    if (ir4==1)
    {
        actual_data.faultIr_status &=0xF7;
    }
    if (ir5==1)
    {
        actual_data.faultIr_status &=0xEF;
    }
    if (ir6==1)
    {
        actual_data.faultIr_status &=0xDF;
    }if (ir7==1)
    {
        actual_data.faultIr_status &=0xBF;
    }
    
 
}
  

//------------------------------------------------------------------------------------------------------------------------------------------------
//SCHEDULER
//------------------------------------------------------------------------------------------------------------------------------------------------
uint8_t schedcount=1;
void T_SC(void const *args)
{    
    printf("\n\r in scheduler");
   
    if(schedcount == 7)                         //to reset the counter
    {
        schedcount = 1;
    }
    if(schedcount%1==0)
    { 
    
    
    pc.printf("\n\r\r\r\r \t\t******ACS******\r\r\r\r\r");
    
    pc.printf("ACSSTATE IS !!!!!! = %x !!\n\r",ACS_STATE);
    
    float acs_start = (float) t_start.read();
      
    F_ACS();
    
    float  acs_end = float( t_start.read() - acs_start ) ;
    printf("\nTime taken for ACS is:\t %f\n\r",acs_end);
    
    pc.printf("\n\r\r\r\r \t\t******ACS EXIT******\r\r\r\r\r");
       
    }
    
    if(schedcount%2==0)
    {
    //    F_EPS();
    }
    if(schedcount%1==0)
    { 
       F_BCN();
    }
    schedcount++;
    printf("\n\r exited scheduler");
}

Timer t_flag;
void FLAG()
{
    
//.............acs..................//    
    if(ACS_INIT_STATUS == 1)
        BAE_STATUS = BAE_STATUS | 0x00000080;  //set ACS_INIT_STATUS flag
    else if(ACS_INIT_STATUS == 0)
        BAE_STATUS &= 0xFFFFFF7F;              //clear ACS_INIT_STATUS flag 
    
    if(ACS_DATA_ACQ_STATUS == 1)
        BAE_STATUS =BAE_STATUS | 0x00000100;     //set ACS_DATA_ACQ_STATUS flag
    else if(ACS_DATA_ACQ_STATUS == 0)
        BAE_STATUS &= 0xFFFFFEFF;      //clear ACS_DATA_ACQ_STATUS flag    
    
    if(ACS_ATS_ENABLE == 1)
        BAE_ENABLE |= 0x00000004;
    else if(ACS_ATS_ENABLE == 0)
        BAE_ENABLE = BAE_ENABLE &0xFFFFFFFB | 0x00000004;
    
    if(ACS_DATA_ACQ_STATUS == 'f')
        BAE_STATUS |= 0x00000200;
    
    if(ACS_MAIN_STATUS == 1)
        BAE_STATUS = (BAE_STATUS | 0x00001000);     //set ACS_MAIN_STATUS flag
   else if(ACS_MAIN_STATUS == 0)
        BAE_STATUS &= 0xFFFFEFFF;     //clear ACS_MAIN_STATUS flag 
    
    if(ACS_STATUS == '0')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF);                // set ACS_STATUS = ACS_CONTROL_OFF
    else if(ACS_STATUS == '1')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x00002000;                    // set ACS_STATUS = ACS_LOW_POWER
    else if(ACS_STATUS == '2')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF)| 0x00004000;                    // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY  
    else if(ACS_STATUS == '3') 
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00006000;                    // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
    else if(ACS_STATUS == '4')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00008000;                    // set ACS_STATUS = ACS_NOMINAL_ONLY
    else if(ACS_STATUS == '5')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000A000;                    // set ACS_STATUS = ACS_AUTO_CONTROL
    else if(ACS_STATUS == '6')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000C000;                    // set ACS_STATUS = ACS_DETUMBLING_ONLY  
    else 
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000E000;                    // set ACS_STATUS = INVALID STATE 
        
    if(ACS_STATE == '0')
        BAE_ENABLE = (BAE_ENABLE & 0xFFFFFF8F);                                         //ACS_STATE = ACS_CONTROL_OFF
    else if(ACS_STATE == '2')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000020);                              //   ACS_STATE = ACS_ZAXIS_MOMENT_ONLY  
    else if(ACS_STATE == '3')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000030);                              //  set ACS_STATUS = ACS_DATA_ACQ_FAILURE
    else if(ACS_STATE == '4')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000040);                              //  ACS_STATE = ACS_NOMINAL_ONLY
    else if(ACS_STATE == '5')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000050);                              //    ACS_STATE = ACS_AUTO_CONTROL
    else if(ACS_STATE == '6')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000060);                             //ACS_STATE = ACS_DETUMBLING_CONTROL
        
//...............eps......................//

    
if (EPS_INIT_STATUS==1)                                  // Set EPS_INIT_STATUS
    BAE_STATUS |= 0x00010000;                     
else if(EPS_INIT_STATUS==0)                              // Clear
    BAE_STATUS &= 0xFFFEFFFF;


if (EPS_MAIN_STATUS==1)                              // Set EPS_MAIIN_STATUS
    BAE_STATUS |= 0x00040000;
else if(EPS_MAIN_STATUS==0)                          // Clear
    BAE_STATUS &= 0xFFFBFFFF;


if (EPS_BATTERY_GAUGE_STATUS==1)              // Set EPS_BATTERY_GAUGE_STATUS
    BAE_STATUS |= 0x00020000;
else if(EPS_BATTERY_GAUGE_STATUS==0)          // Clear
    BAE_STATUS &= 0xFFFDFFFF;


if (EPS_BATTERY_TEMP_STATUS==1)             // Set EPS_BATTERY_TEMP_STATUS
    BAE_STATUS |= 0x00080000;
else if(EPS_BATTERY_TEMP_STATUS==0)       // Clear
    BAE_STATUS &= 0xFFF7FFFF;

if (EPS_STATUS==0)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF);                             // Set EPS_ERR_BATTERY_TEMP
else if (EPS_STATUS==1)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00010000;           // Set EPS_BATTERY_HEATER_DISABLED
else if (EPS_STATUS==2)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00020000;           // Set EPS_ERR_HEATER_SWITCH_OFF
else if (EPS_STATUS==3)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00030000;          // Set EPS_ERR_HEATER_SWITCH_ON
else if (EPS_STATUS==4)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00040000;          // Set EPS_BATTERY_HEATER_OFF
else if (EPS_STATUS==5)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00050000;          // Set EPS_BATTERY_HEATER_ON   
    

    if(EPS_BATTERY_HEAT_ENABLE == 1)
        BAE_ENABLE |= 0x00000080;
    else if(EPS_BATTERY_HEAT_ENABLE == 0)
        BAE_ENABLE = BAE_ENABLE &0xFFFFFF7;    
    

   
    pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE);
}

void FCTN_BAE_INIT()
{
    printf("\n\r Initialising BAE ");
    //..........intial status....//
    ACS_STATE = 8;
    ACS_ATS_ENABLE = 1;
    ACS_DATA_ACQ_ENABLE = 1;
    
    EPS_BATTERY_HEAT_ENABLE = 1;
    actual_data.power_mode=3;
    //............intializing pins................//
    
    ATS2_SW_ENABLE = 1;
    ATS1_SW_ENABLE = 1;
    wait_ms(5);
    ATS1_SW_ENABLE = 0;
    
    ACS_ATS_STATUS = 0x60;  //Set Sensor 1 working , Sensor2 working and powered off by default

    DRV_XY_EN = 1;
    DRV_Z_EN = 1;
    TRZ_SW = 1;
    TRXY_SW = 1;
    
    //............................//
    FCTN_ACS_INIT();
   // FCTN_EPS_INIT();
    //FCTN_BCN_INIT();

    
    FLAG();
}

int main()
{
    pc.printf("\n\r BAE Activated. Testing Version 1.1 \n");
    CDMS_RESET = 1;  
    /*if (BCN_FEN == 0)                       //dummy implementation
    {
        pc.printf("\n\r RF silence ");
        FCTN_BCN_FEN();
        t_rfsilence.start();//Start the timer for RF_Silence
        while(t_rfsilence.read() < RF_SILENCE_TIME); 
    }               
    */
    
    //ACS_INIT_STATUS = 0;
    //ACS_DATA_ACQ_STATUS = 0;
        

    
    //FLAG();
    FCTN_BAE_INIT();
    
    
    //...i2c..
    //strcpy(telemetry,"This is telemetry THis is sample telemetry. ffffffffffffffffffffffffffffff  end");
    slave.address(addr);
    irpt_2_mstr = 0;
    
    ptr_t_i2c = new Thread(T_TC);
    ptr_t_i2c->set_priority(osPriorityHigh);
        
    irpt_4m_mstr.enable_irq();
    irpt_4m_mstr.rise(&FCTN_I2C_ISR);
   // ir1.fall(&ir1clear);   //Battery Gauge - Alert Bar Signal
 /*   ir2.fall(&ir2clear);   //TRXY Driver TR switch Fault
    ir3.fall(&ir3clear);   //TRZ Driver Fault Bar
    ir4.fall(&ir4clear);   //TRZ Driver TR switch Fault
    ir5.fall(&ir5clear);   //CDMS - Switch Fault
    ir6.fall(&ir6clear);   //Beacon- Switch OC bar
    ir7.fall(&ir7clear);   //Charger IC - Fault Bar
    
    */
    RtosTimer t_sc_timer(T_SC,osTimerPeriodic);               // Initiating the scheduler thread
    t_sc_timer.start(10000);
    t_start.start();
    pc.printf("\n\rStarted scheduler %f\n\r",t_start.read()); 
    

    //FCTN_BAE_INIT();
    while(1);                                                   //required to prevent main from terminating
}