Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BAE_vr2_gingerbread2 by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "HK.h" 00004 #include "slave.h" 00005 #include "beacon.h" 00006 #include "ACS.h" 00007 #include "fault.h" 00008 #include "slave.h" 00009 #include "pin_config.h" 00010 00011 Serial pc(USBTX, USBRX); 00012 00013 InterruptIn interrupt(D9); 00014 InterruptIn master_reset(D11); 00015 00016 00017 Timer t; //To know the time of execution each thread 00018 Timer t1; 00019 //To know the time of entering of each thread 00020 00021 Thread *ptr_t_hk_acq; 00022 Thread *ptr_t_acs; 00023 //Thread *ptr_t_acs_write2flash; 00024 Thread *ptr_t_bea; 00025 //Thread *ptr_t_bea_telecommand; 00026 //Thread *ptr_t_fault; 00027 Thread *ptr_t_i2c; 00028 Thread *ptr_t_wdt; 00029 00030 00031 00032 void write_to_master(char); //function to write data to master 00033 00034 I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave 00035 00036 00037 DigitalOut data_ready(D10); 00038 int i2c_status=0; //read/write mode for i2c 0 : write2slave, 1 : write2master 00039 int reset=0; 00040 int temp; 00041 00042 typedef struct 00043 { 00044 char data[25]; // To avoid dynamic memory allocation 00045 int length; 00046 }i2c_data; 00047 00048 00049 00050 00051 00052 00053 Mail<i2c_data,16> i2c_data_receive; 00054 Mail<i2c_data,16> i2c_data_send; 00055 00056 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00057 //TASK 2 : HK 00058 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00059 00060 char hk_data[25]; 00061 extern SensorData Sensor; 00062 extern SensorDataQuantised SensorQuantised; 00063 void t_hk_acq(void const *args) 00064 { 00065 00066 while(1) 00067 { 00068 Thread::signal_wait(0x2); 00069 SensorQuantised.power_mode='0'; 00070 printf("\nTHIS IS HK %f\n",t1.read()); 00071 t.start(); 00072 FAULTS(); 00073 POWER(SensorQuantised.power_mode); //The power mode algorithm is yet to be obtained 00074 FUNC_HK_MAIN(); //Collecting HK data 00075 //thread_2.signal_set(0x4); 00076 //FUNC_I2C_SLAVE_MAIN(25); 00077 ir2master(); 00078 t.stop(); 00079 printf("The time to execute hk_acq is %f seconds\n",t.read()); 00080 t.reset(); 00081 } 00082 } 00083 00084 //--------------------------------------------------------------------------------------------------------------------------------------- 00085 //TASK 1 : ACS 00086 //--------------------------------------------------------------------------------------------------------------------------------------- 00087 00088 int acs_pflag = 1; 00089 void t_acs(void const *args) 00090 { 00091 float *mag_field; 00092 float *omega; 00093 float *moment; 00094 while(1) 00095 { 00096 Thread::signal_wait(0x1); 00097 printf("\nTHIS IS ACS %f\n",t1.read()); 00098 t.start(); 00099 mag_field= FUNC_ACS_MAG_EXEC(); //actual execution 00100 omega = FUNC_ACS_EXEC_GYR(); 00101 Sensor.AngularSpeed[0]= omega[0]; 00102 Sensor.AngularSpeed[1]= omega[1]; 00103 Sensor.AngularSpeed[2]= omega[2]; 00104 00105 Sensor.Bnewvalue[0] = mag_field[0]; 00106 Sensor.Bnewvalue[1] = mag_field[1]; 00107 Sensor.Bnewvalue[2] = mag_field[2]; 00108 00109 if(acs_pflag == 1) 00110 { 00111 moment = FUNC_ACS_CNTRLALGO(mag_field,omega); 00112 FUNC_ACS_GENPWM(moment); 00113 } 00114 00115 t.reset(); 00116 } 00117 } 00118 /* 00119 void t_acs_write2flash(void const *args) 00120 { 00121 while(1) 00122 { 00123 //printf("Writing in the flash\n"); 00124 osEvent evt = q_acs.get(); 00125 if(evt.status == osEventMail) 00126 { 00127 sensor_data *ptr = (sensor_data*)evt.value.p; 00128 func_acs_write2flash(ptr); 00129 q_acs.free(ptr); 00130 } 00131 printf("Writing acs data in the flash\n"); 00132 } 00133 } 00134 00135 */ 00136 //---------------------------------------------------BEACON-------------------------------------------------------------------------------------------- 00137 00138 int beac_flag=0; //To receive telecommand from ground. 00139 00140 00141 /*void t_bea_telecommand(void const *args) 00142 { 00143 char c = pc.getc(); 00144 if(c=='a') 00145 { 00146 printf("Telecommand detected\n"); 00147 beac_flag=1; 00148 } 00149 } 00150 */ 00151 00152 void t_bea(void const *args) 00153 { 00154 00155 while(1) 00156 { 00157 Thread::signal_wait(0x3); 00158 printf("\nTHIS IS BEACON %f\n",t1.read()); 00159 t.start(); 00160 00161 00162 00163 FUNC_BEA(); 00164 00165 00166 if(beac_flag==1) 00167 { 00168 Thread::wait(600000); 00169 beac_flag = 0; 00170 } 00171 00172 printf("The time to execute beacon thread is %f seconds\n",t.read()); 00173 t.reset(); 00174 } 00175 } 00176 00177 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00178 //TASK 4 : FAULT MANAGEMENT 00179 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00180 //Dummy fault rectifier functions 00181 00182 /*Mail<int,16> faults; 00183 00184 void FUNC_FAULT_FUNCT1() 00185 { 00186 printf("\nFault 1 detected... \n"); 00187 } 00188 00189 void FUNC_FAULT_FUNCT2() 00190 { 00191 printf("\nFault 2 detected...\n"); 00192 } 00193 00194 void T_FAULT(void const *args) 00195 { 00196 while(1) 00197 { 00198 osEvent evt = faults.get(); 00199 if(evt.status==osEventMail) 00200 { 00201 int *fault_id= (int *)evt.value.p; 00202 switch(*fault_id) 00203 { 00204 case 1: FUNC_FAULT_FUNCT1(); 00205 break; 00206 case 2: FUNC_FAULT_FUNCT2(); 00207 break; 00208 } 00209 faults.free(fault_id); 00210 } 00211 } 00212 }*/ 00213 00214 extern SensorDataQuantised SensorQuantised; 00215 00216 00217 /*void T_FAULT(void const *args) 00218 { 00219 Sensor.power_mode='0'; 00220 while(1) 00221 { 00222 Thread :: signal_wait(0x2); 00223 FAULTS(); 00224 POWER(Sensor.power_mode); 00225 //Sensor.power_mode++; //testing ... should be removed 00226 } 00227 } 00228 */ 00229 /*------------------------------------------------------------------------------------------------------------------------------------------- 00230 -------------------------------------------------------WATCHDOG----------------------------------------------------------------------------*/ 00231 DigitalOut trigger(PIN63); // has to be changed 00232 void T_WDT(void const * args) 00233 { 00234 trigger = 1; 00235 while(true) 00236 { 00237 Thread::signal_wait(0x5); //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized 00238 printf("\nEntered WD\n"); 00239 trigger = !trigger; 00240 } 00241 } 00242 00243 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00244 //TASK 5 : i2c data 00245 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00246 00247 00248 void C_FUNC_INT() 00249 { 00250 FUNC_INT(); 00251 ptr_t_i2c->signal_set(0x4); 00252 00253 } 00254 00255 void C_FUNC_RESET() 00256 { 00257 FUNC_RESET(); 00258 } 00259 00260 00261 void C_T_I2C_BAE(void const * args) 00262 { 00263 //char data_send,data_receive; 00264 while(1) 00265 { 00266 Thread::signal_wait(0x4); 00267 T_I2C_BAE(); 00268 //i2c_status = temp; 00269 //wait(0.5); 00270 /*printf("\n entered thread\n\r"); 00271 if(i2c_status == 0 && reset !=1) 00272 { 00273 00274 FUNC_I2C_WRITE2CDMS(&data_receive); 00275 i2c_data * i2c_data_r = i2c_data_receive.alloc(); 00276 i2c_data_r->data = data_receive; 00277 i2c_data_r->length = 1; 00278 i2c_data_receive.put(i2c_data_r); 00279 printf("\n Data received from CDMS is %c\n\r",data_receive); 00280 i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread 00281 00282 } 00283 else if(i2c_status ==1 && reset !=1) 00284 { 00285 osEvent evt = i2c_data_send.get(); 00286 if (evt.status == osEventMail) 00287 { 00288 i2c_data *i2c_data_s = (i2c_data*)evt.value.p; 00289 data_send = i2c_data_s -> data; 00290 FUNC_I2C_WRITE2CDMS(&data_send); 00291 printf("\nData sent to CDMS is %c\n\r",data_send); 00292 i2c_data_send.free(i2c_data_s); 00293 i2c_status = 0; 00294 //temp = i2c_status; 00295 } 00296 } 00297 */ 00298 } 00299 } 00300 00301 00302 00303 00304 00305 void FUNC_I2C_WRITE2CDMS(char *data, int length=1) 00306 { 00307 int slave_status = 1; 00308 00309 while(slave_status) 00310 { 00311 slave.address(0x20); 00312 if(slave.receive()==1) 00313 { 00314 slave_status=slave.write(data,length); 00315 00316 00317 } 00318 else if(slave.receive()==3 || slave.receive()==2) 00319 { 00320 slave_status=slave.read(data,length); 00321 } 00322 00323 } 00324 00325 printf("\ndone\n\r"); 00326 00327 } 00328 char data_send[25],data_receive[25]; 00329 void T_I2C_BAE() 00330 { 00331 //char data_send,data_receive; 00332 //while(1) 00333 //{ 00334 // Thread::signal_wait(0x1); 00335 //i2c_status = temp; 00336 //wait(0.5); 00337 printf("\n entered thread %d\n\r",i2c_status); 00338 if(i2c_status == 0 && reset !=1) 00339 { 00340 00341 FUNC_I2C_WRITE2CDMS(data_receive,25); 00342 i2c_data * i2c_data_r = i2c_data_receive.alloc(); 00343 strcpy(i2c_data_r->data,data_receive); 00344 i2c_data_r->length = 25; 00345 i2c_data_receive.put(i2c_data_r); 00346 printf("\n Data received from CDMS is %s \n\r",data_receive); 00347 i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread 00348 00349 } 00350 else if(i2c_status ==1 && reset !=1) 00351 { 00352 osEvent evt = i2c_data_send.get(); 00353 if (evt.status == osEventMail) 00354 { 00355 i2c_data *i2c_data_s = (i2c_data*)evt.value.p; 00356 strcpy(data_send,i2c_data_s -> data); 00357 FUNC_I2C_WRITE2CDMS(data_send,25); 00358 printf("\nData sent to CDMS is %s\n\r",data_send); 00359 i2c_data_send.free(i2c_data_s); 00360 i2c_status = 0; 00361 //temp = i2c_status; 00362 00363 } 00364 } 00365 00366 //} 00367 } 00368 00369 00370 00371 void FUNC_INT() 00372 { 00373 reset = 0; 00374 00375 00376 } 00377 00378 void FUNC_RESET() 00379 { 00380 reset = 1; 00381 } 00382 00383 00384 00385 void ir2master() 00386 { 00387 00388 //char data[25]; 00389 //strcpy(data,"sakthi "); 00390 //strcat(data,"priya"); 00391 strcpy(hk_data,SensorQuantised.Voltage); 00392 strcat(hk_data,SensorQuantised.Current); 00393 strcat(hk_data,SensorQuantised.Temperature); 00394 strcat(hk_data,SensorQuantised.PanelTemperature); 00395 strcat(hk_data,SensorQuantised.AngularSpeed); 00396 strcat(hk_data,SensorQuantised.Bnewvalue); 00397 char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode}; 00398 00399 /*strcat(hk_data,sfaultpoll); 00400 strcat(hk_data,sfaultir); 00401 strcat(hk_data,spower_mode);*/ 00402 strcat(hk_data,fdata); 00403 printf("\nhk data : %s\n",hk_data); 00404 data_ready=0; 00405 //data = pcslave.getc(); 00406 reset =0; 00407 i2c_status=1; 00408 i2c_data * i2c_data_s = i2c_data_send.alloc(); 00409 strcpy(i2c_data_s->data,hk_data); 00410 i2c_data_s->length = 25; 00411 i2c_data_send.put(i2c_data_s); 00412 data_ready=1; 00413 //temp = i2c_status; 00414 } 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 //------------------------------------------------------------------------------------------------------------------------------------------------ 00428 //SCHEDULER 00429 //------------------------------------------------------------------------------------------------------------------------------------------------ 00430 int beacon_sc = 3; 00431 uint16_t schedcount=1; 00432 void t_sc(void const *args) 00433 { 00434 00435 printf("The value of i in scheduler is %d\n",schedcount); 00436 if(schedcount == 65532) //to reset the counter 00437 { 00438 schedcount = 0; 00439 } 00440 00441 if(schedcount%1==0) 00442 { 00443 ptr_t_acs -> signal_set(0x1); 00444 ptr_t_wdt -> signal_set(0x5); 00445 } 00446 if(schedcount%2==0) 00447 { 00448 //ptr_t_fault -> signal_set(0x2); 00449 ptr_t_hk_acq -> signal_set(0x2); 00450 00451 } 00452 if(schedcount%beacon_sc==0) 00453 { 00454 if(beac_flag==0) 00455 { 00456 00457 //ptr_t_bea -> signal_set(0x3); 00458 } 00459 } 00460 schedcount++; 00461 } 00462 00463 //--------------------------------------------------------------------------------------------------------------------------------------------- 00464 00465 int main() 00466 { 00467 t1.start(); 00468 00469 ptr_t_hk_acq = new Thread(t_hk_acq); 00470 ptr_t_acs = new Thread(t_acs); 00471 //ptr_t_acs_write2flash = new Thread(t_acs_write2flash); 00472 ptr_t_bea = new Thread(t_bea); 00473 //ptr_t_bea_telecommand = new Thread(t_bea_telecommand); 00474 //ptr_t_fault = new Thread(T_FAULT); 00475 ptr_t_i2c = new Thread(C_T_I2C_BAE); 00476 //ptr_t_sc = new Thread(t_sc); 00477 ptr_t_wdt = new Thread(T_WDT); 00478 00479 interrupt_fault(); 00480 00481 //ptr_t_fault -> set_priority(osPriorityRealtime); 00482 ptr_t_acs->set_priority(osPriorityHigh); 00483 ptr_t_i2c->set_priority(osPriorityAboveNormal); 00484 ptr_t_hk_acq->set_priority(osPriorityNormal); 00485 //ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal); 00486 ptr_t_bea->set_priority(osPriorityAboveNormal); 00487 //ptr_t_bea_telecommand->set_priority(osPriorityIdle); 00488 //ptr_t_sc->set_priority(osPriorityAboveNormal); 00489 ptr_t_wdt -> set_priority(osPriorityIdle); 00490 00491 00492 // ---------------------------------------------------------------------------------------------- 00493 //printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority()); 00494 printf("\n T_ACS priority is %d",ptr_t_acs->get_priority()); 00495 printf("\n T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority()); 00496 //printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority()); 00497 printf("\n T_BEA priority is %d",ptr_t_bea->get_priority()); 00498 RtosTimer t_sc_timer(t_sc,osTimerPeriodic); 00499 t_sc_timer.start(10000); 00500 printf("\n%f\n",t1.read()); 00501 00502 00503 00504 master_reset.fall(&C_FUNC_RESET); 00505 interrupt.rise(&C_FUNC_INT); 00506 00507 while(1) 00508 { 00509 //Thread::wait(10000); 00510 //ir2master(); 00511 Thread::wait(5000); 00512 } 00513 00514 }
Generated on Thu Jul 28 2022 07:46:36 by
1.7.2
