![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sw_test_workin
Fork of BAE_icecream_1_0 by
mnm.cpp@7:603c2a89effc, 2015-02-27 (annotated)
- Committer:
- sakthipriya
- Date:
- Fri Feb 27 19:06:04 2015 +0000
- Revision:
- 7:603c2a89effc
- Parent:
- 5:4199c0dfed33
jhyyrf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 5:4199c0dfed33 | 1 | #include<mbed.h> |
sakthipriya | 5:4199c0dfed33 | 2 | #include "mnm.h" |
sakthipriya | 5:4199c0dfed33 | 3 | #include "pni.h" //pni header file |
sakthipriya | 5:4199c0dfed33 | 4 | Serial mnm(USBTX,USBRX); //for usb communication |
sakthipriya | 5:4199c0dfed33 | 5 | I2C i2c (PTC9,PTC8); //PTC2-sda,PTC1-scl |
sakthipriya | 5:4199c0dfed33 | 6 | /*void INIT_PNI(void); //initialization of registers happens |
sakthipriya | 5:4199c0dfed33 | 7 | float *EXECUTE_PNI(); //data is obtained |
sakthipriya | 5:4199c0dfed33 | 8 | void T_OUT(); //timeout function to stop infinite loop*/ |
sakthipriya | 5:4199c0dfed33 | 9 | Timeout to; //Timeout variable to |
sakthipriya | 5:4199c0dfed33 | 10 | int toFlag; |
sakthipriya | 5:4199c0dfed33 | 11 | void T_OUT() |
sakthipriya | 5:4199c0dfed33 | 12 | { |
sakthipriya | 5:4199c0dfed33 | 13 | toFlag=0; //as T_OUT function gets called the while loop gets terminated |
sakthipriya | 5:4199c0dfed33 | 14 | } |
sakthipriya | 5:4199c0dfed33 | 15 | |
sakthipriya | 5:4199c0dfed33 | 16 | //DEFINING VARIABLES |
sakthipriya | 5:4199c0dfed33 | 17 | char cmd[2]; |
sakthipriya | 5:4199c0dfed33 | 18 | char raw_gyro[6]; |
sakthipriya | 5:4199c0dfed33 | 19 | char raw_mag[6]; |
sakthipriya | 5:4199c0dfed33 | 20 | char store,status; |
sakthipriya | 5:4199c0dfed33 | 21 | int16_t bit_data; |
sakthipriya | 5:4199c0dfed33 | 22 | float gyro_data[3], mag_data[3],combined_values[6],*data; |
sakthipriya | 5:4199c0dfed33 | 23 | float senstivity_gyro =6.5536; //senstivity is obtained from 2^15/5000dps |
sakthipriya | 5:4199c0dfed33 | 24 | float senstivity_mag =32.768; //senstivity is obtained from 2^15/1000microtesla |
sakthipriya | 5:4199c0dfed33 | 25 | float gyro_error[3]= {0,0,0}, mag_error[3]= {0,0,0}; |
sakthipriya | 7:603c2a89effc | 26 | |
sakthipriya | 7:603c2a89effc | 27 | Timer r0; |
sakthipriya | 7:603c2a89effc | 28 | Timer r1; |
sakthipriya | 7:603c2a89effc | 29 | Timer r2; |
sakthipriya | 7:603c2a89effc | 30 | Timer r3; |
sakthipriya | 7:603c2a89effc | 31 | Timer r4; |
sakthipriya | 7:603c2a89effc | 32 | |
sakthipriya | 7:603c2a89effc | 33 | |
sakthipriya | 5:4199c0dfed33 | 34 | /* |
sakthipriya | 5:4199c0dfed33 | 35 | int main(void) |
sakthipriya | 5:4199c0dfed33 | 36 | { |
sakthipriya | 5:4199c0dfed33 | 37 | |
sakthipriya | 5:4199c0dfed33 | 38 | INIT_PNI(); |
sakthipriya | 5:4199c0dfed33 | 39 | data=EXECUTE_PNI(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3 |
sakthipriya | 5:4199c0dfed33 | 40 | mnm.printf("gyro values\n"); //printing the angular velocity and magnetic field values |
sakthipriya | 5:4199c0dfed33 | 41 | for(int i=0; i<3; i++) { |
sakthipriya | 5:4199c0dfed33 | 42 | mnm.printf("%f\t",data[i]); |
sakthipriya | 5:4199c0dfed33 | 43 | } |
sakthipriya | 5:4199c0dfed33 | 44 | mnm.printf("mag values\n"); |
sakthipriya | 5:4199c0dfed33 | 45 | for(int i=3; i<6; i++) { |
sakthipriya | 5:4199c0dfed33 | 46 | mnm.printf("%f\t",data[i]); |
sakthipriya | 5:4199c0dfed33 | 47 | } |
sakthipriya | 5:4199c0dfed33 | 48 | }*/ |
sakthipriya | 5:4199c0dfed33 | 49 | |
sakthipriya | 5:4199c0dfed33 | 50 | void INIT_PNI() |
sakthipriya | 7:603c2a89effc | 51 | { r0.start(); |
sakthipriya | 5:4199c0dfed33 | 52 | cmd[0]=RESETREQ; |
sakthipriya | 5:4199c0dfed33 | 53 | cmd[1]=BIT_RESREQ; |
sakthipriya | 7:603c2a89effc | 54 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 7:603c2a89effc | 55 | r0.stop(); //When 0x01 is written in reset request register Emulates a hard power down/power up |
sakthipriya | 5:4199c0dfed33 | 56 | wait_ms(2000); //waiting for loading configuration file stored in EEPROM |
sakthipriya | 5:4199c0dfed33 | 57 | cmd[0]=SENTRALSTATUS; |
sakthipriya | 7:603c2a89effc | 58 | r1.start(); |
sakthipriya | 5:4199c0dfed33 | 59 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 5:4199c0dfed33 | 60 | i2c.read(SLAVE_ADDR_READ,&store,1); |
sakthipriya | 7:603c2a89effc | 61 | r1.stop(); |
sakthipriya | 5:4199c0dfed33 | 62 | wait_ms(100); |
sakthipriya | 5:4199c0dfed33 | 63 | //to check whether EEPROM is uploaded |
sakthipriya | 7:603c2a89effc | 64 | |
sakthipriya | 7:603c2a89effc | 65 | /* switch((int)store) { |
sakthipriya | 5:4199c0dfed33 | 66 | case(3): { |
sakthipriya | 7:603c2a89effc | 67 | printf("\nstore :%d\n",store); |
sakthipriya | 5:4199c0dfed33 | 68 | break; |
sakthipriya | 5:4199c0dfed33 | 69 | } |
sakthipriya | 5:4199c0dfed33 | 70 | case(11): { |
sakthipriya | 7:603c2a89effc | 71 | printf("\nstore11 :%d\n",store); |
sakthipriya | 5:4199c0dfed33 | 72 | break; |
sakthipriya | 5:4199c0dfed33 | 73 | } |
sakthipriya | 5:4199c0dfed33 | 74 | default: { |
sakthipriya | 5:4199c0dfed33 | 75 | cmd[0]=RESETREQ; |
sakthipriya | 5:4199c0dfed33 | 76 | cmd[1]=BIT_RESREQ; |
sakthipriya | 5:4199c0dfed33 | 77 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 5:4199c0dfed33 | 78 | wait_ms(2000); |
sakthipriya | 5:4199c0dfed33 | 79 | } |
sakthipriya | 7:603c2a89effc | 80 | }*/ |
sakthipriya | 5:4199c0dfed33 | 81 | //mnm.printf("Sentral Status is %x\n",(int)store); |
sakthipriya | 7:603c2a89effc | 82 | r2.start(); |
sakthipriya | 5:4199c0dfed33 | 83 | cmd[0]=HOST_CTRL; //0x01 is written in HOST CONTROL register to enable the sensors |
sakthipriya | 5:4199c0dfed33 | 84 | cmd[1]=BIT_RUN_ENB; |
sakthipriya | 5:4199c0dfed33 | 85 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 7:603c2a89effc | 86 | r2.stop(); |
sakthipriya | 5:4199c0dfed33 | 87 | wait_ms(100); |
sakthipriya | 7:603c2a89effc | 88 | r3.start(); |
sakthipriya | 5:4199c0dfed33 | 89 | cmd[0]=MAGRATE; //Output data rate of 100Hz is used for magnetometer |
sakthipriya | 5:4199c0dfed33 | 90 | cmd[1]=BIT_MAGODR; |
sakthipriya | 5:4199c0dfed33 | 91 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 7:603c2a89effc | 92 | r3.stop(); |
sakthipriya | 7:603c2a89effc | 93 | wait_ms(100); |
sakthipriya | 7:603c2a89effc | 94 | r4.start(); |
sakthipriya | 5:4199c0dfed33 | 95 | cmd[0]=GYRORATE; //Output data rate of 150Hz is used for gyroscope |
sakthipriya | 5:4199c0dfed33 | 96 | cmd[1]=BIT_GYROODR; |
sakthipriya | 5:4199c0dfed33 | 97 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 7:603c2a89effc | 98 | r4.stop(); |
sakthipriya | 7:603c2a89effc | 99 | wait_ms(100); |
sakthipriya | 5:4199c0dfed33 | 100 | cmd[0]=ALGO_CTRL; //When 0x00 is written to ALGO CONTROL register we get scaled sensor values |
sakthipriya | 5:4199c0dfed33 | 101 | cmd[1]=0x00; |
sakthipriya | 5:4199c0dfed33 | 102 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 5:4199c0dfed33 | 103 | wait_ms(100); |
sakthipriya | 5:4199c0dfed33 | 104 | cmd[0]=ENB_EVT; //enabling the error,gyro values and magnetometer values |
sakthipriya | 5:4199c0dfed33 | 105 | cmd[1]=BIT_EVT_ENB; |
sakthipriya | 5:4199c0dfed33 | 106 | i2c.write(SLAVE_ADDR,cmd,2); |
sakthipriya | 5:4199c0dfed33 | 107 | wait_ms(100); |
sakthipriya | 7:603c2a89effc | 108 | printf("\n \r %d %d %d %d %d",r0.read_us(),r1.read_us(),r2.read_us(),r3.read_us(),r4.read_us()); |
sakthipriya | 5:4199c0dfed33 | 109 | } |
sakthipriya | 5:4199c0dfed33 | 110 | |
sakthipriya | 5:4199c0dfed33 | 111 | float *EXECUTE_PNI() |
sakthipriya | 5:4199c0dfed33 | 112 | { |
sakthipriya | 5:4199c0dfed33 | 113 | //printf("\n\r mnm func \n"); |
sakthipriya | 5:4199c0dfed33 | 114 | toFlag=1; //toFlag is set to 1 so that it enters while loop |
sakthipriya | 5:4199c0dfed33 | 115 | to.attach(&T_OUT,2); //after 2 seconds the while loop gets terminated |
sakthipriya | 5:4199c0dfed33 | 116 | while(toFlag) { |
sakthipriya | 5:4199c0dfed33 | 117 | cmd[0]=EVT_STATUS; |
sakthipriya | 5:4199c0dfed33 | 118 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 5:4199c0dfed33 | 119 | i2c.read(SLAVE_ADDR_READ,&status,1); |
sakthipriya | 5:4199c0dfed33 | 120 | wait_ms(100); |
sakthipriya | 5:4199c0dfed33 | 121 | //mnm.printf("\nEvent Status is %x\n",(int)status); |
sakthipriya | 5:4199c0dfed33 | 122 | //if the 6th and 4th bit are 1 then it implies that gyro and magnetometer values are ready to take |
sakthipriya | 5:4199c0dfed33 | 123 | if(((int)status&40)==40) { |
sakthipriya | 5:4199c0dfed33 | 124 | printf("\nin if of mnm\n"); |
sakthipriya | 5:4199c0dfed33 | 125 | cmd[0]=GYRO_XOUT_H; //0x22 gyro LSB of x |
sakthipriya | 5:4199c0dfed33 | 126 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 5:4199c0dfed33 | 127 | i2c.read(SLAVE_ADDR_READ,raw_gyro,6); |
sakthipriya | 5:4199c0dfed33 | 128 | cmd[0]=MAG_XOUT_H; //LSB of x |
sakthipriya | 5:4199c0dfed33 | 129 | i2c.write(SLAVE_ADDR,cmd,1); |
sakthipriya | 5:4199c0dfed33 | 130 | i2c.read(SLAVE_ADDR_READ,raw_mag,6); |
sakthipriya | 5:4199c0dfed33 | 131 | //mnm.printf("\nGyro Values:\n"); |
sakthipriya | 5:4199c0dfed33 | 132 | for(int i=0; i<3; i++) { |
sakthipriya | 5:4199c0dfed33 | 133 | //concatenating gyro LSB and MSB to get 16 bit signed data values |
sakthipriya | 5:4199c0dfed33 | 134 | bit_data= ((int16_t)raw_gyro[2*i+1]<<8)|(int16_t)raw_gyro[2*i]; |
sakthipriya | 5:4199c0dfed33 | 135 | gyro_data[i]=(float)bit_data; |
sakthipriya | 5:4199c0dfed33 | 136 | gyro_data[i]=gyro_data[i]/senstivity_gyro; |
sakthipriya | 5:4199c0dfed33 | 137 | gyro_data[i]+=gyro_error[i]; |
sakthipriya | 5:4199c0dfed33 | 138 | //mnm.printf("%f\t",gyro_data[i]); |
sakthipriya | 5:4199c0dfed33 | 139 | } |
sakthipriya | 5:4199c0dfed33 | 140 | //mnm.printf("\nMag Values:\n"); |
sakthipriya | 5:4199c0dfed33 | 141 | for(int i=0; i<3; i++) { |
sakthipriya | 5:4199c0dfed33 | 142 | //concatenating mag LSB and MSB to get 16 bit signed data values |
sakthipriya | 5:4199c0dfed33 | 143 | bit_data= ((int16_t)raw_mag[2*i+1]<<8)|(int16_t)raw_mag[2*i]; |
sakthipriya | 5:4199c0dfed33 | 144 | mag_data[i]=(float)bit_data; |
sakthipriya | 5:4199c0dfed33 | 145 | mag_data[i]=mag_data[i]/senstivity_mag; |
sakthipriya | 5:4199c0dfed33 | 146 | mag_data[i]+=mag_error[i]; |
sakthipriya | 5:4199c0dfed33 | 147 | //mnm.printf("%f\t",mag_data[i]); |
sakthipriya | 5:4199c0dfed33 | 148 | } |
sakthipriya | 5:4199c0dfed33 | 149 | for(int i=0; i<3; i++) { |
sakthipriya | 5:4199c0dfed33 | 150 | combined_values[i]=gyro_data[i]; |
sakthipriya | 5:4199c0dfed33 | 151 | combined_values[i+3]=mag_data[i]; |
sakthipriya | 5:4199c0dfed33 | 152 | } |
sakthipriya | 5:4199c0dfed33 | 153 | return(combined_values); //returning poiter combined values |
sakthipriya | 5:4199c0dfed33 | 154 | } |
sakthipriya | 5:4199c0dfed33 | 155 | //checking for the error |
sakthipriya | 5:4199c0dfed33 | 156 | |
sakthipriya | 5:4199c0dfed33 | 157 | else if (((int)status&2)==2) { |
sakthipriya | 5:4199c0dfed33 | 158 | INIT_PNI(); //when there is any error then Again inilization is done to remove error |
sakthipriya | 5:4199c0dfed33 | 159 | } |
sakthipriya | 5:4199c0dfed33 | 160 | |
sakthipriya | 5:4199c0dfed33 | 161 | |
sakthipriya | 5:4199c0dfed33 | 162 | } |
sakthipriya | 5:4199c0dfed33 | 163 | } |