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