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_FRDM_INTEGRATION 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 "pin_config.h" 00009 00010 #define DEFAULT_POW_MODE 0 //power mode initialization 00011 Serial pc(USBTX, USBRX); 00012 00013 InterruptIn interrupt (PIN95); 00014 InterruptIn master_reset(PIN99); 00015 int reset =0; 00016 int i2c_status=0; //read/write mode for i2c 0 : write2slave, 1 : write2master 00017 char data_send[40],data_receive; 00018 00019 Timer t; //To know the time of execution each thread 00020 Timer t1; 00021 //To know the time of entering of each thread 00022 00023 Thread *ptr_t_hk; //every 20 seconds 00024 Thread *ptr_t_acs; //every 10 seconds 00025 //Thread *ptr_t_acs_write2flash; 00026 Thread *ptr_t_bea; //every 30 seconds 00027 //Thread *ptr_t_bea_telecommand; 00028 //Thread *ptr_t_fault; //will be taken care by HK thread and interrupts 00029 Thread *ptr_t_i2c; //for interprocessor communication 00030 Thread *ptr_t_exec_tc; //Taking data from i2c mail box and execute the stuffread 00031 Thread *ptr_t_wdt; //Thread ofr watchdog implementation 00032 00033 00034 00035 00036 00037 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00038 //TASK 2 : HK 00039 //-------------------------------------------------------------------------------------------------------------------------------------------------- 00040 00041 //char hk_data[100]; 00042 extern SensorData Sensor; 00043 typedef struct{ 00044 char data[40]; 00045 }i2c_hk_data; 00046 DigitalOut data_ready(D10); //PIN NAME TO BE CHANGED 00047 00048 Mail<i2c_hk_data,16> i2c_hk_send; 00049 00050 00051 void t_hk(void const *args) 00052 { 00053 Sensor.power_mode = DEFAULT_POW_MODE; 00054 while(1) 00055 { 00056 Thread::signal_wait(0x2); 00057 00058 printf("\nTHIS IS HK %f\n",t1.read()); 00059 //t.start(); 00060 FAULTS(); 00061 POWER(Sensor.power_mode); 00062 FUNC_HK_MAIN(); 00063 reset =0; 00064 i2c_status = 1; 00065 data_ready = 0; 00066 char hk_data[40]={Sensor.Voltage[10],Sensor.Current[10],Sensor.Temperature[10],Sensor.PanelTemperature[2],Sensor.Vcell,Sensor.soc,Sensor.alerts,Sensor.crate,Sensor.faultpoll,Sensor.faultir,Sensor.power_mode}; 00067 i2c_hk_data * i2c_hk_data_s = i2c_hk_send.alloc(); 00068 strcpy(i2c_hk_data_s->data,hk_data); 00069 i2c_hk_send.put(i2c_hk_data_s); 00070 data_ready = 1; 00071 00072 //Collecting HK data 00073 //thread_2.signal_set(0x4); 00074 // FUNC_I2C_SLAVE_MAIN(24); //Put HK data to I2C thread instead 00075 00076 // t.stop(); 00077 //printf("The time to execute hk_acq is %f seconds\n",t.read()); 00078 //t.reset(); 00079 } 00080 } 00081 00082 //--------------------------------------------------------------------------------------------------------------------------------------- 00083 //TASK 1 : ACS 00084 //--------------------------------------------------------------------------------------------------------------------------------------- 00085 /*typedef struct { 00086 float mag_field; 00087 float omega; 00088 } sensor_data; 00089 */ 00090 //Mail <sensor_data, 16> q_acs; //Will be taken care of by HK structure every 20 seconds 00091 00092 /*void func_acs_readdata(sensor_data *ptr) 00093 { 00094 printf("Reading the data\n"); 00095 ptr -> mag_field = 10; 00096 ptr -> omega = 3; 00097 } 00098 00099 void func_acs_ctrlalgo() 00100 { 00101 printf("Executing control algo\n"); 00102 } 00103 00104 00105 00106 void func_acs_write2flash(sensor_data *ptr2) 00107 { 00108 printf("The magnetic field is %.2f T\n\r",ptr2->mag_field); 00109 printf("The angular velocity is %.2f rad/s\n\r",ptr2->omega); 00110 }*/ 00111 00112 int acs_pflag = 1; //for executing acs power modes 00113 void t_acs(void const *args) 00114 { 00115 float *mag_field; 00116 float *omega; 00117 float *moment; 00118 while(1) 00119 { 00120 Thread::signal_wait(0x1); 00121 printf("\nTHIS IS ACS %f\n",t1.read()); 00122 // t.start(); 00123 // sensor_data *ptr = q_acs.alloc(); //done by HK instead 00124 00125 //func_acs_readda=ta(ptr); 00126 mag_field= FUNC_ACS_MAG_EXEC(); //actual execution 00127 omega = FUNC_ACS_EXEC_GYR(); 00128 00129 00130 //q_acs.put(ptr); //done by HK 00131 if(acs_pflag == 1) //for the respective power mode 00132 { 00133 moment = FUNC_ACS_CNTRLALGO(mag_field,omega); 00134 FUNC_ACS_GENPWM(moment); //Generating PWM signal. 00135 } 00136 00137 //t.reset(); 00138 } 00139 } 00140 00141 /*void t_acs_write2flash(void const *args) //unwanted thread 00142 { 00143 while(1) 00144 { 00145 //printf("Writing in the flash\n"); 00146 osEvent evt = q_acs.get(); 00147 if(evt.status == osEventMail) 00148 { 00149 sensor_data *ptr = (sensor_data*)evt.value.p; 00150 func_acs_write2flash(ptr); 00151 q_acs.free(ptr); 00152 } 00153 printf("Writing acs data in the flash\n"); 00154 } 00155 }*/ 00156 00157 00158 //---------------------------------------------------BEACON-------------------------------------------------------------------------------------------- 00159 00160 int beac_flag=0; //To receive telecommand from ground. 00161 00162 00163 /*void t_bea_telecommand(void const *args) //taken care of by t_exec_tc 00164 { 00165 char c = pc.getc(); 00166 if(c=='a') 00167 { 00168 printf("Telecommand detected\n"); 00169 beac_flag=1; 00170 } 00171 } 00172 */ 00173 void t_bea(void const *args) 00174 { 00175 00176 while(1) 00177 { 00178 Thread::signal_wait(0x3); 00179 printf("\nTHIS IS BEACON %f\n",t1.read()); 00180 //t.start(); 00181 00182 00183 00184 FUNC_BEA(); 00185 00186 00187 /*if(beac_flag==1) //beacon standby can be doe to 00188 { 00189 Thread::wait(600000); 00190 beac_flag = 0; 00191 }*/ 00192 00193 //printf("The time to execute beacon thread is %f seconds\n",t.read()); 00194 //t.reset(); 00195 } 00196 } 00197 00198 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00199 //TASK 4 : FAULT MANAGEMENT 00200 //--------------------------------------------------------------------------------------------------------------------------------------------------- 00201 //Dummy fault rectifier functions 00202 00203 /*Mail<int,16> faults; 00204 00205 void FUNC_FAULT_FUNCT1() 00206 { 00207 printf("\nFault 1 detected... \n"); 00208 } 00209 00210 void FUNC_FAULT_FUNCT2() 00211 { 00212 printf("\nFault 2 detected...\n"); 00213 } 00214 00215 void T_FAULT(void const *args) 00216 { 00217 while(1) 00218 { 00219 osEvent evt = faults.get(); 00220 if(evt.status==osEventMail) 00221 { 00222 int *fault_id= (int *)evt.value.p; 00223 switch(*fault_id) 00224 { 00225 case 1: FUNC_FAULT_FUNCT1(); 00226 break; 00227 case 2: FUNC_FAULT_FUNCT2(); 00228 break; 00229 } 00230 faults.free(fault_id); 00231 } 00232 } 00233 }*/ 00234 00235 00236 00237 /* 00238 void T_FAULT(void const *args) 00239 { 00240 Sensor.power_mode='0'; 00241 while(1) 00242 { 00243 Thread :: signal_wait(0x2); 00244 FAULTS(); 00245 POWER(Sensor.power_mode); 00246 //Sensor.power_mode++; //testing ... should be removed 00247 } 00248 } 00249 /*------------------------------------------------------------------------------------------------------------------------------------------- 00250 -------------------------------------------------------WATCHDOG----------------------------------------------------------------------------*/ 00251 DigitalOut trigger(PIN63); // has to be changed 00252 void T_WDT(void const * args) 00253 { 00254 trigger = 1; 00255 while(true) 00256 { 00257 Thread::signal_wait(0x5); //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized 00258 printf("\nEntered WD\n"); 00259 trigger = !trigger; 00260 } 00261 } 00262 //------------------------------------------------------------------------------------------------------------------------------------------------ 00263 //SLAVE 00264 //------------------------------------------------------------------------------------------------------------------------------------------------ 00265 typedef struct 00266 { 00267 char data; // To avoid dynamic memory allocation 00268 }i2c_data; 00269 Mail<i2c_data,16> i2c_data_receive; //to be uncommented when receiving data 00270 00271 00272 void FUNC_INT() 00273 { 00274 reset = 0; 00275 ptr_t_i2c->signal_set(0x4); 00276 } 00277 00278 void FUNC_RESET() 00279 { 00280 reset = 1; 00281 } 00282 00283 00284 void T_I2C_BAE(void const * args) 00285 { 00286 //char data_send,data_receive; 00287 while(1) 00288 { 00289 Thread::signal_wait(0x4); 00290 printf("\n entered thread\n\r"); 00291 if(i2c_status == 0 && reset !=1) 00292 { 00293 00294 FUNC_I2C_WRITE2CDMS(&data_receive,1); 00295 i2c_data * i2c_data_r = i2c_data_receive.alloc(); 00296 i2c_data_r->data = data_receive; 00297 00298 i2c_data_receive.put(i2c_data_r); 00299 printf("\n Data received from CDMS is %c\n\r",data_receive); 00300 i2c_data_receive.free(i2c_data_r); // This has to be done from a differen thread 00301 00302 } 00303 else if(i2c_status ==1 && reset !=1) 00304 { 00305 osEvent evt = i2c_hk_send.get(); 00306 if (evt.status == osEventMail) 00307 { 00308 i2c_hk_data *i2c_hk_data_s = (i2c_hk_data*)evt.value.p; 00309 strcpy(data_send,i2c_hk_data_s -> data); 00310 FUNC_I2C_WRITE2CDMS(data_send,40); 00311 printf("\nData sent to CDMS is %s\n\r",data_send); 00312 i2c_hk_send.free(i2c_hk_data_s); //This has to be done in a different thread 00313 i2c_status = 0; 00314 //temp = i2c_status; 00315 } 00316 } 00317 00318 } 00319 } 00320 00321 00322 //------------------------------------------------------------------------------------------------------------------------------------------------ 00323 //SCHEDULER 00324 //------------------------------------------------------------------------------------------------------------------------------------------------ 00325 int beacon_sc = 3; 00326 uint16_t schedcount=1; 00327 void t_sc(void const *args) 00328 { 00329 00330 printf("The value of i in scheduler is %d\n",schedcount); 00331 if(schedcount == 65532) //to reset the counter 00332 { 00333 schedcount = 0; 00334 } 00335 00336 if(schedcount%1==0) 00337 { 00338 ptr_t_acs -> signal_set(0x1); 00339 } 00340 if(schedcount%2==0) 00341 { 00342 // ptr_t_fault -> signal_set(0x2); 00343 ptr_t_hk -> signal_set(0x2); 00344 00345 } 00346 if(schedcount%beacon_sc==0) 00347 { 00348 if(beac_flag==0) 00349 { 00350 00351 ptr_t_bea -> signal_set(0x3); 00352 } 00353 } 00354 schedcount++; 00355 } 00356 00357 //--------------------------------------------------------------------------------------------------------------------------------------------- 00358 00359 int main() 00360 { 00361 t1.start(); 00362 00363 ptr_t_hk = new Thread(t_hk); 00364 ptr_t_acs = new Thread(t_acs); 00365 00366 ptr_t_bea = new Thread(t_bea); 00367 ptr_t_wdt = new Thread(T_WDT); 00368 ptr_t_i2c = new Thread(T_I2C_BAE); 00369 00370 // ptr_t_fault -> set_priority(osPriorityRealtime); 00371 ptr_t_acs->set_priority(osPriorityHigh); 00372 ptr_t_hk->set_priority(osPriorityNormal); 00373 //ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal); 00374 ptr_t_bea->set_priority(osPriorityAboveNormal); 00375 ptr_t_i2c->set_priority(osPriorityAboveNormal); 00376 //ptr_t_bea_telecommand->set_priority(osPriorityIdle); 00377 //ptr_t_sc->set_priority(osPriorityAboveNormal); 00378 ptr_t_wdt -> set_priority(osPriorityIdle); 00379 00380 00381 // ---------------------------------------------------------------------------------------------- 00382 //printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority()); 00383 printf("\n T_ACS priority is %d",ptr_t_acs->get_priority()); 00384 printf("\n T_HK_ACQ priority is %d",ptr_t_hk->get_priority()); 00385 //printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority()); 00386 printf("\n T_BEA priority is %d",ptr_t_bea->get_priority()); 00387 RtosTimer t_sc_timer(t_sc,osTimerPeriodic); 00388 t_sc_timer.start(10000); 00389 printf("\n%f\n",t1.read()); 00390 00391 interrupt.rise(&FUNC_INT); 00392 master_reset.fall(&FUNC_RESET); 00393 00394 while(1) 00395 { 00396 Thread::wait(10000); 00397 ; 00398 } 00399 00400 }
Generated on Tue Jul 12 2022 19:16:04 by
