acs beacon and hk integrated

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
sakthipriya
Date:
Sat May 16 07:07:56 2015 +0000
Commit message:
acs beacon and hk integrated

Changed in this revision

ACS.cpp Show annotated file Show diff for this revision Revisions of this file
ACS.h Show annotated file Show diff for this revision Revisions of this file
HK.cpp Show annotated file Show diff for this revision Revisions of this file
HK.h Show annotated file Show diff for this revision Revisions of this file
MPU3300.h Show annotated file Show diff for this revision Revisions of this file
beacon.cpp Show annotated file Show diff for this revision Revisions of this file
beacon.h Show annotated file Show diff for this revision Revisions of this file
fault.cpp Show annotated file Show diff for this revision Revisions of this file
fault.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mnm.cpp Show annotated file Show diff for this revision Revisions of this file
mnm.h Show annotated file Show diff for this revision Revisions of this file
pin_config.h Show annotated file Show diff for this revision Revisions of this file
pni.h Show annotated file Show diff for this revision Revisions of this file
slave.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 1a04c0beef21 ACS.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACS.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,566 @@
+#include "ACS.h"
+#include "MPU3300.h"
+#include "pin_config.h"
+#include "math.h"
+
+Serial pc1(USBTX, USBRX);
+SPI spi_acs (PIN16, PIN17, PIN15);        // mosi, miso, sclk  PTE18,19,17
+DigitalOut SSN_MAG (PIN61);               // ssn for magnetometer PTB11
+DigitalInOut DRDY (PIN47);                 // drdy for magnetometer PTA17
+DigitalOut ssn_gyr (PIN62);                //Slave Select pin of gyroscope  PTB16
+InterruptIn dr(PIN81);                     //Interrupt pin for gyro PTC5
+PwmOut PWM1(PIN93);                          //Functions used to generate PWM signal 
+PwmOut PWM2(PIN94); 
+PwmOut PWM3(PIN95);                          //PWM output comes from pins p6
+Ticker tr;                                //Ticker function to give values for limited amount of time for gyro
+Timeout tr_mag;
+uint8_t trflag_mag;
+uint8_t trFlag;                           //ticker Flag for gyro
+uint8_t drFlag;                           //data-ready interrupt flag for gyro
+float pwm1;
+float pwm2;
+float pwm3;
+//--------------------------------TORQUE ROD--------------------------------------------------------------------------------------------------------------//
+
+void FUNC_ACS_GENPWM(float M[3])
+{
+     
+     printf("\n\rEnterd PWMGEN function\n");
+     for(int i = 0 ; i<3;i++)
+     {
+        printf(" %f \t ",M[i]);
+     }
+     float timep = 0.02 ;   //Time period is set to 0.02s  
+     
+     
+     float DCx = 0;         //Duty cycle of Moment in x direction
+     float ix = 0;          //Current sent in x TR's
+     float Mx=M[0];         //Moment in x direction
+     ix = Mx * 0.3 ;        //Moment and Current always have the linear relationship
+     if( ix>0&& ix < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100%
+     {
+        DCx =  6*1000000*pow(ix,4) - 377291*pow(ix,3) + 4689.6*pow(ix,2) + 149.19*ix - 0.0008;
+        PWM1.period(timep);
+        PWM1 = DCx/100 ;
+     }
+     else if( ix >= 0.006&& ix < 0.0116)
+     { 
+        DCx = 1*100000000*pow(ix,4) - 5*1000000*pow(ix,3) + 62603*pow(ix,2) - 199.29*ix + 0.7648;
+        PWM1.period(timep);
+        PWM1 = DCx/100 ;             
+     }
+     else if (ix >= 0.0116&& ix < 0.0624)
+     {
+        DCx = 212444*pow(ix,4) - 33244*pow(ix,3) + 1778.4*pow(ix,2) + 120.91*ix + 0.3878;
+        PWM1.period(timep);
+        PWM1 = DCx/100 ;            
+     }
+     else if(ix >= 0.0624&& ix < 0.555)
+     {
+        printf("\n\rACS entered if\n\r");                                //gotta check what this means
+        DCx =  331.15*pow(ix,4) - 368.09*pow(ix,3) + 140.43*pow(ix,2) + 158.59*ix + 0.0338;
+        PWM1.period(timep);
+        PWM1 = DCx/100 ;            
+     }
+     else if(ix==0)
+     {
+        printf("\n \r ix====0");
+        DCx = 75;
+        PWM1.period(timep);
+        PWM1 = DCx/100 ;            
+     }
+     else                                           //not necessary
+     {
+        // printf("!!!!!!!!!!Error!!!!!!!!!");
+     } 
+     pwm1 = PWM1;
+     printf("\n\r icx :%f pwm : %f \n\r",ix,pwm1);
+     
+     
+     float DCy = 0;         //Duty cycle of Moment in y direction
+     float iy = 0;          //Current sent in y TR's
+     float My=M[1];         //Moment in y direction
+     iy = My * 0.3 ;        //Moment and Current always have the linear relationship
+     if( iy>0&& iy < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100%
+     {
+        DCy =  6*1000000*pow(iy,4) - 377291*pow(iy,3) + 4689.6*pow(iy,2) + 149.19*iy - 0.0008;
+        PWM2.period(timep);
+        PWM2 = DCy/100 ;
+     }
+     else if( iy >= 0.006&& iy < 0.0116)
+     { 
+        DCy = 1*100000000*pow(iy,4) - 5*1000000*pow(iy,3) + 62603*pow(iy,2) - 199.29*iy + 0.7648;
+        PWM2.period(timep);
+        PWM2 = DCy/100 ;             
+     }
+     else if (iy >= 0.0116&& iy < 0.0624)
+     {
+        DCy = 212444*pow(iy,4) - 33244*pow(iy,3) + 1778.4*pow(iy,2) + 120.91*iy + 0.3878;
+        PWM2.period(timep);
+        PWM2 = DCy/100 ;            
+     }
+     else if(iy >= 0.0624&& iy < 0.555)
+     {
+        printf("\n\rACS entered if\n\r");
+        DCy =  331.15*pow(iy,4) - 368.09*pow(iy,3) + 140.43*pow(iy,2) + 158.59*iy + 0.0338;
+        PWM2.period(timep);
+        PWM2 = DCy/100 ;            
+     }
+     else if(iy==0)
+     {
+        DCy = 0;
+        PWM2.period(timep);
+        PWM2 = DCy/100 ;            
+     }
+     else
+     {
+     // printf("!!!!!!!!!!Error!!!!!!!!!");
+     } 
+     pwm2 = PWM2; 
+     printf("\n\r icy :%f pwm : %f \n\r",iy,pwm2);    
+         
+         
+     float DCz = 0;         //Duty cycle of Moment in z direction
+     float iz = 0;          //Current sent in z TR's
+     float Mz=M[2];         //Moment in z direction
+     iz = Mz * 0.3 ;        //Moment and Current always have the linear relationship
+     if( iz>0&& iz < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100%
+     {
+        DCz =  6*1000000*pow(iz,4) - 377291*pow(iz,3) + 4689.6*pow(iz,2) + 149.19*iz - 0.0008;
+        PWM3.period(timep);
+        PWM3 = DCz/100 ;
+     }
+     else if( iz >= 0.006&& iz < 0.0116)
+     { 
+        DCz = 1*100000000*pow(iz,4) - 5*1000000*pow(iz,3) + 62603*pow(iz,2) - 199.29*iz + 0.7648;
+        PWM3.period(timep);
+        PWM3 = DCz/100 ;             
+     }
+     else if (iz >= 0.0116&& iz < 0.0624)
+     {
+        DCz = 212444*pow(iz,4) - 33244*pow(iz,3) + 1778.4*pow(iz,2) + 120.91*iz + 0.3878;
+        PWM3.period(timep);
+        PWM3 = DCz/100 ;            
+     }
+     else if(iz >= 0.0624&& iz < 0.555)
+     {
+        printf("\n\rACS entered if\n\r");
+        DCz =  331.15*pow(iz,4) - 368.09*pow(iz,3) + 140.43*pow(iz,2) + 158.59*iz + 0.0338;
+        PWM3.period(timep);
+        PWM3 = DCz/100 ;            
+     }
+     else if(iz==0)
+     {
+        DCz = 0;
+        PWM3.period(timep);
+        PWM3 = DCz/100 ;            
+     }
+     else
+     {
+     // printf("!!!!!!!!!!Error!!!!!!!!!");
+     }   
+     pwm3 = PWM3; 
+     printf("\n\r icy :%f pwm : %f \n\r",iz,pwm3); 
+    
+     printf("\n\rExited PWMGEN function\n\r");
+}
+/*-------------------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------MAGNETOMETER-------------------------------------------------------------------------------------------------*/
+
+void trsub_mag()
+{
+  trflag_mag=0;
+} 
+
+void FUNC_ACS_MAG_INIT()
+{
+  DRDY = 0;
+  int a ;
+  a=DRDY;
+  printf("\n\r DRDY is %d\n\r",a);
+  SSN_MAG=1;                                   //pin is disabled
+  spi_acs.format(8,0);                         //8bits,Mode 0
+  spi_acs.frequency(100000);                   //clock frequency
+  SSN_MAG=0;                                   // Selecting pin
+  wait_ms(10);                                 //accounts for delay.can be minimised.
+  spi_acs.write(0x83);                      
+  wait_ms(10);              
+  unsigned char i;
+  for(i=0;i<3;i++)                            //initialising values.
+  {
+    spi_acs.write(0x00);                 //MSB of X,y,Z
+    spi_acs.write(0xc8);                 //LSB of X,Y,z;pointer increases automatically.
+  }
+  SSN_MAG=1;
+}
+
+void FUNC_ACS_MAG_EXEC(float mag_field[])
+{
+   printf("\n\rEntered magnetometer function\n\r");
+   DRDY.write(0);
+   int a;
+   a = DRDY;
+   printf("\n\r DRDY is %d\n\r",a);
+   SSN_MAG=0;                               //enabling slave to measure the values
+   wait_ms(10);
+   spi_acs.write(0x82);                     //initiates measurement
+   wait_ms(10);
+   spi_acs.write(0x01);                     //selecting x,y and z axes, measurement starts now
+   SSN_MAG=1;
+   wait_ms(10);
+
+   trflag_mag=1;        
+   tr_mag.attach(&trsub_mag,1);             //runs in background,makes trflag_mag=0 after 1s
+   DRDY.input();
+   while(trflag_mag)              /*initially flag is 1,so loop is executed,if DRDY is high,then data is retrieved and programme ends,else 
+                                 loop runs for at the max 1s and if still DRDY is zero,the flag becomes 0 and loop is not executed and 
+                               programme is terminated*/
+   {
+    wait_ms(5);
+    if(DRDY==1)
+    {
+        printf("\n\r DRDY is high\n");
+        SSN_MAG=0;
+        spi_acs.write(0xc9);                  //command  byte for retrieving data
+        unsigned char axis;
+        float Bnewvalue[3]={0.0,0.0,0.0};
+        int32_t Bvalue[3]={0,0,0}; 
+        int32_t a= pow(2.0,24.0);
+        int32_t b= pow(2.0,23.0);
+        for(axis=0;axis<3;axis++)
+        {
+            Bvalue[axis]=spi_acs.write(0x00)<<16;    //MSB 1 is send first 
+            wait_ms(10);
+            Bvalue[axis]|=spi_acs.write(0x00)<<8;    //MSB 2 is send next
+            wait_ms(10);
+            Bvalue[axis]|=spi_acs.write(0x00);       //LSB is send.....total length is 24 bits(3*8bits)...which are appended to get actual bit configuration
+            if((Bvalue[axis]&b)==b)              
+            {
+                Bvalue[axis]=Bvalue[axis]-a;   //converting 2s complement to  signed decimal
+
+            }
+            Bnewvalue[axis]=(float)Bvalue[axis]*22.0*pow(10.0,-3.0);  //1 LSB=(22nT)...final value of field obtained in micro tesla
+            wait_ms(10);
+            
+            mag_field[axis] = Bnewvalue[axis];
+            printf("\t%lf\n\r",mag_field[axis]);
+        }
+        SSN_MAG=1;
+        printf("\n\r exited magnetometer function\n");
+        //return Bnewvalue;          //return here? doubt..
+        
+        break;
+    }
+   }
+} 
+/*------------------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------torque to moment conversion------------------------------------------------------------------------------------------*/
+void moment_calc (float tauc[3], float b[3], float moment[3])
+{
+  float b1;
+  b1 = pow(b[0],2) + pow(b[1],2) +pow(b[2],2) ;
+  printf("\nvalue of b is %f\n",b1);
+  moment[0] = ((tauc[1]*b[2])-(tauc[2]*b[1]))/b1;
+  moment[1] = ((tauc[2]*b[0])-(tauc[0]*b[2]))/b1;
+  moment[2] = ((tauc[0]*b[1])-(tauc[1]*b[0]))/b1;
+  
+}
+
+
+/*------------------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------CONTROL ALGORITHM------------------------------------------------------------------------------------------*/
+
+void FUNC_ACS_CNTRLALGO(float *b,float *omega,float tauc[3])
+{
+    
+    float db[3]; // inputs
+    //initialization
+    float bb[3] = {0, 0, 0};
+    float d[3] = {0, 0, 0};
+    float Jm[3][3] = {{0.2730, 0, 0}, {0, 0.3018, 0}, {0, 0, 0.3031}};
+    float den = 0; 
+    float den2;
+    int i, j; //temporary variables
+    float Mu[2], z[2], dv[2], v[2], u[2]; //outputs
+    float invJm[3][3];
+    float kmu2 = 0.07, gamma2 = 1.9e4, kz2 = 0.4e-2, kmu = 0.003, gamma = 5.6e4, kz = 0.1e-4;
+    printf("\n\r Entered cntrl algo\n\r");
+    for(int i=0; i<3; i++) 
+        {
+        printf("%f\t",omega[i]);
+        }
+    for(int i=0; i<3; i++) 
+        {
+        printf("%f\t",b[i]);
+        }
+    //structure parameters
+    void inverse (float mat[3][3], float inv[3][3]); 
+    void getInput (float x[9]);
+    tauc[0] =tauc[1] =tauc[2]=0 ;
+    
+    den = sqrt((b[0]*b[0]) + (b[1]*b[1]) + (b[2]*b[2]));
+    den2 = (b[0]*db[0]) + (b[1]*db[1]) + (b[2]*db[2]);
+    for(i=0;i<3;i++)
+    {
+        db[i] = (db[i]*den*den-b[i]*den2) / (pow(den,3));
+        //db[i]/=den*den*den;
+    }
+    for(i=0;i<3;i++)                                        //gotta check what is this
+    {
+        printf("\n\rreached here\n\r");
+        if(den!=0)
+        b[i]=b[i]/den;                                      //there is a problem here. The code gets stuck here.  Maf value is required 
+    }
+        
+    // select kz, kmu, gamma
+    if(b[0]>0.9 || b[0]<-0.9)
+    {
+        kz = kz2;
+        kmu = kmu2;
+        gamma = gamma2;
+    }
+    // calculate Mu, v, dv, z, u
+    for(i=0;i<2;i++)
+    {
+        Mu[i] = b[i+1];
+        v[i] = -kmu*Mu[i];
+        dv[i] = -kmu*db[i+1];
+        z[i] = db[i+1] - v[i];
+        u[i] = -kz*z[i] + dv[i]-(Mu[i] / gamma);
+    }
+    inverse(Jm, invJm);
+    // calculate cross(omega,J*omega)for(i=0;i<3;i++)
+    for(i=0; i<3; i++)                          // for loop included after checking original code
+    { 
+        for(j=0;j<3;j++)
+            bb[i] += omega[j]*(omega[(i+1)%3]*Jm[(i+2)%3][j] - omega[(i+2)%3]*Jm[(i+1)%3][j]);
+    }
+    // calculate invJm*cross(omega,J*omega) store in d
+    for(i=0;i<3;i++)
+    {
+        for(j=0;j<3;j++)
+            d[i] += bb[j]*invJm[i][j];
+    }
+    // calculate d = cross(invJm*cross(omega,J*omega),b) -cross(omega,db) 
+    // bb =[0;u-d(2:3)] 
+    // store in bb
+    bb[1] = u[0] + (d[0]*b[2])-(d[2]*b[0])-(omega[0]*db[2]) + (omega[2]*db[0]);
+    bb[2] = u[1]-(d[0]*b[1]) + (d[1]*b[0]) + (omega[0]*db[1])-(omega[1]*db[0]);
+    bb[0] = 0;
+    // calculate N 
+    // reusing invJm as N
+    for(i=0;i<3;i++)
+    {
+        d[i] = invJm[1][i];
+        invJm[ 1][i] = b[2]*invJm[0][i] - b[0]*invJm[2][i];
+        invJm[2][i] = -b[1]*invJm[0][i] + b[0]*d[i];
+        invJm[0][i] = b[i];
+    }
+    // calculate inv(N) store in Jm
+    inverse(invJm, Jm);
+    // calculate tauc
+    printf("\n \r calculatin tauc");
+    for(i=0;i<3;i++)
+    {
+        for(j=0;j<3;j++)
+            tauc[i] += Jm[i][j]*bb[j];
+        printf(" %f \t",tauc[i]);    
+    }
+}
+
+ 
+void inverse(float mat[3][3], float inv[3][3])
+{
+int i, j;
+float det = 0;
+for(i=0;i<3;i++)
+{ for(j=0;j<3;j++)
+inv[j][i] = (mat[(i+1)%3][(j+1)%3]*mat[(i+2)%3][(j+2)%3]) - (mat[(i+2)%3]
+[(j+1)%3]*mat[(i+1)%3][(j+2)%3]);
+}
+det += (mat[0][0]*inv[0][0]) + (mat[0][1]*inv[1][0]) + (mat[0][2]*inv[2][0]);
+for(i=0;i<3;i++)
+{ for(j=0;j<3;j++)
+inv[i][j] /= det;
+}
+}
+
+
+/*-------------------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------GYROSCOPE-------------------------------------------------------------------------------------------------*/
+
+void trSub();               
+void drSub(); 
+void init_gyro();       
+float * FUNC_ACS_EXEC_GYR();
+
+void drSub()                    //In this function we setting data-ready flag to 1              
+{
+    drFlag=1;
+    //printf("\n dr interrupt detected");
+}
+void trSub()                    //In this function we are setting ticker flag to 0
+{
+    trFlag=0;
+}
+void FUNC_ACS_INIT_GYR()
+{
+    uint8_t response;               
+    ssn_gyr=1;                  //Deselecting the chip 
+    spi_acs.format(8,0);        // Spi format is 8 bits, and clock mode 3 
+    spi_acs.frequency(1000000); //frequency to be set as 1MHz
+    drFlag=0;                   //Intially defining data-ready flag to be 0 
+    dr.mode(PullDown);          
+    dr.rise(&drSub);
+    __disable_irq();
+    
+    /*initializing the register and changing its configuration*/
+    ssn_gyr=0;                           //Selecting chip(Mpu-3300)
+    spi_acs.write(USER_CTRL|READFLAG);   //sending USER_CTRL address with read bit
+    response=spi_acs.write(DUMMYBIT);    //sending dummy bit to get default values of the register
+                        
+    ssn_gyr=1;                  //Deselecting the chip  
+    wait(0.1);                  //waiting according the product specifications 
+    
+    ssn_gyr=0;                  //again selecting the chip  
+    spi_acs.write(USER_CTRL);           //sending USER_CTRL address without read bit 
+    spi_acs.write(response|BIT_I2C_IF_DIS);  //disabling the I2C mode in the register
+    ssn_gyr=1;                  //deselecting the chip 
+    wait(0.1);                  // waiting for 100ms before going for another register 
+    
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1|READFLAG); //Power Management register-1 
+    response=spi_acs.write(DUMMYBIT);
+    ssn_gyr=1;
+    wait(0.1);
+        
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1);
+    response=spi_acs.write(response|BIT_CLKSEL_X);  //Selecting the X axis gyroscope as clock as mentioned above 
+    ssn_gyr=1;                          
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(GYRO_CONFIG|READFLAG); //sending GYRO_CONFIG address with read bit
+    response=spi_acs.write(DUMMYBIT);
+    ssn_gyr=1;
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(GYRO_CONFIG); //sending GYRO_CONFIG address to write to register
+    spi_acs.write(response&(~(BITS_FS_SEL_3|BITS_FS_SEL_4))); //selecting a full scale mode of +/=225 deg/sec
+    ssn_gyr=1;
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(CONFIG|READFLAG); //sending CONFIG address with read bit
+    response=spi_acs.write(DUMMYBIT);
+    ssn_gyr=1;
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(CONFIG); //sending CONFIG address to write to register
+    spi_acs.write(response|BITS_DLPF_CFG); //selecting a bandwidth of 42 hz and delay of 4.8ms
+    ssn_gyr=1;
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(SMPLRT_DIV|READFLAG); //sending SMPLRT_DIV address with read bit
+    response=spi_acs.write(DUMMYBIT);
+    ssn_gyr=1;
+    wait(0.1);
+        
+    ssn_gyr=0;
+    spi_acs.write(SMPLRT_DIV); //sending SMPLRT_DIV address to write to register
+    spi_acs.write(response&BITS_SMPLRT_DIV); //setting the sampling rate division to be 0 to make sample rate = gyroscopic output rate
+    ssn_gyr=1;
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(INT_ENABLE|READFLAG);            //sending address of INT_ENABLE with readflag
+    response=spi_acs.write(DUMMYBIT);              //sending dummy byte to get the default values of the
+                                                                          // regiser
+    ssn_gyr=1;   
+    wait(0.1);
+    
+    ssn_gyr=0;
+    spi_acs.write(INT_ENABLE);                    //sending INT_ENABLE address to write to register
+    spi_acs.write(response|BIT_DATA_RDY_ENABLE);  //Enbling data ready interrupt
+    ssn_gyr=1;
+    wait(0.1);
+    
+    __enable_irq();
+}
+
+void FUNC_ACS_EXEC_GYR(float*omega)
+{
+    printf("\n\rEntered gyro\n\r");
+    uint8_t response;
+    uint8_t MSB,LSB;
+    int16_t bit_data;
+    float data[3],error[3]={0,0,0}; //declaring error array to add to the values when required
+    float senstivity = 145.6;     //senstivity is 145.6 for full scale mode of +/-225 deg/sec
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of INT_ENABLE with readflag
+    response=spi_acs.write(DUMMYBIT); //
+    ssn_gyr=1;
+    wait(0.1);
+        
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
+    response=spi_acs.write(response&(~(BIT_SLEEP))); //waking up the gyroscope from sleep
+    ssn_gyr=1;
+    wait(0.1);
+    
+    trFlag=1;
+    tr.attach(&trSub,1); //executes the function trSub afer 1sec
+    
+    while(trFlag)
+    {
+       // printf("\n\r check1");
+        wait_ms(5);   //This is required for this while loop to exit. I don't know why.
+        if(drFlag==1)
+        {
+            //printf("\n\r check2");
+            ssn_gyr=0;
+            spi_acs.write(GYRO_XOUT_H|READFLAG); //sending address of PWR_MGMT_1 with readflag
+            for(int i=0;i<3;i++)
+            {
+                MSB = spi_acs.write(DUMMYBIT); //reading the MSB values of x,y and z respectively
+                LSB = spi_acs.write(DUMMYBIT); //reading the LSB values of x,y and z respectively
+                bit_data= ((int16_t)MSB<<8)|LSB; //concatenating to get 16 bit 2's complement of the required gyroscope values
+                data[i]=(float)bit_data;
+                data[i]=data[i]/senstivity; //dividing with senstivity to get the readings in deg/sec
+                data[i]+=error[i]; //adding with error to remove offset errors
+            }
+            ssn_gyr=1;      
+            for (int i=0;i<3;i++)
+            {
+                printf("%f\t",data[i]); //printing the angular velocity values
+                omega[i] = data[i];
+            }
+            printf("\n\r");
+            break;
+        }
+            drFlag=0;
+    }
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1|READFLAG); //sending address of PWR_MGMT_1 with readflag
+    response=spi_acs.write(DUMMYBIT);
+    ssn_gyr=1;
+    wait(0.1);
+        
+    ssn_gyr=0;
+    spi_acs.write(PWR_MGMT_1); //sending PWR_MGMT_1 address to write to register
+    response=spi_acs.write(response|BIT_SLEEP); //setting the gyroscope in sleep mode
+    ssn_gyr=1;
+    wait(0.1);
+    printf("\n\rExited gyro\n\r");
+    
+}
+
+ 
+ 
+
+
+          
diff -r 000000000000 -r 1a04c0beef21 ACS.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ACS.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#include "math.h"
+
+void  FUNC_ACS_GENPWM(float *);
+void FUNC_ACS_MAG_EXEC(float *);
+void FUNC_ACS_MAG_INIT();
+//void  Read_data_acs()
+void moment_calc (float* , float* , float* );
+void FUNC_ACS_CNTRLALGO(float*,float*,float a[]);
+void FUNC_ACS_EXEC_GYR(float*);
+void FUNC_ACS_INIT_GYR();
+
diff -r 000000000000 -r 1a04c0beef21 HK.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,190 @@
+#include "HK.h"
+#include "pin_config.h"
+ 
+ 
+//GPIO pins used=> D2-D12, A0-A1
+
+DigitalOut SelectLinesA[]={PIN43,PIN44,PIN45,PIN46};         //to mux1=>voltage mux , PTA 13-16 , CHNGE TO PIN43 LATER
+DigitalOut SelectLinesB[]={PIN56,PIN57,PIN58,PIN59};         //to mux2=>current mux(differential mux) , PTB 3,7,8,9
+DigitalOut SelectLinesC[]={PIN64,PIN65,PIN66,PIN67};         //to mux3=>temp mux   PTB 18-21
+
+//--------------------------------------------MSB is SelectLines[0],LSB is SelectLines[3]-------------------------------- 
+
+AnalogIn CurrentInput(PIN53);                                // output from Current Mux PTB0
+AnalogIn VoltageInput(PIN54);                                // output from Voltage Multiplexer  PTB1
+AnalogIn TemperatureInput(PIN55);                            /*PTB2 output from Temperature Multiplexer,thermistor Multiplexer- same multiplexer for both(lines 1-4 for thermistor,line 0 for temperature sensor)*/ 
+
+
+
+int quantiz(float start,float step,float x)                  // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation)
+{
+    int y=(x-start)/step;
+    if(y<=0)y=0;
+    if(y>=15)y=15;
+    return y;
+}
+ 
+void init_beacon(ShortBeacy* x,SensorDataQuantised y)  
+{
+    (*x).Voltage[0]=2;                                       //quantised value
+    (*x).Temp[0]=y.PanelTemperature[0];                      //quantised value
+    (*x).Temp[1]=y.PanelTemperature[1];                      //quantised value
+    (*x).AngularSpeed[0]=y.AngularSpeed[0];
+    (*x).AngularSpeed[1]=y.AngularSpeed[1];
+    
+    (*x).SubsystemStatus[0]=145;                             //dummy values----------to be changed-------------------
+    (*x).ErrorFlag[0]=3;                                     //dummy values----------to be changed-------------------
+}
+
+SensorData Sensor; 
+SensorDataQuantised SensorQuantised;
+ShortBeacy Shortbeacon;
+void FUNC_HK_MAIN()             
+{
+    //define structure variables    
+ 
+    //initialise all selectlines to zeroes->1st line of muxes selected
+    SelectLinesA[0]=SelectLinesA[1]=SelectLinesA[2]=SelectLinesA[3]=0;
+    SelectLinesB[0]=SelectLinesB[1]=SelectLinesB[2]=0;
+    SelectLinesC[0]=SelectLinesC[1]=SelectLinesC[2]=SelectLinesC[3]=0;
+    
+    int LoopIterator;
+    int SelectLineIterator;
+    
+    float resistance_thermistor,voltage_thermistor;//for thermistor
+ 
+    //measurement from voltage sensor=> 16 sensors in place
+    for(LoopIterator=0; LoopIterator<16; LoopIterator++) 
+    {       
+        //read the sensor values and stores them in 'SensorData' structure's variable 'Sensor'
+        Sensor.Voltage[LoopIterator]=(VoltageInput.read()*3.3*5.545454);//resistors in voltage divider=>15Mohm,3.3Mohm
+               
+        if(LoopIterator%2==0)
+            SensorQuantised.Voltage[LoopIterator/2]=quantiz(vstart,vstep,Sensor.Voltage[LoopIterator]);
+                           
+        else
+            SensorQuantised.Voltage[(LoopIterator)/2]=SensorQuantised.Voltage[(LoopIterator)/2]<<4+quantiz(vstart,vstep,Sensor.Voltage[LoopIterator]);
+            
+        //iterate the select lines from 0 to 15
+        for(SelectLineIterator=3;SelectLineIterator>=0;SelectLineIterator--)
+        {
+            if(SelectLinesA[SelectLineIterator]==0)
+            {
+                SelectLinesA[SelectLineIterator]=1;
+                break;
+            }
+            else SelectLinesA[SelectLineIterator]=0;
+        }
+    
+        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
+    }
+ 
+ 
+    //measurement from current sensor=>  8 sensors in place 
+    for(LoopIterator=0; LoopIterator<8; LoopIterator++) 
+    {       
+        //read the sensor values and stores them in 'SensorData' structure variable 'Sensor'
+        Sensor.Current[LoopIterator]=(CurrentInput.read()*3.3/(50*rsens));
+        if(LoopIterator%2==0)
+            SensorQuantised.Current[LoopIterator/2]=quantiz(cstart,cstep,Sensor.Current[LoopIterator]);
+        else
+            SensorQuantised.Current[(LoopIterator)/2]=SensorQuantised.Current[(LoopIterator)/2]<<4+quantiz(cstart,cstep,Sensor.Current[LoopIterator]);
+
+        //iterate the select lines from 0 to 7
+        for(SelectLineIterator=2;SelectLineIterator>=0;SelectLineIterator--)
+        {
+            if(SelectLinesB[SelectLineIterator]==0)
+            {
+                SelectLinesB[SelectLineIterator]=1;
+                break;
+            }
+            else SelectLinesB[SelectLineIterator]=0;
+    
+        }
+    
+        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
+    }
+    
+    
+    //measurement of temperature
+    //temperature measurement=> 4 thermistors, 1 temperature sensor
+    //mux line 1=>temp sensor, mux lines 2 to 5 =>thermistors
+
+    for(LoopIterator=0; LoopIterator<5; LoopIterator++) 
+    {        
+        //read the sensor values and stores them in 'SensorData' structure variable 'Sensor'
+        Sensor.Temperature[LoopIterator]=(-90.7*3.3*TemperatureInput.read()+190.1543);
+        voltage_thermistor=TemperatureInput.read()*3.3;//voltage across thermistor
+        resistance_thermistor=24000*voltage_thermistor/(3.3-voltage_thermistor);//resistance of thermistor
+        if (LoopIterator==0)
+        {
+            printf(" \n\rTemp =%f",-90.7*3.3*TemperatureInput.read()+190.1543);
+        }
+        
+        if(LoopIterator%2==0)
+        {                
+            if(LoopIterator<1)                      //->corresponding to temperature sensor
+                SensorQuantised.Temperature[(LoopIterator)/2]=quantiz(tstart,tstep,Sensor.Temperature[LoopIterator]);
+         
+            else                                    //->corresponding to thermistor
+            {    
+                if(voltage_thermistor<1.378) //Temperature>12 degC
+                    Sensor.PanelTemperature[(LoopIterator-1)]=(3694/log(24.032242*resistance_thermistor));
+                    
+                else   
+                    Sensor.PanelTemperature[(LoopIterator-1)]=(3365.4792/log(7.60404*resistance_thermistor));
+                    
+                    
+                SensorQuantised.PanelTemperature[(LoopIterator-1)/2]=quantiz(tstart_thermistor,tstep_thermistor,Sensor.PanelTemperature[(LoopIterator-1)]);
+                    
+            }
+        } 
+        else
+        {           
+            if(LoopIterator<1)
+                SensorQuantised.Temperature[(LoopIterator)/2]=SensorQuantised.Temperature[(LoopIterator)/2]<<4+quantiz(tstart,tstep,Sensor.Temperature[LoopIterator]); 
+            
+            else
+            {  
+                if(voltage_thermistor<1.378) //Temperature>12 degC 
+                    Sensor.PanelTemperature[LoopIterator-1]=(3694/log(24.032242*resistance_thermistor));
+                else
+                    Sensor.PanelTemperature[LoopIterator-1]=(3365.4792/log(7.60404*resistance_thermistor));
+                    
+                SensorQuantised.PanelTemperature[(LoopIterator-1)/2]=SensorQuantised.PanelTemperature[(LoopIterator-1)/2]<<4+quantiz(tstart_thermistor,tstep_thermistor,Sensor.PanelTemperature[LoopIterator-1]);
+            }
+        }
+       // The following lines are used to iterate the select lines from 0 to 4
+       for(SelectLineIterator=3;SelectLineIterator>=0;SelectLineIterator--)
+       {
+            if(SelectLinesC[SelectLineIterator]==0)
+            {
+                SelectLinesC[SelectLineIterator]=1;
+                break;
+            }
+            else SelectLinesC[SelectLineIterator]=0;
+        }
+    
+        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
+    }
+
+
+    //update magnetometer data->
+    //populate values in structure variable 'Sensor' from data to be given by Green
+     SensorQuantised.AngularSpeed[0]=quantiz(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[1]);
+     SensorQuantised.AngularSpeed[0]=SensorQuantised.AngularSpeed[0]<<4+quantiz(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[0]);
+     SensorQuantised.AngularSpeed[1]=quantiz(AngularSpeed_start,AngularSpeed_step,Sensor.AngularSpeed[2]);
+     
+    //update gyro data->
+    //populate values in structure variable 'Sensor' from data to be given by Green
+     SensorQuantised.Bnewvalue[0]=quantiz(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[1]);
+     SensorQuantised.Bnewvalue[0]=SensorQuantised.Bnewvalue[0]<<4+quantiz(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[0]);
+     SensorQuantised.Bnewvalue[1]=quantiz(Bnewvalue_start,Bnewvalue_step,Sensor.Bnewvalue[2]);
+
+     //update beacon structure
+     init_beacon(&Shortbeacon,SensorQuantised);//Shortbeacon is passed 
+     printf("\n here temperature :%d",SensorQuantised.Temperature);    
+}
+
+
+    
diff -r 000000000000 -r 1a04c0beef21 HK.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HK.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,69 @@
+//to be saved as HK.h
+
+#include "mbed.h"
+#define tstart -40
+#define tstep 8
+#define tstep_thermistor 8//verify!!
+#define tstart_thermistor -40
+#define vstart 3.3
+#define vstep 0.84667
+#define cstart 0.0691
+#define cstep 0.09133
+#define rsens 0.095
+#define Bnewvalue_start -100//in microTesla...max possible field is .0001 T 
+#define Bnewvalue_step 13.333
+#define AngularSpeed_start -10//max possible ang. velocity in space is 10 deg/sec
+#define AngularSpeed_step 1.3333 
+
+
+
+typedef struct SensorData
+{
+    float Voltage[16];
+    float Current[8];
+    float Temperature[1];
+    float PanelTemperature[4];
+    float BatteryTemperature;   //to be populated
+    char faultpoll;             //polled faults
+    char faultir;               //interrupted faults
+    char power_mode;            //power modes
+    
+    float AngularSpeed[3];      //in order x,y,z
+    float Bnewvalue[3];         //in order Bx,By,Bz
+   
+    
+} SensorData;
+
+ 
+typedef struct SensorDataQuantised {
+    char Voltage[8];
+    char Current[4];
+    char Temperature[1];
+    char PanelTemperature[2];//read by the 4 thermistors on solar panels
+    char BatteryTemperature;    //to be populated   
+    char faultpoll;             //polled faults
+    char faultir;               //interrupted faults
+    char power_mode;            //power modes
+    char AngularSpeed[2];
+    char Bnewvalue[2];
+    
+    //float magnetometer,gyro=>to be addes
+} SensorDataQuantised;
+ 
+ 
+typedef struct ShortBeacon 
+{
+    char Voltage[1];                            //battery voltage from gauge, needs  to be quantised
+    char AngularSpeed[2];                       //all the 3 data
+    char SubsystemStatus[1];                    //power modes
+    char Temp[2];                               //temp of solar panel
+    //Temp[0]'s LSB=> PanelTemperature[0], Temp[0]'s MSB=> PanelTemperature[1], Temp[1]'s LSB=> PanelTemperature[2], Temp[1]'s MSB=> PanelTemperature[3]
+    char ErrorFlag[1];                          //fault
+}ShortBeacy; 
+ 
+ 
+ 
+void FUNC_HK_MAIN();
+ 
+int quantiz(float start,float step,float x);
+void init_beacon(ShortBeacy* x,SensorDataQuantised y);
diff -r 000000000000 -r 1a04c0beef21 MPU3300.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPU3300.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,26 @@
+//MPU 3300 registers
+#define SMPLRT_DIV    0x19
+#define CONFIG        0x1A
+#define GYRO_CONFIG   0x1B
+#define GYRO_XOUT_H   0x43
+#define GYRO_XOUT_L   0x44
+#define GYRO_YOUT_H   0x45
+#define GYRO_YOUT_L   0x46
+#define GYRO_ZOUT_H   0x47
+#define GYRO_ZOUT_L   0x48
+#define USER_CTRL     0x6A
+#define PWR_MGMT_1    0x6B
+#define INT_ENABLE    0x38
+
+//MPU configuration bits
+#define READFLAG                  0x80
+#define DUMMYBIT                  0x00
+#define BITS_DLPF_CFG             0x07
+#define BITS_FS_SEL_3             0x08
+#define BITS_FS_SEL_4             0x10
+#define BIT_I2C_IF_DIS            0x10
+#define BITS_SMPLRT_DIV           0x00
+#define BIT_SLEEP                 0x40
+#define BIT_DATA_RDY_ENABLE       0x01
+#define BIT_DATA_RDY_INT          0x01
+#define BIT_CLKSEL_X              0x01
diff -r 000000000000 -r 1a04c0beef21 beacon.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/beacon.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,238 @@
+//switch off the sync!!!!!!!
+//switch off the preamble!!!!!!!
+/*for crc in tx:
+regIrq2(0x28) :
+
+regpacketconfig 1(0x37) :
+set crc detection/calc. on : | 0x10
+crcautoclearoff : | 0x08  
+
+for data whitening : regpacketconfig 1(0x37) :| 0x40
+for 
+
+
+
+*/
+// 6CC000 for 435 MHz
+//set all values as FF for checking on spectrum analyzer
+#include "beacon.h"
+#include "HK.h"
+#include "pin_config.h"
+Serial chavan(USBTX, USBRX);           // tx, rx
+//SPI spi(PIN2,PIN1,PIN3);               // mosi, miso, sclk
+DigitalOut cs(PIN6);                 //slave select or chip select
+//SPI spi(PTD6,PTD7,PTD5);               // mosi, miso, sclk
+SPI spi(PIN16,PIN17,PIN15);
+//DigitalOut cs_bar(PTC11);              //slave select or chip select
+//InterruptIn button(p9);
+//#define TIMES 16      
+//Timer t;
+
+/*void interrupt_func()
+{
+    chavan.printf("INTERRUPT_FUNC TRIGGERED\n  wait for 3 secs\n"); 
+    wait(3);
+    
+}*/
+
+
+    
+extern  ShortBeacy Shortbeacon;
+
+void writereg(uint8_t reg,uint8_t val)
+{
+    cs = 0;__disable_irq();spi.write(reg | 0x80);spi.write(val);__enable_irq();cs = 1;
+}
+uint8_t readreg(uint8_t reg)
+{
+    int val;cs = 0;__disable_irq();spi.write(reg & ~0x80);val = spi.write(0);__enable_irq();cs = 1;return val;
+}
+void clearTxBuf()
+{
+    writereg(RF22_REG_08_OPERATING_MODE2,0x01);
+    writereg(RF22_REG_08_OPERATING_MODE2,0x00);
+}
+void clearRxBuf()
+{
+    writereg(RF22_REG_08_OPERATING_MODE2,0x02);
+    writereg(RF22_REG_08_OPERATING_MODE2,0x00);
+}
+int setFrequency(float centre,float afcPullInRange)
+{
+//freq setting begins 
+    uint8_t fbsel = 0x40;
+    uint8_t afclimiter;
+    if (centre >= 480.0) {
+        centre /= 2;
+        fbsel |= 0x20;
+        afclimiter = afcPullInRange * 1000000.0 / 1250.0;
+    } else {
+        if (afcPullInRange < 0.0 || afcPullInRange > 0.159375)
+            return false;
+        afclimiter = afcPullInRange * 1000000.0 / 625.0;
+    }
+    centre /= 10.0;
+    float integerPart = floor(centre);
+    float fractionalPart = centre - integerPart;
+ 
+    uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23
+    fbsel |= fb;
+    uint16_t fc = fractionalPart * 64000;
+    writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0);  // REVISIT
+    writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0);
+    writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
+    writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
+    writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
+    writereg(RF22_REG_2A_AFC_LIMITER, afclimiter);
+    return 0;
+}
+    
+
+
+void init_beacon()
+{
+    //reset()
+    writereg(RF22_REG_07_OPERATING_MODE1,0x80);        //sw_reset
+    wait(1);                    //takes time to reset                                  
+
+    clearTxBuf();                                                             
+    clearRxBuf();                                                             
+    //txfifoalmostempty
+    writereg(RF22_REG_7D_TX_FIFO_CONTROL2,5);
+    //rxfifoalmostfull
+    writereg(RF22_REG_7E_RX_FIFO_CONTROL,20);
+    //Packet-engine registers
+    writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x8E);    //RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM
+    //&0x77 = diasable packet rx-tx handling
+    writereg(RF22_REG_32_HEADER_CONTROL1,0x88);    //RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3
+    writereg(RF22_REG_33_HEADER_CONTROL2,0x42);    //RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2
+    writereg(RF22_REG_34_PREAMBLE_LENGTH,8);       //RF22_REG_34_PREAMBLE_LENGTH, nibbles); preamble length = 8;
+    writereg(RF22_REG_36_SYNC_WORD3,0x2D);    //syncword3=2D
+    writereg(RF22_REG_37_SYNC_WORD2,0xD4);    //syncword2=D4
+    writereg(RF22_REG_3F_CHECK_HEADER3,0);    //RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS
+    writereg(RF22_REG_3A_TRANSMIT_HEADER3,0xab);    //header_to
+    writereg(RF22_REG_3B_TRANSMIT_HEADER2,0xbc);    //header_from 
+    writereg(RF22_REG_3C_TRANSMIT_HEADER1,0xcd);    //header_ids
+    writereg(RF22_REG_3D_TRANSMIT_HEADER0,0xde);    //header_flags
+    writereg(RF22_REG_3F_CHECK_HEADER3,0xab);   
+    writereg(RF22_REG_40_CHECK_HEADER2,0xbc);   
+    writereg(RF22_REG_41_CHECK_HEADER1,0xcd);   
+    writereg(RF22_REG_42_CHECK_HEADER0,0xde);
+    
+    //RSSI threshold for clear channel indicator
+    writereg(RF22_REG_27_RSSI_THRESHOLD,0xA5);         //55 for -80dBm, 2D for -100dBm, 7D for -60dBm, A5 for -40dBm, CD for -20 dBm
+    
+    writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state                        ??
+    writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state                        ??
+    
+    //interrupts
+    // spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM |RF22_ENRXFFAFULL | RF22_ENPKSENT |RF22_ENPKVALID| RF22_ENCRCERROR);
+    // spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL);
+    
+    setFrequency(435.0, 0.05);
+    
+    //return !(statusRead() & RF22_FREQERR);
+    if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
+    printf("frequency not set properly\n");
+    //frequency set
+
+    //setModemConfig(FSK_Rb2_4Fd36);       FSK_Rb2_4Fd36,       ///< FSK, No Manchester, Rb = 2.4kbs,  Fd = 36kHz
+    //setmodemregisters
+    //0x1b, 0x03, 0x41, 0x60, 0x27, 0x52, 0x00, 0x07, 0x40, 0x0a, 0x1e, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x22, 0x3a = FSK_RB2_4FD36
+    //0xc8, 0x03, 0x39, 0x20, 0x68, 0xdc, 0x00, 0x6b, 0x2a, 0x08, 0x2a, 0x80, 0x60, 0x13, 0xa9, 0x2c, 0x21, 0x08 = OOK,2.4, 335
+    writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0x2B);
+    writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03);
+    writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x41);
+    writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x60);                     
+    writereg(RF22_REG_22_CLOCK_RECOVERY_OFFSET1,0x27);           //updated 20 to 25 reg values from excel sheet for 1.2 Khz freq. deviation,fsk
+    writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0x52);
+    writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00);
+    writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x51);
+    /*writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2a);
+    writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x08);*/    //not required for fsk (OOK counter value)
+    writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x1e);         //??
+    writereg(RF22_REG_58,0x80);
+    writereg(RF22_REG_69_AGC_OVERRIDE1,0x60);
+    writereg(RF22_REG_6E_TX_DATA_RATE1,0x09);
+    writereg(RF22_REG_6F_TX_DATA_RATE0,0xd5);
+    writereg(RF22_REG_70_MODULATION_CONTROL1,0x2c);
+    writereg(RF22_REG_71_MODULATION_CONTROL2,0x22);//ook = 0x21 //fsk = 0x22
+    writereg(RF22_REG_72_FREQUENCY_DEVIATION,0x02);
+    //set tx power
+    writereg(RF22_REG_6D_TX_POWER,0x07);    //20dbm
+    writereg(RF22_REG_3E_PACKET_LENGTH,TX_DATA); //packet length     
+}
+
+void FUNC_BEA()
+{
+    init_beacon();
+    printf("\nBeacon function entered\n");
+    wait(1);                     // wait for POR to complete   //change the timing later
+    cs=1;                          // chip must be deselected
+    wait(1);                    //change the time later
+    spi.format(8,0);
+    spi.frequency(10000000);       //10MHz SCLK    
+    if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08) printf("spi connection valid\n");
+    else printf("error in spi connection\n");
+
+       
+    
+    //********
+    //button.rise(&interrupt_func);         //interrupt enabled ( rising edge of pin 9)
+    wait(0.02);                                                           // pl. update this value  or even avoid it!!!                  
+    //extract values from short_beacon[]
+    uint8_t byte_counter = 0;
+    /*struct Short_beacon{
+        uint8_t Voltage[1];
+        uint8_t AngularSpeed[2];
+        uint8_t SubsystemStatus[1];
+        uint8_t Temp[3];
+        uint8_t ErrorFlag[1];
+        }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} };
+    */
+    //filling hk data
+    uint8_t short_beacon[] = { 0xAB, 0x8A, 0xE2, 0xBB, 0xB8, 0xA2, 0x8E,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]};
+    
+    for(int i = 0; i < 15 ; i++)
+    {
+        printf("0x%X\n",(short_beacon[i]));    
+    }
+    //tx settings begin
+    //setModeIdle();
+    writereg(RF22_REG_07_OPERATING_MODE1,0x01);        //ready mode
+    //fillTxBuf(data, len);
+    clearTxBuf();  
+        
+    //Set to Tx mode
+    writereg(RF22_REG_07_OPERATING_MODE1,0x09);
+    
+    while(byte_counter!=15){
+        //Check for fifoThresh
+        while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
+        //writing again
+        cs = 0;
+        spi.write(0xFF);   
+        for(int i=7; i>=0 ;i--)
+        {
+            //pc.printf("%d\n",byte_counter);
+            if((short_beacon[byte_counter] & (uint8_t) pow(2.0,i))!=0)
+        {
+            spi.write(0xFF);
+            spi.write(0xFF);
+        }
+        else
+        {
+            spi.write(0x00);
+            spi.write(0x00);
+            
+        }
+        }
+        cs = 1;
+        byte_counter++;
+        
+    }  
+    //rf22.waitPacketSent();
+    while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04);//pc.printf(" chck pkt sent!\n");     
+    printf("\nBeacon function exiting\n");
+    
+}
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 beacon.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/beacon.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,108 @@
+#include "mbed.h"
+
+
+ 
+#define TIMES 20
+#define RX_DATA 240      //in bytes
+#define TX_DATA 240      //in bytes
+
+void writereg(uint8_t reg,uint8_t val);
+uint8_t readreg(uint8_t reg);
+void clearTxBuf();
+void clearRxBuf();
+int setFrequency(float,float);
+void init_beacon();
+void FUNC_BEA();
+
+#define RF22_MAX_MESSAGE_LEN 255
+// These values we set for FIFO thresholds
+#define RF22_TXFFAEM_THRESHOLD 4
+#define RF22_RXFFAFULL_THRESHOLD 55
+
+// Register names
+#define RF22_REG_00_DEVICE_TYPE                         0x00
+#define RF22_REG_02_DEVICE_STATUS                       0x02
+#define RF22_REG_03_INTERRUPT_STATUS1                   0x03
+#define RF22_REG_04_INTERRUPT_STATUS2                   0x04
+#define RF22_REG_07_OPERATING_MODE1                     0x07
+#define RF22_REG_08_OPERATING_MODE2                     0x08
+#define RF22_REG_09_OSCILLATOR_LOAD_CAPACITANCE         0x09
+#define RF22_REG_0B_GPIO_CONFIGURATION0                 0x0b
+#define RF22_REG_0C_GPIO_CONFIGURATION1                 0x0c
+#define RF22_REG_0D_GPIO_CONFIGURATION2                 0x0d
+#define RF22_REG_1C_IF_FILTER_BANDWIDTH                 0x1c
+#define RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE   0x1f
+#define RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE    0x20
+#define RF22_REG_21_CLOCK_RECOVERY_OFFSET2              0x21
+#define RF22_REG_22_CLOCK_RECOVERY_OFFSET1              0x22
+#define RF22_REG_23_CLOCK_RECOVERY_OFFSET0              0x23
+#define RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1    0x24
+#define RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0    0x25
+#define RF22_REG_26_RSSI                                0x26
+#define RF22_REG_27_RSSI_THRESHOLD                      0x27
+#define RF22_REG_28_ANTENNA_DIVERSITY1                  0x28
+#define RF22_REG_29_ANTENNA_DIVERSITY2                  0x29
+#define RF22_REG_2A_AFC_LIMITER                         0x2a
+#define RF22_REG_2B_AFC_CORRECTION_READ                 0x2b
+#define RF22_REG_2C_OOK_COUNTER_VALUE_1                 0x2c
+#define RF22_REG_2D_OOK_COUNTER_VALUE_2                 0x2d
+#define RF22_REG_2E_SLICER_PEAK_HOLD                    0x2e
+#define RF22_REG_30_DATA_ACCESS_CONTROL                 0x30
+#define RF22_REG_31_EZMAC_STATUS                        0x31
+#define RF22_REG_32_HEADER_CONTROL1                     0x32
+#define RF22_REG_33_HEADER_CONTROL2                     0x33
+#define RF22_REG_34_PREAMBLE_LENGTH                     0x34
+#define RF22_REG_35_PREAMBLE_DETECTION_CONTROL1         0x35
+#define RF22_REG_36_SYNC_WORD3                          0x36
+#define RF22_REG_37_SYNC_WORD2                          0x37
+#define RF22_REG_38_SYNC_WORD1                          0x38
+#define RF22_REG_39_SYNC_WORD0                          0x39
+#define RF22_REG_3A_TRANSMIT_HEADER3                    0x3a
+#define RF22_REG_3B_TRANSMIT_HEADER2                    0x3b
+#define RF22_REG_3C_TRANSMIT_HEADER1                    0x3c
+#define RF22_REG_3D_TRANSMIT_HEADER0                    0x3d
+#define RF22_REG_3E_PACKET_LENGTH                       0x3e
+#define RF22_REG_3F_CHECK_HEADER3                       0x3f
+#define RF22_REG_40_CHECK_HEADER2                       0x40
+#define RF22_REG_41_CHECK_HEADER1                       0x41
+#define RF22_REG_42_CHECK_HEADER0                       0x42
+#define RF22_REG_43_HEADER_ENABLE3                      0x43
+#define RF22_REG_44_HEADER_ENABLE2                      0x44
+#define RF22_REG_45_HEADER_ENABLE1                      0x45
+#define RF22_REG_46_HEADER_ENABLE0                      0x46
+#define RF22_REG_47_RECEIVED_HEADER3                    0x47
+#define RF22_REG_48_RECEIVED_HEADER2                    0x48
+#define RF22_REG_49_RECEIVED_HEADER1                    0x49
+#define RF22_REG_4A_RECEIVED_HEADER0                    0x4a
+#define RF22_REG_4B_RECEIVED_PACKET_LENGTH              0x4b
+#define RF22_REG_58                                     0x58
+#define RF22_REG_60_CHANNEL_FILTER_COEFFICIENT_ADDRESS  0x60
+#define RF22_REG_61_CHANNEL_FILTER_COEFFICIENT_VALUE    0x61
+#define RF22_REG_62_CRYSTAL_OSCILLATOR_POR_CONTROL      0x62
+#define RF22_REG_63_RC_OSCILLATOR_COARSE_CALIBRATION    0x63
+#define RF22_REG_64_RC_OSCILLATOR_FINE_CALIBRATION      0x64
+#define RF22_REG_65_LDO_CONTROL_OVERRIDE                0x65
+#define RF22_REG_66_LDO_LEVEL_SETTINGS                  0x66
+#define RF22_REG_67_DELTA_SIGMA_ADC_TUNING1             0x67
+#define RF22_REG_68_DELTA_SIGMA_ADC_TUNING2             0x68
+#define RF22_REG_69_AGC_OVERRIDE1                       0x69
+#define RF22_REG_6A_AGC_OVERRIDE2                       0x6a
+#define RF22_REG_6B_GFSK_FIR_FILTER_COEFFICIENT_ADDRESS 0x6b
+#define RF22_REG_6C_GFSK_FIR_FILTER_COEFFICIENT_VALUE   0x6c
+#define RF22_REG_6D_TX_POWER                            0x6d
+#define RF22_REG_6E_TX_DATA_RATE1                       0x6e
+#define RF22_REG_6F_TX_DATA_RATE0                       0x6f
+#define RF22_REG_70_MODULATION_CONTROL1                 0x70
+#define RF22_REG_71_MODULATION_CONTROL2                 0x71
+#define RF22_REG_72_FREQUENCY_DEVIATION                 0x72
+#define RF22_REG_73_FREQUENCY_OFFSET1                   0x73
+#define RF22_REG_74_FREQUENCY_OFFSET2                   0x74
+#define RF22_REG_75_FREQUENCY_BAND_SELECT               0x75
+#define RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1          0x76
+#define RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0          0x77
+#define RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT    0x79
+#define RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE         0x7a
+#define RF22_REG_7C_TX_FIFO_CONTROL1                    0x7c
+#define RF22_REG_7D_TX_FIFO_CONTROL2                    0x7d
+#define RF22_REG_7E_RX_FIFO_CONTROL                     0x7e
+#define RF22_REG_7F_FIFO_ACCESS                         0x7f
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 fault.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fault.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,171 @@
+#include "fault.h"
+#include "HK.h"
+
+
+DigitalIn fault0(FAULT0,PullUp);
+DigitalIn fault1(FAULT1,PullUp);
+DigitalIn fault2(FAULT2,PullUp);
+DigitalIn fault3(FAULT3,PullUp);
+DigitalIn fault4(FAULT4,PullUp);
+//DigitalIn fault5(FAULT5,PullUp);
+//DigitalIn fault6(FAULT6,PullUp);
+//DigitalIn fault7(FAULT7,PullUp);
+//DigitalIn fault8(FAULT8,PullUp);
+
+
+InterruptIn fault_IR1(FAULT5);
+InterruptIn fault_IR2(FAULT6);
+InterruptIn fault_IR3(FAULT7);
+InterruptIn fault_IR4(FAULT8);
+InterruptIn fault_IR5(FAULT9);
+
+
+
+DigitalOut clear1(FAULT_CLEAR1,0);
+DigitalOut clear2(FAULT_CLEAR2,0);
+DigitalOut clear3(FAULT_CLEAR3,0);
+DigitalOut clear4(FAULT_CLEAR4,0);
+DigitalOut clear5(FAULT_CLEAR5);
+DigitalOut clear6(FAULT_CLEAR6);
+DigitalOut clear7(FAULT_CLEAR7);
+DigitalOut clear8(FAULT_CLEAR8);
+DigitalOut clear9(FAULT_CLEAR9);
+
+
+DigitalOut acs_active(ACS);
+
+
+BusIn fault_poll(FAULT0,FAULT1,FAULT2,FAULT3,FAULT4);
+BusIn fault_ir(FAULT5,FAULT6,FAULT7,FAULT8,FAULT9);
+
+BusOut clear_poll(FAULT_CLEAR1,FAULT_CLEAR2,FAULT_CLEAR3,FAULT_CLEAR4);//to send fault data along with hk
+BusOut clear_ir(FAULT_CLEAR5,FAULT_CLEAR6,FAULT_CLEAR7,FAULT_CLEAR8,FAULT_CLEAR9);
+
+
+extern SensorDataQuantised SensorQuantised;
+extern int beacon_sc;  //to switch beacon between low and high power mode
+extern int acs_pflag;  //to activate/deactivate control algo  
+char out_poll;
+char out_ir;
+
+void FUNC_HK_FAULTS()
+{
+        
+        printf("Entered Fault management \n");
+        /*if(fault0==0)printf("CHARGER IS CHARGING THE BATTERY ");
+        if(fault0==1)printf("CHRGER GONE OFFLINE, BATTERY DRAINING");
+        
+        if(fault1==0)
+        {  clear1=1;printf("");}
+        else
+        { clear1=0;}
+        
+         if(fault2==0)
+        {  clear2=1;printf("");}
+        else
+        { clear2=0;}
+        
+         if(fault3==0)
+        {  clear3=1;printf("");}
+        else
+        { clear3=0;}
+        
+         if(fault4==0)
+        {  clear4=1;printf("");}
+        else
+        { clear4=0;}*/
+      
+     
+        
+         
+        
+        
+        
+        
+        //clear1 = !fault1; 
+        //clear2 = !fault2; 
+        //clear3 = !fault3; 
+        //clear4 = !fault4; 
+        //clear5 = !fault5; 
+        //clear6 = !fault6; 
+        //clear7 = !fault7; 
+        //clear8 = !fault8; 
+        
+        
+       // out_poll = clear_poll;
+        //out_ir = clear_ir;
+        SensorQuantised.faultpoll = fault_poll ;
+        SensorQuantised.faultir=fault_ir ;
+        printf(" %d , %d \n ",SensorQuantised.faultpoll, SensorQuantised.faultir ) ;
+        
+}
+
+
+
+void Clear_IR1()
+{clear5=!clear5;
+}
+
+void Clear_IR2()
+{clear6=!clear6;
+}
+
+void Clear_IR3()
+{clear7=!clear7;
+}
+
+void Clear_IR4()
+{clear8=!clear8;
+}
+
+void Clear_IR5()
+{clear9=!clear9;
+}
+
+void interrupt_fault()
+{
+    fault_IR1.rise(&Clear_IR1);
+    fault_IR2.rise(&Clear_IR2);
+    fault_IR3.rise(&Clear_IR3);
+    fault_IR4.rise(&Clear_IR4);
+    fault_IR5.rise(&Clear_IR5);
+    fault_IR1.fall(&Clear_IR1);
+    fault_IR2.fall(&Clear_IR2);
+    fault_IR3.fall(&Clear_IR3);
+    fault_IR4.fall(&Clear_IR4);
+    fault_IR5.fall(&Clear_IR5);
+}
+    
+
+void FUNC_HK_POWER(char flag)                  //flag corresponds to the power mode
+{
+    printf("Entered Power Management \n"); 
+    printf("Entering mode %c \n", flag);
+    switch (flag)
+    {
+        case '0': beacon_sc = 6;            //least power mode
+                  acs_pflag = 0;
+                  acs_active = 0;            //switching off a component of acs 
+        break;
+        case '1': beacon_sc = 3;
+                  acs_pflag = 0;
+                  acs_active = 0;
+        break;
+        case '2': beacon_sc = 3;
+                  acs_pflag = 0;
+                  acs_active = 0;
+        break;
+        case '3': beacon_sc = 3;             //normal mode  
+                  acs_pflag = 1;
+                  acs_active = 1;
+        break;    
+    }
+  /*  if (flag == '0')
+    beacon_sc = 30;
+    else
+    beacon_sc = 3;
+    if (flag == '3')
+    acs_pflag = 1;
+    else
+    acs_pflag = 0; */
+}
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 fault.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fault.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,35 @@
+#include "mbed.h"
+#include "pin_config.h"
+
+#define FAULT0 PIN31      //  CHARGEBAR
+#define FAULT1 PIN42      //  FAULTBAR
+#define FAULT2 PIN40      //  3V3APGOOD 
+#define FAULT3 PIN39      //  3V3BPGOOD                         
+#define FAULT4  PIN41     //  3V3CPGOOD      
+#define FAULT5  PIN79      //  3V3AOCBAR 
+#define FAULT6  PIN80      //  3V3COC  
+#define FAULT7  PIN89     //   TRZ
+#define FAULT8  PIN83      //  TRXTRY  
+#define FAULT9 PIN97      //  SW0FAULT  
+
+//CONTROL SIGNALS
+#define FAULT_CLEAR1 D8                            
+#define FAULT_CLEAR2 D9
+#define FAULT_CLEAR3 D10
+#define FAULT_CLEAR4 D11
+#define FAULT_CLEAR5 D12
+#define FAULT_CLEAR6 D13
+#define FAULT_CLEAR7 PTC7
+#define FAULT_CLEAR8 PTC10
+#define FAULT_CLEAR9 PTC11
+
+
+
+#define ACS PTE2
+#define TX PTE3
+#define PAYLOAD PTE6
+
+
+void FUNC_HK_FAULTS();
+void FUNC_HK_POWER(char flag);
+void interrupt_fault();
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,422 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "HK.h"
+#include "slave.h"
+#include "beacon.h"
+#include "ACS.h"
+#include "fault.h"
+#include "slave.h"
+#include "mnm.h"
+
+Serial pc(USBTX, USBRX);
+
+InterruptIn interrupt(PIN97);                                      //I2c interrupt from CDMS
+DigitalOut data_ready(PIN90);                                     //Sends interrupt to CDMS
+
+Timer t;                                                        //To know the time of execution each thread
+Timer t1;                                                       //To know the time of entering  of each thread
+Timer t2;                                                       //To check the time sync in i2c communication
+Timer t3;                                                       //To know the time taken by i2c read/write function
+Timer i1;
+Timer i2;
+/*****************************************************************Threads USed***********************************************************************************/
+//Thread *ptr_t_hk_acq;
+//Thread *ptr_t_acs;
+//Thread *ptr_t_bea;
+Thread *ptr_t_i2c;
+Thread *ptr_t_wdt;
+Thread *ptr_t_abh;  
+
+/****************************************************************configuring I2c*********************************************************************************/
+I2CSlave slave(PIN72,PIN71);                                        //configuring pins p27, p28 as I2Cslave
+
+int i2c_status=0;                                               //read or write mode for i2c 0: write2slave 1: write2master
+typedef struct                                                  //structure of i2c data 
+{
+    char data[25];                          
+    int length;
+}i2c_data;
+
+
+//Mail<i2c_data,16> i2c_data_receive;
+Mail<i2c_data,16> i2c_data_send;
+
+void F_HK_ACQ();
+void F_ACS();
+void F_BEA();
+
+
+//---------------------------------------------------------------------------------------------------------------------------------------
+//ACS HK BEACON
+//---------------------------------------------------------------------------------------------------------------------------------------
+int beacon_sc = 3;
+uint16_t tcount=1;
+void T_ABH(void const *args)
+{
+ while(1)
+   {
+        
+        Thread::signal_wait(0x1);
+        if(tcount == 65532)                         //to reset the counter
+        {
+        tcount = 0;
+        }
+        if(tcount%1==0)
+        {
+        F_ACS();
+        }
+        if(tcount%2==0)
+        {
+        F_HK_ACQ();
+        }
+        if(tcount%beacon_sc==0)
+        {
+        F_BEA();
+        }
+      
+        tcount++;
+   }     
+    
+
+}
+
+
+//--------------------------------------------------------------------------------------------------------------------------------------------------
+//TASK 2 : HK
+//--------------------------------------------------------------------------------------------------------------------------------------------------
+
+char hk_data[25];
+extern SensorDataQuantised SensorQuantised;
+void F_HK_ACQ()
+{
+    SensorQuantised.power_mode='3';                          //default power mode(dummy)
+    printf("\n\rTHIS IS HK    %f\n\r",t1.read());
+    t.start();
+    FUNC_HK_FAULTS();                                        // !Actual fault management is not implemented
+    FUNC_HK_POWER(SensorQuantised.power_mode);               // !The power mode algorithm is yet to be obtained
+    FUNC_HK_MAIN();                                          //Collecting HK data
+    FUNC_I2C_IR2CDMS();                                      //sending HK data to CDMS
+    t.stop();
+    printf("The time to execute hk_acq is %f seconds\n\r",t.read());
+    t.reset();
+}
+
+
+
+//---------------------------------------------------------------------------------------------------------------------------------------
+//TASK 1 : ACS
+//---------------------------------------------------------------------------------------------------------------------------------------
+
+int acs_pflag = 1;
+void F_ACS()
+{
+    float mag_field[3];
+    float omega[3];
+    float *mnm_data;
+    float mag_field1[3];
+    float omega1[3];
+    float tauc1[3];
+    float moment[3];
+    printf("\n\rEntered ACS   %f\n",t1.read());
+    t.start();
+    FUNC_ACS_MAG_EXEC(mag_field); 
+    for(int i=0; i<3; i++) 
+        {
+        printf("%f\t",mag_field[i]);
+        }                         
+    FUNC_ACS_EXEC_GYR(omega);
+    acs_pflag =1;                       
+    omega[0] = 1.0;            //to be removed later 
+    omega[1] = 1.0;
+    omega[2] = 1.0;
+        
+    /* mnm_data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
+    printf("\n\rmnm gyro values\n"); //printing the angular velocity and magnetic field values
+    for(int i=0; i<3; i++) 
+    {
+        printf("%f\t",mnm_data[i]);
+    }
+    printf("\n\r mnm mag values\n");
+    for(int i=3; i<6; i++) 
+    {
+        printf("%f\t",mnm_data[i]);
+    }
+    for(int i = 0 ; i<3;i++)
+    {
+        omega1[i] = mnm_data[i];
+    }
+    for( int i = 3;i<6;i++)
+    {
+        mag_field1[i-3] = mnm_data[i];
+    }
+    */
+    if(acs_pflag == 1)
+    {
+        FUNC_ACS_CNTRLALGO(mag_field,omega,tauc1);
+        printf("\n\r control algo values ");
+        for(int i=0; i<3; i++) 
+        {
+            printf("%f\t",tauc1[i]);
+        }
+        moment_calc (tauc1, mag_field,moment);
+        printf("\n\r moment values ");
+        for(int i=0; i<3; i++) 
+        {
+            printf("%f\t",moment[i]);
+        }
+        FUNC_ACS_GENPWM(moment);                   
+    }
+    t.reset();
+}
+
+//---------------------------------------------------BEACON--------------------------------------------------------------------------------------------
+
+int beac_flag=0;                                                            //To receive telecommand from ground.
+
+
+/*void T_BEA_TELECOMMAND(void const *args)
+{
+    char c = pc.getc();
+    if(c=='a')
+    {
+        printf("Telecommand detected\n\r");
+        beac_flag=1;
+    }
+}
+*/
+
+void F_BEA()
+{
+    printf("\n\rTHIS IS BEACON    %f\n\r",t1.read());
+    t.start();
+    FUNC_BEA();
+    if(beac_flag==1)
+    {
+        Thread::wait(600000);
+        beac_flag = 0;
+    }
+    printf("The time to execute beacon thread is %f seconds\n\r",t.read());
+    t.reset();
+}
+
+
+
+extern SensorDataQuantised SensorQuantised;
+
+/*-------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------------------WATCHDOG----------------------------------------------------------------------------*/
+DigitalOut trigger(PIN63);                                             // has to be changed
+void T_WDT(void const * args)
+{
+    trigger = 1;
+    while(true)
+    {
+        Thread::signal_wait(0x5);                               //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized
+        printf("\n\rEntered WD\n\r");
+        trigger = !trigger;
+    }
+}
+
+//---------------------------------------------------------------------------------------------------------------------------------------------------
+//TASK 5 : i2c data
+//---------------------------------------------------------------------------------------------------------------------------------------------------
+
+void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
+{
+    int slave_status = 1;
+    int inter_test = interrupt;
+    int slr = slave.receive();
+    wait_ms(20);
+    //printf("\n\r time is %d\n",t2.read_us());
+    //if(interrupt ==1)
+    //{ 
+        //printf("\n\r slave status %d",slave.receive());
+        //t2.stop();
+        if(slave.receive() == 0)
+            t2.stop();
+        if( slave.receive()==1)
+        {
+            t2.stop();
+            t3.start();
+            slave_status=slave.write(data,length);
+            t3.stop();
+        }
+        else if( slave.receive()==3 ||  slave.receive()==2)
+        {
+            t2.stop();
+            t3.start();
+            slave_status=slave.read(data,length);
+            t3.stop();
+        }
+    //}
+  //  printf("\n\r time taken to receive intrpt 4m cdms %d",i1.read_us());
+    //i1.reset();
+    printf("\n\r slave status %d",slave.receive());
+    printf("\n\r time taken from interrupt to reach i2c fn %d",t2.read_us());
+    t2.stop();
+    t2.reset();
+    printf("\n\r time to execute i2c function %d",t3.read_us());
+    t3.reset();
+}
+   
+char data_send[25],data_receive;
+void T_I2C_BAE(void const * args)
+{
+     while(1)
+    {
+        Thread::signal_wait(0x4);
+        int something = interrupt;
+       // printf("\n\r interrupt %d",interrupt);     
+        if(i2c_status == 0 )
+        
+        {
+           // wait_ms(23);
+            FUNC_I2C_WRITE2CDMS(&data_receive,1);
+            /*i2c_data * i2c_data_r = i2c_data_receive.alloc();
+            i2c_data_r->data = data_receive;
+            i2c_data_r->length = 1;
+            i2c_data_receive.put(i2c_data_r);*/
+            printf("\n\r Data received from CDMS is %c \n\r",data_receive);
+            FUNC_I2C_TC_EXECUTE(data_receive);                             // This has to be done from a differen thread
+            
+        }
+        else if(i2c_status ==1)
+        {
+            osEvent evt = i2c_data_send.get();
+            if (evt.status == osEventMail) 
+            {
+                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
+                strcpy(data_send,i2c_data_s -> data);
+                //wait_ms(25);
+                
+                FUNC_I2C_WRITE2CDMS(data_send,25);
+                printf("\n\rData sent to CDMS is %s\n\r",data_send);
+                
+                i2c_data_send.free(i2c_data_s);
+                i2c_status = 0;
+            }
+        }  
+           
+    }
+}
+
+        
+
+void FUNC_I2C_INT()
+{
+   //i1.stop();
+   
+  // t3.start();
+   ptr_t_i2c->signal_set(0x4);
+   //printf("\n ceckh\n");
+   t2.start();
+ //      printf("\n\r time taken from interrupt to reach i2c fn %d",t2.read_us());
+}
+
+void FUNC_I2C_IR2CDMS()
+{
+        data_ready=0; 
+        //char data[25];
+        strcpy(hk_data,"hk_Data");
+        strcat(hk_data,SensorQuantised.Voltage);
+        strcat(hk_data,SensorQuantised.Current); 
+        strcat(hk_data,SensorQuantised.Temperature); 
+        strcat(hk_data,SensorQuantised.PanelTemperature);
+        strcat(hk_data,SensorQuantised.AngularSpeed);
+        strcat(hk_data,SensorQuantised.Bnewvalue);
+        char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode};
+
+        /*strcat(hk_data,sfaultpoll);
+        strcat(hk_data,sfaultir);
+        strcat(hk_data,spower_mode);*/
+        strcat(hk_data,fdata);
+        printf("\n\r hk data being sent %s ",hk_data);
+        //for(int i=0;i<100000000000;i++)
+        //;
+            
+        /*for(int d=0;d<23;d++)                          //was written just to check hk data
+        {
+            if(hk_data[d]>10)
+            printf("\n\rhk data : %d\n\r",hk_data[d]);
+        }   */ 
+       
+        //data = pcslave.getc();
+        
+        i2c_status=1;
+        i2c_data * i2c_data_s = i2c_data_send.alloc();
+        strcpy(i2c_data_s->data,hk_data);
+        i2c_data_s->length = 25;
+        i2c_data_send.put(i2c_data_s); 
+        data_ready=1;
+     //   i1.start();
+        //temp = i2c_status;
+}
+    
+
+//------------------------------------------------------------------------------------------------------------------------------------------------
+//TELECOMMAND
+//------------------------------------------------------------------------------------------------------------------------------------------------
+void FUNC_I2C_TC_EXECUTE (char command)  
+{ switch(command)
+   {   case '0' : printf("command 0 executed");
+       break;
+       case '1' : printf("command 1 executed");
+       break;
+       case '2' : printf("command 2 executed");
+       break;
+       case '3' : printf("command 3 executed");
+    }
+}  
+   
+
+//------------------------------------------------------------------------------------------------------------------------------------------------
+//SCHEDULER
+//------------------------------------------------------------------------------------------------------------------------------------------------
+
+void T_SC(void const *args)
+{
+        ptr_t_abh -> signal_set(0x1);
+        ptr_t_wdt -> signal_set(0x5);               
+
+}  
+//---------------------------------------------------------------------------------------------------------------------------------------------
+
+int main()
+{
+    t1.start();
+    printf("\n\rIITMSAT BAE Activated \n");
+    //INIT_PNI();                                       // Initializing mnm blue
+    //FUNC_ACS_MAG_INIT();                              // Initializing magnetometer
+    //FUNC_ACS_INIT_GYR();                              // Initializing Gyroscope
+    slave.address(0x20);                              // setting slave address for BAE for I2C communication
+    //init_beacon();
+    ptr_t_abh = new Thread(T_ABH);
+    ptr_t_i2c = new Thread(T_I2C_BAE);
+    //ptr_t_sc = new Thread(T_SC);
+    ptr_t_wdt = new Thread(T_WDT);
+  
+    interrupt_fault();                      // Dummy function called when a fault interrupt is detected
+    
+    ptr_t_abh->set_priority(osPriorityAboveNormal);
+    ptr_t_i2c->set_priority(osPriorityHigh);
+    //ptr_t_hk_acq->set_priority(osPriorityAboveNormal);
+    //ptr_t_bea->set_priority(osPriorityAboveNormal);
+    //ptr_t_sc->set_priority(osPriorityAboveNormal);
+    ptr_t_wdt -> set_priority(osPriorityIdle);
+    
+  
+   // ----------------------------------------------------------------------------------------------
+    printf("\n\r T_ABH priority is %d",ptr_t_abh->get_priority());
+    //printf("\n\r T_I2C priority is %d",ptr_t_hk_acq->get_priority());
+    //printf("\n\r T_WDT priority is %d",ptr_t_bea->get_priority());  
+    RtosTimer t_sc_timer(T_SC,osTimerPeriodic);
+    t_sc_timer.start(10000);
+    printf("\n\r%f\n\r",t1.read()); 
+     
+    interrupt.rise(&FUNC_I2C_INT);                             //interrupt received from CDMS
+    while(1)                                                   //required to prevent main from terminating
+    {   
+        Thread::wait(5000);
+    }
+    
+}
diff -r 000000000000 -r 1a04c0beef21 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#13a25134ac60
diff -r 000000000000 -r 1a04c0beef21 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 mnm.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mnm.cpp	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,164 @@
+#include<mbed.h>
+#include "mnm.h"
+#include "pin_config.h"
+#include "pni.h" //pni header file
+Serial mnm(USBTX,USBRX); //for usb communication
+I2C i2c (PIN85,PIN84); //PTC9-sda,PTC8-scl
+/*void INIT_PNI(void); //initialization of registers happens
+float *EXECUTE_PNI(); //data is obtained
+void T_OUT(); //timeout function to stop infinite loop*/
+Timeout to; //Timeout variable to
+int toFlag; 
+void T_OUT()
+{
+    toFlag=0; //as T_OUT function gets called the while loop gets terminated
+}
+
+//DEFINING VARIABLES
+char cmd[2];
+char raw_gyro[6];
+char raw_mag[6];
+char store,status;
+int16_t bit_data;
+float gyro_data[3], mag_data[3],combined_values[6],*data;
+float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps
+float senstivity_mag  =32.768; //senstivity is obtained from 2^15/1000microtesla
+float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0};
+
+Timer r0;
+Timer r1;
+Timer r2;
+Timer r3;
+Timer r4;
+
+
+/*
+int main(void)
+{
+
+    INIT_PNI();
+    data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
+    mnm.printf("gyro values\n"); //printing the angular velocity and magnetic field values
+    for(int i=0; i<3; i++) {
+        mnm.printf("%f\t",data[i]);
+    }
+    mnm.printf("mag values\n");
+    for(int i=3; i<6; i++) {
+        mnm.printf("%f\t",data[i]);
+    }
+}*/
+
+void  INIT_PNI()
+{   r0.start();
+    cmd[0]=RESETREQ;
+    cmd[1]=BIT_RESREQ;
+    i2c.write(SLAVE_ADDR,cmd,2);
+     r0.stop(); //When 0x01 is written in reset request register Emulates a hard power down/power up
+    wait_ms(2000); //waiting for loading configuration file stored in EEPROM
+    cmd[0]=SENTRALSTATUS;
+    r1.start();
+    i2c.write(SLAVE_ADDR,cmd,1);
+    i2c.read(SLAVE_ADDR_READ,&store,1);
+    r1.stop();
+    wait_ms(100);
+    //to check whether EEPROM is uploaded
+
+ /*   switch((int)store) { 
+        case(3): {
+                printf("\nstore :%d\n",store);
+            break;
+        }
+        case(11): {
+                printf("\nstore11 :%d\n",store);
+            break;
+        }
+        default: {
+            cmd[0]=RESETREQ;
+            cmd[1]=BIT_RESREQ;
+            i2c.write(SLAVE_ADDR,cmd,2);
+            wait_ms(2000);
+        }
+    }*/
+    //mnm.printf("Sentral Status is %x\n",(int)store);
+    r2.start();
+    cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors
+    cmd[1]=BIT_RUN_ENB;
+    i2c.write(SLAVE_ADDR,cmd,2);
+      r2.stop();
+    wait_ms(100);
+    r3.start();
+    cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer
+    cmd[1]=BIT_MAGODR;
+    i2c.write(SLAVE_ADDR,cmd,2);
+      r3.stop();
+      wait_ms(100);
+    r4.start();
+    cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope
+    cmd[1]=BIT_GYROODR;
+    i2c.write(SLAVE_ADDR,cmd,2);
+      r4.stop();
+      wait_ms(100);
+    cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values
+    cmd[1]=0x00;
+    i2c.write(SLAVE_ADDR,cmd,2);
+    wait_ms(100);
+    cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values
+    cmd[1]=BIT_EVT_ENB;
+    i2c.write(SLAVE_ADDR,cmd,2);
+    wait_ms(100);
+    printf("\n \r %d %d %d %d %d",r0.read_us(),r1.read_us(),r2.read_us(),r3.read_us(),r4.read_us());
+}
+
+float *EXECUTE_PNI()
+{
+    //printf("\n\r mnm func \n");
+    toFlag=1; //toFlag is set to 1 so that it enters while loop
+    to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated 
+    while(toFlag) {
+        cmd[0]=EVT_STATUS;
+        i2c.write(SLAVE_ADDR,cmd,1);
+        i2c.read(SLAVE_ADDR_READ,&status,1);
+        wait_ms(100);
+        //mnm.printf("\nEvent Status is %x\n",(int)status);
+        //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take
+        if(((int)status&40)==40) {
+            printf("\nin if of mnm\n");
+            cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x 
+            i2c.write(SLAVE_ADDR,cmd,1);
+            i2c.read(SLAVE_ADDR_READ,raw_gyro,6);
+            cmd[0]=MAG_XOUT_H; //LSB of x
+            i2c.write(SLAVE_ADDR,cmd,1);
+            i2c.read(SLAVE_ADDR_READ,raw_mag,6);
+            //mnm.printf("\nGyro Values:\n");
+            for(int i=0; i<3; i++) {
+                //concatenating gyro LSB and MSB to get 16 bit signed data values
+                bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i]; 
+                gyro_data[i]=(float)bit_data;
+                gyro_data[i]=gyro_data[i]/senstivity_gyro;
+                gyro_data[i]+=gyro_error[i];
+                //mnm.printf("%f\t",gyro_data[i]);
+            }
+            //mnm.printf("\nMag Values:\n");
+            for(int i=0; i<3; i++) {
+                //concatenating mag LSB and MSB to get 16 bit signed data values
+                bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i];
+                mag_data[i]=(float)bit_data;
+                mag_data[i]=mag_data[i]/senstivity_mag;
+                mag_data[i]+=mag_error[i];
+                //mnm.printf("%f\t",mag_data[i]);
+            }
+            for(int i=0; i<3; i++) {
+                combined_values[i]=gyro_data[i];
+                combined_values[i+3]=mag_data[i];
+            }
+            return(combined_values); //returning poiter combined values
+        } 
+       //checking for the error
+        
+        else if (((int)status&2)==2) {
+            INIT_PNI(); //when there is any error then Again inilization is done to remove error
+        }
+        
+        
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 mnm.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mnm.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,3 @@
+void INIT_PNI(void); //initialization of registers happens
+float *EXECUTE_PNI(); //data is obtained
+void T_OUT(); //timeout function to stop infinite loop
diff -r 000000000000 -r 1a04c0beef21 pin_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pin_config.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,101 @@
+// 100 LQFP format pin assignment
+#define PIN1 PTE0
+#define PIN2 PTE1
+#define PIN3 PTE2
+#define PIN4 PTE3
+#define PIN5 PTE4
+#define PIN6 PTE5
+#define PIN7 PTE6
+//#define 8 
+//#define 9 
+//#define 10 
+//#define 11 
+//#define 12 
+//#define 13 
+#define PIN14 PTE16
+#define PIN15 PTE17
+#define PIN16 PTE18
+#define PIN17 PTE19
+#define PIN18 PTE20
+#define PIN19 PTE21
+#define PIN20 PTE22
+#define PIN21 PTE23
+//#define 22 
+//#define 23 
+//#define 24 
+//#define 25 
+#define PIN26 PTE29
+#define PIN27 PTE30
+#define PIN28 PTE31
+//#define 29 
+//#define 30 
+#define PIN31 PTE24
+#define PIN32 PTE25
+#define PIN33 PTE26
+#define PIN34 PTA0
+#define PIN35 PTA1
+#define PIN36 PTA2
+#define PIN37 PTA3
+#define PIN38 PTA4
+#define PIN39 PTA5
+#define PIN40 PTA6
+#define PIN41 PTA7
+#define PIN42 PTA12
+#define PIN43 PTA13
+#define PIN44 PTA14
+#define PIN45 PTA15
+#define PIN46 PTA16
+#define PIN47 PTA17
+//#define 48 
+//#define 49 
+#define PIN50 PTA18
+#define PIN51 PTA19
+#define PIN52 PTA20
+#define PIN53 PTB0
+#define PIN54 PTB1
+#define PIN55 PTB2
+#define PIN56 PTB3
+#define PIN57 PTB7
+#define PIN58 PTB8
+#define PIN59 PTB9
+#define PIN60 PTB10
+#define PIN61 PTB11
+#define PIN62 PTB16
+#define PIN63 PTB17
+#define PIN64 PTB18
+#define PIN65 PTB19
+#define PIN66 PTB20
+#define PIN67 PTB21
+#define PIN68 PTB22
+#define PIN69 PTB23
+#define PIN70 PTC0
+#define PIN71 PTC1
+#define PIN72 PTC2
+#define PIN73 PTC3
+//#define 74 
+//#define 75 
+#define PIN76 PTC20
+#define PIN77 PTC21
+#define PIN78 PTC22
+#define PIN79 PTC23
+#define PIN80 PTC4
+#define PIN81 PTC5
+#define PIN82 PTC6
+#define PIN83 PTC7
+#define PIN84 PTC8
+#define PIN85 PTC9
+#define PIN86 PTC10
+#define PIN87 PTC11
+#define PIN88 PTC12
+#define PIN89 PTC13
+#define PIN90 PTC16
+#define PIN91 PTC17
+#define PIN92 PTC18
+#define PIN93 PTD0
+#define PIN94 PTD1
+#define PIN95 PTD2
+#define PIN96 PTD3
+#define PIN97 PTD4
+#define PIN98 PTD5
+#define PIN99 PTD6
+#define PIN100 PTD7
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 pni.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pni.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,37 @@
+#define SLAVE_ADDR         0x50
+#define SLAVE_ADDR_READ    0x51
+#define SENTRALSTATUS      0x37
+#define RESETREQ           0x9B
+#define MAGRATE            0x55
+#define ACCERATE           0x56
+#define GYRORATE           0x57
+#define QRATE_DIV          0x32
+#define ALGO_CTRL           0x54
+#define ENB_EVT            0x33
+#define HOST_CTRL           0x34
+#define EVT_STATUS         0x35
+#define ALGO_STATUS        0x38
+#define GYRO_XOUT_H        0x22
+#define MAG_XOUT_H         0X12
+
+//Configaration bits
+#define BIT_RESREQ     0x01
+#define BIT_EEP_DET    0x01
+#define BIT_EEP_UPDN   0x02
+#define BIT_EEP_UPERR  0x04
+#define BIT_EEP_IDLE   0x08
+#define BIT_EEP_NODET  0x10
+#define BIT_STBY       0x01
+#define BIT_RAW_ENB    0x02
+#define BIT_HPR_OUT    0x04
+#define BIT_CPU_RES    0x01
+#define BIT_ERR        0x02
+#define BIT_QRES       0x04
+#define BIT_MAG_RES    0x08
+#define BIT_ACC_RES    0x10
+#define BIT_GYRO_RES   0x20
+#define BIT_GYROODR    0x0F
+#define BIT_MAGODR     0x64
+#define BIT_RUN_ENB    0x01
+#define BIT_ALGO_RAW   0x02
+#define BIT_EVT_ENB    0X2A
\ No newline at end of file
diff -r 000000000000 -r 1a04c0beef21 slave.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/slave.h	Sat May 16 07:07:56 2015 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#define WriteGeneral 3
+#define ReadAddressed 1
+#define slave_address 0x20
+
+
+void FUNC_I2C_WRITE2CDMS(char* ,int);
+void FUNC_I2C_IR2CDMS();
+void FUNC_I2C_INT();
+void FUNC_I2C_RESET();
+void FUNC_I2C_TC_EXECUTE (char command) ; 
+