UCR Robosub manual control / PID tuning interface

Dependencies:   mbed HMC5883L

Committer:
roger_wee
Date:
Thu Jul 27 05:45:10 2017 +0000
Revision:
1:3f291f2f80d3
Parent:
0:048a74dd7c3a
control edit

Who changed what in which revision?

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