sakthi priya amirtharaj / Mbed 2 deprecated pcb_test_v1_1_1

Dependencies:   mbed-rtos mbed

Fork of pcb_test_v1_1 by sakthi priya amirtharaj

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mnm.cpp Source File

mnm.cpp

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