The one with the new HK

Dependencies:   mbed-rtos mbed

Fork of BAE_vr2_1_1 by Seeker of Truth ,

Committer:
raizel_varun
Date:
Tue Dec 09 07:59:56 2014 +0000
Revision:
6:e65b1ab79f36
Parent:
5:255b43e8e21a
Child:
7:f06840d848e3
BAE

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
sakthipriya 5:255b43e8e21a 13 Timer t; //To know the time of execution each thread
sakthipriya 4:8f6c24eca109 14 Timer t1;
sakthipriya 5:255b43e8e21a 15 //To know the time of entering 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
sakthipriya 5:255b43e8e21a 81 int acs_pflag = 1;
greenroshks 0:8b0d43fe6c05 82 void t_acs(void const *args)
greenroshks 0:8b0d43fe6c05 83 {
greenroshks 0:8b0d43fe6c05 84 while(1)
greenroshks 0:8b0d43fe6c05 85 {
greenroshks 0:8b0d43fe6c05 86 Thread::signal_wait(0x1);
greenroshks 0:8b0d43fe6c05 87 printf("\nTHIS IS ACS %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 88 t.start();
greenroshks 0:8b0d43fe6c05 89 sensor_data *ptr = q_acs.alloc();
greenroshks 0:8b0d43fe6c05 90
greenroshks 0:8b0d43fe6c05 91 func_acs_readdata(ptr);
greenroshks 0:8b0d43fe6c05 92 q_acs.put(ptr);
sakthipriya 5:255b43e8e21a 93 if(acs_pflag == 1)
sakthipriya 5:255b43e8e21a 94 {
greenroshks 0:8b0d43fe6c05 95 func_acs_ctrlalgo();
greenroshks 0:8b0d43fe6c05 96 FUNC_ACS_GENPWM(); //Generating PWM signal.
sakthipriya 5:255b43e8e21a 97 }
greenroshks 0:8b0d43fe6c05 98
greenroshks 0:8b0d43fe6c05 99 t.reset();
greenroshks 0:8b0d43fe6c05 100 }
greenroshks 0:8b0d43fe6c05 101 }
greenroshks 0:8b0d43fe6c05 102
greenroshks 0:8b0d43fe6c05 103 void t_acs_write2flash(void const *args)
greenroshks 0:8b0d43fe6c05 104 {
greenroshks 0:8b0d43fe6c05 105 while(1)
greenroshks 0:8b0d43fe6c05 106 {
greenroshks 0:8b0d43fe6c05 107 //printf("Writing in the flash\n");
greenroshks 0:8b0d43fe6c05 108 osEvent evt = q_acs.get();
greenroshks 0:8b0d43fe6c05 109 if(evt.status == osEventMail)
greenroshks 0:8b0d43fe6c05 110 {
greenroshks 0:8b0d43fe6c05 111 sensor_data *ptr = (sensor_data*)evt.value.p;
greenroshks 0:8b0d43fe6c05 112 func_acs_write2flash(ptr);
greenroshks 0:8b0d43fe6c05 113 q_acs.free(ptr);
greenroshks 0:8b0d43fe6c05 114 }
greenroshks 0:8b0d43fe6c05 115 printf("Writing acs data in the flash\n");
greenroshks 0:8b0d43fe6c05 116 }
greenroshks 0:8b0d43fe6c05 117 }
greenroshks 0:8b0d43fe6c05 118
greenroshks 0:8b0d43fe6c05 119
greenroshks 0:8b0d43fe6c05 120 //---------------------------------------------------BEACON--------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 121
greenroshks 0:8b0d43fe6c05 122 int beac_flag=0; //To receive telecommand from ground.
greenroshks 0:8b0d43fe6c05 123
greenroshks 0:8b0d43fe6c05 124
greenroshks 0:8b0d43fe6c05 125 void t_bea_telecommand(void const *args)
greenroshks 0:8b0d43fe6c05 126 {
greenroshks 0:8b0d43fe6c05 127 char c = pc.getc();
greenroshks 0:8b0d43fe6c05 128 if(c=='a')
greenroshks 0:8b0d43fe6c05 129 {
greenroshks 0:8b0d43fe6c05 130 printf("Telecommand detected\n");
greenroshks 0:8b0d43fe6c05 131 beac_flag=1;
greenroshks 0:8b0d43fe6c05 132 }
greenroshks 0:8b0d43fe6c05 133 }
greenroshks 0:8b0d43fe6c05 134
greenroshks 0:8b0d43fe6c05 135 void t_bea(void const *args)
greenroshks 0:8b0d43fe6c05 136 {
greenroshks 0:8b0d43fe6c05 137
greenroshks 0:8b0d43fe6c05 138 while(1)
greenroshks 0:8b0d43fe6c05 139 {
greenroshks 0:8b0d43fe6c05 140 Thread::signal_wait(0x3);
greenroshks 0:8b0d43fe6c05 141 printf("\nTHIS IS BEACON %f\n",t1.read());
greenroshks 0:8b0d43fe6c05 142 t.start();
greenroshks 0:8b0d43fe6c05 143
greenroshks 0:8b0d43fe6c05 144
greenroshks 0:8b0d43fe6c05 145
greenroshks 0:8b0d43fe6c05 146 FUNC_BEA();
greenroshks 0:8b0d43fe6c05 147
greenroshks 0:8b0d43fe6c05 148
greenroshks 0:8b0d43fe6c05 149 if(beac_flag==1)
greenroshks 0:8b0d43fe6c05 150 {
greenroshks 0:8b0d43fe6c05 151 Thread::wait(600000);
greenroshks 0:8b0d43fe6c05 152 beac_flag = 0;
greenroshks 0:8b0d43fe6c05 153 }
greenroshks 0:8b0d43fe6c05 154
greenroshks 0:8b0d43fe6c05 155 printf("The time to execute beacon thread is %f seconds\n",t.read());
greenroshks 0:8b0d43fe6c05 156 t.reset();
greenroshks 0:8b0d43fe6c05 157 }
greenroshks 0:8b0d43fe6c05 158 }
greenroshks 0:8b0d43fe6c05 159
greenroshks 0:8b0d43fe6c05 160 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 161 //TASK 4 : FAULT MANAGEMENT
greenroshks 0:8b0d43fe6c05 162 //---------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 163 //Dummy fault rectifier functions
greenroshks 0:8b0d43fe6c05 164
sakthipriya 2:80b8a2e999f7 165 /*Mail<int,16> faults;
greenroshks 0:8b0d43fe6c05 166
greenroshks 0:8b0d43fe6c05 167 void FUNC_FAULT_FUNCT1()
greenroshks 0:8b0d43fe6c05 168 {
greenroshks 0:8b0d43fe6c05 169 printf("\nFault 1 detected... \n");
greenroshks 0:8b0d43fe6c05 170 }
greenroshks 0:8b0d43fe6c05 171
greenroshks 0:8b0d43fe6c05 172 void FUNC_FAULT_FUNCT2()
greenroshks 0:8b0d43fe6c05 173 {
greenroshks 0:8b0d43fe6c05 174 printf("\nFault 2 detected...\n");
greenroshks 0:8b0d43fe6c05 175 }
greenroshks 0:8b0d43fe6c05 176
greenroshks 0:8b0d43fe6c05 177 void T_FAULT(void const *args)
greenroshks 0:8b0d43fe6c05 178 {
greenroshks 0:8b0d43fe6c05 179 while(1)
greenroshks 0:8b0d43fe6c05 180 {
greenroshks 0:8b0d43fe6c05 181 osEvent evt = faults.get();
greenroshks 0:8b0d43fe6c05 182 if(evt.status==osEventMail)
greenroshks 0:8b0d43fe6c05 183 {
greenroshks 0:8b0d43fe6c05 184 int *fault_id= (int *)evt.value.p;
greenroshks 0:8b0d43fe6c05 185 switch(*fault_id)
greenroshks 0:8b0d43fe6c05 186 {
greenroshks 0:8b0d43fe6c05 187 case 1: FUNC_FAULT_FUNCT1();
greenroshks 0:8b0d43fe6c05 188 break;
greenroshks 0:8b0d43fe6c05 189 case 2: FUNC_FAULT_FUNCT2();
greenroshks 0:8b0d43fe6c05 190 break;
greenroshks 0:8b0d43fe6c05 191 }
greenroshks 0:8b0d43fe6c05 192 faults.free(fault_id);
greenroshks 0:8b0d43fe6c05 193 }
greenroshks 0:8b0d43fe6c05 194 }
sakthipriya 2:80b8a2e999f7 195 }*/
sakthipriya 5:255b43e8e21a 196
sakthipriya 5:255b43e8e21a 197 extern SensorData Sensor;
raizel_varun 6:e65b1ab79f36 198
raizel_varun 6:e65b1ab79f36 199
sakthipriya 2:80b8a2e999f7 200 void T_FAULT(void const *args)
sakthipriya 4:8f6c24eca109 201 {
sakthipriya 5:255b43e8e21a 202 Sensor.power_mode='0';
sakthipriya 2:80b8a2e999f7 203 while(1)
sakthipriya 2:80b8a2e999f7 204 {
sakthipriya 2:80b8a2e999f7 205 Thread :: signal_wait(0x2);
sakthipriya 2:80b8a2e999f7 206 FAULTS();
sakthipriya 5:255b43e8e21a 207 POWER(Sensor.power_mode);
sakthipriya 5:255b43e8e21a 208 //Sensor.power_mode++; //testing ... should be removed
sakthipriya 2:80b8a2e999f7 209 }
greenroshks 0:8b0d43fe6c05 210 }
greenroshks 0:8b0d43fe6c05 211
greenroshks 0:8b0d43fe6c05 212 //------------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 213 //SCHEDULER
greenroshks 0:8b0d43fe6c05 214 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 4:8f6c24eca109 215 int beacon_sc = 3;
greenroshks 0:8b0d43fe6c05 216 uint16_t schedcount=1;
greenroshks 0:8b0d43fe6c05 217 void t_sc(void const *args)
greenroshks 0:8b0d43fe6c05 218 {
greenroshks 0:8b0d43fe6c05 219
greenroshks 0:8b0d43fe6c05 220 printf("The value of i in scheduler is %d\n",schedcount);
greenroshks 0:8b0d43fe6c05 221 if(schedcount == 65532) //to reset the counter
greenroshks 0:8b0d43fe6c05 222 {
greenroshks 0:8b0d43fe6c05 223 schedcount = 0;
greenroshks 0:8b0d43fe6c05 224 }
greenroshks 0:8b0d43fe6c05 225
greenroshks 0:8b0d43fe6c05 226 if(schedcount%1==0)
greenroshks 0:8b0d43fe6c05 227 {
greenroshks 0:8b0d43fe6c05 228 ptr_t_acs -> signal_set(0x1);
greenroshks 0:8b0d43fe6c05 229 }
greenroshks 0:8b0d43fe6c05 230 if(schedcount%2==0)
greenroshks 0:8b0d43fe6c05 231 {
sakthipriya 3:02b45de29c0f 232 ptr_t_fault -> signal_set(0x2);
greenroshks 0:8b0d43fe6c05 233 ptr_t_hk_acq -> signal_set(0x2);
sakthipriya 2:80b8a2e999f7 234
greenroshks 0:8b0d43fe6c05 235 }
sakthipriya 4:8f6c24eca109 236 if(schedcount%beacon_sc==0)
greenroshks 0:8b0d43fe6c05 237 {
greenroshks 0:8b0d43fe6c05 238 if(beac_flag==0)
greenroshks 0:8b0d43fe6c05 239 {
greenroshks 0:8b0d43fe6c05 240
greenroshks 0:8b0d43fe6c05 241 ptr_t_bea -> signal_set(0x3);
greenroshks 0:8b0d43fe6c05 242 }
greenroshks 0:8b0d43fe6c05 243 }
greenroshks 0:8b0d43fe6c05 244 schedcount++;
greenroshks 0:8b0d43fe6c05 245 }
greenroshks 0:8b0d43fe6c05 246
greenroshks 0:8b0d43fe6c05 247 //---------------------------------------------------------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 248
greenroshks 0:8b0d43fe6c05 249 int main()
greenroshks 0:8b0d43fe6c05 250 {
greenroshks 0:8b0d43fe6c05 251 t1.start();
greenroshks 0:8b0d43fe6c05 252
greenroshks 0:8b0d43fe6c05 253 ptr_t_hk_acq = new Thread(t_hk_acq);
greenroshks 0:8b0d43fe6c05 254 ptr_t_acs = new Thread(t_acs);
greenroshks 0:8b0d43fe6c05 255 ptr_t_acs_write2flash = new Thread(t_acs_write2flash);
greenroshks 0:8b0d43fe6c05 256 ptr_t_bea = new Thread(t_bea);
greenroshks 0:8b0d43fe6c05 257 ptr_t_bea_telecommand = new Thread(t_bea_telecommand);
greenroshks 0:8b0d43fe6c05 258 ptr_t_fault = new Thread(T_FAULT);
greenroshks 0:8b0d43fe6c05 259 //ptr_t_sc = new Thread(t_sc);
greenroshks 0:8b0d43fe6c05 260
greenroshks 0:8b0d43fe6c05 261 ptr_t_fault -> set_priority(osPriorityRealtime);
greenroshks 0:8b0d43fe6c05 262 ptr_t_acs->set_priority(osPriorityHigh);
greenroshks 0:8b0d43fe6c05 263 ptr_t_hk_acq->set_priority(osPriorityNormal);
greenroshks 0:8b0d43fe6c05 264 ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal);
greenroshks 0:8b0d43fe6c05 265 ptr_t_bea->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 266 ptr_t_bea_telecommand->set_priority(osPriorityIdle);
greenroshks 0:8b0d43fe6c05 267 //ptr_t_sc->set_priority(osPriorityAboveNormal);
greenroshks 0:8b0d43fe6c05 268
greenroshks 0:8b0d43fe6c05 269
greenroshks 0:8b0d43fe6c05 270 // ----------------------------------------------------------------------------------------------
greenroshks 0:8b0d43fe6c05 271 printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority());
greenroshks 0:8b0d43fe6c05 272 printf("\n T_ACS priority is %d",ptr_t_acs->get_priority());
greenroshks 0:8b0d43fe6c05 273 printf("\n T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
greenroshks 0:8b0d43fe6c05 274 printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority());
greenroshks 0:8b0d43fe6c05 275 printf("\n T_BEA priority is %d",ptr_t_bea->get_priority());
greenroshks 0:8b0d43fe6c05 276 RtosTimer t_sc_timer(t_sc,osTimerPeriodic);
greenroshks 0:8b0d43fe6c05 277 t_sc_timer.start(10000);
greenroshks 0:8b0d43fe6c05 278 printf("\n%f\n",t1.read());
greenroshks 0:8b0d43fe6c05 279
greenroshks 0:8b0d43fe6c05 280 while(1)
greenroshks 0:8b0d43fe6c05 281 {
greenroshks 0:8b0d43fe6c05 282 Thread::wait(10000);
greenroshks 0:8b0d43fe6c05 283 ;
greenroshks 0:8b0d43fe6c05 284 }
greenroshks 0:8b0d43fe6c05 285
greenroshks 0:8b0d43fe6c05 286 }