Rtos code for BAE microcontroller, IITMSAT

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
greenroshks
Date:
2014-06-26
Revision:
1:5a364ac20fa4
Parent:
0:f1f148215d00
Child:
2:1792c9cda669

File content as of revision 1:5a364ac20fa4:

#include "mbed.h"
#include "rtos.h"
//#include "reset.cpp"

Serial pc(USBTX,USBRX);
Timer t;
Thread *t_acs;
Thread *t_bea;
Thread *t_hk_acq;
Thread *t_acs_write2flash;
Thread *t_hk_write2cdms;



int flag = 0;                                   //for detecting keypress from computer


/*----------------------------------------------------------------------------------------------------------------------------------------------------
  TASK 1:ACS
----------------------------------------------------------------------------------------------------------------------------------------------------*/
typedef struct
{
    int B;
    int omega;
}sens_data;

void FUNC_ACS_READDATA(sens_data * s);

Mail <sens_data,16> q_sensor;
/*----------------------------------------------------------------------------------------------------------------------------------------------------
  Definitions of functions for thread T_ACS_MAIN begin here
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
//This function is called only once after the microcontroller is reset.
void FUNC_ACS_SENSORINIT()
{
    /*
    
    Lines of code to be given by Shruti and Jahnavi
    
    */
    pc.printf("\nInitializing the sensors.. \n");
}

void FUNC_ACS_READDATA(sens_data * s)
{
    /*
    
    Lines of code to be given by Shruti and Jahnavi
    
    Request sensor data
    
    */
    
    
    // this is the dummy data
    s->B=10;
    s->omega=100;
    
    pc.printf("\nReceived sensor data\n");
}

float FUNC_ACS_CONTROLALGO(sens_data * s)
{
    /*
    
    Lines of code to be given by Shruti and Jahnavi
    
    Control algorithm implementation
    
    */
    float m;
    s->B=10 *10;
    s->omega=100*10;
    m = 10;
    pc.printf("\nImplemented control algo\n");
    
    return(m);
}

void FUNC_ACS_PWMGEN(float m)
{
    float dc;
    dc = m*2;
    
    /*
    
    Lines of code to be given by Kumar and Karthik
    
    PWM implementation
    
    */
    
       
    pc.printf("\nGenerating pwm signal\n");
    
    
}

// Definitions of functions for thread T_ACS_MAIN end here
//----------------------------------------------------------------------------------------------------------------------------------------------------



// Definitions of functions for thread T_ACS_WRITE2FLASH starts here
//----------------------------------------------------------------------------------------------------------------------------------------------------

void FUNC_ACS_WRITE2FLASH(sens_data *s1)
{
    pc.printf("\nWritng ACS data to BAE flash\n");
    pc.printf("\nB      :%d",s1->B);
    pc.printf("\nomega  :%d",s1->omega);
    
}

// Definitions of functions for thread T_ACS_WRITE2FLASH end here
//----------------------------------------------------------------------------------------------------------------------------------------------------

// This is the main thread of ACS

void T_ACS_MAIN(void const * args)
{
    while(1)
    {
        
        float m;
        Thread::signal_wait(0x1);  // waiting for signal from main every ten seconds
        printf("\nACS   :%fn",t.read());
        printf("\nACS thread spriority is %d\n",t_acs->get_priority());
        printf("\nBeacon thread priority  :%d\n",t_bea->get_priority());
        printf("\nACS_WRITE2FLASH thread priority is %d\n",t_acs_write2flash->get_priority());
        printf("\nHK_ACQ thread priority is %d\n",t_hk_acq->get_priority());
        printf("\nHK_WRITE2CDMS thread priority is %d\n",t_hk_write2cdms->get_priority());
        
        sens_data * s = q_sensor.alloc(); // please delete the  memory after it is used.
        
        FUNC_ACS_READDATA(s);
        q_sensor.put(s);
        m=FUNC_ACS_CONTROLALGO(s);
        FUNC_ACS_PWMGEN(m);
        pc.printf("\nTorque rods actuated\n");
        //Thread::wait(9899);
    }    
}

void T_ACS_WRITE2FLASH(void const * args)
{
        while(1)
        {
        
            osEvent evt=q_sensor.get();
            if(evt.status==osEventMail)
            {
             printf("\nACS thread status is %d\n",t_acs->get_state());
            printf("\nBeacon thread status  :%d\n",t_bea->get_state());
            printf("\nACS_WRITE2FLASH thread status is %d\n",t_acs_write2flash->get_state());
            printf("\nHK_ACQ thread status is %d\n",t_hk_acq->get_state());
            printf("\nHK_WRITE2CDMS thread status is %d\n",t_hk_write2cdms->get_state());   
                sens_data *s1 =(sens_data *)evt.value.p;// please see
                FUNC_ACS_WRITE2FLASH(s1);
                
                q_sensor.free(s1);
            }
        }
}

/*-----------------------------------------------------------------------------------------------------------------------------------------------------
  TASK 2 : HK
------------------------------------------------------------------------------------------------------------------------------------------------------*/

//Functions for the thread T_HK_ACQ begins here
//----------------------------------------------------------------------------------------------------------------------------------------------------
void FUNC_HK_ACQ_STORE()
{
    /*
        Code to be obtained from pranoy and bharath
    */
    
    pc.printf("\nAcivating Mux\n");
    pc.printf("\nActivating ADC\n");
    pc.printf("\nWriting HK data to flash memory\n");
}

//Functions of the thread T_HK_SEND2CDMS starts here.
//----------------------------------------------------------------------------------------------------------------------------------------------------


void FUNC_HK_READFLASH()
{
    /*
       Code to be given by Chaitanya and Vasanthi
                                                    */
    
    pc.printf("\nReading HK data from flash memeory\n");
}

void FUNC_HK_SEND2CDMS()
{
     /*
       Code to be given by Chaitanya and Vasanthi
                                                    */
    
    pc.printf("\nSending HK data to CDMS\n");
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
// Defintion of thread for sending data to CDMS
//----------------------------------------------------------------------------------------------------------------------------------------------------
void T_HK_SEND2CDMS(void const * args)
{
    while(1)
    {
        Thread::signal_wait(0x1);                                       // Waiting for signal from T_HK_ACQ
        printf("\nACS thread status is %d\n",t_acs->get_state());
        printf("\nBeacon thread status  :%d\n",t_bea->get_state());
        printf("\nACS_WRITE2FLASH thread status is %d\n",t_acs_write2flash->get_state());
        printf("\nHK_ACQ thread status is %d\n",t_hk_acq->get_state());
        printf("\nHK_WRITE2CDMS thread status is %d\n",t_hk_write2cdms->get_state());
        FUNC_HK_READFLASH();
        FUNC_HK_SEND2CDMS();
    }
}

//----------------------------------------------------------------------------------------------------------------------------------------------
//Definition for the thread to collect HK data and store to flash memory
//----------------------------------------------------------------------------------------------------------------------------------------------

void T_HK_ACQ(void const *args)
{
    //t_hk_write2cdms = new Thread(T_HK_SEND2CDMS);
    //t_hk_write2cdms->set_priority(osPriorityLow);
    while(1)
    {
    
        Thread::signal_wait(0x1);                                   //Waiting for the signal from main every 20 seconds
        printf("\n\nHK  :%f\n",t.read());
        /*printf("\nACS thread status is %d\n",t_acs->get_state());
        printf("\nBeacon thread status  :%d\n",t_bea->get_state());
        printf("\nACS_WRITE2FLASH thread status is %d\n",t_acs_write2flash->get_state());
        printf("\nHK_ACQ thread status is %d\n",t_hk_acq->get_state());
        printf("\nHK_WRITE2CDMS thread status is %d\n",t_hk_write2cdms->get_state());*/
        
        FUNC_HK_ACQ_STORE();
        t_hk_write2cdms->signal_set(0x1);
        //Thread::wait(19925);
    }
    
}

/*--------------------------------------------------------------------------------------------------------------------------------------------------
 TASK 4 : BEACON
-----------------------------------------------------------------------------------------------------------------------------------------------------*/


void FUNC_BEA_READBAEFLASH()
{
    /*
      Code to be given by Nathaniel
                                        */
    
    pc.printf("\nReading HK data from BAE flash\n");
}

void FUNC_BEA_WRITE2SPI()
{
    /*
      Code to be given by Nathaniel
                                        */
    
    pc.printf("\nSending data through SPI\n");
}


void T_BEA(void const * args)
{
    while(1)
    {
    
        Thread::signal_wait(0x1);                                       //waiting for signal from main every 30 seconds.
        pc.printf("\nBEACON     :%f\n",t.read());
        printf("\nACS thread status is %d\n",t_acs->get_state());
        printf("\nBeacon thread status  :%d\n",t_bea->get_state());
        printf("\nACS_WRITE2FLASH thread status is %d\n",t_acs_write2flash->get_state());
        printf("\nHK_ACQ thread status is %d\n",t_hk_acq->get_state());
        printf("\nHK_WRITE2CDMS thread status is %d\n",t_hk_write2cdms->get_state());    
        
        if(flag==0)
        {
               
            FUNC_BEA_READBAEFLASH();
            FUNC_BEA_WRITE2SPI();
           // Thread::wait(30000);
        }
        else
        {
            //led=1;
            Thread::wait(60000);
            
            flag=0;
            //myled=0;
        }
     }
    
}

void keypress(void const *args)
{
    
    while(1)
    {
        if(pc.getc()=='s')
        {
            pc.printf("\nTime of telecommand %f",t.read());
            flag=1;
        }
    }
}
    

int schedcount=1;   
void scheduler(void const * args)
{
    t_acs->signal_set(0x1);
    
    if(schedcount%2==0)
       {
            pc.printf("\nHK signal at %f",t.read());
            t_hk_acq->signal_set(0x1);
            
       } 
    if(schedcount%3==0)
        {
            pc.printf("\nBeacon signal at %f",t.read());
            t_bea->signal_set(0x1);
            
        }
    schedcount++;
}

    
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
  MAIN
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
int main()
{
        t.start();
       // int i =0;
        t_acs = new Thread(T_ACS_MAIN);
        t_bea = new Thread(T_BEA);
        t_hk_acq = new Thread(T_HK_ACQ);
        t_acs_write2flash = new Thread(T_ACS_WRITE2FLASH);
        Thread key(keypress);
       // Thread scd(scheduler);
          t_hk_write2cdms = new Thread(T_HK_SEND2CDMS);           
  
        t_acs->set_priority(osPriorityLow);
        t_bea->set_priority(osPriorityAboveNormal);
        t_hk_acq->set_priority(osPriorityNormal);
        t_acs_write2flash->set_priority(osPriorityBelowNormal);
        //t_hk_write2cdms->set_priority(osPriorityLow);
        key.set_priority(osPriorityIdle);
        
        
        /*RtosTimer t_hk_timer(T_HK_ACQ,osTimerPeriodic);
        RtosTimer t_bea_timer(T_BEA,osTimerPeriodic);
        RtosTimer t_acs_timer(T_ACS_MAIN,osTimerPeriodic);
        
        t_hk_timer.start(10000);
        t_bea_timer.start(20000);
        t_acs_timer.start(30000);*/
        
        RtosTimer schedule(scheduler,osTimerPeriodic);
        schedule.start(10000);
        t_hk_write2cdms->set_priority(osPriorityLow);
        pc.printf("\n T_HK_SEND2CDMS priority is %d",t_hk_write2cdms->get_priority());
        while(1)
        {
            /*if(i%10==0)
                {
                    t_acs->signal_set(0x1);
                    pc.printf("Priority of t_hk_write2cdms is %d and t_acs_write2flash is %d",t_hk_write2cdms->get_priority(),t_acs_write2flash->get_priority());
                }
            if(i%20==0)
                t_hk_acq->signal_set(0x1);
            if(i%30==0)
                t_bea->signal_set(0x1);
            
            i++;*/
            Thread::wait(1000);
            pc.printf("\n T_ACS_ACQ priority is %d",t_acs->get_priority());

            pc.printf("\n%f\n",t.read());
        }
}