Rtos code for BAE microcontroller, IITMSAT

Dependencies:   mbed-rtos mbed

main.cpp

Committer:
greenroshks
Date:
2014-06-23
Revision:
0:f1f148215d00
Child:
1:5a364ac20fa4

File content as of revision 0:f1f148215d00:

#include "mbed.h"
#include "rtos.h"

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;

Mail <sens_data,16> q_sensor;

void FUNC_ACS_READDATA(sens_data * s)
{
    s->B=10;
    s->omega=100;
    pc.printf("\nReading sensor data\n");
}

void FUNC_ACS_CONTROLALGO(sens_data * s)
{
    pc.printf("\nRunning control algo\n");
}

void FUNC_ACS_PWMGEN()
{
    pc.printf("\nGenerating pwm signal\n");
}

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);
    
}

void T_ACS(void const * args)
{
    while(1)
    {
        Thread::signal_wait(0x1);
        printf("\nACS   :%fn",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());
        
        sens_data * s = q_sensor.alloc();
        FUNC_ACS_READDATA(s);
        q_sensor.put(s);
        FUNC_ACS_CONTROLALGO(s);
        FUNC_ACS_PWMGEN();
        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;
                FUNC_ACS_WRITE2FLASH(s1);
            }
        }
}

/*-----------------------------------------------------------------------------------------------------------------------------------------------------
  TASK 2 : HK
------------------------------------------------------------------------------------------------------------------------------------------------------*/
void FUNC_HK_MUX()
{
    pc.printf("\nAcivating Mux\n");
}

void FUNC_HK_ADC()
{
    pc.printf("\nActivating ADC\n");
}

void FUNC_HK_WRITE2FLASH()
{
    pc.printf("\nWriting HK data to flash memory\n");
}

void FUNC_HK_READFLASH()
{
    pc.printf("\nReading HK data from flash memeory\n");
}

void FUNC_HK_SEND2CDMS()
{
    pc.printf("\nSending HK data to CDMS\n");
}

void T_HK_SEND2CDMS(void const * args)
{
    while(1)
    {
        Thread::signal_wait(0x1);
        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();
    }
}

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);
        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_MUX();
        FUNC_HK_ADC();
        FUNC_HK_WRITE2FLASH();
        t_hk_write2cdms->signal_set(0x1);
        //Thread::wait(19925);
 
    }
}

/*--------------------------------------------------------------------------------------------------------------------------------------------------
 TASK 4 : BEACON
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
void FUNC_BEA_READBAEFLASH()
{
    pc.printf("\nReading HK data from BAE flash\n");
}

void FUNC_BEA_WRITE2SPI()
{
    pc.printf("\nSending data through SPI\n");
}


void T_BEA(void const * args)
{
    while(1)
    {
        Thread::signal_wait(0x1);
        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;
        }
    }
}
    
    
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
  MAIN
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
int main()
{
        t.start();
        int i =0;
        t_acs = new Thread(T_ACS);
        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);
        pc.printf("\n\nHello world\n");
          t_hk_write2cdms = new Thread(T_HK_SEND2CDMS);           
          
   // 
     pc.printf("\nHello universe\n");
       
        t_acs->set_priority(osPriorityHigh);
        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);
        
        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);
            Thread::wait(991);
            pc.printf("\n%f\n",t.read());
            i++;
        }
}