![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bae wrking isr no bcn
Fork of TV_BAE_conops1_1_1 by
Revision 0:913c9e982740, committed 2015-11-03
- Comitter:
- sakthipriya
- Date:
- Tue Nov 03 14:46:51 2015 +0000
- Commit message:
- i2c in rtos working
Changed in this revision
diff -r 000000000000 -r 913c9e982740 ACS.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACS.cpp Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,503 @@ +/*------------------------------------------------------------------------------------------------------------------------------------------------------ +-------------------------------------------CONTROL ALGORITHM------------------------------------------------------------------------------------------*/ +#include <mbed.h> +#include <math.h> + +#include "pni.h" //pni header file +#include "pin_config.h" +#include "ACS.h" + + +//********************************flags******************************************// +extern uint32_t BAE_STATUS; +extern uint32_t BAE_ENABLE; +extern char ACS_INIT_STATUS; +extern char ACS_DATA_ACQ_STATUS; +extern char ACS_ATS_STATUS; +extern char ACS_MAIN_STATUS; +extern char ACS_STATUS; + +extern char ACS_ATS_ENABLE; +extern char ACS_DATA_ACQ_ENABLE; +extern char ACS_STATE; + +DigitalOut phase_TR_x(PIN27); // PHASE pin for x-torquerod +DigitalOut phase_TR_y(PIN28); // PHASE pin for y-torquerod +DigitalOut phase_TR_z(PIN86); // PHASE pin for z-torquerod + +extern PwmOut PWM1; //x //Functions used to generate PWM signal +extern PwmOut PWM2; //y +extern PwmOut PWM3; //z //PWM output comes from pins p6 + +int g_err_flag_TR_x=0; // setting x-flag to zero +int g_err_flag_TR_y=0; // setting y-flag to zero +int g_err_flag_TR_z=0; // setting z-flag to zero + +extern float data[6]; + + +//DigitalOut gpo1(PTC0); // enable of att sens2 switch +//DigitalOut gpo2(PTC16); // enable of att sens switch + + +Serial pc_acs(USBTX,USBRX); //for usb communication +void inverse(float mat[3][3],float inv[3][3]); + +int ctrl_count = 0; +float bcopy[3]; +float moment[3]; + ///////algo working well +void FCTN_ACS_CNTRLALGO(float b[3],float omega[3]) +{ + float db[3]; + float bb[3]={0,0,0}; + float d[3]={0,0,0}; + float Jm[3][3]={{0.2730,0,0},{0,0.3018,0},{0,0,0.3031}}; + float den=0,den2; + int i,j; //temporary variables + float Mu[2],z[2],dv[2],v[2],u[2],tauc[3]={0,0,0}; //outputs + float invJm[3][3]; + float kmu2=0.07,gamma2=1.9e4,kz2=0.4e-2,kmu=0.003,gamma=5.6e4,kz=0.1e-4; + + //................. calculating db values........................... + if(ctrl_count!=0) + { + for(i=0;i<3;i++) + db[i]= (b[i]-bcopy[i])/10; + } + else + { + for(i=0;i<3;i++) + db[i]= 0; + } + ctrl_count++; + //.................................................................. + printf("\n\r Entered cntrl algo\n\r"); + for(int i=0; i<3; i++) + { + printf("%f\t",omega[i]); + } + for(int i=0; i<3; i++) + { + printf("%f\t",b[i]); + } + + //.........................algo...................................... + den=sqrt((b[0]*b[0])+(b[1]*b[1])+(b[2]*b[2])); + den2=(b[0]*db[0])+(b[1]*db[1])+(b[2]*db[2]); + for(i=0;i<3;i++) + { + db[i]=((db[i]*den*den)-(b[i]*(den2)))/(pow(den,3)); + //db[i]/=den*den*den; + } + for(i=0;i<3;i++) + { + b[i]/=den; + } + // select kz, kmu, gamma + if(b[0]>0.9||b[0]<-0.9) + { + kz=kz2; + kmu=kmu2; + gamma=gamma2; + } + // calculate Mu, v, dv, z, u + for(i=0;i<2;i++) + { + Mu[i]=b[i+1]; + v[i]=-kmu*Mu[i]; + dv[i]=-kmu*db[i+1]; + z[i]=db[i+1]-v[i]; + u[i]=-kz*z[i]+dv[i]-(Mu[i]/gamma); + } + inverse(Jm,invJm); + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + bb[i]+=omega[j]*(omega[(i+1)%3]*Jm[(i+2)%3][j]-omega[(i+2)%3]*Jm[(i+1)%3][j]); + } + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + d[i]+=bb[j]*invJm[i][j]; + } + bb[1]=u[0]+(d[0]*b[2])-(d[2]*b[0])-(omega[0]*db[2])+(omega[2]*db[0]); + bb[2]=u[1]-(d[0]*b[1])+(d[1]*b[0])+(omega[0]*db[1])-(omega[1]*db[0]); + bb[0]=0; + for(i=0;i<3;i++) + { + d[i]=invJm[1][i]; + invJm[1][i]=b[2]*invJm[0][i]-b[0]*invJm[2][i]; + invJm[2][i]=-b[1]*invJm[0][i]+b[0]*d[i]; + invJm[0][i]=b[i]; + } + inverse(invJm,Jm); + printf("\n \r calculating tauc"); + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + tauc[i]+=Jm[i][j]*bb[j]; // calculating torque values + printf(" %f \t",tauc[i]); + } + //..........................tauc to moment conversion.......................... + printf("\n \r calculating moment"); + for(i=0;i<3;i++) + bcopy[i]=b[i]*den; + for(i=0;i<3;i++) + { + moment[i]=bcopy[(i+1)%3]*tauc[(i+2)%3]-bcopy[(i+2)%3]*tauc[(i+1)%3]; + moment[i]/=den; + printf(" %f \t",moment[i]); + } + printf("\n\r exited control algo\n"); +} +//..........................function to find inverse.................. +void inverse(float mat[3][3],float inv[3][3]) +{ + int i,j; + float det=0; + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + inv[j][i]=(mat[(i+1)%3][(j+1)%3]*mat[(i+2)%3][(j+2)%3])-(mat[(i+2)%3][(j+1)%3]*mat[(i+1)%3][(j+2)%3]); + } + det+=(mat[0][0]*inv[0][0])+(mat[0][1]*inv[1][0])+(mat[0][2]*inv[2][0]); + for(i=0;i<3;i++) + { + for(j=0;j<3;j++) + inv[i][j]/=det; + } +} + + +I2C i2c (PTC9,PTC8); //PTC9-sda,PTC8-scl for the attitude sensors and battery gauge + +void FCTN_ACS_INIT(void); //initialization of registers happens +void FCTN_ATS_DATA_ACQ(); //data is obtained +void T_OUT(); //timeout function to stop infinite loop +Timeout to; //Timeout variable to +int toFlag; + +int count =0; // Time for which the BAE uC is running (in seconds) +void T_OUT() +{ + toFlag=0; //as T_OUT function gets called the while loop gets terminated +} + + +//DEFINING VARIABLES +char cmd[2]; +char raw_gyro[6]; +char raw_mag[6]; +char store,status; +int16_t bit_data; +float gyro_data[3], mag_data[3],combined_values[6]; +float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps +float senstivity_mag =32.768; //senstivity is obtained from 2^15/1000microtesla +float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0}; + +void FCTN_ACS_INIT() +{ + ACS_INIT_STATUS = 's'; //set ACS_INIT_STATUS flag + FLAG(); + pc_acs.printf("Attitude sensor init called \n \r"); + //FLAG(); + cmd[0]=RESETREQ; + cmd[1]=BIT_RESREQ; + i2c.write(SLAVE_ADDR,cmd,2); //When 0x01 is written in reset request register Emulates a hard power down/power up + wait_ms(2000); //waiting for loading configuration file stored in EEPROM + cmd[0]=SENTRALSTATUS; + i2c.write(SLAVE_ADDR,cmd,1); + i2c.read(SLAVE_ADDR_READ,&store,1); + wait_ms(100); + //to check whether EEPROM is uploaded + switch((int)store) { + case(3): { + break; + } + case(11): { + break; + } + default: { + cmd[0]=RESETREQ; + cmd[1]=BIT_RESREQ; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(2000); + } + } + pc_acs.printf("Sentral Status is %x\n \r",(int)store); + cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors + cmd[1]=BIT_RUN_ENB; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(100); + cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer + cmd[1]=BIT_MAGODR; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(100); + cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope + cmd[1]=BIT_GYROODR; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(100); + cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values + cmd[1]=0x00; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(100); + cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values + cmd[1]=BIT_EVT_ENB; + i2c.write(SLAVE_ADDR,cmd,2); + wait_ms(100); + ACS_INIT_STATUS = 'c'; //set ACS_INIT_STATUS flag +} + +void FCTN_ATS_DATA_ACQ() +{ + ACS_DATA_ACQ_STATUS = 's'; //set ACS_DATA_ACQ_STATUS flag for att sens 2 + if( ACS_ATS_ENABLE == 'e') + { + FLAG(); + pc_acs.printf("attitude sensor execution called \n \r"); + toFlag=1; //toFlag is set to 1 so that it enters while loop + to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated + while(toFlag) { + cmd[0]=EVT_STATUS; + i2c.write(SLAVE_ADDR,cmd,1); + i2c.read(SLAVE_ADDR_READ,&status,1); + wait_ms(100); + pc_acs.printf("Event Status is %x\n \r",(int)status); + //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take + if(((int)status&40)==40) { + cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x + i2c.write(SLAVE_ADDR,cmd,1); + i2c.read(SLAVE_ADDR_READ,raw_gyro,6); + cmd[0]=MAG_XOUT_H; //LSB of x + i2c.write(SLAVE_ADDR,cmd,1); + i2c.read(SLAVE_ADDR_READ,raw_mag,6); + // pc_acs.printf("\nGyro Values:\n"); + for(int i=0; i<3; i++) { + //concatenating gyro LSB and MSB to get 16 bit signed data values + bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i]; + gyro_data[i]=(float)bit_data; + gyro_data[i]=gyro_data[i]/senstivity_gyro; + gyro_data[i]+=gyro_error[i]; + // pc_acs.printf("%f\t",gyro_data[i]); + } + // pc_acs.printf("\nMag Values:\n"); + for(int i=0; i<3; i++) { + //concatenating mag LSB and MSB to get 16 bit signed data values + bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i]; + mag_data[i]=(float)bit_data; + mag_data[i]=mag_data[i]/senstivity_mag; + mag_data[i]+=mag_error[i]; + // pc_acs.printf("%f\t",mag_data[i]); + } + for(int i=0; i<3; i++) { + data[i]=gyro_data[i]; + data[i+3]=mag_data[i]; + } + // return(combined_values); //returning poiter combined values + } + //checking for the error + else if (((int)status&2)==2) { + FCTN_ACS_INIT(); //when there is any error then Again inilization is done to remove error + } + } + } + else //ACS_DATA_ACQ_STATUS = ACS_DATA_ACQ_FAILURE + { + ACS_DATA_ACQ_STATUS = 'f'; + } + ACS_DATA_ACQ_STATUS = 'c'; //clear ACS_DATA_ACQ_STATUS flag for att sens 2 +} + +void FCTN_ACS_GENPWM_MAIN(float Moment[3]) +{ + printf("\n\rEntered executable PWMGEN function\n"); // entering the PWMGEN executable function + + float l_duty_cycle_x=0; //Duty cycle of Moment in x direction + float l_current_x=0; //Current sent in x TR's + float l_duty_cycle_y=0; //Duty cycle of Moment in y direction + float l_current_y=0; //Current sent in y TR's + float l_duty_cycle_z=0; //Duty cycle of Moment in z direction + float l_current_z=0; //Current sent in z TR's + + + for(int i = 0 ; i<3;i++) + { + // printf(" %f \t ",Moment[i]); // taking the moment values from control algorithm as inputs + } + + //----------------------------- x-direction TR --------------------------------------------// + + + float l_moment_x = Moment[0]; //Moment in x direction + + phase_TR_x = 1; // setting the default current direction + if (l_moment_x <0) + { + phase_TR_x = 0; // if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high + l_moment_x = abs(l_moment_x); + } + + l_current_x = l_moment_x * TR_CONSTANT ; //Moment and Current always have the linear relationship + pc_acs.printf("current in trx is %f \r \n",l_current_x); + if( l_current_x>0 && l_current_x < 0.006 ) //Current and Duty cycle have the linear relationship between 1% and 100% + { + l_duty_cycle_x = 6*1000000*pow(l_current_x,4) - 377291*pow(l_current_x,3) + 4689.6*pow(l_current_x,2) + 149.19*l_current_x - 0.0008; // calculating upto 0.1% dutycycle by polynomial interpolation + pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x); + PWM1.period(TIME_PERIOD); + PWM1 = l_duty_cycle_x/100 ; + } + else if( l_current_x >= 0.006 && l_current_x < 0.0116) + { + l_duty_cycle_x = 1*100000000*pow(l_current_x,4) - 5*1000000*pow(l_current_x,3) + 62603*pow(l_current_x,2) - 199.29*l_current_x + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation + pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x); + PWM1.period(TIME_PERIOD); + PWM1 = l_duty_cycle_x/100 ; + } + else if (l_current_x >= 0.0116 && l_current_x < 0.0624) + { + l_duty_cycle_x = 212444*pow(l_current_x,4) - 33244*pow(l_current_x,3) + 1778.4*pow(l_current_x,2) + 120.91*l_current_x + 0.3878; // calculating upto 10% dutycycle by polynomial interpolation + pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x); + PWM1.period(TIME_PERIOD); + PWM1 = l_duty_cycle_x/100 ; + } + else if(l_current_x >= 0.0624 && l_current_x < 0.555) + { + l_duty_cycle_x = 331.15*pow(l_current_x,4) - 368.09*pow(l_current_x,3) + 140.43*pow(l_current_x,2) + 158.59*l_current_x + 0.0338; // calculating upto 100% dutycycle by polynomial interpolation + pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x); + PWM1.period(TIME_PERIOD); + PWM1 = l_duty_cycle_x/100 ; + } + else if(l_current_x==0) + { + printf("\n \r l_current_x====0"); + l_duty_cycle_x = 0; // default value of duty cycle + pc_acs.printf("DC for trx is %f \r \n",l_duty_cycle_x); + PWM1.period(TIME_PERIOD); + PWM1 = l_duty_cycle_x/100 ; + } + else //not necessary + { + g_err_flag_TR_x = 1; + } + + //------------------------------------- y-direction TR--------------------------------------// + + + float l_moment_y = Moment[1]; //Moment in y direction + + phase_TR_y = 1; // setting the default current direction + if (l_moment_y <0) + { + phase_TR_y = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high + l_moment_y = abs(l_moment_y); + } + + + l_current_y = l_moment_y * TR_CONSTANT ; //Moment and Current always have the linear relationship + pc_acs.printf("current in try is %f \r \n",l_current_y); + if( l_current_y>0 && l_current_y < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100% + { + l_duty_cycle_y = 6*1000000*pow(l_current_y,4) - 377291*pow(l_current_y,3) + 4689.6*pow(l_current_y,2) + 149.19*l_current_y - 0.0008; // calculating upto 0.1% dutycycle by polynomial interpolation + pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y); + PWM2.period(TIME_PERIOD); + PWM2 = l_duty_cycle_y/100 ; + } + else if( l_current_y >= 0.006 && l_current_y < 0.0116) + { + l_duty_cycle_y = 1*100000000*pow(l_current_y,4) - 5*1000000*pow(l_current_y,3) + 62603*pow(l_current_y,2) - 199.29*l_current_y + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation + pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y); + PWM2.period(TIME_PERIOD); + PWM2 = l_duty_cycle_y/100 ; + } + else if (l_current_y >= 0.0116&& l_current_y < 0.0624) + { + l_duty_cycle_y = 212444*pow(l_current_y,4) - 33244*pow(l_current_y,3) + 1778.4*pow(l_current_y,2) + 120.91*l_current_y + 0.3878;// calculating upto 10% dutycycle by polynomial interpolation + pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y); + PWM2.period(TIME_PERIOD); + PWM2 = l_duty_cycle_y/100 ; + } + else if(l_current_y >= 0.0624 && l_current_y < 0.555) + { + l_duty_cycle_y = 331.15*pow(l_current_y,4) - 368.09*pow(l_current_y,3) + 140.43*pow(l_current_y,2) + 158.59*l_current_y + 0.0338;// calculating upto 100% dutycycle by polynomial interpolation + pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y); + PWM2.period(TIME_PERIOD); + PWM2 = l_duty_cycle_y/100 ; + } + else if(l_current_y==0) + { + printf("\n \r l_current_y====0"); + l_duty_cycle_y = 0; // default value of duty cycle + pc_acs.printf("DC for try is %f \r \n",l_duty_cycle_y); + PWM2.period(TIME_PERIOD); + PWM2 = l_duty_cycle_y/100 ; + } + else // not necessary + { + g_err_flag_TR_y = 1; + } + + //----------------------------------------------- z-direction TR -------------------------// + + + float l_moment_z = Moment[2]; //Moment in z direction + + phase_TR_z = 1; // setting the default current direction + if (l_moment_z <0) + { + phase_TR_z = 0; //if the moment value is negative, we send the abs value of corresponding current in opposite direction by setting the phase pin high + l_moment_z = abs(l_moment_z); + } + + + l_current_z = l_moment_z * TR_CONSTANT ; //Moment and Current always have the linear relationship + pc_acs.printf("current in trz is %f \r \n",l_current_z); + if( l_current_z>0 && l_current_z < 0.006 )//Current and Duty cycle have the linear relationship between 1% and 100% + { + l_duty_cycle_z = 6*1000000*pow(l_current_z,4) - 377291*pow(l_current_z,3) + 4689.6*pow(l_current_z,2) + 149.19*l_current_z - 0.0008;// calculating upto 0.1% dutycycle by polynomial interpolation + pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z); + PWM3.period(TIME_PERIOD); + PWM3 = l_duty_cycle_z/100 ; + } + else if( l_current_z >= 0.006 && l_current_z < 0.0116) + { + l_duty_cycle_z = 1*100000000*pow(l_current_z,4) - 5*1000000*pow(l_current_z,3) + 62603*pow(l_current_z,2) - 199.29*l_current_z + 0.7648;// calculating upto 1% dutycycle by polynomial interpolation + pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z); + PWM3.period(TIME_PERIOD); + PWM3 = l_duty_cycle_z/100 ; + } + else if (l_current_z >= 0.0116 && l_current_z < 0.0624) + { + l_duty_cycle_z = 212444*pow(l_current_z,4) - 33244*pow(l_current_z,3) + 1778.4*pow(l_current_z,2) + 120.91*l_current_z + 0.3878;// calculating upto 10% dutycycle by polynomial interpolation + pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z); + PWM3.period(TIME_PERIOD); + PWM3 = l_duty_cycle_z/100 ; + } + else if(l_current_z >= 0.0624 && l_current_z < 0.555) + { + l_duty_cycle_z = 331.15*pow(l_current_z,4) - 368.09*pow(l_current_z,3) + 140.43*pow(l_current_z,2) + 158.59*l_current_z + 0.0338;// calculating upto 100% dutycycle by polynomial interpolation + pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z); + PWM3.period(TIME_PERIOD); + PWM3 = l_duty_cycle_z/100 ; + } + else if(l_current_z==0) + { + printf("\n \r l_current_z====0"); + l_duty_cycle_z = 0; // default value of duty cycle + pc_acs.printf("DC for trz is %f \r \n",l_duty_cycle_z); + PWM3.period(TIME_PERIOD); + PWM3 = l_duty_cycle_z/100 ; + } + else // not necessary + { + g_err_flag_TR_z = 1; + } + + //-----------------------------------------exiting the function-----------------------------------// + + printf("\n\rExited executable PWMGEN function\n\r"); // stating the successful exit of TR function + +} + + + \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 ACS.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ACS.h Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +#include "math.h" +#include "pni.h" + +//........................................... +#define TIME_PERIOD 0.02 +#define TR_CONSTANT 0.3 + +void FCTN_ACS_GENPWM_MAIN(float*); +void FCTN_ACS_CNTRLALGO(float*,float*); +void inverse(float mat[3][3],float inv[3][3]); +extern void FLAG(); + +void FCTN_ATS_SWITCH(bool); +void FCTN_ACS_INIT(); //initialization of registers happens +//void FCTN_ATS_DATA_ACQ(float*,float*); // main function: checks errors, gets data, switches on/off the sensor +//void FCTN_GET_DATA(float*,float*); //data is obtained +void FCTN_T_OUT(); //timeout function to stop infinite loop +void FCTN_ATS_DATA_ACQ();
diff -r 000000000000 -r 913c9e982740 BCN.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BCN.cpp Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,403 @@ +#include "BCN.h" +#include <stdio.h> +#include "pin_config.h" + +Serial pc_bcn(USBTX, USBRX); //tx,rx +SPI spi(PIN16, PIN17, PIN15); // mosi, miso, sclk +DigitalOut cs(PIN6); //slave select or chip select +Timer t; +Timer t_i; +Ticker loop; + + +//GLOBAL VARIABLES +uint8_t BCN_INIT_STATUS; +uint8_t BCN_TX_MAIN_STATUS; +uint8_t BCN_TX_STATUS; +uint8_t BCN_TX_EN = 1; //hardcoding for now //check where is this variable toggled?? +uint8_t BCN_FEN = 1; //write this to non-erasable memory. +uint8_t BCN_STANDBY = 0; //hardcoding for now //check where is this variable toggled?? +uint8_t BCN_TS_BUFFER; // For Temperature + + +void P_BCN_INIT() +{ + pc_bcn.printf("P_BCN_INIT\n"); + BCN_INIT_STATUS = 1; + Init_BEACON_HW(); + if (BCN_FEN == 0) + t.start();//Start the timer for RF_Silence + BCN_INIT_STATUS = 0; +} +void P_BCN_FEN() +{ + pc_bcn.printf("P_FEN\n"); + BCN_FEN = 1;//write this value to flash +} +void P_BCN_TX_MAIN() +{ + pc_bcn.printf("P_BCN_TX_MAIN\n"); + BCN_TX_MAIN_STATUS = 1; + + if(BCN_FEN == 1) + { + if(BCN_TX_EN == 1) + { + //Measure and store BCN temperature in BCN_TS_BUFFER + BCN_TS_BUFFER = check_Temperature(); + pc_bcn.printf("\n\ntemperature = %d\n\n",BCN_TS_BUFFER); + //Get BCN_HK data from BCN HW(SPI) //Store BCN_HK data in BCN_HK_BUFFER + if(BCN_STANDBY == 1 ) + { + Set_BCN_TX_STATUS(BCN_TX_STANDBY); + BCN_TX_MAIN_STATUS = 0; + + // break; + } + else + { + //transmit short beacon and long beacon + t_i.start(); + int begin = t_i.read_us(); + SHORT_BCN_TX(); + LONG_BCN_TX(); + t_i.stop(); + int end = t_i.read_us(); + pc_bcn.printf("The time required for short and long is %d seconds\r\n", end-begin); + + if(Check_ACK_RECEIVED() == 1) + { + Set_BCN_TX_STATUS(BCN_TX_SUCCESS); + BCN_TX_MAIN_STATUS = 0; + } + else + { + Set_BCN_TX_STATUS(BCN_TX_FAILURE); + BCN_TX_MAIN_STATUS = 0; + + } + } + } + else + { + Set_BCN_TX_STATUS(BCN_TX_DISABLED); + BCN_TX_MAIN_STATUS = 0; + } + } + else + { + Set_BCN_TX_STATUS(BCN_RF_SILENCE); //Window of RF Silence: None of the Txs should be on. + BCN_TX_MAIN_STATUS = 0; + } +} + +void Set_BCN_TX_STATUS(uint8_t STATUS) +{ + BCN_TX_STATUS = STATUS; +} + +uint8_t check_Temperature() +{ + uint8_t temperature; + writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80); + writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20); + wait(1); + temperature = readreg(RF22_REG_11_ADC_Value); + temperature = (float)temperature*0.5 - 64; + return temperature; +} + +void SHORT_BCN_TX() +{ + writereg(RF22_REG_6E_TX_DATA_RATE,0x01); + writereg(RF22_REG_6F_TX_DATA_RATE,0x4F);//160bps + writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length + /* + init(); + //init complete + pc_bcn.printf("init complete.....press t to send\n"); + while(pc_bcn.getc()=='t') + { */ + //button.rise(&interrupt_func); //interrupt enabled ( rising edge of pin 9) + wait(0.02); // pl. update this value or even avoid it!!! + int i=0; + //extract values from short_beacon[] + struct Short_beacon + { + uint8_t Voltage[1]; + uint8_t AngularSpeed[2]; + uint8_t SubsystemStatus[1]; + uint8_t Temp[3]; + uint8_t ErrorFlag[1]; + }Shortbeacon = { {0x88}, {0x99, 0xAA} , {0xAA},{0xAA,0xDD,0xEE}, {0x00} }; + + //filling hk data + //uint8_t short_beacon[] = { 0xAB, 0x8A, 0xE2, 0xBB, 0xB8, 0xA2, 0x8E,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]}; + uint8_t short_beacon[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]}; + + //uint8_t short_beacon[15]; +// for(int i = 0;i<15;i++) +// { +// short_beacon[i] = 0xAA; +// } + + //writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode ?? + clearTxBuf(); + //writing data first time + int byte = 0; + cs = 0; + spi.write(0xFF); + pc_bcn.printf("line 151"); + for (int byte_counter = 0; byte_counter <15 ; byte_counter++) + { + for(int j = 3; j >= 0 ; j--) + { + if((short_beacon[byte_counter] & (uint8_t) pow(2.0,(j*2+1)))!= pow(2.0,(j*2+1))) + { + byte=0x00; + } + else + { + byte=0xF0; + } + if((short_beacon[byte_counter] & (uint8_t) pow(2.0,j*2))!= pow(2.0,j*2)) + { + byte=byte | 0x00; + } + else + { + byte=byte | 0x0F; + } + spi.write(byte); + } + } + + cs = 1; + pc_bcn.printf("line 177\n"); + //Set to Tx mode + writereg(RF22_REG_07_OPERATING_MODE1,0x09); + + //Check for fifoThresh + while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); + //pc_bcn.printf("fifothresh1?\n"); + pc_bcn.printf("line 184\n"); + //rf22.waitPacketSent(); + while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04); + //pc_bcn.printf(" chk pkt sent!\r\n"); + pc_bcn.printf("Short packet sent\r\n"); + + writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode + +} +void LONG_BCN_TX() +{ + writereg(RF22_REG_6E_TX_DATA_RATE,0x04); + writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps + writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length + wait(0.02); + + + //get long_beacon array + + uint8_t Long_beacon[75]; + for(int i = 0;i<75;i++) + { + Long_beacon[i] = 0xA0; + } + + + + + //setModeIdle(); + writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode + clearTxBuf(); + //writing data first time + cs = 0; + spi.write(0xFF); + for(int i=0; i<60;i++) + { + spi.write(Long_beacon[i]); + } + cs = 1; + + //Set to Tx mode + writereg(RF22_REG_07_OPERATING_MODE1,0x09); + + //Check for fifoThresh + while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); + + cs = 0; + spi.write(0xFF); + for(int i=60; i<75;i++) + { + spi.write(Long_beacon[i]); + } + cs = 1; + wait(0.01); + //Check for fifoThresh + while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20); + + //Check for packetsent interrupt + while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04); + + pc_bcn.printf("Long packet sent\r\n"); + + writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode +} +void reset_uC() +{ + P_BCN_INIT(); +} +void writereg(uint8_t reg,uint8_t val) +{ + uint8_t count = 0; + for(;;count++) + { + int read_val =0; cs = 0;spi.write(reg | 0x80);spi.write(val);cs = 1; + if(reg != 0x7 && reg != 0x58 && reg != 0xF) + { + read_val = readreg(reg); + if (read_val == val) + { + break; + } + else if(count == 5) + { + reset_uC(), printf("reg = 0x%X\n",reg);break; + } + } + else + break; + } +} +uint8_t readreg(uint8_t reg) +{ + uint8_t val;cs = 0;spi.write(reg & ~0x80);val = spi.write(0);cs = 1;return val; +} +void clearTxBuf() +{ + writereg(RF22_REG_08_OPERATING_MODE2,0x01); + writereg(RF22_REG_08_OPERATING_MODE2,0x00); +} +uint8_t setFrequency(double centre,float afcPullInRange) +{ + uint8_t fbsel = 0x40; + uint8_t afclimiter; + if (centre >= 480.0) { + centre /= 2; + fbsel |= 0x20; + afclimiter = afcPullInRange * 1000000.0 / 1250.0; + } else { + if (afcPullInRange < 0.0 || afcPullInRange > 0.159375) + return false; + afclimiter = afcPullInRange * 1000000.0 / 625.0; + } + centre /= 10.0; + double integerPart = floor(centre); + double fractionalPart = centre - integerPart; + + uint8_t fb = (uint8_t)integerPart - 24; // Range 0 to 23 + fbsel |= fb; + uint16_t fc = fractionalPart * 64000; + writereg(RF22_REG_73_FREQUENCY_OFFSET1, 0); // REVISIT + writereg(RF22_REG_74_FREQUENCY_OFFSET2, 0); + writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel); + writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8); + writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff); + writereg(RF22_REG_2A_AFC_LIMITER, afclimiter); + return 0; +} + + +void Init_BEACON_HW() +{ + cs=1; // chip must be deselected + spi.format(8,0); + spi.frequency(10000000); //10MHz SCLK + + //should either have a flag for invalid SPI or discard this for actual case or add reset + if (readreg(RF22_REG_00_DEVICE_TYPE) == 0x08) + pc_bcn.printf("spi connection valid\r\n"); + else + {pc_bcn.printf("error in spi connection\r\n"); + reset_uC(); + } + + writereg(RF22_REG_07_OPERATING_MODE1,0x80); //sw_reset + wait(1); //takes time to reset + + clearTxBuf(); + + writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode + + //txfifoalmostempty + writereg(RF22_REG_7D_TX_FIFO_CONTROL2,30); + + //Packet-engine registers + writereg(RF22_REG_30_DATA_ACCESS_CONTROL,0x00); + + writereg(RF22_REG_33_HEADER_CONTROL2,0x08); + writereg(RF22_REG_34_PREAMBLE_LENGTH,0x00); + + writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state + writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state + + setFrequency(435.0,05); + + if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00) + { + //pc_bcn.printf("frequency not set properly\r\n"); + reset_uC(); + } + + //set Modem Configuration + writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0xdf); + writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03); + writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x39); + writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x20); + writereg(RF22_REG_22_CLOCK_RECOVERY_OFFSET1,0x68); //updated 20 to 25 reg values from excel sheet for 1.2 Khz freq. deviation,fsk + writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0xdc); + writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00); + writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x6B); + writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2C); + writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x11); //not required for fsk (OOK counter value) + writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x2A); //?? + writereg(RF22_REG_58,0x80); + writereg(RF22_REG_69_AGC_OVERRIDE1,0x60); + + //Data rate set later + + writereg(RF22_REG_70_MODULATION_CONTROL1,0x20); + writereg(RF22_REG_71_MODULATION_CONTROL2,0x21);//ook = 0x21 + + //set tx power + writereg(RF22_REG_6D_TX_POWER,0x07); //20dbm + + //TX_packet_length written later +} +bool Check_ACK_RECEIVED() +{ + if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04) + { + printf("Packet sent: ACK received\r\n"); + return 1; + } + else + { + pc_bcn.printf("Packet not sent\r\n"); + return 0; + } +} +/*int main() +{ + P_BCN_INIT(); + + loop.attach(&P_BCN_TX_MAIN, 10.0); + + while(t.read() < RF_SILENCE_TIME); + + P_BCN_FEN(); + + while(1); + +}*/ \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 BCN.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BCN.h Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,127 @@ +#include "mbed.h" + +//STATES +#define BCN_RF_SILENCE 0 +#define BCN_TX_DISABLED 1 +#define BCN_TX_STANDBY 2 +#define BCN_TX_FAILURE 3 +#define BCN_TX_SUCCESS 4 + +//Size of tx data +#define SHORT_TX_DATA 60 //in bytes +#define LONG_TX_DATA 75 //in bytes + +//#define RF_SILENCE_TIME 5 //in seconds +#define RF_SILENCE_TIME 10 + +//FUNCTION PROTOTYPING +void P_BCN_INIT(); +void P_BCN_FEN(); +void P_BCN_TX_MAIN(); +void Set_BCN_TX_STATUS(uint8_t); +uint8_t check_Temperature(); +void SHORT_BCN_TX(); +void LONG_BCN_TX(); +void Init_BEACON_HW(); +void writereg(uint8_t,uint8_t); +uint8_t readreg(uint8_t); +void clearTxBuf(); +uint8_t setFrequency(double,float); +bool Check_ACK_RECEIVED(); +void reset_uC(); + +#define RF22_MAX_MESSAGE_LEN 255 + +//These values we set for FIFO thresholds +#define RF22_TXFFAEM_THRESHOLD 40 + +//Registers +#define RF22_REG_00_DEVICE_TYPE 0x00 +#define RF22_REG_02_DEVICE_STATUS 0x02 +#define RF22_REG_03_INTERRUPT_STATUS1 0x03 +#define RF22_REG_04_INTERRUPT_STATUS2 0x04 +#define RF22_REG_07_OPERATING_MODE1 0x07 +#define RF22_REG_08_OPERATING_MODE2 0x08 +#define RF22_REG_09_OSCILLATOR_LOAD_CAPACITANCE 0x09 +#define RF22_REG_0B_GPIO_CONFIGURATION0 0x0b +#define RF22_REG_0C_GPIO_CONFIGURATION1 0x0c +#define RF22_REG_0D_GPIO_CONFIGURATION2 0x0d +#define RF22_REG_0F_ADC_CONFIGURATION 0x0f +#define RF22_REG_11_ADC_Value 0x11 +#define RF22_REG_12_Temperature_Sensor_Calibration 0x12 +#define RF22_REG_1C_IF_FILTER_BANDWIDTH 0x1c +#define RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE 0x1f +#define RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE 0x20 +#define RF22_REG_21_CLOCK_RECOVERY_OFFSET2 0x21 +#define RF22_REG_22_CLOCK_RECOVERY_OFFSET1 0x22 +#define RF22_REG_23_CLOCK_RECOVERY_OFFSET0 0x23 +#define RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1 0x24 +#define RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0 0x25 +#define RF22_REG_26_RSSI 0x26 +#define RF22_REG_27_RSSI_THRESHOLD 0x27 +#define RF22_REG_28_ANTENNA_DIVERSITY1 0x28 +#define RF22_REG_29_ANTENNA_DIVERSITY2 0x29 +#define RF22_REG_2A_AFC_LIMITER 0x2a +#define RF22_REG_2B_AFC_CORRECTION_READ 0x2b +#define RF22_REG_2C_OOK_COUNTER_VALUE_1 0x2c +#define RF22_REG_2D_OOK_COUNTER_VALUE_2 0x2d +#define RF22_REG_2E_SLICER_PEAK_HOLD 0x2e +#define RF22_REG_30_DATA_ACCESS_CONTROL 0x30 +#define RF22_REG_31_EZMAC_STATUS 0x31 +#define RF22_REG_32_HEADER_CONTROL1 0x32 +#define RF22_REG_33_HEADER_CONTROL2 0x33 +#define RF22_REG_34_PREAMBLE_LENGTH 0x34 +#define RF22_REG_35_PREAMBLE_DETECTION_CONTROL1 0x35 +#define RF22_REG_36_SYNC_WORD3 0x36 +#define RF22_REG_37_SYNC_WORD2 0x37 +#define RF22_REG_38_SYNC_WORD1 0x38 +#define RF22_REG_39_SYNC_WORD0 0x39 +#define RF22_REG_3A_TRANSMIT_HEADER3 0x3a +#define RF22_REG_3B_TRANSMIT_HEADER2 0x3b +#define RF22_REG_3C_TRANSMIT_HEADER1 0x3c +#define RF22_REG_3D_TRANSMIT_HEADER0 0x3d +#define RF22_REG_3E_PACKET_LENGTH 0x3e +#define RF22_REG_3F_CHECK_HEADER3 0x3f +#define RF22_REG_40_CHECK_HEADER2 0x40 +#define RF22_REG_41_CHECK_HEADER1 0x41 +#define RF22_REG_42_CHECK_HEADER0 0x42 +#define RF22_REG_43_HEADER_ENABLE3 0x43 +#define RF22_REG_44_HEADER_ENABLE2 0x44 +#define RF22_REG_45_HEADER_ENABLE1 0x45 +#define RF22_REG_46_HEADER_ENABLE0 0x46 +#define RF22_REG_47_RECEIVED_HEADER3 0x47 +#define RF22_REG_48_RECEIVED_HEADER2 0x48 +#define RF22_REG_49_RECEIVED_HEADER1 0x49 +#define RF22_REG_4A_RECEIVED_HEADER0 0x4a +#define RF22_REG_4B_RECEIVED_PACKET_LENGTH 0x4b +#define RF22_REG_58 0x58 +#define RF22_REG_60_CHANNEL_FILTER_COEFFICIENT_ADDRESS 0x60 +#define RF22_REG_61_CHANNEL_FILTER_COEFFICIENT_VALUE 0x61 +#define RF22_REG_62_CRYSTAL_OSCILLATOR_POR_CONTROL 0x62 +#define RF22_REG_63_RC_OSCILLATOR_COARSE_CALIBRATION 0x63 +#define RF22_REG_64_RC_OSCILLATOR_FINE_CALIBRATION 0x64 +#define RF22_REG_65_LDO_CONTROL_OVERRIDE 0x65 +#define RF22_REG_66_LDO_LEVEL_SETTINGS 0x66 +#define RF22_REG_67_DELTA_SIGMA_ADC_TUNING1 0x67 +#define RF22_REG_68_DELTA_SIGMA_ADC_TUNING2 0x68 +#define RF22_REG_69_AGC_OVERRIDE1 0x69 +#define RF22_REG_6A_AGC_OVERRIDE2 0x6a +#define RF22_REG_6B_GFSK_FIR_FILTER_COEFFICIENT_ADDRESS 0x6b +#define RF22_REG_6C_GFSK_FIR_FILTER_COEFFICIENT_VALUE 0x6c +#define RF22_REG_6D_TX_POWER 0x6d +#define RF22_REG_6E_TX_DATA_RATE 0x6e +#define RF22_REG_6F_TX_DATA_RATE 0x6f +#define RF22_REG_70_MODULATION_CONTROL1 0x70 +#define RF22_REG_71_MODULATION_CONTROL2 0x71 +#define RF22_REG_72_FREQUENCY_DEVIATION 0x72 +#define RF22_REG_73_FREQUENCY_OFFSET1 0x73 +#define RF22_REG_74_FREQUENCY_OFFSET2 0x74 +#define RF22_REG_75_FREQUENCY_BAND_SELECT 0x75 +#define RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1 0x76 +#define RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0 0x77 +#define RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT 0x79 +#define RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE 0x7a +#define RF22_REG_7C_TX_FIFO_CONTROL1 0x7c +#define RF22_REG_7D_TX_FIFO_CONTROL2 0x7d +#define RF22_REG_7E_RX_FIFO_CONTROL 0x7e +#define RF22_REG_7F_FIFO_ACCESS 0x7f \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 EPS.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EPS.cpp Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,376 @@ +#include "EPS.h" +#include "pin_config.h" +/***********************************************global variable declaration***************************************************************/ +extern uint32_t BAE_STATUS; +extern uint32_t BAE_ENABLE; +extern char hk_data[25]; +const char RCOMP0= 0x97; +SensorData Sensor; +SensorDataQuantised SensorQuantised; +ShortBeacy Shortbeacon; + + +/***********************************************Configuring Peripherals*******************************************************************/ +Serial pc_eps(USBTX,USBRX); + +I2C BG_I2C(D14,D15); //i2c btwn bae and battery gauge +DigitalOut SelectLinesA[] = {PIN43,PIN44,PIN45,PIN46}; //to mux1=>voltage mux , PTA 13-16 , CHNGE TO PIN43 LATER +DigitalOut SelectLinesB[] = {PIN56,PIN57,PIN58,PIN59}; //to mux2=>current mux(differential mux) , PTB 3,7,8,9 + //MSB is SelectLines[0],LSB is SelectLines[3] +AnalogIn CurrentInput(PIN53); // output from Current Mux PTB0 +AnalogIn VoltageInput(PIN54); // output from Voltage Multiplexer PTB1 +AnalogIn Vbatt_ang(VBATT); //VBATT of battery gauge + +SPI BTemp_spi(PTD6,PTD7,PTD5); //MOSI,MISO,SLK +DigitalOut BTemp_ssn1(PTD4); //Slave select1 +DigitalOut BTemp_ssn2(PTD2);//Slave select2 +DigitalOut BTemp_PS(PTB0); +DigitalOut BTemp_HS(PTB1); + +DigitalOut TRXY(TRXY_DR_EN); //active high +DigitalOut TRZ(TRZ_DR_EN); //active high +DigitalOut EN3V3A(ENBL3V3A); +DigitalOut EN_BTRY_HT(BATT_HEAT); +//DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT); + +//*********************************************************flags********************************************************// +extern char EPS_INIT_STATUS ; +extern char EPS_BATTERY_GAUGE_STATUS ; +extern char EPS_MAIN_STATUS; +extern char EPS_BATTERY_TEMP_STATUS ; +extern char EPS_STATUS ; + +extern char EPS_BATTERY_HEAT_ENABLE ; + +//----------------------------------------------------EPS INIT---------------------------------------------------------------------------// +void FCTN_EPS_INIT() +{ + printf("\n\r eps init \n"); + EPS_INIT_STATUS = 's' ; //set EPS_INIT_STATUS flag + FLAG(); + FCTN_EPS_BG_INIT(); + FCTN_EPS_BTEMP_INIT(); + EN3V3A = 1; //enable dc dc converter A + char value=BG_alertFlags(); + unsigned short value_u= (short int )value; + value_u &=0x0001; + if(value_u ==0x0001) // battery gauge not initialised + { + Sensor.power_mode = 1; + Sensor.SOC = 80; //dummy + FCTN_EPS_POWERMODE(Sensor.SOC); + EPS_BATTERY_GAUGE_STATUS = 'c'; //clear EPS_BATTERY_GAUGE_STATUS + } + else + { + Sensor.SOC = BG_soc(); + Sensor.SOC = 80; //dummy + Sensor.Vbatt = Vbatt_ang.read()*3.3; + FCTN_EPS_POWERMODE(Sensor.SOC); + EPS_BATTERY_GAUGE_STATUS = 's'; //set EPS_BATTERY_GAUGE_STATUS + } + + EPS_INIT_STATUS = 'c' ; //clear EPS_INIT_STATUS flag + +} + +//---------------------------------------------battery Temp sensor code------------------------------------------------------------------// +void FCTN_EPS_BTEMP_INIT() +{ + BTemp_ssn1=1;BTemp_ssn2=1; + BTemp_PS=0; //power switch control enable + BTemp_HS=0; //heater switch + BTemp_spi.format(8,3); + BTemp_spi.frequency(1000000); +} + +//----------------------------------------------------Battery Gauge code-----------------------------------------------------------------// + +void FCTN_EPS_BG_INIT() +{ + BG_disableSleep(); + BG_disableHibernate(); + BG_socChangeAlertEnabled(true); //enabling alert on soc changing by 1% + BG_emptyAlertThreshold(32);//setting empty alert threshold to 32% soc + BG_vAlertMinMaxThreshold();//set min, max value of Valrt register + BG_vResetThresholdSet();//set threshold voltage for reset + BG_vResetAlertEnabled(true);//enable alert on reset for V < Vreset +} + +unsigned short BG_readReg(char reg) +{ + //Create a temporary buffer + char buff[2] = {0,0}; + + //Select the register + BG_I2C.write(BG_ADDR, ®, 1, true); + + //Read the 16-bit register + BG_I2C.read(BG_ADDR, buff, 2); + + //Return the combined 16-bit value + return (buff[0] << 8) | buff[1]; +} + +void BG_writeReg(char reg, unsigned short data) +{ + //Create a temporary buffer + char buff[3]; + + //Load the register address and 16-bit data + buff[0] = reg; + buff[1] = data >> 8; + buff[2] = data; + + //Write the data + BG_I2C.write(BG_ADDR, buff, 3); +} + + + +// Command the MAX17049 to perform a power-on reset +void BG_reset() +{ + //Write the POR command + BG_writeReg(REG_CMD, 0x5400); +} + +// Command the MAX17049 to perform a QuickStart +void BG_quickStart() +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_MODE); + + //Set the QuickStart bit + value |= (1 << 14); + + //Write the value back out + BG_writeReg(REG_MODE, value); +} + + +//disable sleep +void BG_disableSleep() +{ + unsigned short value = BG_readReg(REG_MODE); + value &= ~(1 << 13); + BG_writeReg(REG_MODE, value); +} + +//disable the hibernate of the MAX17049 +void BG_disableHibernate() +{ + BG_writeReg(REG_HIBRT, 0x0000); +} + + +// Enable or disable the SOC 1% change alert on the MAX17049 +void BG_socChangeAlertEnabled(bool enabled) +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Set or clear the ALSC bit + if (enabled) + value |= (1 << 6); + else + value &= ~(1 << 6); + + //Write the value back out + BG_writeReg(REG_CONFIG, value); +} + +float BG_compensation() +{ + //Read the 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Return only the upper byte + return (char)(value >> 8); +} + +void BG_compensation(char rcomp) +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Update the register value + value &= 0x00FF; + value |= rcomp << 8; + + //Write the value back out + BG_writeReg(REG_CONFIG, value); +} + + +void BG_tempCompensation(float temp) +{ + //Calculate the new RCOMP value + char rcomp; + if (temp > 20.0) { + rcomp = RCOMP0 + (temp - 20.0) * -0.5; + } else { + rcomp = RCOMP0 + (temp - 20.0) * -5.0; + } + + //Update the RCOMP value + BG_compensation(rcomp); +} + +// Determine whether or not the MAX17049 is asserting the ALRT pin +bool BG_alerting() +{ + //Read the 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Return the status of the ALRT bit + if (value & (1 << 5)) + return true; + else + return false; +} + +// Command the MAX17049 to de-assert the ALRT pin +void BG_clearAlert() +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Clear the ALRT bit + value &= ~(1 << 5); + + //Write the value back out + BG_writeReg(REG_CONFIG, value); +} + + +//Set the SOC empty alert threshold of the MAX17049 +void BG_emptyAlertThreshold(char threshold) +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_CONFIG); + + //Update the register value + value &= 0xFFE0; + value |= 32 - threshold; + + //Write the 16-bit register + BG_writeReg(REG_CONFIG, value); +} + +// Set the low and high voltage alert threshold of the MAX17049 +void BG_vAlertMinMaxThreshold() +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_VALRT); + + //Mask off the old value + value = 0x96D2; // threshold is betweeen 6 and 8.4 v + + //Write the 16-bit register + BG_writeReg(REG_VALRT, value); +} + + +// Set the reset voltage threshold of the MAX17049 +void BG_vResetThresholdSet() +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_VRESET_ID); + + //Mask off the old //value + value &= 0x00FF;//Dis=0 + + value |= 0x7C00;//corresponding to 2.5 V + + + //write the 16-bit register + BG_writeReg(REG_VRESET_ID, value); +} + + +// Enable or disable the voltage reset alert on the MAX17049 +void BG_vResetAlertEnabled(bool enabled) +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_STATUS); + + //Set or clear the EnVR bit + if (enabled) + value |= (1 << 14); + else + value &= ~(1 << 14); + + //Write the value back out + BG_writeReg(REG_STATUS, value); +} + +//Get the current alert flags on the MAX17049 +//refer datasheet-status registers section to decode it. +char BG_alertFlags() +{ + //Read the 16-bit register value + unsigned short value = BG_readReg(REG_STATUS); + + //Return only the flag bits + return (value >> 8) & 0x3F; +} + +// Clear all the alert flags on the MAX17049 +void BG_clearAlertFlags() +{ + //Read the current 16-bit register value + unsigned short value = BG_readReg(REG_STATUS); + + //Clear the specified flag bits + value &= ~( 0x3F<< 8); + + //Write the value back out + BG_writeReg(REG_STATUS, value); +} + +// Get the current cell voltage measurement of the MAX17049 +float BG_vcell() +{ + //Read the 16-bit raw Vcell value + unsigned short value = BG_readReg(REG_VCELL); + + //Return Vcell in volts + return value * 0.000078125*2; +} + +// Get the current state of charge measurement of the MAX17049 as a float +float BG_soc() +{ + //Read the 16-bit raw SOC value + unsigned short value = BG_readReg(REG_SOC); + + //Return SOC in percent + return value * 0.00390625; +} + + + +// Get the current C rate measurement of the MAX17049 +float BG_crate() +{ + //Read the 16-bit raw C/Rate value + short value = BG_readReg(REG_CRATE); + + //Return C/Rate in %/hr + return value * 0.208; +} + + +//----------------------------------------------------Power algo code--------------------------------------------------------------------// +void FCTN_EPS_POWERMODE(float soc) //dummy algo +{ + if(soc >= 80) + Sensor.power_mode = 4; + else if(soc >= 70 & soc < 80) + Sensor.power_mode = 3; + else if(soc >= 60 & soc < 70) + Sensor.power_mode = 2; + else if(soc < 60) + Sensor.power_mode = 1; +} \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 EPS.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EPS.h Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,127 @@ +#include "mbed.h" +#include "pin_config.h" +//.............Power switching.......... +#define TRXY_DR_EN PIN82 //torque rod driver enable +#define TRZ_DR_EN PIN88 +#define ENBL3V3A PIN33 +#define VBATT PIN20 +#define BATT_HEAT PIN96 +//#define BATT_HEAT_OUTPUT +//......................HK..................... +#define vstart 3.3 +#define vstep 0.84667 +#define cstart 0.0691 +#define cstep 0.09133 +#define tstart -40 +#define tstep 8.33 +#define rsens 0.025 +#define Bnewvalue_start -100//in microTesla...max possible field is .0001 T +#define Bnewvalue_step 13.333 +#define AngularSpeed_start -10//max possible ang. velocity in space is 10 deg/sec +#define AngularSpeed_step 1.3333 +//...............Battery Gauge................ +#define BG_ADDR (0x6C) //slave address of battery gauge +//I2C register addresses +#define REG_VCELL 0x02 +#define REG_SOC 0x04 +#define REG_MODE 0x06 +#define REG_VERSION 0x08 +#define REG_HIBRT 0x0A +#define REG_CONFIG 0x0C +#define REG_VALRT 0x14 +#define REG_CRATE 0x16 +#define REG_VRESET_ID 0x18 +#define REG_STATUS 0x1A +#define REG_TABLE 0x40 +#define REG_CMD 0xFE +//..............Battery heater............... +#define BHT_LOWTHRESH 30 // to be fixed +#define BHT_HIGHTHRESH 40 // to be fixed + +typedef struct SensorData +{ + float SOC; //added recently + float Vbatt; //added recently + float Voltage[15]; + float Current[7]; + float Temperature[1]; + //float PanelTemperature[4]; // not finalised ///there wer 4 values here? + float BatteryTemperature[2]; + char fault_poll; //polled faults + char fault_ir; //interrupted faults + char clear_fault_poll; //to be confirmed + char clear_fault_ir; + char power_mode; //power modes + float AngularSpeed[3]; //in order x,y,z + float Bnewvalue[3]; //in order Bx,By,Bz +} SensorData; + + +typedef struct SensorDataQuantised +{ + char Voltage[8]; + char Current[4]; + char Temperature[1]; + //char PanelTemperature[2];//read by the 4 thermistors on solar panels + char BatteryTemperature; //to be populated + char faultpoll; //polled faults + char faultir; //interrupted faults + char power_mode; //power modes + char AngularSpeed[2]; + char Bnewvalue[2]; + + //float magnetometer,gyro=>to be addes +} SensorDataQuantised; + + +typedef struct ShortBeacon +{ + char Voltage[1]; //battery voltage from gauge, needs to be quantised + char AngularSpeed[2]; //all the 3 data + char SubsystemStatus[1]; //power modes + char Temp[2]; //temp of solar panel + //Temp[0]'s LSB=> PanelTemperature[0], Temp[0]'s MSB=> PanelTemperature[1], Temp[1]'s LSB=> PanelTemperature[2], Temp[1]'s MSB=> PanelTemperature[3] + char ErrorFlag[1]; //fault +}ShortBeacy; + + +/******************************************************function headers************************************************************************/ +void FCTN_EPS_INIT(); +void FCTN_EPS_BTEMP_INIT(); +void FCTN_EPS_BTEMP_MAIN(float*); +void FCTN_EPS_BTRY_HEATER(); +void FCTN_EPS_BG_INIT(); +void FCTN_EPS_BG_MAIN(); +void FCTN_EPS_POWERMODE(float); +void FCTN_EPS_CTRLPOWER(int) ; +int FCTN_QUANTIZE(float ,float ,float ); +void FCTN_WRITE_BEASTRUCT(ShortBeacy* ,SensorDataQuantised ); +void FCTN_EPS_HK_MAIN(); +void FCTN_HK_DATA_CATENATE(); + +extern void FLAG(); + + +unsigned short BG_readReg(char); +void BG_writeReg(char, unsigned short); +void BG_reset(); //not used in code +void BG_quickStart(); //not used in code +void BG_disableSleep(); +void BG_disableHibernate(); +void BG_socChangeAlertEnabled(bool); +float BG_compensation(); //not used in code +void BG_compensation(char); +void BG_tempCompensation(float); +bool BG_alerting(); +void BG_clearAlert(); +void BG_emptyAlertThreshold(char); +void BG_vAlertMinMaxThreshold(); +void BG_vResetThresholdSet(); +void BG_vResetAlertEnabled(bool); +char BG_alertFlags(); +void BG_clearAlertFlags(); +float BG_vcell(); +float BG_soc(); + + +
diff -r 000000000000 -r 913c9e982740 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,464 @@ +#include "mbed.h" +#include "rtos.h" +#include "pin_config.h" +#include "ACS.h" +#include "EPS.h" +#include "BCN.h" + +#define tm_len 1100 +#define tc_len 150 +#define hk_len 150 + +//***************************************************** flags *************************************************************// +uint32_t BAE_STATUS = 0x00000000; +uint32_t BAE_ENABLE = 0xFFFFFFFF; + +//.........acs...............// +char ACS_INIT_STATUS = 'q'; +char ACS_DATA_ACQ_STATUS = 'q'; +char ACS_ATS_STATUS = 'q'; +char ACS_MAIN_STATUS = 'q'; +char ACS_STATUS = 'q'; + +char ACS_ATS_ENABLE = 'q'; +char ACS_DATA_ACQ_ENABLE = 'q'; +char ACS_STATE = 'q'; + +//.....................eps...................// +char EPS_INIT_STATUS = 'q'; +char EPS_BATTERY_GAUGE_STATUS = 'q'; +char EPS_MAIN_STATUS = 'q'; +char EPS_BATTERY_TEMP_STATUS = 'q'; +char EPS_STATUS = 'q'; + +char EPS_BATTERY_HEAT_ENABLE = 'q'; + +//*************************************Global declarations************************************************// +const int addr = 0x20; //slave address + +Timer t_rfsilence; +Timer t_start; +Serial pc(USBTX, USBRX); +int power_flag_dummy=2; +float data[6]; + +extern float moment[3]; +extern SensorData Sensor; +extern uint8_t BCN_FEN; + +bool write_ack = 1; +bool read_ack = 1; +char hk_data[hk_len]; +char telecommand[tc_len]; +char telemetry[tm_len]; +char data_send_flag = 'h'; + +//*****************************************************Assigning pins******************************************************// +DigitalOut gpo1(PTC0); // enable of att sens2 switch +DigitalOut gpo2(PTC16); // enable of att sens switch +InterruptIn irpt_4m_mstr(PIN38); //I2c interrupt from CDMS +DigitalOut irpt_2_mstr(PIN4); //I2C interrupt to CDMS +I2CSlave slave (PIN1,PIN2); + + +//gpo1 = 0; +PwmOut PWM1(PIN93); //x //Functions used to generate PWM signal +PwmOut PWM2(PIN94); //y +PwmOut PWM3(PIN95); //z //PWM output comes from pins p6 + + +/*****************************************************************Threads USed***********************************************************************************/ +Thread *ptr_t_acs; +Thread *ptr_t_eps; +Thread *ptr_t_bcn; +Thread *ptr_t_i2c; + +/*********************************************************FCTN HEADERS***********************************************************************************/ + +void FCTN_ISR_I2C(); +void FCTN_TM(); + +//*******************************************ACS THREAD**************************************************// + +void T_ACS(void const *args) +{ + float b1[3]={-23.376,-37.56,14.739}, omega1[3]={-1.52,2.746,0.7629}, moment1[3]= {1.0498,-1.0535,1.3246}; + //b1[3] = {22, 22,10}; + //omega1[3] = {2.1,3.0,1.5}; + // gpo1 = 0; // att sens2 switch is disabled + // gpo2 = 0; // att sens switch is disabled + + while(1) + { + + Thread::signal_wait(0x1); + ACS_MAIN_STATUS = 's'; //set ACS_MAIN_STATUS flag + PWM1 = 0; //clear pwm pins + PWM2 = 0; //clear pwm pins + PWM3 = 0; //clear pwm pins + pc.printf("\n\rEntered ACS %f\n",t_start.read()); + + if(ACS_DATA_ACQ_ENABLE == 'e')// check if ACS_DATA_ACQ_ENABLE = 1? + { + FLAG(); + FCTN_ATS_DATA_ACQ(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3 + pc.printf("gyro values\n\r"); //printing the angular velocity and magnetic field values + for(int i=0; i<3; i++) + { + pc.printf("%f\n\r",data[i]); + } + pc.printf("mag values\n\r"); + for(int i=3; i<6; i++) + { + pc.printf("%f\n\r",data[i]); + + for(int i=0;i<3;i++) + { + omega1[i]= data[i]; + b1[i] = data[i+3]; + } + } + }//if ACS_DATA_ACQ_ENABLE = 1 + else + { + // Z axis actuation is the only final solution, + } + if(ACS_STATE == '0') // check ACS_STATE = ACS_CONTROL_OFF? + { + printf("\n\r acs control off\n"); + FLAG(); + ACS_STATUS = '0'; // set ACS_STATUS = ACS_CONTROL_OFF + PWM1 = 0; //clear pwm pins + PWM2 = 0; //clear pwm pins + PWM3 = 0; //clear pwm pins + } + else + { + if(Sensor.power_mode>1) + + { + if(ACS_STATE == '2') // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY + { + FLAG(); + printf("\n\r z axis moment only\n"); + ACS_STATUS = '2'; // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY + // FCTN_ACS_CNTRLALGO(b1, omega1); + moment[0] = 0; + moment[1] = 0; + moment[2] =1.3;// is a dummy value + FCTN_ACS_GENPWM_MAIN(moment) ; + } + else + { + if(ACS_STATE == '3') // check ACS_STATE = ACS_DATA_ACQ_FAILURE + { + FLAG(); + printf("\n\r acs data failure "); + ACS_STATUS = '3'; // set ACS_STATUS = ACS_DATA_ACQ_FAILURE + PWM1 = 0; //clear pwm pins + PWM2 = 0; //clear pwm pins + PWM3 = 0; //clear pwm pins + } + else + { + if(ACS_STATE == '4') // check ACS_STATE = ACS_NOMINAL_ONLY + { + FLAG(); + printf("\n\r nominal"); + ACS_STATUS = '4'; // set ACS_STATUS = ACS_NOMINAL_ONLY + FCTN_ACS_CNTRLALGO(b1,omega1); + printf("\n\r moment values returned by control algo \n"); + for(int i=0; i<3; i++) + { + printf("%f\t",moment[i]); + } + FCTN_ACS_GENPWM_MAIN(moment) ; + } + else + { + if(ACS_STATE == '5') // check ACS_STATE = ACS_AUTO_CONTROL + { + FLAG(); + printf("\n\r auto control"); + ACS_STATUS = '5'; // set ACS_STATUS = ACS_AUTO_CONTROL + //FCTN_ACS_AUTOCTRL_LOGIC // gotta include this code + } + else + { + if(ACS_STATE == '6') // check ACS_STATE = ACS_DETUMBLING_ONLY + { + FLAG(); + printf("\n\r Entered detumbling \n"); + ACS_STATUS = '6'; // set ACS_STATUS = ACS_DETUMBLING_ONLY + FCTN_ACS_CNTRLALGO(b1,omega1); // detumbling code has to be included + FCTN_ACS_GENPWM_MAIN(moment) ; + } + else + { + FLAG(); + printf("\n\r invalid state"); + ACS_STATUS = '7' ; // set ACS_STATUS = INVALID STATE + PWM1 = 0; //clear pwm pins + PWM2 = 0; //clear pwm pins + PWM3 = 0; //clear pwm pins + }//else of invalid + }//else of autocontrol + }//else of nominal + }//else of data acg failure + + }//else fo z axis moment only + }//if power >2 + else + { + FLAG(); + printf("\n\r low power"); + ACS_STATUS = '1'; // set ACS_STATUS = ACS_LOW_POWER + PWM1 = 0; //clear pwm pins + PWM2 = 0; //clear pwm pins + PWM3 = 0; //clear pwm pins + } + } //else for acs control off + + + + ACS_MAIN_STATUS = 'c'; //clear ACS_MAIN_STATUS flag + }//while ends + + +} +//***************************************************EPS THREAD***********************************************// + +void T_EPS(void const *args) +{ + while(1) + { + Thread::signal_wait(0x2); + pc.printf("\n\rEntered EPS %f\n",t_start.read()); + + } + +} + +//**************************************************BCN THREAD*******************************************************************// + +void T_BCN(void const *args) +{ + while(1) + { + Thread::signal_wait(0x3); + pc.printf("\n\rEntered BCN %f\n",t_start.read()); + + P_BCN_TX_MAIN(); + } + +} + +//**************************************************TCTM THREAD*******************************************************************// + +void T_TC(void const * args) +{ + while(1) + { + Thread::signal_wait(0x1); + pc.printf("\n\rTELECOMMAND received from CDMS is %s \n",telecommand); + pc.printf("\n\r Executing Telecommand \n"); + FCTN_TM(); + } +} + +void FCTN_TM() +{ + pc.printf("\n\r Telemetry Generation \n"); + data_send_flag = 't'; + irpt_2_mstr = 1; + wait_us(100); + irpt_2_mstr = 0; + + +} + + +//******************************************************* I2C *******************************************************************// + +void FCTN_I2C_ISR() +{ + wait_us(200); // can be between 38 to 15700 + if( slave.receive() == 0) + slave.stop(); + else if( slave.receive() == 1) // slave writes to master + { + if(data_send_flag == 'h') + write_ack=slave.write(hk_data,hk_len); + else if(data_send_flag == 't') + { + write_ack=slave.write(telemetry,tm_len); + data_send_flag = 'h'; + } + + } + else if( slave.receive()==3 || slave.receive()==2) // slave read + { + read_ack=slave.read(telecommand,tc_len); + ptr_t_i2c->signal_set(0x1); + } +} + + +//------------------------------------------------------------------------------------------------------------------------------------------------ +//SCHEDULER +//------------------------------------------------------------------------------------------------------------------------------------------------ +uint8_t schedcount=1; +void T_SC(void const *args) +{ + printf("\n\r in scheduler"); + + if(schedcount == 7) //to reset the counter + { + schedcount = 1; + } + if(schedcount%1==0) + { + ptr_t_acs -> signal_set(0x1); + } + + if(schedcount%2==0) + { + // ptr_t_eps -> signal_set(0x2); + + } + if(schedcount%3==0) + { + //ptr_t_bcn -> signal_set(0x3); + } + schedcount++; + printf("\n\r exited scheduler"); +} + +Timer t_flag; +void FLAG() +{ + +//.............acs..................// + if(ACS_INIT_STATUS == 's') + BAE_STATUS = BAE_STATUS | 0x00000080; //set ACS_INIT_STATUS flag + else if(ACS_INIT_STATUS == 'c') + BAE_STATUS &= 0xFFFFFF7F; //clear ACS_INIT_STATUS flag + + if(ACS_DATA_ACQ_STATUS == 's') + BAE_STATUS =BAE_STATUS | 0x00000100; //set ACS_DATA_ACQ_STATUS flag + else if(ACS_DATA_ACQ_STATUS == 'c') + BAE_STATUS &= 0xFFFFFEFF; //clear ACS_DATA_ACQ_STATUS flag + + if(ACS_ATS_ENABLE == 'e') + BAE_ENABLE |= 0x00000004; + else if(ACS_ATS_ENABLE == 'd') + BAE_ENABLE = BAE_ENABLE &0xFFFFFFFB | 0x00000004; + + if(ACS_DATA_ACQ_STATUS == 'f') + BAE_STATUS |= 0x00000200; + + if(ACS_MAIN_STATUS == 's') + BAE_STATUS = (BAE_STATUS | 0x00001000); //set ACS_MAIN_STATUS flag + else if(ACS_MAIN_STATUS == 'c') + BAE_STATUS &= 0xFFFFEFFF; //clear ACS_MAIN_STATUS flag + + if(ACS_STATUS == '0') + BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF); // set ACS_STATUS = ACS_CONTROL_OFF + else if(ACS_STATUS == '1') + BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x00002000; // set ACS_STATUS = ACS_LOW_POWER + else if(ACS_STATUS == '2') + BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF)| 0x00004000; // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY + else if(ACS_STATUS == '3') + BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00006000; // set ACS_STATUS = ACS_DATA_ACQ_FAILURE + else if(ACS_STATUS == '4') + BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00008000; // set ACS_STATUS = ACS_NOMINAL_ONLY + else if(ACS_STATUS == '5') + BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000A000; // set ACS_STATUS = ACS_AUTO_CONTROL + else if(ACS_STATUS == '6') + BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000C000; // set ACS_STATUS = ACS_DETUMBLING_ONLY + else + BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000E000; // set ACS_STATUS = INVALID STATE + + if(ACS_STATE == '0') + BAE_ENABLE = (BAE_ENABLE & 0xFFFFFF8F); //ACS_STATE = ACS_CONTROL_OFF + else if(ACS_STATE == '2') + BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000020); // ACS_STATE = ACS_ZAXIS_MOMENT_ONLY + else if(ACS_STATE == '3') + BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000030); // set ACS_STATUS = ACS_DATA_ACQ_FAILURE + else if(ACS_STATE == '4') + BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000040); // ACS_STATE = ACS_NOMINAL_ONLY + else if(ACS_STATE == '5') + BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000050); // ACS_STATE = ACS_AUTO_CONTROL + else if(ACS_STATE == '6') + BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000060); //ACS_STATE = ACS_DETUMBLING_CONTROL + +//...............eps......................// + if(EPS_INIT_STATUS == 's') + BAE_STATUS |= 0x00010000; //set EPS_INIT_STATUS flag + else if(EPS_INIT_STATUS == 'c') + BAE_STATUS &= 0xFFFEFFFF; //clear EPS_INIT_STATUS flag + if(EPS_BATTERY_GAUGE_STATUS == 'c') + BAE_STATUS &= 0xFFFDFFFF; //clear EPS_BATTERY_GAUGE_STATUS + else if(EPS_BATTERY_GAUGE_STATUS == 's') + BAE_STATUS |= 0x00020000; //set EPS_BATTERY_GAUGE_STATUS + + + pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE); +} + +void FCTN_BAE_INIT() +{ + printf("\n\r Initialising BAE "); + FCTN_ACS_INIT(); + FCTN_EPS_INIT(); + //P_BCN_INIT(); + FLAG(); +} + +int main() +{ + pc.printf("\n\r BAE Activated. Testing Version 1.1 \n"); + + /* if (BCN_FEN == 0) //dummy implementation + { + pc.printf("\n\r RF silence "); + P_BCN_FEN(); + t_rfsilence.start();//Start the timer for RF_Silence + while(t_rfsilence.read() < RF_SILENCE_TIME); + } + */ + ACS_STATE = '0'; + //ACS_INIT_STATUS = 'c'; + //ACS_DATA_ACQ_STATUS = 'c'; + gpo1 = 0; + FLAG(); + FCTN_BAE_INIT(); + ACS_ATS_ENABLE = 'e'; + ACS_DATA_ACQ_ENABLE = 'e'; + + //...i2c.. + strcpy(telemetry,"This is telemetry THis is sample telemetry. fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff end"); + strcpy(hk_data," This is hk hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh h end"); + slave.address(addr); + irpt_2_mstr = 0; + + ptr_t_i2c = new Thread(T_TC); + ptr_t_i2c->set_priority(osPriorityHigh); + ptr_t_acs = new Thread(T_ACS); + ptr_t_acs->set_priority(osPriorityAboveNormal); + ptr_t_eps = new Thread(T_EPS); + ptr_t_eps->set_priority(osPriorityAboveNormal); + ptr_t_bcn = new Thread(T_BCN); + ptr_t_bcn->set_priority(osPriorityAboveNormal); + + irpt_4m_mstr.enable_irq(); + irpt_4m_mstr.rise(&FCTN_I2C_ISR); + RtosTimer t_sc_timer(T_SC,osTimerPeriodic); // Initiating the scheduler thread + t_sc_timer.start(10000); + t_start.start(); + pc.printf("\n\rStarted scheduler %f\n\r",t_start.read()); + + gpo1 = 0; // att sens2 switch is enabled + //FCTN_BAE_INIT(); + while(1); //required to prevent main from terminating +} \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#12552ef4e980
diff -r 000000000000 -r 913c9e982740 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 pin_config.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pin_config.h Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,101 @@ +// 100 LQFP format pin assignment +#define PIN1 PTE0 +#define PIN2 PTE1 +#define PIN3 PTE2 +#define PIN4 PTE3 +#define PIN5 PTE4 +#define PIN6 PTE5 +#define PIN7 PTE6 +//#define 8 +//#define 9 +//#define 10 +//#define 11 +//#define 12 +//#define 13 +#define PIN14 PTE16 +#define PIN15 PTE17 +#define PIN16 PTE18 +#define PIN17 PTE19 +#define PIN18 PTE20 +#define PIN19 PTE21 +#define PIN20 PTE22 +#define PIN21 PTE23 +//#define 22 +//#define 23 +//#define 24 +//#define 25 +#define PIN26 PTE29 +#define PIN27 PTE30 +#define PIN28 PTE31 +//#define 29 +//#define 30 +#define PIN31 PTE24 +#define PIN32 PTE25 +#define PIN33 PTE26 +#define PIN34 PTA0 +#define PIN35 PTA1 +#define PIN36 PTA2 +#define PIN37 PTA3 +#define PIN38 PTA4 +#define PIN39 PTA5 +#define PIN40 PTA6 +#define PIN41 PTA7 +#define PIN42 PTA12 +#define PIN43 PTA13 +#define PIN44 PTA14 +#define PIN45 PTA15 +#define PIN46 PTA16 +#define PIN47 PTA17 +//#define 48 +//#define 49 +#define PIN50 PTA18 +#define PIN51 PTA19 +#define PIN52 PTA20 +#define PIN53 PTB0 +#define PIN54 PTB1 +#define PIN55 PTB2 +#define PIN56 PTB3 +#define PIN57 PTB7 +#define PIN58 PTB8 +#define PIN59 PTB9 +#define PIN60 PTB10 +#define PIN61 PTB11 +#define PIN62 PTB16 +#define PIN63 PTB17 +#define PIN64 PTB18 +#define PIN65 PTB19 +#define PIN66 PTB20 +#define PIN67 PTB21 +#define PIN68 PTB22 +#define PIN69 PTB23 +#define PIN70 PTC0 +#define PIN71 PTC1 +#define PIN72 PTC2 +#define PIN73 PTC3 +//#define 74 +//#define 75 +#define PIN76 PTC20 +#define PIN77 PTC21 +#define PIN78 PTC22 +#define PIN79 PTC23 +#define PIN80 PTC4 +#define PIN81 PTC5 +#define PIN82 PTC6 +#define PIN83 PTC7 +#define PIN84 PTC8 +#define PIN85 PTC9 +#define PIN86 PTC10 +#define PIN87 PTC11 +#define PIN88 PTC12 +#define PIN89 PTC13 +#define PIN90 PTC16 +#define PIN91 PTC17 +#define PIN92 PTC18 +#define PIN93 PTD0 +#define PIN94 PTD1 +#define PIN95 PTD2 +#define PIN96 PTD3 +#define PIN97 PTD4 +#define PIN98 PTD5 +#define PIN99 PTD6 +#define PIN100 PTD7 \ No newline at end of file
diff -r 000000000000 -r 913c9e982740 pni.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pni.h Tue Nov 03 14:46:51 2015 +0000 @@ -0,0 +1,37 @@ +#define SLAVE_ADDR 0x50 +#define SLAVE_ADDR_READ 0x51 +#define SENTRALSTATUS 0x37 +#define RESETREQ 0x9B +#define MAGRATE 0x55 +#define ACCERATE 0x56 +#define GYRORATE 0x57 +#define QRATE_DIV 0x32 +#define ALGO_CTRL 0x54 +#define ENB_EVT 0x33 +#define HOST_CTRL 0x34 +#define EVT_STATUS 0x35 +#define ALGO_STATUS 0x38 +#define GYRO_XOUT_H 0x22 +#define MAG_XOUT_H 0X12 + +//Configaration bits +#define BIT_RESREQ 0x01 +#define BIT_EEP_DET 0x01 +#define BIT_EEP_UPDN 0x02 +#define BIT_EEP_UPERR 0x04 +#define BIT_EEP_IDLE 0x08 +#define BIT_EEP_NODET 0x10 +#define BIT_STBY 0x01 +#define BIT_RAW_ENB 0x02 +#define BIT_HPR_OUT 0x04 +#define BIT_CPU_RES 0x01 +#define BIT_ERR 0x02 +#define BIT_QRES 0x04 +#define BIT_MAG_RES 0x08 +#define BIT_ACC_RES 0x10 +#define BIT_GYRO_RES 0x20 +#define BIT_GYROODR 0x0F +#define BIT_MAGODR 0x64 +#define BIT_RUN_ENB 0x01 +#define BIT_ALGO_RAW 0x02 +#define BIT_EVT_ENB 0X2A \ No newline at end of file