no beacon yes yes beacon no

Dependencies:   mbed-rtos mbed

Fork of all_combined_week6 by green rosh

Committer:
greenroshks
Date:
Fri Sep 19 05:12:40 2014 +0000
Revision:
7:b3f876f605d9
Parent:
6:82153349cc9b
latest

Who changed what in which revision?

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