![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
QITH FLAGS
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of TF_conops_BAE1_3 by
main.cpp
00001 /************************************************************Header files*****************************************************************/ 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "pin_config.h" 00006 #include "EPS.h" 00007 #include "beacon.h" 00008 #include "ACS.h" 00009 #include "FreescaleIAP.h" //required for r/w to flash 00010 00011 /***********************************************************For Testing - TO BE REMOVED***************************************************/ 00012 00013 Serial pc(USBTX, USBRX); //for debugging purpose. will be removed along with printf 00014 Timer t_exec; //To know the time of execution each thread 00015 Timer t_start; //To know the time of entering of each thread 00016 Timer t_i2c_start; //To check the time sync in i2c communication 00017 Timer t_i2c_exec; //To know the time taken by i2c read/write function 00018 00019 /**********************************************************configuring peripherals********************************************************/ 00020 I2CSlave slave(PIN1,PIN2); //configuring pins as I2Cslave (sda ,scl) 00021 InterruptIn irpt_4m_cdms(PIN97); //I2c interrupt from CDMS 00022 DigitalOut irpt_2_bae(PIN90); //Sets interrupt to CDMS 00023 00024 extern PwmOut PWM1; 00025 extern PwmOut PWM2; 00026 extern PwmOut PWM3; 00027 00028 00029 /****************************************************global variables*********************************************************************/ 00030 // flags--------------------------------------------- 00031 uint32_t BAE_STATUS = 0x00000000; 00032 uint32_t BAE_ENABLE = 0xFFFFFFFF; 00033 //--------------------------------------------------- 00034 char hk_data[25]; 00035 extern SensorDataQuantised SensorQuantised; 00036 /*****************************************************************Threads USed***********************************************************************************/ 00037 Thread *ptr_t_eps; 00038 Thread *ptr_t_acs; 00039 Thread *ptr_t_bea; 00040 Thread *ptr_t_i2c; 00041 //Thread *ptr_t_wdt; 00042 00043 00044 /**************************************************************funtion header**********************************************************************************/ 00045 00046 void FCTN_HK_DATA_CATENATE(); //combines hk structure into a string 00047 00048 00049 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00050 //BEACON THREAD 00051 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00052 00053 int beac_flag=0; //To receive telecommand from ground. 00054 00055 void T_BEA(void const *args) 00056 { 00057 00058 while(1) 00059 { 00060 Thread::signal_wait(0x3); 00061 printf("\n\rTHIS IS BEACON %f\n\r",t_start.read()); 00062 t_exec.start(); 00063 FCTN_BEA_TX_MAIN(); 00064 if(beac_flag==1) 00065 { 00066 Thread::wait(600000); 00067 beac_flag = 0; 00068 } 00069 t_exec.stop(); 00070 printf("The time to execute beacon thread is %f seconds\n\r",t_exec.read()); 00071 t_exec.reset(); 00072 } 00073 } 00074 00075 00076 00077 //--------------------------------------------------------------------------------------------------------------------------------------- 00078 //ACS THREAD 00079 //--------------------------------------------------------------------------------------------------------------------------------------- 00080 int power_flag = 4; //temporary dummy flag 00081 int acs_pflag = 1; 00082 void T_ACS(void const *args) 00083 { 00084 float mag_field1[3]; 00085 float omega1[3]; 00086 //float tauc1[3]; 00087 float moment[3]; 00088 //float *mnm_data; 00089 while(1) 00090 { 00091 Thread::signal_wait(0x1); 00092 printf("\n\rEntered ACS %f\n",t_start.read()); 00093 t_exec.start(); 00094 BAE_STATUS = (BAE_STATUS & 0xFFFEFFFF) + 0x00001000; //set ACS_MAIN_STATUS flag to 1 00095 PWM1 = 0; //clear pwm pins 00096 PWM2 = 0; //clear pwm pins 00097 PWM3 = 0; //clear pwm pins 00098 //--------------------------------------- 00099 omega1[0] = 1 ; //for testing - to be removed 00100 omega1[1] = 2 ; 00101 omega1[2] = 3 ; 00102 mag_field1[0] = 10; 00103 mag_field1[1] = 20; 00104 mag_field1[2] = 30; 00105 //-------------------------------------- 00106 if (BAE_ENABLE & 0x00000008 == 0x00000008) // check if ACS_DATA_ACQ_ENABLE = 1? 00107 { 00108 FCTN_ACS_DATA_ACQ(omega1,mag_field1); 00109 printf("\n\rmnm gyro values\n"); //printing the angular velocity and magnetic field values 00110 for(int i=0; i<3; i++) 00111 { 00112 printf("%f\t",omega1[i]); 00113 } 00114 printf("\n\r mnm mag values\n"); 00115 for(int i=0; i<3; i++) 00116 { 00117 printf("%f\t",mag_field1[i]); 00118 } 00119 } 00120 if(BAE_ENABLE & 0x00000000 == 0x00000000) // check ACS_STATE = ACS_CONTROL_OFF? 00121 { 00122 BAE_STATUS = (BAE_STATUS & 0xFFF1FFFF) + 0x00000000; // set ACS_STATUS = ACS_CONTROL_OFF 00123 PWM1 = 0; //clear pwm pins 00124 PWM2 = 0; //clear pwm pins 00125 PWM3 = 0; //clear pwm pins 00126 } 00127 else 00128 { 00129 if(power_flag > 1) 00130 { 00131 if(BAE_ENABLE & 0x00000020 == 0x00000020) // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY 00132 { 00133 BAE_STATUS = (BAE_STATUS & 0xFFF1FFFF) + 0x00004000; // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY 00134 FCTN_ACS_CNTRLALGO(mag_field1,omega1,moment); 00135 printf("\n\r moment values returned by control algo \n"); 00136 moment[0] = 0; 00137 moment[1] = 0; 00138 printf("\n\r z axis moment %f \n",moment[2]); 00139 FCTN_ACS_GENPWM_MAIN(moment) ; 00140 } 00141 else 00142 { 00143 if(BAE_ENABLE & 0x00000030 == 0x00000030) // check ACS_STATE = ACS_DATA_ACQ_FAILURE 00144 { 00145 BAE_STATUS = (BAE_STATUS & 0xFFF1FFFF) + 0x00006000; // set ACS_STATUS = ACS_DATA_ACQ_FAILURE 00146 PWM1 = 0; //clear pwm pins 00147 PWM2 = 0; //clear pwm pins 00148 PWM3 = 0; //clear pwm pins 00149 } 00150 else 00151 { 00152 if(BAE_ENABLE & 0x00000040 == 0x00000040) // check ACS_STATE = ACS_NOMINAL_ONLY 00153 { 00154 BAE_STATUS = (BAE_STATUS & 0xFFF1FFFF) + 0x00008000; // set ACS_STATUS = ACS_NOMINAL_ONLY 00155 FCTN_ACS_CNTRLALGO(mag_field1,omega1,moment); 00156 printf("\n\r moment values returned by control algo \n"); 00157 for(int i=0; i<3; i++) 00158 { 00159 printf("%f\t",moment[i]); 00160 } 00161 FCTN_ACS_GENPWM_MAIN(moment) ; 00162 } 00163 else 00164 { 00165 if(BAE_ENABLE & 0x00000050 == 0x00000050) // check ACS_STATE = ACS_AUTO_CONTROL 00166 { 00167 BAE_STATUS =(BAE_STATUS & 0xFFF1FFFF) + 0x0000A000; // set ACS_STATUS = ACS_AUTO_CONTROL 00168 //FCTN_ACS_AUTOCTRL_LOGIC // gotta include this code 00169 } 00170 else 00171 { 00172 if(BAE_ENABLE & 0x00000060 == 0x00000060) // check ACS_STATE = ACS_DETUMBLING_ONLY 00173 { 00174 BAE_STATUS =(BAE_STATUS & 0xFFF1FFFF) + 0x0000C000; // set ACS_STATUS = ACS_DETUMBLING_ONLY 00175 FCTN_ACS_CNTRLALGO(mag_field1,omega1,moment); // detumbling code has to be included 00176 FCTN_ACS_GENPWM_MAIN(moment) ; 00177 } 00178 else 00179 { 00180 BAE_STATUS =(BAE_STATUS & 0xFFF1FFFF) + 0x0000E000; // set ACS_STATUS = INVALID STATE 00181 PWM1 = 0; //clear pwm pins 00182 PWM2 = 0; //clear pwm pins 00183 PWM3 = 0; //clear pwm pins 00184 } 00185 } 00186 } 00187 } 00188 } 00189 } 00190 else 00191 { 00192 BAE_STATUS =(BAE_STATUS & 0xFFF1FFFF) + 0x00000004; // set ACS_STATUS = ACS_LOW_POWER 00193 PWM1 = 0; //clear pwm pins 00194 PWM2 = 0; //clear pwm pins 00195 PWM3 = 0; //clear pwm pins 00196 } 00197 } 00198 00199 BAE_STATUS &= 0xFFFFEFFF; //clear ACS_MAIN_STATUS flag 00200 t_exec.stop(); 00201 printf("\n exited acs main %f ",t_exec.read()); 00202 t_exec.reset(); 00203 } 00204 } 00205 00206 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00207 //EPS THREAD 00208 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00209 00210 void T_EPS(void const *args) 00211 { 00212 00213 while(1) 00214 { 00215 Thread::signal_wait(0x2); 00216 printf("\n\rTHIS IS EPS %f\n\r",t_start.read()); 00217 t_exec.start(); 00218 BAE_STATUS |= 0x00040000; //set EPS_MAIN_STATUS flag 00219 FCTN_EPS_HK_MAIN() ; //Collecting HK data 00220 //FUNC_EPS_FAULTS(); //Actual fault management is not implemented 00221 FCTN_EPS_BG_MAIN(); //requesting soc and power mode 00222 //FUNC_HK_POWER(SensorQuantised.power_mode); // !The power mode algorithm is yet to be obtained 00223 00224 FCTN_HK_DATA_CATENATE(); //sending HK data to CDMS 00225 BAE_STATUS &= 0xFFFBFFFF; //clear EPS_MAIN_STATUS flag 00226 t_exec.stop(); 00227 printf("The time to execute hk_acq is %f seconds\n\r",t_exec.read()); 00228 t_exec.reset(); 00229 } 00230 } 00231 00232 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00233 //I2C THREAD 00234 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00235 00236 00237 //.................................................................................................................................... 00238 00239 void FCTN_I2C_READ(char *data,int length = 1); 00240 void FCTN_I2C_WRITE(char *data,int length = 1); 00241 00242 Timer t; // time taken from isr to reach i2c function 00243 Timer t1; //time taken by read or write function in i2c function 00244 Timer t2; //time taken by read function when first command from master comes 00245 bool write_ack = 1; 00246 bool read_ack = 1; 00247 char short_tc[10]; 00248 char long_tc[134]; 00249 char mstr_cmd; 00250 bool cmd_flag = 1; 00251 int length; 00252 //char* data; 00253 bool cmd_err = 1; 00254 00255 00256 void T_I2C(void const * args) 00257 { 00258 while(1) 00259 { 00260 Thread::signal_wait(0x1); 00261 // printf("\n\r check2\n"); 00262 wait_us(100); 00263 00264 if(cmd_flag == 1) 00265 { 00266 t.stop(); 00267 if( slave.receive()==3 || slave.receive()==2) // slave read 00268 { 00269 00270 t1.start(); 00271 read_ack=slave.read(&mstr_cmd,1); 00272 t1.stop(); 00273 if(read_ack == 0) 00274 printf("\n\r Data received from CDMS is %c \n",mstr_cmd); 00275 00276 switch(mstr_cmd) 00277 { 00278 case 's': 00279 length = 10; 00280 cmd_flag = 0; 00281 break; 00282 00283 00284 case 'l': 00285 length = 134; 00286 cmd_flag = 0; 00287 break; 00288 00289 case 'h': 00290 length = 25; 00291 cmd_flag = 0; 00292 break; 00293 00294 default: 00295 printf("\n\r invalid command \n"); 00296 //cmd_err = 0; 00297 cmd_flag = 1; 00298 } 00299 00300 00301 } 00302 } 00303 else if(cmd_flag ==0 ) 00304 { 00305 t.stop(); 00306 if( slave.receive()==3 || slave.receive()==2) // slave read 00307 { 00308 00309 char* data=(char*)malloc(length*sizeof(char)); 00310 FCTN_I2C_READ(data,length); 00311 free(data); 00312 cmd_flag = 1; 00313 } 00314 if( slave.receive() == 1) // slave writes to master 00315 { 00316 FCTN_I2C_WRITE(hk_data,length ); 00317 cmd_flag = 1; 00318 } 00319 } 00320 printf("\n \r %d %d\n",t.read_us(),t1.read_us()); 00321 t.reset(); 00322 t1.reset(); 00323 00324 00325 } 00326 } 00327 00328 void FCTN_I2C_READ(char *data, int length ) 00329 { 00330 t1.start(); 00331 read_ack=slave.read(data,length); 00332 t1.stop(); 00333 if(read_ack == 0) 00334 printf("\n\rData received from CDMS is %s \n",data); 00335 else 00336 printf("\n\r data not received \n"); 00337 } 00338 00339 void FCTN_I2C_WRITE(char *data,int length) 00340 { 00341 t1.start(); 00342 write_ack=slave.write(data,length); 00343 t1.stop(); 00344 if(write_ack == 0) 00345 printf("\n\rData sent to CDMS is %s \n",data); 00346 else 00347 printf("\n\r data not sent\n"); 00348 } 00349 00350 void FCTN_ISR_I2C() 00351 { 00352 ptr_t_i2c->signal_set(0x1); 00353 t.start(); 00354 } 00355 00356 void FCTN_HK_DATA_CATENATE() 00357 { 00358 strcpy(hk_data,"hk_Data"); 00359 strcat(hk_data,SensorQuantised.Voltage); 00360 strcat(hk_data,SensorQuantised.Current); 00361 strcat(hk_data,SensorQuantised.Temperature); 00362 strcat(hk_data,SensorQuantised.AngularSpeed); 00363 strcat(hk_data,SensorQuantised.Bnewvalue); 00364 char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode}; 00365 /*strcat(hk_data,sfaultpoll); 00366 strcat(hk_data,sfaultir); 00367 strcat(hk_data,spower_mode);*/ 00368 strcat(hk_data,fdata); 00369 printf("\n\r hk data being sent %s ",hk_data); 00370 } 00371 00372 00373 //------------------------------------------------------------------------------------------------------------------------------------------------ 00374 //SCHEDULER THREAD 00375 //------------------------------------------------------------------------------------------------------------------------------------------------ 00376 int beacon_sc = 3; 00377 uint8_t schedcount=1; 00378 void T_SC(void const *args) 00379 { 00380 00381 00382 if(schedcount == 4) //to reset the counter 00383 { 00384 schedcount = 1; 00385 } 00386 printf("\n\rThe value of i in scheduler is %d\n\r",schedcount); 00387 if(schedcount%1==0) 00388 { 00389 ptr_t_acs -> signal_set(0x1); 00390 } 00391 00392 if(schedcount%2==0) 00393 { 00394 ptr_t_eps -> signal_set(0x2); 00395 00396 } 00397 if(schedcount%beacon_sc==0) 00398 { 00399 if(beac_flag==0) 00400 { 00401 ptr_t_bea -> signal_set(0x3); 00402 } 00403 } 00404 schedcount++; 00405 } 00406 00407 //--------------------------------------------------------------BAE_INIT------------------------------------------------------------------// 00408 00409 void FCTN_BAE_INIT() 00410 { 00411 BAE_STATUS |= 0x00000001; // set BAE_INIT_STATUS to 1 00412 irpt_4m_cdms.disable_irq(); // disable interrupts 00413 slave.address(0x20); // setting slave address for BAE for I2C communication 00414 RtosTimer t_sc_timer(T_SC,osTimerPeriodic); // Initiating the scheduler thread - starting RTOS Timer 00415 t_sc_timer.start(10000); 00416 printf("\n\rStarted scheduler %f\n\r",t_start.read()); 00417 FCTN_EPS_INIT(); // EPS INIT 00418 FCTN_ACS_INIT(); // ACS INIT 00419 FCTN_BEA_INIT(); // Beacon INIT 00420 //read bootup_count from flash 00421 //bootup_count++; 00422 //update bootup_count in flash 00423 irpt_4m_cdms.enable_irq(); 00424 BAE_STATUS &= 0xFFFFFFFE; // clear BAE_INIT_STATUS to 0 00425 } 00426 00427 00428 //-------------------------------------------------------------------BAE_MAIN-------------------------------------------------------------// 00429 00430 int main() 00431 { 00432 t_start.start(); 00433 printf("\n\rIITMSAT BAE Activated \n"); 00434 00435 //INITIALISING THREADS 00436 ptr_t_eps = new Thread(T_EPS); 00437 ptr_t_acs = new Thread(T_ACS); 00438 ptr_t_bea = new Thread(T_BEA); 00439 ptr_t_i2c = new Thread(T_I2C); 00440 00441 //INITIALISING INTERRUPTS 00442 //interrupt_fault(); //*to be included-function called when a fault interrupt is detected 00443 irpt_4m_cdms.fall(&FCTN_ISR_I2C); //interrupt received from CDMS 00444 00445 //Setting priority to threads 00446 ptr_t_acs->set_priority(osPriorityAboveNormal); 00447 ptr_t_eps->set_priority(osPriorityAboveNormal); 00448 ptr_t_bea->set_priority(osPriorityAboveNormal); 00449 ptr_t_i2c->set_priority(osPriorityHigh); 00450 00451 //---------------------------------------------------------------------------------------------- 00452 printf("\n\r T_ACS priority is %d",ptr_t_acs->get_priority()); 00453 printf("\n\r T_EPS priority is %d",ptr_t_eps->get_priority()); 00454 printf("\n\r T_BEA priority is %d",ptr_t_bea->get_priority()); 00455 printf("\n\r T_I2C priority is %d",ptr_t_i2c->get_priority()); 00456 //---------------------------------------------------------------------------------------------- 00457 FCTN_BAE_INIT(); 00458 while(1) //required to prevent main from terminating 00459 { 00460 ; 00461 } 00462 00463 }
Generated on Tue Jul 12 2022 23:01:05 by
![doxygen](doxygen.png)