acs beacon and hk integrated

Dependencies:   mbed-rtos mbed

Revision:
0:1a04c0beef21
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);
+    }
+    
+}