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