Suchakhree Srisukprom
/
Test_Megre
k
GetAcceloroY.cpp@1:426fbd0d126a, 2015-12-09 (annotated)
- Committer:
- Suchakhree
- Date:
- Wed Dec 09 10:19:11 2015 +0000
- Revision:
- 1:426fbd0d126a
????????????????? ????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Suchakhree | 1:426fbd0d126a | 1 | /* |
Suchakhree | 1:426fbd0d126a | 2 | //int main() |
Suchakhree | 1:426fbd0d126a | 3 | t.start(); |
Suchakhree | 1:426fbd0d126a | 4 | //___ Set up I2C: use fast (400 kHz) I2C ___ |
Suchakhree | 1:426fbd0d126a | 5 | i2c.frequency(400000); |
Suchakhree | 1:426fbd0d126a | 6 | // Read the WHO_AM_I register, this is a good test of communication |
Suchakhree | 1:426fbd0d126a | 7 | whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); |
Suchakhree | 1:426fbd0d126a | 8 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
Suchakhree | 1:426fbd0d126a | 9 | if (I2Cstate != 0) // error on I2C |
Suchakhree | 1:426fbd0d126a | 10 | pc.printf("I2C failure while reading WHO_AM_I register"); |
Suchakhree | 1:426fbd0d126a | 11 | |
Suchakhree | 1:426fbd0d126a | 12 | if (whoami == 0x71) // WHO_AM_I should always be 0x71 |
Suchakhree | 1:426fbd0d126a | 13 | { |
Suchakhree | 1:426fbd0d126a | 14 | pc.printf("MPU9250 is online...\n\r"); |
Suchakhree | 1:426fbd0d126a | 15 | sprintf(buffer, "0x%x", whoami); |
Suchakhree | 1:426fbd0d126a | 16 | wait(1); |
Suchakhree | 1:426fbd0d126a | 17 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
Suchakhree | 1:426fbd0d126a | 18 | mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values (accelerometer and gyroscope self test) |
Suchakhree | 1:426fbd0d126a | 19 | mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometer, load biases in bias registers |
Suchakhree | 1:426fbd0d126a | 20 | wait(2); |
Suchakhree | 1:426fbd0d126a | 21 | //Initialize device for active mode read of acclerometer, gyroscope, and temperature |
Suchakhree | 1:426fbd0d126a | 22 | mpu9250.initMPU9250(); |
Suchakhree | 1:426fbd0d126a | 23 | pc.printf("MPU9250 initialized for active data mode....\n\r"); |
Suchakhree | 1:426fbd0d126a | 24 | //Initialize device for active mode read of magnetometer, 16 bit resolution, 100Hz. |
Suchakhree | 1:426fbd0d126a | 25 | mpu9250.initAK8963(magCalibration); |
Suchakhree | 1:426fbd0d126a | 26 | wait(1); |
Suchakhree | 1:426fbd0d126a | 27 | } |
Suchakhree | 1:426fbd0d126a | 28 | |
Suchakhree | 1:426fbd0d126a | 29 | else // Connection failure |
Suchakhree | 1:426fbd0d126a | 30 | { |
Suchakhree | 1:426fbd0d126a | 31 | pc.printf("Could not connect to MPU9250: \n\r"); |
Suchakhree | 1:426fbd0d126a | 32 | pc.printf("%#x \n", whoami); |
Suchakhree | 1:426fbd0d126a | 33 | sprintf(buffer, "WHO_AM_I 0x%x", whoami); |
Suchakhree | 1:426fbd0d126a | 34 | //while(1) ; // Loop forever if communication doesn't happen |
Suchakhree | 1:426fbd0d126a | 35 | } |
Suchakhree | 1:426fbd0d126a | 36 | |
Suchakhree | 1:426fbd0d126a | 37 | mpu9250.getAres(); // Get accelerometer sensitivity |
Suchakhree | 1:426fbd0d126a | 38 | mpu9250.getGres(); // Get gyro sensitivity |
Suchakhree | 1:426fbd0d126a | 39 | mpu9250.getMres(); // Get magnetometer sensitivity |
Suchakhree | 1:426fbd0d126a | 40 | magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated |
Suchakhree | 1:426fbd0d126a | 41 | magbias[1] = +120.; // User environmental x-axis correction in milliGauss |
Suchakhree | 1:426fbd0d126a | 42 | magbias[2] = +125.; // User environmental x-axis correction in milliGauss |
Suchakhree | 1:426fbd0d126a | 43 | |
Suchakhree | 1:426fbd0d126a | 44 | |
Suchakhree | 1:426fbd0d126a | 45 | Ay[0]=GetAcceloroY(); pc.printf("1: %d\n",Ay[0]); |
Suchakhree | 1:426fbd0d126a | 46 | Ay[1]=GetAcceloroY(); pc.printf("2: %d\n",Ay[1]); |
Suchakhree | 1:426fbd0d126a | 47 | if( Ay[1]+Ay[0]< -20 ) pc.printf("#BR$\n\n\n"); |
Suchakhree | 1:426fbd0d126a | 48 | |
Suchakhree | 1:426fbd0d126a | 49 | |
Suchakhree | 1:426fbd0d126a | 50 | //Function 4 GetAcceloroY |
Suchakhree | 1:426fbd0d126a | 51 | |
Suchakhree | 1:426fbd0d126a | 52 | int GetAcceloroY() |
Suchakhree | 1:426fbd0d126a | 53 | { |
Suchakhree | 1:426fbd0d126a | 54 | // If intPin goes high, all data registers have new data |
Suchakhree | 1:426fbd0d126a | 55 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
Suchakhree | 1:426fbd0d126a | 56 | mpu9250.readAccelData(accelCount); // Read the x/y/z adc values |
Suchakhree | 1:426fbd0d126a | 57 | // Now we'll calculate the accleration value into actual g's |
Suchakhree | 1:426fbd0d126a | 58 | if (I2Cstate != 0) //error on I2C |
Suchakhree | 1:426fbd0d126a | 59 | pc.printf("I2C error ocurred while reading accelerometer data. I2Cstate = %d \n\r", I2Cstate); |
Suchakhree | 1:426fbd0d126a | 60 | else{ // I2C read or write ok |
Suchakhree | 1:426fbd0d126a | 61 | I2Cstate = 1; |
Suchakhree | 1:426fbd0d126a | 62 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
Suchakhree | 1:426fbd0d126a | 63 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
Suchakhree | 1:426fbd0d126a | 64 | az = (float)accelCount[2]*aRes - accelBias[2]; |
Suchakhree | 1:426fbd0d126a | 65 | } |
Suchakhree | 1:426fbd0d126a | 66 | mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values |
Suchakhree | 1:426fbd0d126a | 67 | // Calculate the gyro value into actual degrees per second |
Suchakhree | 1:426fbd0d126a | 68 | if (I2Cstate != 0) //error on I2C |
Suchakhree | 1:426fbd0d126a | 69 | pc.printf("I2C error ocurred while reading gyrometer data. I2Cstate = %d \n\r", I2Cstate); |
Suchakhree | 1:426fbd0d126a | 70 | else{ // I2C read or write ok |
Suchakhree | 1:426fbd0d126a | 71 | I2Cstate = 1; |
Suchakhree | 1:426fbd0d126a | 72 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
Suchakhree | 1:426fbd0d126a | 73 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
Suchakhree | 1:426fbd0d126a | 74 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
Suchakhree | 1:426fbd0d126a | 75 | } |
Suchakhree | 1:426fbd0d126a | 76 | mpu9250.readMagData(magCount); // Read the x/y/z adc values |
Suchakhree | 1:426fbd0d126a | 77 | // Calculate the magnetometer values in milliGauss |
Suchakhree | 1:426fbd0d126a | 78 | // Include factory calibration per data sheet and user environmental corrections |
Suchakhree | 1:426fbd0d126a | 79 | if (I2Cstate != 0) //error on I2C |
Suchakhree | 1:426fbd0d126a | 80 | pc.printf("I2C error ocurred while reading magnetometer data. I2Cstate = %d \n\r", I2Cstate); |
Suchakhree | 1:426fbd0d126a | 81 | else{ // I2C read or write ok |
Suchakhree | 1:426fbd0d126a | 82 | I2Cstate = 1; |
Suchakhree | 1:426fbd0d126a | 83 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set |
Suchakhree | 1:426fbd0d126a | 84 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; |
Suchakhree | 1:426fbd0d126a | 85 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; |
Suchakhree | 1:426fbd0d126a | 86 | } |
Suchakhree | 1:426fbd0d126a | 87 | |
Suchakhree | 1:426fbd0d126a | 88 | mpu9250.getCompassOrientation(orientation); |
Suchakhree | 1:426fbd0d126a | 89 | } |
Suchakhree | 1:426fbd0d126a | 90 | |
Suchakhree | 1:426fbd0d126a | 91 | Now = t.read_us(); |
Suchakhree | 1:426fbd0d126a | 92 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
Suchakhree | 1:426fbd0d126a | 93 | lastUpdate = Now; |
Suchakhree | 1:426fbd0d126a | 94 | sum += deltat; |
Suchakhree | 1:426fbd0d126a | 95 | sumCount++; |
Suchakhree | 1:426fbd0d126a | 96 | |
Suchakhree | 1:426fbd0d126a | 97 | // Pass gyro rate as rad/s |
Suchakhree | 1:426fbd0d126a | 98 | // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
Suchakhree | 1:426fbd0d126a | 99 | mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
Suchakhree | 1:426fbd0d126a | 100 | |
Suchakhree | 1:426fbd0d126a | 101 | // Serial print and/or display at 1.5 s rate independent of data rates |
Suchakhree | 1:426fbd0d126a | 102 | delt_t = t.read_ms() - count; |
Suchakhree | 1:426fbd0d126a | 103 | if (delt_t > 500) { // update LCD once per half-second independent of read rate |
Suchakhree | 1:426fbd0d126a | 104 | mpu9250.MadgwickQuaternionUpdate(ax,ay,az,gx,gy,gz,mx,my,mz); |
Suchakhree | 1:426fbd0d126a | 105 | pc.printf(" ay = %.2f\n", 1000*ay); |
Suchakhree | 1:426fbd0d126a | 106 | tempCount = mpu9250.readTempData(); // Read the adc values |
Suchakhree | 1:426fbd0d126a | 107 | if (I2Cstate != 0) //error on I2C |
Suchakhree | 1:426fbd0d126a | 108 | pc.printf("I2C error ocurred while reading sensor temp. I2Cstate = %d \n\r", I2Cstate); |
Suchakhree | 1:426fbd0d126a | 109 | else{ // I2C read or write ok |
Suchakhree | 1:426fbd0d126a | 110 | I2Cstate = 1; |
Suchakhree | 1:426fbd0d126a | 111 | temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
Suchakhree | 1:426fbd0d126a | 112 | //pc.printf(" temperature = %f C\n\r", temperature); |
Suchakhree | 1:426fbd0d126a | 113 | } |
Suchakhree | 1:426fbd0d126a | 114 | count = t.read_ms(); |
Suchakhree | 1:426fbd0d126a | 115 | if(count > 1<<21) { |
Suchakhree | 1:426fbd0d126a | 116 | t.start(); // start the timer over again if ~30 minutes has passed |
Suchakhree | 1:426fbd0d126a | 117 | count = 0; |
Suchakhree | 1:426fbd0d126a | 118 | deltat= 0; |
Suchakhree | 1:426fbd0d126a | 119 | lastUpdate = t.read_us(); |
Suchakhree | 1:426fbd0d126a | 120 | } |
Suchakhree | 1:426fbd0d126a | 121 | sum = 0; |
Suchakhree | 1:426fbd0d126a | 122 | sumCount = 0; |
Suchakhree | 1:426fbd0d126a | 123 | } |
Suchakhree | 1:426fbd0d126a | 124 | return ay*1000; |
Suchakhree | 1:426fbd0d126a | 125 | }*/ |