Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BAE_hw_test1_2 by
mnm.cpp
00001 #include<mbed.h> 00002 #include "mnm.h" 00003 #include "pni.h" //pni header file 00004 Serial mnm(USBTX,USBRX); //for usb communication 00005 I2C i2c (PTC9,PTC8); //PTC2-sda,PTC1-scl 00006 /*void INIT_PNI(void); //initialization of registers happens 00007 float *EXECUTE_PNI(); //data is obtained 00008 void T_OUT(); //timeout function to stop infinite loop*/ 00009 Timeout to; //Timeout variable to 00010 int toFlag; 00011 void T_OUT() 00012 { 00013 toFlag=0; //as T_OUT function gets called the while loop gets terminated 00014 } 00015 00016 //DEFINING VARIABLES 00017 char cmd[2]; 00018 char raw_gyro[6]; 00019 char raw_mag[6]; 00020 char store,status; 00021 int16_t bit_data; 00022 float gyro_data[3], mag_data[3],combined_values[6],*data; 00023 float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps 00024 float senstivity_mag =32.768; //senstivity is obtained from 2^15/1000microtesla 00025 float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0}; 00026 00027 Timer r0; 00028 Timer r1; 00029 Timer r2; 00030 Timer r3; 00031 Timer r4; 00032 00033 00034 /* 00035 int main(void) 00036 { 00037 00038 INIT_PNI(); 00039 data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3 00040 mnm.printf("gyro values\n"); //printing the angular velocity and magnetic field values 00041 for(int i=0; i<3; i++) { 00042 mnm.printf("%f\t",data[i]); 00043 } 00044 mnm.printf("mag values\n"); 00045 for(int i=3; i<6; i++) { 00046 mnm.printf("%f\t",data[i]); 00047 } 00048 }*/ 00049 00050 void INIT_PNI() 00051 { r0.start(); 00052 cmd[0]=RESETREQ; 00053 cmd[1]=BIT_RESREQ; 00054 i2c.write(SLAVE_ADDR,cmd,2); 00055 r0.stop(); //When 0x01 is written in reset request register Emulates a hard power down/power up 00056 wait_ms(2000); //waiting for loading configuration file stored in EEPROM 00057 cmd[0]=SENTRALSTATUS; 00058 r1.start(); 00059 i2c.write(SLAVE_ADDR,cmd,1); 00060 i2c.read(SLAVE_ADDR_READ,&store,1); 00061 r1.stop(); 00062 wait_ms(100); 00063 //to check whether EEPROM is uploaded 00064 00065 /* switch((int)store) { 00066 case(3): { 00067 printf("\nstore :%d\n",store); 00068 break; 00069 } 00070 case(11): { 00071 printf("\nstore11 :%d\n",store); 00072 break; 00073 } 00074 default: { 00075 cmd[0]=RESETREQ; 00076 cmd[1]=BIT_RESREQ; 00077 i2c.write(SLAVE_ADDR,cmd,2); 00078 wait_ms(2000); 00079 } 00080 }*/ 00081 //mnm.printf("Sentral Status is %x\n",(int)store); 00082 r2.start(); 00083 cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors 00084 cmd[1]=BIT_RUN_ENB; 00085 i2c.write(SLAVE_ADDR,cmd,2); 00086 r2.stop(); 00087 wait_ms(100); 00088 r3.start(); 00089 cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer 00090 cmd[1]=BIT_MAGODR; 00091 i2c.write(SLAVE_ADDR,cmd,2); 00092 r3.stop(); 00093 wait_ms(100); 00094 r4.start(); 00095 cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope 00096 cmd[1]=BIT_GYROODR; 00097 i2c.write(SLAVE_ADDR,cmd,2); 00098 r4.stop(); 00099 wait_ms(100); 00100 cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values 00101 cmd[1]=0x00; 00102 i2c.write(SLAVE_ADDR,cmd,2); 00103 wait_ms(100); 00104 cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values 00105 cmd[1]=BIT_EVT_ENB; 00106 i2c.write(SLAVE_ADDR,cmd,2); 00107 wait_ms(100); 00108 printf("\n \r %d %d %d %d %d",r0.read_us(),r1.read_us(),r2.read_us(),r3.read_us(),r4.read_us()); 00109 } 00110 00111 float *EXECUTE_PNI() 00112 { 00113 //printf("\n\r mnm func \n"); 00114 toFlag=1; //toFlag is set to 1 so that it enters while loop 00115 to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated 00116 while(toFlag) { 00117 cmd[0]=EVT_STATUS; 00118 i2c.write(SLAVE_ADDR,cmd,1); 00119 i2c.read(SLAVE_ADDR_READ,&status,1); 00120 wait_ms(100); 00121 //mnm.printf("\nEvent Status is %x\n",(int)status); 00122 //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take 00123 if(((int)status&40)==40) { 00124 printf("\nin if of mnm\n"); 00125 cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x 00126 i2c.write(SLAVE_ADDR,cmd,1); 00127 i2c.read(SLAVE_ADDR_READ,raw_gyro,6); 00128 cmd[0]=MAG_XOUT_H; //LSB of x 00129 i2c.write(SLAVE_ADDR,cmd,1); 00130 i2c.read(SLAVE_ADDR_READ,raw_mag,6); 00131 //mnm.printf("\nGyro Values:\n"); 00132 for(int i=0; i<3; i++) { 00133 //concatenating gyro LSB and MSB to get 16 bit signed data values 00134 bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i]; 00135 gyro_data[i]=(float)bit_data; 00136 gyro_data[i]=gyro_data[i]/senstivity_gyro; 00137 gyro_data[i]+=gyro_error[i]; 00138 //mnm.printf("%f\t",gyro_data[i]); 00139 } 00140 //mnm.printf("\nMag Values:\n"); 00141 for(int i=0; i<3; i++) { 00142 //concatenating mag LSB and MSB to get 16 bit signed data values 00143 bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i]; 00144 mag_data[i]=(float)bit_data; 00145 mag_data[i]=mag_data[i]/senstivity_mag; 00146 mag_data[i]+=mag_error[i]; 00147 //mnm.printf("%f\t",mag_data[i]); 00148 } 00149 for(int i=0; i<3; i++) { 00150 combined_values[i]=gyro_data[i]; 00151 combined_values[i+3]=mag_data[i]; 00152 } 00153 return(combined_values); //returning poiter combined values 00154 } 00155 //checking for the error 00156 00157 else if (((int)status&2)==2) { 00158 INIT_PNI(); //when there is any error then Again inilization is done to remove error 00159 } 00160 00161 00162 } 00163 }
Generated on Mon Jul 25 2022 05:05:59 by
1.7.2
