Not done - might need - for later
Dependencies: FXOS8700CQ MPU6050IMU mbed
main.cpp@0:7eb9e18d5839, 2015-03-31 (annotated)
- Committer:
- joshwilkins2013
- Date:
- Tue Mar 31 22:31:41 2015 +0000
- Revision:
- 0:7eb9e18d5839
Not done - might need - for later
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joshwilkins2013 | 0:7eb9e18d5839 | 1 | #include "mbed.h" |
joshwilkins2013 | 0:7eb9e18d5839 | 2 | #include "MPU6050.h" |
joshwilkins2013 | 0:7eb9e18d5839 | 3 | #include "FXOS8700CQ.h" |
joshwilkins2013 | 0:7eb9e18d5839 | 4 | |
joshwilkins2013 | 0:7eb9e18d5839 | 5 | float sum = 0; |
joshwilkins2013 | 0:7eb9e18d5839 | 6 | uint32_t sumCount = 0; |
joshwilkins2013 | 0:7eb9e18d5839 | 7 | |
joshwilkins2013 | 0:7eb9e18d5839 | 8 | MPU6050 mpu6050; |
joshwilkins2013 | 0:7eb9e18d5839 | 9 | FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1) |
joshwilkins2013 | 0:7eb9e18d5839 | 10 | |
joshwilkins2013 | 0:7eb9e18d5839 | 11 | Timer t; |
joshwilkins2013 | 0:7eb9e18d5839 | 12 | |
joshwilkins2013 | 0:7eb9e18d5839 | 13 | InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt |
joshwilkins2013 | 0:7eb9e18d5839 | 14 | bool fxos_int2_triggered = false; // Interrupt status flags and data |
joshwilkins2013 | 0:7eb9e18d5839 | 15 | |
joshwilkins2013 | 0:7eb9e18d5839 | 16 | SRAWDATA accel_data; // Storage for the data from the sensor |
joshwilkins2013 | 0:7eb9e18d5839 | 17 | SRAWDATA magn_data; |
joshwilkins2013 | 0:7eb9e18d5839 | 18 | |
joshwilkins2013 | 0:7eb9e18d5839 | 19 | Serial pc(USBTX, USBRX); // tx, rx |
joshwilkins2013 | 0:7eb9e18d5839 | 20 | |
joshwilkins2013 | 0:7eb9e18d5839 | 21 | void trigger_fxos_int2(void){ |
joshwilkins2013 | 0:7eb9e18d5839 | 22 | fxos_int2_triggered = true; |
joshwilkins2013 | 0:7eb9e18d5839 | 23 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 24 | |
joshwilkins2013 | 0:7eb9e18d5839 | 25 | void print_reading(){ |
joshwilkins2013 | 0:7eb9e18d5839 | 26 | pc.printf("A X:%5d,Y:%5d,Z:%5d M X:%5d,Y:%5d,Z:%5d\r\n", |
joshwilkins2013 | 0:7eb9e18d5839 | 27 | accel_data.x, accel_data.y, accel_data.z, |
joshwilkins2013 | 0:7eb9e18d5839 | 28 | magn_data.x, magn_data.y, magn_data.z); |
joshwilkins2013 | 0:7eb9e18d5839 | 29 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 30 | |
joshwilkins2013 | 0:7eb9e18d5839 | 31 | int main(){ |
joshwilkins2013 | 0:7eb9e18d5839 | 32 | t.start(); |
joshwilkins2013 | 0:7eb9e18d5839 | 33 | pc.baud(115200); // 200Hz x line of output data! |
joshwilkins2013 | 0:7eb9e18d5839 | 34 | |
joshwilkins2013 | 0:7eb9e18d5839 | 35 | printf("\r\n\nFXOS8700Q Who Am I= %X\r\n", fxos.get_whoami()); |
joshwilkins2013 | 0:7eb9e18d5839 | 36 | |
joshwilkins2013 | 0:7eb9e18d5839 | 37 | fxos_int2.fall(&trigger_fxos_int2); // Iterrupt for active-low interrupt line from FXOS |
joshwilkins2013 | 0:7eb9e18d5839 | 38 | fxos.enable(); |
joshwilkins2013 | 0:7eb9e18d5839 | 39 | |
joshwilkins2013 | 0:7eb9e18d5839 | 40 | pc.printf("Started data collection. Accelerometer at max %dg.\r\n", |
joshwilkins2013 | 0:7eb9e18d5839 | 41 | fxos.get_accel_scale()); |
joshwilkins2013 | 0:7eb9e18d5839 | 42 | |
joshwilkins2013 | 0:7eb9e18d5839 | 43 | fxos.get_data(&accel_data, &magn_data); // clear interrupt from device |
joshwilkins2013 | 0:7eb9e18d5839 | 44 | fxos_int2_triggered = false; // un-trigger |
joshwilkins2013 | 0:7eb9e18d5839 | 45 | |
joshwilkins2013 | 0:7eb9e18d5839 | 46 | //Set up I2C |
joshwilkins2013 | 0:7eb9e18d5839 | 47 | i2c.frequency(400000); // use fast (400 kHz) I2C |
joshwilkins2013 | 0:7eb9e18d5839 | 48 | |
joshwilkins2013 | 0:7eb9e18d5839 | 49 | // Read the WHO_AM_I register, this is a good test of communication |
joshwilkins2013 | 0:7eb9e18d5839 | 50 | uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 |
joshwilkins2013 | 0:7eb9e18d5839 | 51 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 52 | |
joshwilkins2013 | 0:7eb9e18d5839 | 53 | if (whoami == 0x68){ // WHO_AM_I should always be 0x68 |
joshwilkins2013 | 0:7eb9e18d5839 | 54 | pc.printf("MPU6050 is online..."); |
joshwilkins2013 | 0:7eb9e18d5839 | 55 | wait(1); |
joshwilkins2013 | 0:7eb9e18d5839 | 56 | |
joshwilkins2013 | 0:7eb9e18d5839 | 57 | mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values |
joshwilkins2013 | 0:7eb9e18d5839 | 58 | pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 59 | pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 60 | pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 61 | pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 62 | pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 63 | pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 64 | wait(1); |
joshwilkins2013 | 0:7eb9e18d5839 | 65 | |
joshwilkins2013 | 0:7eb9e18d5839 | 66 | 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){ |
joshwilkins2013 | 0:7eb9e18d5839 | 67 | mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration |
joshwilkins2013 | 0:7eb9e18d5839 | 68 | mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
joshwilkins2013 | 0:7eb9e18d5839 | 69 | mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
joshwilkins2013 | 0:7eb9e18d5839 | 70 | wait(2); |
joshwilkins2013 | 0:7eb9e18d5839 | 71 | }else pc.printf("Device did not the pass self-test!\n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 72 | }else{ |
joshwilkins2013 | 0:7eb9e18d5839 | 73 | pc.printf("Could not connect to MPU6050: \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 74 | pc.printf("%#x \n", whoami); |
joshwilkins2013 | 0:7eb9e18d5839 | 75 | while(1) ; // Loop forever if communication doesn't happen |
joshwilkins2013 | 0:7eb9e18d5839 | 76 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 77 | |
joshwilkins2013 | 0:7eb9e18d5839 | 78 | while(1){ |
joshwilkins2013 | 0:7eb9e18d5839 | 79 | // If data ready bit set, all data registers have new data |
joshwilkins2013 | 0:7eb9e18d5839 | 80 | if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt |
joshwilkins2013 | 0:7eb9e18d5839 | 81 | mpu6050.readAccelData(accelCount); // Read the x/y/z adc values |
joshwilkins2013 | 0:7eb9e18d5839 | 82 | mpu6050.getAres(); |
joshwilkins2013 | 0:7eb9e18d5839 | 83 | |
joshwilkins2013 | 0:7eb9e18d5839 | 84 | // Now we'll calculate the accleration value into actual g's |
joshwilkins2013 | 0:7eb9e18d5839 | 85 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
joshwilkins2013 | 0:7eb9e18d5839 | 86 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
joshwilkins2013 | 0:7eb9e18d5839 | 87 | az = (float)accelCount[2]*aRes - accelBias[2]; |
joshwilkins2013 | 0:7eb9e18d5839 | 88 | |
joshwilkins2013 | 0:7eb9e18d5839 | 89 | mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values |
joshwilkins2013 | 0:7eb9e18d5839 | 90 | mpu6050.getGres(); |
joshwilkins2013 | 0:7eb9e18d5839 | 91 | |
joshwilkins2013 | 0:7eb9e18d5839 | 92 | // Calculate the gyro value into actual degrees per second |
joshwilkins2013 | 0:7eb9e18d5839 | 93 | gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set |
joshwilkins2013 | 0:7eb9e18d5839 | 94 | gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; |
joshwilkins2013 | 0:7eb9e18d5839 | 95 | gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; |
joshwilkins2013 | 0:7eb9e18d5839 | 96 | |
joshwilkins2013 | 0:7eb9e18d5839 | 97 | tempCount = mpu6050.readTempData(); // Read the x/y/z adc values |
joshwilkins2013 | 0:7eb9e18d5839 | 98 | temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade |
joshwilkins2013 | 0:7eb9e18d5839 | 99 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 100 | |
joshwilkins2013 | 0:7eb9e18d5839 | 101 | if(fxos_int2_triggered){ |
joshwilkins2013 | 0:7eb9e18d5839 | 102 | fxos_int2_triggered = false; // un-trigger |
joshwilkins2013 | 0:7eb9e18d5839 | 103 | fxos.get_data(&accel_data, &magn_data); |
joshwilkins2013 | 0:7eb9e18d5839 | 104 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 105 | |
joshwilkins2013 | 0:7eb9e18d5839 | 106 | Now = t.read_us(); |
joshwilkins2013 | 0:7eb9e18d5839 | 107 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
joshwilkins2013 | 0:7eb9e18d5839 | 108 | lastUpdate = Now; |
joshwilkins2013 | 0:7eb9e18d5839 | 109 | |
joshwilkins2013 | 0:7eb9e18d5839 | 110 | sum += deltat; |
joshwilkins2013 | 0:7eb9e18d5839 | 111 | sumCount++; |
joshwilkins2013 | 0:7eb9e18d5839 | 112 | |
joshwilkins2013 | 0:7eb9e18d5839 | 113 | if(lastUpdate - firstUpdate > 10000000.0f) { |
joshwilkins2013 | 0:7eb9e18d5839 | 114 | beta = 0.04; // decrease filter gain after stabilized |
joshwilkins2013 | 0:7eb9e18d5839 | 115 | zeta = 0.015; // increasey bias drift gain after stabilized |
joshwilkins2013 | 0:7eb9e18d5839 | 116 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 117 | |
joshwilkins2013 | 0:7eb9e18d5839 | 118 | // Pass gyro rate as rad/s |
joshwilkins2013 | 0:7eb9e18d5839 | 119 | mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); |
joshwilkins2013 | 0:7eb9e18d5839 | 120 | |
joshwilkins2013 | 0:7eb9e18d5839 | 121 | // Serial print and/or display at 0.5 s rate independent of data rates |
joshwilkins2013 | 0:7eb9e18d5839 | 122 | delt_t = t.read_ms() - count; |
joshwilkins2013 | 0:7eb9e18d5839 | 123 | |
joshwilkins2013 | 0:7eb9e18d5839 | 124 | pc.printf("ax = %f", 1000*ax); |
joshwilkins2013 | 0:7eb9e18d5839 | 125 | pc.printf(" ay = %f", 1000*ay); |
joshwilkins2013 | 0:7eb9e18d5839 | 126 | pc.printf(" az = %f mg\n\r", 1000*az); |
joshwilkins2013 | 0:7eb9e18d5839 | 127 | |
joshwilkins2013 | 0:7eb9e18d5839 | 128 | pc.printf("gx = %f", gx); |
joshwilkins2013 | 0:7eb9e18d5839 | 129 | pc.printf(" gy = %f", gy); |
joshwilkins2013 | 0:7eb9e18d5839 | 130 | pc.printf(" gz = %f deg/s\n\r", gz); |
joshwilkins2013 | 0:7eb9e18d5839 | 131 | |
joshwilkins2013 | 0:7eb9e18d5839 | 132 | pc.printf(" temperature = %f C\n\r", temperature); |
joshwilkins2013 | 0:7eb9e18d5839 | 133 | |
joshwilkins2013 | 0:7eb9e18d5839 | 134 | pc.printf("q0 = %f\n\r", q[0]); |
joshwilkins2013 | 0:7eb9e18d5839 | 135 | pc.printf("q1 = %f\n\r", q[1]); |
joshwilkins2013 | 0:7eb9e18d5839 | 136 | pc.printf("q2 = %f\n\r", q[2]); |
joshwilkins2013 | 0:7eb9e18d5839 | 137 | pc.printf("q3 = %f\n\r", q[3]); |
joshwilkins2013 | 0:7eb9e18d5839 | 138 | |
joshwilkins2013 | 0:7eb9e18d5839 | 139 | |
joshwilkins2013 | 0:7eb9e18d5839 | 140 | |
joshwilkins2013 | 0:7eb9e18d5839 | 141 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
joshwilkins2013 | 0:7eb9e18d5839 | 142 | // In this coordinate system, the positive z-axis is down toward Earth. |
joshwilkins2013 | 0:7eb9e18d5839 | 143 | // 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. |
joshwilkins2013 | 0:7eb9e18d5839 | 144 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
joshwilkins2013 | 0:7eb9e18d5839 | 145 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
joshwilkins2013 | 0:7eb9e18d5839 | 146 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
joshwilkins2013 | 0:7eb9e18d5839 | 147 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
joshwilkins2013 | 0:7eb9e18d5839 | 148 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
joshwilkins2013 | 0:7eb9e18d5839 | 149 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
joshwilkins2013 | 0:7eb9e18d5839 | 150 | 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]); |
joshwilkins2013 | 0:7eb9e18d5839 | 151 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
joshwilkins2013 | 0:7eb9e18d5839 | 152 | 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]); |
joshwilkins2013 | 0:7eb9e18d5839 | 153 | pitch *= 180.0f / PI; |
joshwilkins2013 | 0:7eb9e18d5839 | 154 | yaw *= 180.0f / PI; |
joshwilkins2013 | 0:7eb9e18d5839 | 155 | roll *= 180.0f / PI; |
joshwilkins2013 | 0:7eb9e18d5839 | 156 | |
joshwilkins2013 | 0:7eb9e18d5839 | 157 | // pc.printf("Yaw, Pitch, Roll: \n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 158 | // pc.printf("%f", yaw); |
joshwilkins2013 | 0:7eb9e18d5839 | 159 | // pc.printf(", "); |
joshwilkins2013 | 0:7eb9e18d5839 | 160 | // pc.printf("%f", pitch); |
joshwilkins2013 | 0:7eb9e18d5839 | 161 | // pc.printf(", "); |
joshwilkins2013 | 0:7eb9e18d5839 | 162 | // pc.printf("%f\n\r", roll); |
joshwilkins2013 | 0:7eb9e18d5839 | 163 | // pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r"); |
joshwilkins2013 | 0:7eb9e18d5839 | 164 | |
joshwilkins2013 | 0:7eb9e18d5839 | 165 | pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); |
joshwilkins2013 | 0:7eb9e18d5839 | 166 | pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
joshwilkins2013 | 0:7eb9e18d5839 | 167 | |
joshwilkins2013 | 0:7eb9e18d5839 | 168 | count = t.read_ms(); |
joshwilkins2013 | 0:7eb9e18d5839 | 169 | sum = 0; |
joshwilkins2013 | 0:7eb9e18d5839 | 170 | sumCount = 0; |
joshwilkins2013 | 0:7eb9e18d5839 | 171 | } |
joshwilkins2013 | 0:7eb9e18d5839 | 172 | } |