.

Dependents:   sensor_library_test sensor_library_test RTOS_Controller_v2 RTOS_Controller_v3 ... more

Committer:
aolgu003
Date:
Fri Jul 29 15:33:48 2016 +0000
Revision:
1:9a71704df32d
Parent:
0:055e2d78cb0f
Fixed issues with PID and added the switch to trigger the system.

Who changed what in which revision?

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