Controller for Seagoat in the RoboSub competition

Dependencies:   Servo mbed

Fork of ESC by Matteo Terruzzi

Committer:
gelmes
Date:
Tue Jul 26 17:22:33 2016 +0000
Revision:
5:07bbe020eb65
Parent:
3:5ffe7e9c0bb3
This is a working implementation of the Controller;

Who changed what in which revision?

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