beacon on/off implemented and working

Dependencies:   mbed-rtos mbed

Fork of i2c_configuring by sakthi priya amirtharaj

Committer:
sakthipriya
Date:
Sun May 17 19:40:48 2015 +0000
Revision:
6:4fd79755f4c9
Parent:
5:bf1f3504cd9d
beacon on/off implemented and working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sakthipriya 0:e91ee0e99213 1 #include "mbed.h"
sakthipriya 0:e91ee0e99213 2 #include "rtos.h"
sakthipriya 0:e91ee0e99213 3 #include "HK.h"
sakthipriya 0:e91ee0e99213 4 #include "slave.h"
sakthipriya 0:e91ee0e99213 5 #include "beacon.h"
sakthipriya 0:e91ee0e99213 6 #include "ACS.h"
sakthipriya 0:e91ee0e99213 7 #include "fault.h"
sakthipriya 0:e91ee0e99213 8 #include "slave.h"
sakthipriya 0:e91ee0e99213 9 #include "mnm.h"
sakthipriya 0:e91ee0e99213 10
sakthipriya 0:e91ee0e99213 11 Serial pc(USBTX, USBRX);
sakthipriya 0:e91ee0e99213 12
sakthipriya 5:bf1f3504cd9d 13 //InterruptIn interrupt( PIN97); //I2c interrupt from CDMS
sakthipriya 5:bf1f3504cd9d 14 //DigitalOut data_ready(PIN90); //Sends interrupt to CDMS
sakthipriya 5:bf1f3504cd9d 15 InterruptIn interrupt(D9); //I2c interrupt from CDMS
sakthipriya 5:bf1f3504cd9d 16 DigitalOut data_ready(D10);
sakthipriya 0:e91ee0e99213 17
sakthipriya 0:e91ee0e99213 18 Timer t; //To know the time of execution each thread
sakthipriya 0:e91ee0e99213 19 Timer t1; //To know the time of entering of each thread
sakthipriya 0:e91ee0e99213 20 Timer t2; //To check the time sync in i2c communication
sakthipriya 0:e91ee0e99213 21 Timer t3; //To know the time taken by i2c read/write function
sakthipriya 3:0931a8800543 22 Timer i1;
sakthipriya 3:0931a8800543 23 Timer i2;
sakthipriya 5:bf1f3504cd9d 24 Timeout bea_timeout ; // timeout used to revive beacon 10 min after it is switched off
sakthipriya 0:e91ee0e99213 25 /*****************************************************************Threads USed***********************************************************************************/
gkumar 4:65a2d0b97d01 26 Thread *ptr_t_hk_acq; //pointer:::::::::to read state of one thread from another
sakthipriya 0:e91ee0e99213 27 Thread *ptr_t_acs;
sakthipriya 0:e91ee0e99213 28 Thread *ptr_t_bea;
sakthipriya 0:e91ee0e99213 29 Thread *ptr_t_i2c;
sakthipriya 5:bf1f3504cd9d 30 Thread *ptr_t_wdt;
sakthipriya 5:bf1f3504cd9d 31
sakthipriya 5:bf1f3504cd9d 32 /****************************************************************FUNC_HEADER**********************************************************************************/
sakthipriya 5:bf1f3504cd9d 33 void FUNC_I2C_INT();
sakthipriya 5:bf1f3504cd9d 34 void FUNC_I2C_IR2CDMS();
sakthipriya 5:bf1f3504cd9d 35 void FUNC_I2C_TC_EXECUTE (char command);
sakthipriya 5:bf1f3504cd9d 36 void FUNC_BEA_TIMEOUT();
sakthipriya 0:e91ee0e99213 37
sakthipriya 0:e91ee0e99213 38 /****************************************************************configuring I2c*********************************************************************************/
sakthipriya 3:0931a8800543 39 I2CSlave slave(PIN1,PIN2); //configuring pins p27, p28 as I2Cslave
sakthipriya 3:0931a8800543 40 //I2CSlave slave (D14,D15);
sakthipriya 0:e91ee0e99213 41 int i2c_status=0; //read or write mode for i2c 0: write2slave 1: write2master
sakthipriya 0:e91ee0e99213 42 typedef struct //structure of i2c data
sakthipriya 0:e91ee0e99213 43 {
sakthipriya 0:e91ee0e99213 44 char data[25];
sakthipriya 0:e91ee0e99213 45 int length;
sakthipriya 0:e91ee0e99213 46 }i2c_data;
sakthipriya 0:e91ee0e99213 47
sakthipriya 0:e91ee0e99213 48
sakthipriya 0:e91ee0e99213 49 //Mail<i2c_data,16> i2c_data_receive;
sakthipriya 0:e91ee0e99213 50 Mail<i2c_data,16> i2c_data_send;
sakthipriya 0:e91ee0e99213 51
sakthipriya 0:e91ee0e99213 52 //--------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 53 //TASK 2 : HK
sakthipriya 0:e91ee0e99213 54 //--------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 55
sakthipriya 0:e91ee0e99213 56 char hk_data[25];
gkumar 4:65a2d0b97d01 57 extern SensorDataQuantised SensorQuantised; /// SensorDataQuantised: struct instance :SensorQuantised
sakthipriya 0:e91ee0e99213 58 void T_HK_ACQ(void const *args)
sakthipriya 0:e91ee0e99213 59 {
sakthipriya 0:e91ee0e99213 60
sakthipriya 0:e91ee0e99213 61 while(1)
sakthipriya 0:e91ee0e99213 62 {
gkumar 4:65a2d0b97d01 63 Thread::signal_wait(0x2); // 0x2 random should be same in master n slave
sakthipriya 0:e91ee0e99213 64 SensorQuantised.power_mode='3'; //default power mode(dummy)
sakthipriya 0:e91ee0e99213 65 printf("\n\rTHIS IS HK %f\n\r",t1.read());
sakthipriya 0:e91ee0e99213 66 t.start();
gkumar 4:65a2d0b97d01 67 FUNC_HK_FAULTS(); // !Actual fault management is not implemented : polling
gkumar 4:65a2d0b97d01 68 FUNC_HK_POWER(SensorQuantised.power_mode); // !The power mode algorithm is yet to be obtained // status of power modde will decide actions
gkumar 4:65a2d0b97d01 69 FUNC_HK_MAIN(); //Collecting HK data for ADC n
sakthipriya 6:4fd79755f4c9 70 //FUNC_I2C_IR2CDMS(); //sending HK data to CDMS
sakthipriya 0:e91ee0e99213 71 t.stop();
sakthipriya 0:e91ee0e99213 72 printf("The time to execute hk_acq is %f seconds\n\r",t.read());
sakthipriya 0:e91ee0e99213 73 t.reset();
sakthipriya 0:e91ee0e99213 74 }
sakthipriya 0:e91ee0e99213 75 }
sakthipriya 0:e91ee0e99213 76
sakthipriya 0:e91ee0e99213 77 //---------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 78 //TASK 1 : ACS
sakthipriya 0:e91ee0e99213 79 //---------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 80
gkumar 4:65a2d0b97d01 81 int acs_pflag = 1; // power mode decision flag for ACS
sakthipriya 0:e91ee0e99213 82 void T_ACS(void const *args)
sakthipriya 0:e91ee0e99213 83 {
gkumar 4:65a2d0b97d01 84 float mag_field[3]; // rm3000
gkumar 4:65a2d0b97d01 85 float omega[3]; //mpu3300
gkumar 4:65a2d0b97d01 86 float *mnm_data; //
sakthipriya 0:e91ee0e99213 87 float mag_field1[3];
sakthipriya 0:e91ee0e99213 88 float omega1[3];
sakthipriya 0:e91ee0e99213 89 float tauc1[3];
sakthipriya 0:e91ee0e99213 90 float moment[3];
sakthipriya 0:e91ee0e99213 91 while(1)
sakthipriya 0:e91ee0e99213 92 {
sakthipriya 0:e91ee0e99213 93 Thread::signal_wait(0x1);
sakthipriya 0:e91ee0e99213 94 printf("\n\rEntered ACS %f\n",t1.read());
sakthipriya 0:e91ee0e99213 95 t.start();
gkumar 4:65a2d0b97d01 96 FUNC_ACS_MAG_EXEC(mag_field); ///rm3000
sakthipriya 3:0931a8800543 97 //printf("\n\r check");
sakthipriya 0:e91ee0e99213 98 for(int i=0; i<3; i++)
sakthipriya 0:e91ee0e99213 99 {
sakthipriya 0:e91ee0e99213 100 printf("%f\t",mag_field[i]);
sakthipriya 0:e91ee0e99213 101 }
gkumar 4:65a2d0b97d01 102 FUNC_ACS_EXEC_GYR(omega); // mpu3300
sakthipriya 0:e91ee0e99213 103 acs_pflag =1; //to be removed later
sakthipriya 0:e91ee0e99213 104 omega[0] = 1.0;
sakthipriya 0:e91ee0e99213 105 omega[1] = 1.0;
sakthipriya 0:e91ee0e99213 106 omega[2] = 1.0;
sakthipriya 0:e91ee0e99213 107
sakthipriya 0:e91ee0e99213 108
sakthipriya 0:e91ee0e99213 109
gkumar 4:65a2d0b97d01 110 mnm_data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
sakthipriya 0:e91ee0e99213 111 printf("\n\rmnm gyro values\n"); //printing the angular velocity and magnetic field values
sakthipriya 0:e91ee0e99213 112 for(int i=0; i<3; i++)
sakthipriya 0:e91ee0e99213 113 {
sakthipriya 0:e91ee0e99213 114 printf("%f\t",mnm_data[i]);
sakthipriya 0:e91ee0e99213 115 }
sakthipriya 0:e91ee0e99213 116 printf("\n\r mnm mag values\n");
sakthipriya 0:e91ee0e99213 117 for(int i=3; i<6; i++)
sakthipriya 0:e91ee0e99213 118 {
sakthipriya 0:e91ee0e99213 119 printf("%f\t",mnm_data[i]);
sakthipriya 0:e91ee0e99213 120 }
sakthipriya 0:e91ee0e99213 121 for(int i = 0 ; i<3;i++)
sakthipriya 0:e91ee0e99213 122 {
sakthipriya 0:e91ee0e99213 123 omega1[i] = mnm_data[i];
sakthipriya 0:e91ee0e99213 124 }
sakthipriya 0:e91ee0e99213 125 for( int i = 3;i<6;i++)
sakthipriya 0:e91ee0e99213 126 {
sakthipriya 0:e91ee0e99213 127 mag_field1[i-3] = mnm_data[i];
sakthipriya 0:e91ee0e99213 128 }
gkumar 4:65a2d0b97d01 129
gkumar 4:65a2d0b97d01 130 if(acs_pflag == 1)
sakthipriya 0:e91ee0e99213 131 {
sakthipriya 0:e91ee0e99213 132
sakthipriya 0:e91ee0e99213 133 FUNC_ACS_CNTRLALGO(mag_field,omega,tauc1);
sakthipriya 0:e91ee0e99213 134 printf("\n\r control algo values ");
sakthipriya 0:e91ee0e99213 135 for(int i=0; i<3; i++)
sakthipriya 0:e91ee0e99213 136 {
sakthipriya 0:e91ee0e99213 137 printf("%f\t",tauc1[i]);
sakthipriya 0:e91ee0e99213 138 }
sakthipriya 0:e91ee0e99213 139 moment_calc (tauc1, mag_field,moment);
sakthipriya 0:e91ee0e99213 140 printf("\n\r moment values ");
sakthipriya 0:e91ee0e99213 141 for(int i=0; i<3; i++)
sakthipriya 0:e91ee0e99213 142 {
sakthipriya 0:e91ee0e99213 143 printf("%f\t",moment[i]);
sakthipriya 0:e91ee0e99213 144 }
sakthipriya 0:e91ee0e99213 145 FUNC_ACS_GENPWM(moment);
sakthipriya 0:e91ee0e99213 146 }
sakthipriya 0:e91ee0e99213 147 t.reset();
sakthipriya 0:e91ee0e99213 148 }
sakthipriya 0:e91ee0e99213 149 }
sakthipriya 0:e91ee0e99213 150
sakthipriya 0:e91ee0e99213 151 //---------------------------------------------------BEACON--------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 152
gkumar 4:65a2d0b97d01 153 int beac_flag=0; //To receive telecommand from ground. Beacon stopped when telecomand recieved durring
gkumar 4:65a2d0b97d01 154
sakthipriya 0:e91ee0e99213 155
sakthipriya 0:e91ee0e99213 156
sakthipriya 0:e91ee0e99213 157 /*void T_BEA_TELECOMMAND(void const *args)
sakthipriya 0:e91ee0e99213 158 {
sakthipriya 0:e91ee0e99213 159 char c = pc.getc();
sakthipriya 0:e91ee0e99213 160 if(c=='a')
sakthipriya 0:e91ee0e99213 161 {
sakthipriya 0:e91ee0e99213 162 printf("Telecommand detected\n\r");
sakthipriya 0:e91ee0e99213 163 beac_flag=1;
sakthipriya 0:e91ee0e99213 164 }
sakthipriya 0:e91ee0e99213 165 }
sakthipriya 0:e91ee0e99213 166 */
sakthipriya 0:e91ee0e99213 167
sakthipriya 0:e91ee0e99213 168 void T_BEA(void const *args)
sakthipriya 0:e91ee0e99213 169 {
sakthipriya 0:e91ee0e99213 170
sakthipriya 0:e91ee0e99213 171 while(1)
sakthipriya 0:e91ee0e99213 172 {
sakthipriya 0:e91ee0e99213 173 Thread::signal_wait(0x3);
sakthipriya 0:e91ee0e99213 174 printf("\n\rTHIS IS BEACON %f\n\r",t1.read());
sakthipriya 0:e91ee0e99213 175 t.start();
sakthipriya 0:e91ee0e99213 176 FUNC_BEA();
gkumar 4:65a2d0b97d01 177 if(beac_flag==1) // use scheduler instead
sakthipriya 0:e91ee0e99213 178 {
sakthipriya 0:e91ee0e99213 179 Thread::wait(600000);
sakthipriya 0:e91ee0e99213 180 beac_flag = 0;
sakthipriya 0:e91ee0e99213 181 }
sakthipriya 0:e91ee0e99213 182 printf("The time to execute beacon thread is %f seconds\n\r",t.read());
sakthipriya 0:e91ee0e99213 183 t.reset();
sakthipriya 0:e91ee0e99213 184 }
sakthipriya 0:e91ee0e99213 185 }
sakthipriya 0:e91ee0e99213 186
sakthipriya 0:e91ee0e99213 187
sakthipriya 0:e91ee0e99213 188
gkumar 4:65a2d0b97d01 189 extern SensorDataQuantised SensorQuantised; // ????????
sakthipriya 0:e91ee0e99213 190
sakthipriya 0:e91ee0e99213 191 /*-------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 192 -------------------------------------------------------WATCHDOG----------------------------------------------------------------------------*/
sakthipriya 0:e91ee0e99213 193 DigitalOut trigger(PIN63); // has to be changed
sakthipriya 0:e91ee0e99213 194 void T_WDT(void const * args)
sakthipriya 0:e91ee0e99213 195 {
gkumar 4:65a2d0b97d01 196 trigger = 1; // gpio to WD input
sakthipriya 0:e91ee0e99213 197 while(true)
sakthipriya 0:e91ee0e99213 198 {
sakthipriya 0:e91ee0e99213 199 Thread::signal_wait(0x5); //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized
sakthipriya 0:e91ee0e99213 200 printf("\n\rEntered WD\n\r");
gkumar 4:65a2d0b97d01 201 trigger = !trigger; /// falling edge triggered
sakthipriya 0:e91ee0e99213 202 }
sakthipriya 0:e91ee0e99213 203 }
sakthipriya 0:e91ee0e99213 204
sakthipriya 0:e91ee0e99213 205 //---------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 206 //TASK 5 : i2c data
sakthipriya 0:e91ee0e99213 207 //---------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 208
gkumar 4:65a2d0b97d01 209 void FUNC_I2C_WRITE2CDMS(char *data, int length=1) /// BAE to CDMS read/write
sakthipriya 0:e91ee0e99213 210 {
sakthipriya 0:e91ee0e99213 211 int slave_status = 1;
sakthipriya 5:bf1f3504cd9d 212 //int inter_test = interrupt;
sakthipriya 5:bf1f3504cd9d 213 //int slr = slave.receive();
sakthipriya 5:bf1f3504cd9d 214 //wait_ms(20);
sakthipriya 3:0931a8800543 215 //printf("\n\r time is %d\n",t2.read_us());
sakthipriya 3:0931a8800543 216 //if(interrupt ==1)
sakthipriya 3:0931a8800543 217 //{
sakthipriya 5:bf1f3504cd9d 218 wait_us(100);
sakthipriya 5:bf1f3504cd9d 219 int slave_r = slave.receive();
sakthipriya 5:bf1f3504cd9d 220 printf("\n%d\r",slave_r);
sakthipriya 5:bf1f3504cd9d 221 // printf("\n%d\r",slave.receive());
sakthipriya 5:bf1f3504cd9d 222 t2.stop();
sakthipriya 5:bf1f3504cd9d 223 // wait_ms(2);
gkumar 4:65a2d0b97d01 224 if(slave.receive() == 0) // 0 when not ack refer doc.
sakthipriya 5:bf1f3504cd9d 225 //t2.stop();
gkumar 4:65a2d0b97d01 226 if( slave.receive()==1) // slave to master
sakthipriya 0:e91ee0e99213 227 {
sakthipriya 5:bf1f3504cd9d 228 // t2.stop();
sakthipriya 0:e91ee0e99213 229 t3.start();
gkumar 4:65a2d0b97d01 230 slave_status=slave.write(data,length); // ack to
sakthipriya 5:bf1f3504cd9d 231
sakthipriya 0:e91ee0e99213 232 t3.stop();
sakthipriya 5:bf1f3504cd9d 233 slave.stop();
sakthipriya 5:bf1f3504cd9d 234 //printf("\n\r slave status %d",slave.receive());
sakthipriya 0:e91ee0e99213 235 }
sakthipriya 5:bf1f3504cd9d 236 if( slave.receive()==3 || slave.receive()==2) // master to slave
sakthipriya 0:e91ee0e99213 237 {
sakthipriya 5:bf1f3504cd9d 238 //t2.stop();
sakthipriya 0:e91ee0e99213 239 t3.start();
sakthipriya 0:e91ee0e99213 240 slave_status=slave.read(data,length);
sakthipriya 0:e91ee0e99213 241 t3.stop();
sakthipriya 5:bf1f3504cd9d 242 slave.stop();
sakthipriya 5:bf1f3504cd9d 243 //printf("\n\r slave status %d",slave.receive());
sakthipriya 0:e91ee0e99213 244 }
sakthipriya 3:0931a8800543 245 //}
sakthipriya 3:0931a8800543 246 // printf("\n\r time taken to receive intrpt 4m cdms %d",i1.read_us());
sakthipriya 3:0931a8800543 247 //i1.reset();
sakthipriya 5:bf1f3504cd9d 248 printf("\n\r slave status %d %d ",t2.read_us(),t3.read_us());
sakthipriya 3:0931a8800543 249 // printf("\n\r time taken from interrupt to reach i2c fn %d",t2.read_us());
sakthipriya 5:bf1f3504cd9d 250 //t2.stop();
sakthipriya 0:e91ee0e99213 251 t2.reset();
sakthipriya 3:0931a8800543 252 //printf("\n\r time to execute i2c function %d",t3.read_us());
sakthipriya 0:e91ee0e99213 253 t3.reset();
sakthipriya 0:e91ee0e99213 254 }
sakthipriya 0:e91ee0e99213 255
sakthipriya 0:e91ee0e99213 256 char data_send[25],data_receive;
sakthipriya 0:e91ee0e99213 257 void T_I2C_BAE(void const * args)
sakthipriya 0:e91ee0e99213 258 {
sakthipriya 0:e91ee0e99213 259 while(1)
sakthipriya 0:e91ee0e99213 260 {
sakthipriya 0:e91ee0e99213 261 Thread::signal_wait(0x4);
sakthipriya 3:0931a8800543 262 int something = interrupt;
sakthipriya 3:0931a8800543 263 // printf("\n\r interrupt %d",interrupt);
gkumar 4:65a2d0b97d01 264 if(i2c_status == 0 ) // read from CDMS (master)
sakthipriya 3:0931a8800543 265
sakthipriya 0:e91ee0e99213 266 {
sakthipriya 3:0931a8800543 267 // wait_ms(23);
sakthipriya 0:e91ee0e99213 268 FUNC_I2C_WRITE2CDMS(&data_receive,1);
sakthipriya 0:e91ee0e99213 269 /*i2c_data * i2c_data_r = i2c_data_receive.alloc();
sakthipriya 0:e91ee0e99213 270 i2c_data_r->data = data_receive;
sakthipriya 0:e91ee0e99213 271 i2c_data_r->length = 1;
sakthipriya 0:e91ee0e99213 272 i2c_data_receive.put(i2c_data_r);*/
sakthipriya 0:e91ee0e99213 273 printf("\n\r Data received from CDMS is %c \n\r",data_receive);
sakthipriya 5:bf1f3504cd9d 274 // FUNC_I2C_TC_EXECUTE(data_receive); // telecommand // This has to be done from a differen thread
sakthipriya 0:e91ee0e99213 275
sakthipriya 0:e91ee0e99213 276 }
sakthipriya 0:e91ee0e99213 277 else if(i2c_status ==1)
sakthipriya 0:e91ee0e99213 278 {
gkumar 4:65a2d0b97d01 279 osEvent evt = i2c_data_send.get(); //
gkumar 4:65a2d0b97d01 280 if (evt.status == osEventMail) //
sakthipriya 0:e91ee0e99213 281 {
gkumar 4:65a2d0b97d01 282 i2c_data *i2c_data_s = (i2c_data*)evt.value.p; //syntax recieving address of data from queue and storing it in struct
gkumar 4:65a2d0b97d01 283 strcpy(data_send,i2c_data_s -> data); // -> data from pointer
sakthipriya 3:0931a8800543 284 // wait_ms(13);
sakthipriya 3:0931a8800543 285
gkumar 4:65a2d0b97d01 286 FUNC_I2C_WRITE2CDMS(data_send,25);
sakthipriya 0:e91ee0e99213 287 printf("\n\rData sent to CDMS is %s\n\r",data_send);
sakthipriya 0:e91ee0e99213 288
gkumar 4:65a2d0b97d01 289 i2c_data_send.free(i2c_data_s); // freeing the memory location from queue
sakthipriya 0:e91ee0e99213 290 i2c_status = 0;
sakthipriya 0:e91ee0e99213 291 }
sakthipriya 0:e91ee0e99213 292 }
sakthipriya 0:e91ee0e99213 293
sakthipriya 0:e91ee0e99213 294 }
sakthipriya 0:e91ee0e99213 295 }
sakthipriya 0:e91ee0e99213 296
sakthipriya 0:e91ee0e99213 297
sakthipriya 0:e91ee0e99213 298
gkumar 4:65a2d0b97d01 299 void FUNC_I2C_INT() // ISR from CDMS to BAE
sakthipriya 0:e91ee0e99213 300 {
sakthipriya 3:0931a8800543 301 //i1.stop();
sakthipriya 0:e91ee0e99213 302
sakthipriya 0:e91ee0e99213 303 // t3.start();
gkumar 4:65a2d0b97d01 304 ptr_t_i2c->signal_set(0x4); // printf dosen't work....why??????????????????
sakthipriya 3:0931a8800543 305 //printf("\n ceckh\n");
sakthipriya 3:0931a8800543 306 t2.start();
sakthipriya 3:0931a8800543 307 // printf("\n\r time taken from interrupt to reach i2c fn %d",t2.read_us());
sakthipriya 0:e91ee0e99213 308 }
sakthipriya 0:e91ee0e99213 309
sakthipriya 0:e91ee0e99213 310 void FUNC_I2C_IR2CDMS()
gkumar 4:65a2d0b97d01 311 { // structure cannot sent via i2c so struct quantized to string n send to cdms n store in SD
sakthipriya 0:e91ee0e99213 312 data_ready=0;
sakthipriya 0:e91ee0e99213 313 //char data[25];
sakthipriya 0:e91ee0e99213 314 strcpy(hk_data,"hk_Data");
sakthipriya 0:e91ee0e99213 315 strcat(hk_data,SensorQuantised.Voltage);
sakthipriya 0:e91ee0e99213 316 strcat(hk_data,SensorQuantised.Current);
sakthipriya 0:e91ee0e99213 317 strcat(hk_data,SensorQuantised.Temperature);
sakthipriya 0:e91ee0e99213 318 strcat(hk_data,SensorQuantised.PanelTemperature);
sakthipriya 0:e91ee0e99213 319 strcat(hk_data,SensorQuantised.AngularSpeed);
sakthipriya 0:e91ee0e99213 320 strcat(hk_data,SensorQuantised.Bnewvalue);
sakthipriya 0:e91ee0e99213 321 char fdata[5] = {SensorQuantised.BatteryTemperature,SensorQuantised.faultpoll,SensorQuantised.faultir,SensorQuantised.power_mode};
sakthipriya 0:e91ee0e99213 322
sakthipriya 0:e91ee0e99213 323 /*strcat(hk_data,sfaultpoll);
sakthipriya 0:e91ee0e99213 324 strcat(hk_data,sfaultir);
sakthipriya 0:e91ee0e99213 325 strcat(hk_data,spower_mode);*/
sakthipriya 0:e91ee0e99213 326 strcat(hk_data,fdata);
sakthipriya 0:e91ee0e99213 327 printf("\n\r hk data being sent %s ",hk_data);
sakthipriya 0:e91ee0e99213 328 //for(int i=0;i<100000000000;i++)
sakthipriya 0:e91ee0e99213 329 //;
sakthipriya 0:e91ee0e99213 330
sakthipriya 0:e91ee0e99213 331 /*for(int d=0;d<23;d++) //was written just to check hk data
sakthipriya 0:e91ee0e99213 332 {
sakthipriya 0:e91ee0e99213 333 if(hk_data[d]>10)
sakthipriya 0:e91ee0e99213 334 printf("\n\rhk data : %d\n\r",hk_data[d]);
sakthipriya 0:e91ee0e99213 335 } */
sakthipriya 0:e91ee0e99213 336
sakthipriya 0:e91ee0e99213 337 //data = pcslave.getc();
sakthipriya 0:e91ee0e99213 338
sakthipriya 0:e91ee0e99213 339 i2c_status=1;
gkumar 4:65a2d0b97d01 340 i2c_data * i2c_data_s = i2c_data_send.alloc(); // writing to queue _____ dynamic memory allocation
sakthipriya 0:e91ee0e99213 341 strcpy(i2c_data_s->data,hk_data);
sakthipriya 0:e91ee0e99213 342 i2c_data_s->length = 25;
sakthipriya 0:e91ee0e99213 343 i2c_data_send.put(i2c_data_s);
sakthipriya 0:e91ee0e99213 344 data_ready=1;
sakthipriya 3:0931a8800543 345 // i1.start();
sakthipriya 0:e91ee0e99213 346 //temp = i2c_status;
sakthipriya 0:e91ee0e99213 347 }
sakthipriya 0:e91ee0e99213 348
sakthipriya 0:e91ee0e99213 349
sakthipriya 0:e91ee0e99213 350 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 351 //TELECOMMAND
sakthipriya 0:e91ee0e99213 352 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 353 void FUNC_I2C_TC_EXECUTE (char command)
sakthipriya 0:e91ee0e99213 354 { switch(command)
sakthipriya 6:4fd79755f4c9 355 { case '0' : printf("\n\rcommand 0 executed"); // assuming telecommand to switch of beacon
sakthipriya 5:bf1f3504cd9d 356 beac_flag = 1;
sakthipriya 6:4fd79755f4c9 357 bea_timeout.attach(&FUNC_BEA_TIMEOUT,35.0 );
sakthipriya 6:4fd79755f4c9 358 printf("\n\rbeacon switched off");
sakthipriya 0:e91ee0e99213 359 break;
sakthipriya 6:4fd79755f4c9 360 case '1' : printf("\n\rcommand 1 executed");
sakthipriya 0:e91ee0e99213 361 break;
sakthipriya 6:4fd79755f4c9 362 case '2' : printf("\n\rcommand 2 executed");
sakthipriya 0:e91ee0e99213 363 break;
sakthipriya 6:4fd79755f4c9 364 case '3' : printf("\n\rcommand 3 executed");
sakthipriya 0:e91ee0e99213 365 }
sakthipriya 5:bf1f3504cd9d 366 }
sakthipriya 5:bf1f3504cd9d 367
sakthipriya 5:bf1f3504cd9d 368 void FUNC_BEA_TIMEOUT()
sakthipriya 5:bf1f3504cd9d 369 {
sakthipriya 6:4fd79755f4c9 370 // printf("\n\rbea on");
sakthipriya 5:bf1f3504cd9d 371 beac_flag = 0;
sakthipriya 6:4fd79755f4c9 372
sakthipriya 0:e91ee0e99213 373 }
sakthipriya 0:e91ee0e99213 374
sakthipriya 0:e91ee0e99213 375
sakthipriya 0:e91ee0e99213 376 //------------------------------------------------------------------------------------------------------------------------------------------------
gkumar 4:65a2d0b97d01 377 //SCHEDULER RTOS timer can be called periodically
sakthipriya 0:e91ee0e99213 378 //------------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 379 int beacon_sc = 3;
sakthipriya 0:e91ee0e99213 380 uint16_t schedcount=1;
sakthipriya 0:e91ee0e99213 381 void T_SC(void const *args)
sakthipriya 0:e91ee0e99213 382 {
sakthipriya 0:e91ee0e99213 383 //DRDY=0;
sakthipriya 6:4fd79755f4c9 384 printf("\n\rThe value of i in scheduler is %d\n",schedcount);
gkumar 4:65a2d0b97d01 385 if(schedcount == 65532) //to reset the counter // when it reaches here back to intial state
sakthipriya 0:e91ee0e99213 386 {
sakthipriya 0:e91ee0e99213 387 schedcount = 0;
sakthipriya 0:e91ee0e99213 388 }
sakthipriya 0:e91ee0e99213 389
sakthipriya 0:e91ee0e99213 390 if(schedcount%1==0)
sakthipriya 0:e91ee0e99213 391 {
sakthipriya 6:4fd79755f4c9 392 ptr_t_acs -> signal_set(0x1);
sakthipriya 6:4fd79755f4c9 393 ptr_t_wdt -> signal_set(0x5);
sakthipriya 0:e91ee0e99213 394 }
sakthipriya 0:e91ee0e99213 395 if(schedcount%2==0)
sakthipriya 0:e91ee0e99213 396 {
sakthipriya 0:e91ee0e99213 397 // ptr_t_fault -> signal_set(0x2);
sakthipriya 6:4fd79755f4c9 398 ptr_t_hk_acq -> signal_set(0x2);
sakthipriya 0:e91ee0e99213 399
sakthipriya 0:e91ee0e99213 400 }
sakthipriya 0:e91ee0e99213 401 if(schedcount%beacon_sc==0)
sakthipriya 0:e91ee0e99213 402 {
sakthipriya 0:e91ee0e99213 403 if(beac_flag==0)
sakthipriya 0:e91ee0e99213 404 {
sakthipriya 6:4fd79755f4c9 405 ptr_t_bea -> signal_set(0x3);
sakthipriya 0:e91ee0e99213 406 }
sakthipriya 0:e91ee0e99213 407 }
sakthipriya 0:e91ee0e99213 408 schedcount++;
sakthipriya 0:e91ee0e99213 409 }
sakthipriya 0:e91ee0e99213 410
sakthipriya 0:e91ee0e99213 411 //---------------------------------------------------------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 412
sakthipriya 0:e91ee0e99213 413 int main()
sakthipriya 0:e91ee0e99213 414 {
sakthipriya 0:e91ee0e99213 415 t1.start();
sakthipriya 0:e91ee0e99213 416 printf("\n\rIITMSAT BAE Activated \n");
sakthipriya 3:0931a8800543 417 //INIT_PNI(); // Initializing mnm blue
sakthipriya 3:0931a8800543 418 //FUNC_ACS_MAG_INIT(); // Initializing magnetometer
sakthipriya 0:e91ee0e99213 419 //FUNC_ACS_INIT_GYR(); // Initializing Gyroscope
sakthipriya 0:e91ee0e99213 420 slave.address(0x20); // setting slave address for BAE for I2C communication
sakthipriya 3:0931a8800543 421 //init_beacon();
sakthipriya 0:e91ee0e99213 422 ptr_t_hk_acq = new Thread(T_HK_ACQ);
sakthipriya 0:e91ee0e99213 423 ptr_t_acs = new Thread(T_ACS);
sakthipriya 0:e91ee0e99213 424 ptr_t_bea = new Thread(T_BEA);
sakthipriya 0:e91ee0e99213 425 ptr_t_i2c = new Thread(T_I2C_BAE);
sakthipriya 0:e91ee0e99213 426 //ptr_t_sc = new Thread(T_SC);
sakthipriya 0:e91ee0e99213 427 ptr_t_wdt = new Thread(T_WDT);
sakthipriya 0:e91ee0e99213 428
gkumar 4:65a2d0b97d01 429 interrupt_fault(); // Dummy function called when a fault interrupt is detected // intrrupt based fault
sakthipriya 0:e91ee0e99213 430
sakthipriya 0:e91ee0e99213 431 ptr_t_acs->set_priority(osPriorityAboveNormal);
sakthipriya 0:e91ee0e99213 432 ptr_t_i2c->set_priority(osPriorityHigh);
sakthipriya 0:e91ee0e99213 433 ptr_t_hk_acq->set_priority(osPriorityAboveNormal);
sakthipriya 0:e91ee0e99213 434 ptr_t_bea->set_priority(osPriorityAboveNormal);
sakthipriya 0:e91ee0e99213 435 //ptr_t_sc->set_priority(osPriorityAboveNormal);
sakthipriya 0:e91ee0e99213 436 ptr_t_wdt -> set_priority(osPriorityIdle);
sakthipriya 0:e91ee0e99213 437
sakthipriya 0:e91ee0e99213 438
sakthipriya 0:e91ee0e99213 439 // ----------------------------------------------------------------------------------------------
sakthipriya 0:e91ee0e99213 440 printf("\n\r T_ACS priority is %d",ptr_t_acs->get_priority());
sakthipriya 0:e91ee0e99213 441 printf("\n\r T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
sakthipriya 0:e91ee0e99213 442 printf("\n\r T_BEA priority is %d",ptr_t_bea->get_priority());
gkumar 4:65a2d0b97d01 443 RtosTimer t_sc_timer(T_SC,osTimerPeriodic); //values -3 tpo +3
sakthipriya 0:e91ee0e99213 444 t_sc_timer.start(10000);
sakthipriya 0:e91ee0e99213 445 printf("\n\r%f\n\r",t1.read());
sakthipriya 6:4fd79755f4c9 446 FUNC_I2C_TC_EXECUTE ('0');
sakthipriya 6:4fd79755f4c9 447 printf("\n \r check1 \n");
raizel_varun 2:3d9ca9554adf 448 interrupt.rise(&FUNC_I2C_INT); //interrupt received from CDMS
sakthipriya 0:e91ee0e99213 449 while(1) //required to prevent main from terminating
sakthipriya 0:e91ee0e99213 450 {
gkumar 4:65a2d0b97d01 451 Thread::wait(5000); // main by default a thread of priority 0
sakthipriya 6:4fd79755f4c9 452 // printf("\n \r check \n");
sakthipriya 6:4fd79755f4c9 453 // FUNC_I2C_TC_EXECUTE ('0'); // this has to come through i2c from cdms. just for testing beacon on/off
sakthipriya 6:4fd79755f4c9 454
sakthipriya 6:4fd79755f4c9 455 //
sakthipriya 6:4fd79755f4c9 456
sakthipriya 0:e91ee0e99213 457 }
sakthipriya 0:e91ee0e99213 458
sakthipriya 0:e91ee0e99213 459 }