slave integrated

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDMTESIN2 by Seeker of Truth ,

Committer:
sakthipriya
Date:
Wed Dec 03 11:23:37 2014 +0000
Revision:
4:8f6c24eca109
Parent:
3:02b45de29c0f
Child:
5:255b43e8e21a
fault and power added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
greenroshks 0:8b0d43fe6c05 1 #include "mbed.h"
greenroshks 0:8b0d43fe6c05 2 #include "rtos.h"
greenroshks 0:8b0d43fe6c05 3 #include "HK.h"
greenroshks 0:8b0d43fe6c05 4 #include "slave.h"
greenroshks 0:8b0d43fe6c05 5 #include "beacon.h"
greenroshks 0:8b0d43fe6c05 6 #include "ACS.h"
sakthipriya 2:80b8a2e999f7 7 #include "fault.h"
greenroshks 0:8b0d43fe6c05 8
greenroshks 0:8b0d43fe6c05 9 Serial pc(USBTX, USBRX);
greenroshks 0:8b0d43fe6c05 10
greenroshks 0:8b0d43fe6c05 11
greenroshks 0:8b0d43fe6c05 12
greenroshks 0:8b0d43fe6c05 13 Timer t; //To know the time of entering each thread
sakthipriya 4:8f6c24eca109 14 Timer t1;
sakthipriya 4:8f6c24eca109 15 //To know the time of execution of each thread
greenroshks 0:8b0d43fe6c05 16
greenroshks 0:8b0d43fe6c05 17 Thread *ptr_t_hk_acq;
greenroshks 0:8b0d43fe6c05 18 Thread *ptr_t_acs;
greenroshks 0:8b0d43fe6c05 19 Thread *ptr_t_acs_write2flash;
greenroshks 0:8b0d43fe6c05 20 Thread *ptr_t_bea;
greenroshks 0:8b0d43fe6c05 21 Thread *ptr_t_bea_telecommand;
greenroshks 0:8b0d43fe6c05 22 Thread *ptr_t_fault;
greenroshks 0:8b0d43fe6c05 23
greenroshks 0:8b0d43fe6c05 24
greenroshks 0:8b0d43fe6c05 25 //--------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 26 //TASK 2 : HK
greenroshks 0:8b0d43fe6c05 27 //--------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 28
greenroshks 0:8b0d43fe6c05 29
greenroshks 0:8b0d43fe6c05 30
greenroshks 0:8b0d43fe6c05 31 void t_hk_acq(void const *args)
greenroshks 0:8b0d43fe6c05 32 {
greenroshks 0:8b0d43fe6c05 33
greenroshks 0:8b0d43fe6c05 34 while(1)
greenroshks 0:8b0d43fe6c05 35 {
greenroshks 0:8b0d43fe6c05 36 Thread::signal_wait(0x2);
greenroshks 0:8b0d43fe6c05 37
greenroshks 0:8b0d43fe6c05 38 printf("\nTHIS IS HK %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 39 t.start();
greenroshks 0:8b0d43fe6c05 40
greenroshks 0:8b0d43fe6c05 41 FUNC_HK_MAIN(); //Collecting HK data
greenroshks 0:8b0d43fe6c05 42 //thread_2.signal_set(0x4);
greenroshks 0:8b0d43fe6c05 43 FUNC_I2C_SLAVE_MAIN(24); //Sending to CDMS via I2C
greenroshks 0:8b0d43fe6c05 44
greenroshks 0:8b0d43fe6c05 45 t.stop();
greenroshks 0:8b0d43fe6c05 46 printf("The time to execute hk_acq is %f seconds\n",t.read());
greenroshks 0:8b0d43fe6c05 47 t.reset();
greenroshks 0:8b0d43fe6c05 48 }
greenroshks 0:8b0d43fe6c05 49 }
greenroshks 0:8b0d43fe6c05 50
greenroshks 0:8b0d43fe6c05 51 //---------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 52 //TASK 1 : ACS
greenroshks 0:8b0d43fe6c05 53 //---------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 54 typedef struct {
greenroshks 0:8b0d43fe6c05 55 float mag_field;
greenroshks 0:8b0d43fe6c05 56 float omega;
greenroshks 0:8b0d43fe6c05 57 } sensor_data;
greenroshks 0:8b0d43fe6c05 58
greenroshks 0:8b0d43fe6c05 59 Mail <sensor_data, 16> q_acs;
greenroshks 0:8b0d43fe6c05 60
greenroshks 0:8b0d43fe6c05 61 void func_acs_readdata(sensor_data *ptr)
greenroshks 0:8b0d43fe6c05 62 {
greenroshks 0:8b0d43fe6c05 63 printf("Reading the data\n");
greenroshks 0:8b0d43fe6c05 64 ptr -> mag_field = 10;
greenroshks 0:8b0d43fe6c05 65 ptr -> omega = 3;
greenroshks 0:8b0d43fe6c05 66 }
greenroshks 0:8b0d43fe6c05 67
greenroshks 0:8b0d43fe6c05 68 void func_acs_ctrlalgo()
greenroshks 0:8b0d43fe6c05 69 {
greenroshks 0:8b0d43fe6c05 70 printf("Executing control algo\n");
greenroshks 0:8b0d43fe6c05 71 }
greenroshks 0:8b0d43fe6c05 72
greenroshks 0:8b0d43fe6c05 73
greenroshks 0:8b0d43fe6c05 74
greenroshks 0:8b0d43fe6c05 75 void func_acs_write2flash(sensor_data *ptr2)
greenroshks 0:8b0d43fe6c05 76 {
greenroshks 0:8b0d43fe6c05 77 printf("The magnetic field is %.2f T\n\r",ptr2->mag_field);
greenroshks 0:8b0d43fe6c05 78 printf("The angular velocity is %.2f rad/s\n\r",ptr2->omega);
greenroshks 0:8b0d43fe6c05 79 }
greenroshks 0:8b0d43fe6c05 80
greenroshks 0:8b0d43fe6c05 81 void t_acs(void const *args)
greenroshks 0:8b0d43fe6c05 82 {
greenroshks 0:8b0d43fe6c05 83 while(1)
greenroshks 0:8b0d43fe6c05 84 {
greenroshks 0:8b0d43fe6c05 85 Thread::signal_wait(0x1);
greenroshks 0:8b0d43fe6c05 86 printf("\nTHIS IS ACS %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 87 t.start();
greenroshks 0:8b0d43fe6c05 88 sensor_data *ptr = q_acs.alloc();
greenroshks 0:8b0d43fe6c05 89
greenroshks 0:8b0d43fe6c05 90 func_acs_readdata(ptr);
greenroshks 0:8b0d43fe6c05 91 q_acs.put(ptr);
greenroshks 0:8b0d43fe6c05 92 func_acs_ctrlalgo();
greenroshks 0:8b0d43fe6c05 93 FUNC_ACS_GENPWM(); //Generating PWM signal.
greenroshks 0:8b0d43fe6c05 94
greenroshks 0:8b0d43fe6c05 95
greenroshks 0:8b0d43fe6c05 96 t.reset();
greenroshks 0:8b0d43fe6c05 97 }
greenroshks 0:8b0d43fe6c05 98 }
greenroshks 0:8b0d43fe6c05 99
greenroshks 0:8b0d43fe6c05 100 void t_acs_write2flash(void const *args)
greenroshks 0:8b0d43fe6c05 101 {
greenroshks 0:8b0d43fe6c05 102 while(1)
greenroshks 0:8b0d43fe6c05 103 {
greenroshks 0:8b0d43fe6c05 104 //printf("Writing in the flash\n");
greenroshks 0:8b0d43fe6c05 105 osEvent evt = q_acs.get();
greenroshks 0:8b0d43fe6c05 106 if(evt.status == osEventMail)
greenroshks 0:8b0d43fe6c05 107 {
greenroshks 0:8b0d43fe6c05 108 sensor_data *ptr = (sensor_data*)evt.value.p;
greenroshks 0:8b0d43fe6c05 109 func_acs_write2flash(ptr);
greenroshks 0:8b0d43fe6c05 110 q_acs.free(ptr);
greenroshks 0:8b0d43fe6c05 111 }
greenroshks 0:8b0d43fe6c05 112 printf("Writing acs data in the flash\n");
greenroshks 0:8b0d43fe6c05 113 }
greenroshks 0:8b0d43fe6c05 114 }
greenroshks 0:8b0d43fe6c05 115
greenroshks 0:8b0d43fe6c05 116
greenroshks 0:8b0d43fe6c05 117 //---------------------------------------------------BEACON--------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 118
greenroshks 0:8b0d43fe6c05 119 int beac_flag=0; //To receive telecommand from ground.
greenroshks 0:8b0d43fe6c05 120
greenroshks 0:8b0d43fe6c05 121
greenroshks 0:8b0d43fe6c05 122 void t_bea_telecommand(void const *args)
greenroshks 0:8b0d43fe6c05 123 {
greenroshks 0:8b0d43fe6c05 124 char c = pc.getc();
greenroshks 0:8b0d43fe6c05 125 if(c=='a')
greenroshks 0:8b0d43fe6c05 126 {
greenroshks 0:8b0d43fe6c05 127 printf("Telecommand detected\n");
greenroshks 0:8b0d43fe6c05 128 beac_flag=1;
greenroshks 0:8b0d43fe6c05 129 }
greenroshks 0:8b0d43fe6c05 130 }
greenroshks 0:8b0d43fe6c05 131
greenroshks 0:8b0d43fe6c05 132 void t_bea(void const *args)
greenroshks 0:8b0d43fe6c05 133 {
greenroshks 0:8b0d43fe6c05 134
greenroshks 0:8b0d43fe6c05 135 while(1)
greenroshks 0:8b0d43fe6c05 136 {
greenroshks 0:8b0d43fe6c05 137 Thread::signal_wait(0x3);
greenroshks 0:8b0d43fe6c05 138 printf("\nTHIS IS BEACON %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 139 t.start();
greenroshks 0:8b0d43fe6c05 140
greenroshks 0:8b0d43fe6c05 141
greenroshks 0:8b0d43fe6c05 142
greenroshks 0:8b0d43fe6c05 143 FUNC_BEA();
greenroshks 0:8b0d43fe6c05 144
greenroshks 0:8b0d43fe6c05 145
greenroshks 0:8b0d43fe6c05 146 if(beac_flag==1)
greenroshks 0:8b0d43fe6c05 147 {
greenroshks 0:8b0d43fe6c05 148 Thread::wait(600000);
greenroshks 0:8b0d43fe6c05 149 beac_flag = 0;
greenroshks 0:8b0d43fe6c05 150 }
greenroshks 0:8b0d43fe6c05 151
greenroshks 0:8b0d43fe6c05 152 printf("The time to execute beacon thread is %f seconds\n",t.read());
greenroshks 0:8b0d43fe6c05 153 t.reset();
greenroshks 0:8b0d43fe6c05 154 }
greenroshks 0:8b0d43fe6c05 155 }
greenroshks 0:8b0d43fe6c05 156
greenroshks 0:8b0d43fe6c05 157 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 158 //TASK 4 : FAULT MANAGEMENT
greenroshks 0:8b0d43fe6c05 159 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 160 //Dummy fault rectifier functions
greenroshks 0:8b0d43fe6c05 161
sakthipriya 2:80b8a2e999f7 162 /*Mail<int,16> faults;
greenroshks 0:8b0d43fe6c05 163
greenroshks 0:8b0d43fe6c05 164 void FUNC_FAULT_FUNCT1()
greenroshks 0:8b0d43fe6c05 165 {
greenroshks 0:8b0d43fe6c05 166 printf("\nFault 1 detected... \n");
greenroshks 0:8b0d43fe6c05 167 }
greenroshks 0:8b0d43fe6c05 168
greenroshks 0:8b0d43fe6c05 169 void FUNC_FAULT_FUNCT2()
greenroshks 0:8b0d43fe6c05 170 {
greenroshks 0:8b0d43fe6c05 171 printf("\nFault 2 detected...\n");
greenroshks 0:8b0d43fe6c05 172 }
greenroshks 0:8b0d43fe6c05 173
greenroshks 0:8b0d43fe6c05 174 void T_FAULT(void const *args)
greenroshks 0:8b0d43fe6c05 175 {
greenroshks 0:8b0d43fe6c05 176 while(1)
greenroshks 0:8b0d43fe6c05 177 {
greenroshks 0:8b0d43fe6c05 178 osEvent evt = faults.get();
greenroshks 0:8b0d43fe6c05 179 if(evt.status==osEventMail)
greenroshks 0:8b0d43fe6c05 180 {
greenroshks 0:8b0d43fe6c05 181 int *fault_id= (int *)evt.value.p;
greenroshks 0:8b0d43fe6c05 182 switch(*fault_id)
greenroshks 0:8b0d43fe6c05 183 {
greenroshks 0:8b0d43fe6c05 184 case 1: FUNC_FAULT_FUNCT1();
greenroshks 0:8b0d43fe6c05 185 break;
greenroshks 0:8b0d43fe6c05 186 case 2: FUNC_FAULT_FUNCT2();
greenroshks 0:8b0d43fe6c05 187 break;
greenroshks 0:8b0d43fe6c05 188 }
greenroshks 0:8b0d43fe6c05 189 faults.free(fault_id);
greenroshks 0:8b0d43fe6c05 190 }
greenroshks 0:8b0d43fe6c05 191 }
sakthipriya 2:80b8a2e999f7 192 }*/
sakthipriya 2:80b8a2e999f7 193 void T_FAULT(void const *args)
sakthipriya 4:8f6c24eca109 194 {
sakthipriya 4:8f6c24eca109 195 char flag2='1';
sakthipriya 2:80b8a2e999f7 196 while(1)
sakthipriya 2:80b8a2e999f7 197 {
sakthipriya 2:80b8a2e999f7 198 Thread :: signal_wait(0x2);
sakthipriya 2:80b8a2e999f7 199 FAULTS();
sakthipriya 4:8f6c24eca109 200 POWER(flag2);
sakthipriya 4:8f6c24eca109 201 flag2++; //testing ... should be removed
sakthipriya 2:80b8a2e999f7 202 }
greenroshks 0:8b0d43fe6c05 203 }
greenroshks 0:8b0d43fe6c05 204
greenroshks 0:8b0d43fe6c05 205 //------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 206 //SCHEDULER
greenroshks 0:8b0d43fe6c05 207 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 4:8f6c24eca109 208 int beacon_sc = 3;
greenroshks 0:8b0d43fe6c05 209 uint16_t schedcount=1;
greenroshks 0:8b0d43fe6c05 210 void t_sc(void const *args)
greenroshks 0:8b0d43fe6c05 211 {
greenroshks 0:8b0d43fe6c05 212
greenroshks 0:8b0d43fe6c05 213 printf("The value of i in scheduler is %d\n",schedcount);
greenroshks 0:8b0d43fe6c05 214 if(schedcount == 65532) //to reset the counter
greenroshks 0:8b0d43fe6c05 215 {
greenroshks 0:8b0d43fe6c05 216 schedcount = 0;
greenroshks 0:8b0d43fe6c05 217 }
greenroshks 0:8b0d43fe6c05 218
greenroshks 0:8b0d43fe6c05 219 if(schedcount%1==0)
greenroshks 0:8b0d43fe6c05 220 {
greenroshks 0:8b0d43fe6c05 221 ptr_t_acs -> signal_set(0x1);
greenroshks 0:8b0d43fe6c05 222 }
greenroshks 0:8b0d43fe6c05 223 if(schedcount%2==0)
greenroshks 0:8b0d43fe6c05 224 {
sakthipriya 3:02b45de29c0f 225 ptr_t_fault -> signal_set(0x2);
greenroshks 0:8b0d43fe6c05 226 ptr_t_hk_acq -> signal_set(0x2);
sakthipriya 2:80b8a2e999f7 227
greenroshks 0:8b0d43fe6c05 228 }
sakthipriya 4:8f6c24eca109 229 if(schedcount%beacon_sc==0)
greenroshks 0:8b0d43fe6c05 230 {
greenroshks 0:8b0d43fe6c05 231 if(beac_flag==0)
greenroshks 0:8b0d43fe6c05 232 {
greenroshks 0:8b0d43fe6c05 233
greenroshks 0:8b0d43fe6c05 234 ptr_t_bea -> signal_set(0x3);
greenroshks 0:8b0d43fe6c05 235 }
greenroshks 0:8b0d43fe6c05 236 }
greenroshks 0:8b0d43fe6c05 237 schedcount++;
greenroshks 0:8b0d43fe6c05 238 }
greenroshks 0:8b0d43fe6c05 239
greenroshks 0:8b0d43fe6c05 240 //---------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 241
greenroshks 0:8b0d43fe6c05 242 int main()
greenroshks 0:8b0d43fe6c05 243 {
greenroshks 0:8b0d43fe6c05 244 t1.start();
greenroshks 0:8b0d43fe6c05 245
greenroshks 0:8b0d43fe6c05 246 ptr_t_hk_acq = new Thread(t_hk_acq);
greenroshks 0:8b0d43fe6c05 247 ptr_t_acs = new Thread(t_acs);
greenroshks 0:8b0d43fe6c05 248 ptr_t_acs_write2flash = new Thread(t_acs_write2flash);
greenroshks 0:8b0d43fe6c05 249 ptr_t_bea = new Thread(t_bea);
greenroshks 0:8b0d43fe6c05 250 ptr_t_bea_telecommand = new Thread(t_bea_telecommand);
greenroshks 0:8b0d43fe6c05 251 ptr_t_fault = new Thread(T_FAULT);
greenroshks 0:8b0d43fe6c05 252 //ptr_t_sc = new Thread(t_sc);
greenroshks 0:8b0d43fe6c05 253
greenroshks 0:8b0d43fe6c05 254 ptr_t_fault -> set_priority(osPriorityRealtime);
greenroshks 0:8b0d43fe6c05 255 ptr_t_acs->set_priority(osPriorityHigh);
greenroshks 0:8b0d43fe6c05 256 ptr_t_hk_acq->set_priority(osPriorityNormal);
greenroshks 0:8b0d43fe6c05 257 ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal);
greenroshks 0:8b0d43fe6c05 258 ptr_t_bea->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 259 ptr_t_bea_telecommand->set_priority(osPriorityIdle);
greenroshks 0:8b0d43fe6c05 260 //ptr_t_sc->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 261
greenroshks 0:8b0d43fe6c05 262
greenroshks 0:8b0d43fe6c05 263 // ----------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 264 printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority());
greenroshks 0:8b0d43fe6c05 265 printf("\n T_ACS priority is %d",ptr_t_acs->get_priority());
greenroshks 0:8b0d43fe6c05 266 printf("\n T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
greenroshks 0:8b0d43fe6c05 267 printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority());
greenroshks 0:8b0d43fe6c05 268 printf("\n T_BEA priority is %d",ptr_t_bea->get_priority());
greenroshks 0:8b0d43fe6c05 269 RtosTimer t_sc_timer(t_sc,osTimerPeriodic);
greenroshks 0:8b0d43fe6c05 270 t_sc_timer.start(10000);
greenroshks 0:8b0d43fe6c05 271 printf("\n%f\n",t1.read());
greenroshks 0:8b0d43fe6c05 272
greenroshks 0:8b0d43fe6c05 273 while(1)
greenroshks 0:8b0d43fe6c05 274 {
greenroshks 0:8b0d43fe6c05 275 Thread::wait(10000);
greenroshks 0:8b0d43fe6c05 276 ;
greenroshks 0:8b0d43fe6c05 277 }
greenroshks 0:8b0d43fe6c05 278
greenroshks 0:8b0d43fe6c05 279 }