.

Dependents:   sensor_library_test sensor_library_test RTOS_Controller_v2 RTOS_Controller_v3 ... more

Committer:
aolgu003
Date:
Mon Jul 25 00:20:03 2016 +0000
Revision:
0:055e2d78cb0f
Child:
1:9a71704df32d
Nothing was changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aolgu003 0:055e2d78cb0f 1
aolgu003 0:055e2d78cb0f 2 #include "MPU6050.h"
aolgu003 0:055e2d78cb0f 3 Serial pc(USBTX, USBRX); // tx, rx
aolgu003 0:055e2d78cb0f 4
aolgu003 0:055e2d78cb0f 5 float sum = 0;
aolgu003 0:055e2d78cb0f 6 uint32_t sumCount = 0;
aolgu003 0:055e2d78cb0f 7
aolgu003 0:055e2d78cb0f 8 Timer t;
aolgu003 0:055e2d78cb0f 9
aolgu003 0:055e2d78cb0f 10 void IMUinit(MPU6050 &mpu6050)
aolgu003 0:055e2d78cb0f 11 {
aolgu003 0:055e2d78cb0f 12 t.start();
aolgu003 0:055e2d78cb0f 13
aolgu003 0:055e2d78cb0f 14 // Read the WHO_AM_I register, this is a good test of communication
aolgu003 0:055e2d78cb0f 15 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050
aolgu003 0:055e2d78cb0f 16 pc.printf("I AM 0x%x\n\r", whoami);
aolgu003 0:055e2d78cb0f 17 pc.printf("I SHOULD BE 0x68\n\r");
aolgu003 0:055e2d78cb0f 18
aolgu003 0:055e2d78cb0f 19 if (whoami == 0x68) { // WHO_AM_I should always be 0x68
aolgu003 0:055e2d78cb0f 20 pc.printf("MPU6050 is online...");
aolgu003 0:055e2d78cb0f 21 wait(1);
aolgu003 0:055e2d78cb0f 22 //lcd.clear();
aolgu003 0:055e2d78cb0f 23 //lcd.printString("MPU6050 OK", 0, 0);
aolgu003 0:055e2d78cb0f 24
aolgu003 0:055e2d78cb0f 25
aolgu003 0:055e2d78cb0f 26 mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values
aolgu003 0:055e2d78cb0f 27 pc.printf("x-axis self test: acceleration trim within : ");
aolgu003 0:055e2d78cb0f 28 pc.printf("%f", SelfTest[0]);
aolgu003 0:055e2d78cb0f 29 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 30 pc.printf("y-axis self test: acceleration trim within : ");
aolgu003 0:055e2d78cb0f 31 pc.printf("%f", SelfTest[1]);
aolgu003 0:055e2d78cb0f 32 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 33 pc.printf("z-axis self test: acceleration trim within : ");
aolgu003 0:055e2d78cb0f 34 pc.printf("%f", SelfTest[2]);
aolgu003 0:055e2d78cb0f 35 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 36 pc.printf("x-axis self test: gyration trim within : ");
aolgu003 0:055e2d78cb0f 37 pc.printf("%f", SelfTest[3]);
aolgu003 0:055e2d78cb0f 38 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 39 pc.printf("y-axis self test: gyration trim within : ");
aolgu003 0:055e2d78cb0f 40 pc.printf("%f", SelfTest[4]);
aolgu003 0:055e2d78cb0f 41 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 42 pc.printf("z-axis self test: gyration trim within : ");
aolgu003 0:055e2d78cb0f 43 pc.printf("%f", SelfTest[5]);
aolgu003 0:055e2d78cb0f 44 pc.printf("% of factory value \n\r");
aolgu003 0:055e2d78cb0f 45 wait(1);
aolgu003 0:055e2d78cb0f 46
aolgu003 0:055e2d78cb0f 47 if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f) {
aolgu003 0:055e2d78cb0f 48 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
aolgu003 0:055e2d78cb0f 49 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
aolgu003 0:055e2d78cb0f 50 mpu6050.initMPU6050();
aolgu003 0:055e2d78cb0f 51 pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
aolgu003 0:055e2d78cb0f 52 wait(2);
aolgu003 0:055e2d78cb0f 53 } else {
aolgu003 0:055e2d78cb0f 54 pc.printf("Device did not the pass self-test!\n\r");
aolgu003 0:055e2d78cb0f 55 }
aolgu003 0:055e2d78cb0f 56 } else {
aolgu003 0:055e2d78cb0f 57 pc.printf("Could not connect to MPU6050: \n\r");
aolgu003 0:055e2d78cb0f 58 pc.printf("%#x \n", whoami);
aolgu003 0:055e2d78cb0f 59
aolgu003 0:055e2d78cb0f 60 while(1) ; // Loop forever if communication doesn't happen
aolgu003 0:055e2d78cb0f 61 }
aolgu003 0:055e2d78cb0f 62 }
aolgu003 0:055e2d78cb0f 63
aolgu003 0:055e2d78cb0f 64
aolgu003 0:055e2d78cb0f 65 void IMUPrintData(MPU6050 &mpu6050)
aolgu003 0:055e2d78cb0f 66 {
aolgu003 0:055e2d78cb0f 67 pc.printf("Beginning IMU read\n");
aolgu003 0:055e2d78cb0f 68 // If data ready bit set, all data registers have new data
aolgu003 0:055e2d78cb0f 69 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
aolgu003 0:055e2d78cb0f 70 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 71 mpu6050.getAres();
aolgu003 0:055e2d78cb0f 72
aolgu003 0:055e2d78cb0f 73 // Now we'll calculate the accleration value into actual g's
aolgu003 0:055e2d78cb0f 74 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
aolgu003 0:055e2d78cb0f 75 ay = (float)accelCount[1]*aRes - accelBias[1];
aolgu003 0:055e2d78cb0f 76 az = (float)accelCount[2]*aRes - accelBias[2];
aolgu003 0:055e2d78cb0f 77
aolgu003 0:055e2d78cb0f 78 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 79 mpu6050.getGres();
aolgu003 0:055e2d78cb0f 80
aolgu003 0:055e2d78cb0f 81 // Calculate the gyro value into actual degrees per second
aolgu003 0:055e2d78cb0f 82 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
aolgu003 0:055e2d78cb0f 83 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
aolgu003 0:055e2d78cb0f 84 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
aolgu003 0:055e2d78cb0f 85
aolgu003 0:055e2d78cb0f 86 tempCount = mpu6050.readTempData(); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 87 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
aolgu003 0:055e2d78cb0f 88 }
aolgu003 0:055e2d78cb0f 89
aolgu003 0:055e2d78cb0f 90 Now = t.read_us();
aolgu003 0:055e2d78cb0f 91 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
aolgu003 0:055e2d78cb0f 92 lastUpdate = Now;
aolgu003 0:055e2d78cb0f 93
aolgu003 0:055e2d78cb0f 94 sum += deltat;
aolgu003 0:055e2d78cb0f 95 sumCount++;
aolgu003 0:055e2d78cb0f 96
aolgu003 0:055e2d78cb0f 97 if(lastUpdate - firstUpdate > 10000000.0f) {
aolgu003 0:055e2d78cb0f 98 beta = 0.04; // decrease filter gain after stabilized
aolgu003 0:055e2d78cb0f 99 zeta = 0.015; // increasey bias drift gain after stabilized
aolgu003 0:055e2d78cb0f 100 }
aolgu003 0:055e2d78cb0f 101
aolgu003 0:055e2d78cb0f 102 // Pass gyro rate as rad/s
aolgu003 0:055e2d78cb0f 103 mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
aolgu003 0:055e2d78cb0f 104
aolgu003 0:055e2d78cb0f 105 // Serial print and/or display at 0.5 s rate independent of data rates
aolgu003 0:055e2d78cb0f 106 delt_t = t.read_ms() - count;
aolgu003 0:055e2d78cb0f 107 if (delt_t > 500) { // update LCD once per half-second independent of read rate
aolgu003 0:055e2d78cb0f 108
aolgu003 0:055e2d78cb0f 109 pc.printf("ax = %f", 1000*ax);
aolgu003 0:055e2d78cb0f 110 pc.printf(" ay = %f", 1000*ay);
aolgu003 0:055e2d78cb0f 111 pc.printf(" az = %f mg\n\r", 1000*az);
aolgu003 0:055e2d78cb0f 112
aolgu003 0:055e2d78cb0f 113 pc.printf("gx = %f", gx);
aolgu003 0:055e2d78cb0f 114 pc.printf(" gy = %f", gy);
aolgu003 0:055e2d78cb0f 115 pc.printf(" gz = %f deg/s\n\r", gz);
aolgu003 0:055e2d78cb0f 116
aolgu003 0:055e2d78cb0f 117 pc.printf(" temperature = %f C\n\r", temperature);
aolgu003 0:055e2d78cb0f 118
aolgu003 0:055e2d78cb0f 119 pc.printf("q0 = %f\n\r", q[0]);
aolgu003 0:055e2d78cb0f 120 pc.printf("q1 = %f\n\r", q[1]);
aolgu003 0:055e2d78cb0f 121 pc.printf("q2 = %f\n\r", q[2]);
aolgu003 0:055e2d78cb0f 122 pc.printf("q3 = %f\n\r", q[3]);
aolgu003 0:055e2d78cb0f 123
aolgu003 0:055e2d78cb0f 124 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
aolgu003 0:055e2d78cb0f 125 // In this coordinate system, the positive z-axis is down toward Earth.
aolgu003 0:055e2d78cb0f 126 // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
aolgu003 0:055e2d78cb0f 127 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
aolgu003 0:055e2d78cb0f 128 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
aolgu003 0:055e2d78cb0f 129 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
aolgu003 0:055e2d78cb0f 130 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
aolgu003 0:055e2d78cb0f 131 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
aolgu003 0:055e2d78cb0f 132 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
aolgu003 0:055e2d78cb0f 133 yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
aolgu003 0:055e2d78cb0f 134 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
aolgu003 0:055e2d78cb0f 135 roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
aolgu003 0:055e2d78cb0f 136 pitch *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 137 yaw *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 138 roll *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 139
aolgu003 0:055e2d78cb0f 140 // pc.printf("Yaw, Pitch, Roll: \n\r");
aolgu003 0:055e2d78cb0f 141 // pc.printf("%f", yaw);
aolgu003 0:055e2d78cb0f 142 // pc.printf(", ");
aolgu003 0:055e2d78cb0f 143 // pc.printf("%f", pitch);
aolgu003 0:055e2d78cb0f 144 // pc.printf(", ");
aolgu003 0:055e2d78cb0f 145 // pc.printf("%f\n\r", roll);
aolgu003 0:055e2d78cb0f 146 // pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r");
aolgu003 0:055e2d78cb0f 147
aolgu003 0:055e2d78cb0f 148 pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
aolgu003 0:055e2d78cb0f 149 pc.printf("average rate = %f\n\r", (float) sumCount/sum);
aolgu003 0:055e2d78cb0f 150
aolgu003 0:055e2d78cb0f 151 //myled= !myled;
aolgu003 0:055e2d78cb0f 152 count = t.read_ms();
aolgu003 0:055e2d78cb0f 153 sum = 0;
aolgu003 0:055e2d78cb0f 154 sumCount = 0;
aolgu003 0:055e2d78cb0f 155 }
aolgu003 0:055e2d78cb0f 156 }
aolgu003 0:055e2d78cb0f 157
aolgu003 0:055e2d78cb0f 158 void IMUUpdate(MPU6050 &mpu6050)
aolgu003 0:055e2d78cb0f 159 {
aolgu003 0:055e2d78cb0f 160 // If data ready bit set, all data registers have new data
aolgu003 0:055e2d78cb0f 161 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
aolgu003 0:055e2d78cb0f 162 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 163 mpu6050.getAres();
aolgu003 0:055e2d78cb0f 164
aolgu003 0:055e2d78cb0f 165 // Now we'll calculate the accleration value into actual g's
aolgu003 0:055e2d78cb0f 166 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
aolgu003 0:055e2d78cb0f 167 ay = (float)accelCount[1]*aRes - accelBias[1];
aolgu003 0:055e2d78cb0f 168 az = (float)accelCount[2]*aRes - accelBias[2];
aolgu003 0:055e2d78cb0f 169
aolgu003 0:055e2d78cb0f 170 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 171 mpu6050.getGres();
aolgu003 0:055e2d78cb0f 172
aolgu003 0:055e2d78cb0f 173 // Calculate the gyro value into actual degrees per second
aolgu003 0:055e2d78cb0f 174 gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set
aolgu003 0:055e2d78cb0f 175 gy = (float)gyroCount[1]*gRes; // - gyroBias[1];
aolgu003 0:055e2d78cb0f 176 gz = (float)gyroCount[2]*gRes; // - gyroBias[2];
aolgu003 0:055e2d78cb0f 177
aolgu003 0:055e2d78cb0f 178 tempCount = mpu6050.readTempData(); // Read the x/y/z adc values
aolgu003 0:055e2d78cb0f 179 temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade
aolgu003 0:055e2d78cb0f 180 }
aolgu003 0:055e2d78cb0f 181
aolgu003 0:055e2d78cb0f 182 Now = t.read_us();
aolgu003 0:055e2d78cb0f 183 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
aolgu003 0:055e2d78cb0f 184 lastUpdate = Now;
aolgu003 0:055e2d78cb0f 185
aolgu003 0:055e2d78cb0f 186 sum += deltat;
aolgu003 0:055e2d78cb0f 187 sumCount++;
aolgu003 0:055e2d78cb0f 188
aolgu003 0:055e2d78cb0f 189 if(lastUpdate - firstUpdate > 10000000.0f) {
aolgu003 0:055e2d78cb0f 190 beta = 0.04; // decrease filter gain after stabilized
aolgu003 0:055e2d78cb0f 191 zeta = 0.015; // increasey bias drift gain after stabilized
aolgu003 0:055e2d78cb0f 192 }
aolgu003 0:055e2d78cb0f 193
aolgu003 0:055e2d78cb0f 194 // Pass gyro rate as rad/s
aolgu003 0:055e2d78cb0f 195 mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
aolgu003 0:055e2d78cb0f 196
aolgu003 0:055e2d78cb0f 197 // Serial print and/or display at 0.5 s rate independent of data rates
aolgu003 0:055e2d78cb0f 198 delt_t = t.read_ms() - count;
aolgu003 0:055e2d78cb0f 199
aolgu003 0:055e2d78cb0f 200 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
aolgu003 0:055e2d78cb0f 201 // In this coordinate system, the positive z-axis is down toward Earth.
aolgu003 0:055e2d78cb0f 202 // Yaw is the angle between Sensor x-axis and Earth magnetic North (or true North if corrected for local declination, looking down on the sensor positive yaw is counterclockwise.
aolgu003 0:055e2d78cb0f 203 // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
aolgu003 0:055e2d78cb0f 204 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
aolgu003 0:055e2d78cb0f 205 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
aolgu003 0:055e2d78cb0f 206 // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
aolgu003 0:055e2d78cb0f 207 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
aolgu003 0:055e2d78cb0f 208 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
aolgu003 0:055e2d78cb0f 209 yaw = atan2(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]);
aolgu003 0:055e2d78cb0f 210 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
aolgu003 0:055e2d78cb0f 211 roll = atan2(2.0f * (q[0] * q[1] + q[2] * q[3]), q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3]);
aolgu003 0:055e2d78cb0f 212 pitch *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 213 yaw *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 214 roll *= 180.0f / PI;
aolgu003 0:055e2d78cb0f 215
aolgu003 0:055e2d78cb0f 216 count = t.read_ms();
aolgu003 0:055e2d78cb0f 217 sum = 0;
aolgu003 0:055e2d78cb0f 218 sumCount = 0;
aolgu003 0:055e2d78cb0f 219
aolgu003 0:055e2d78cb0f 220 }