![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
九軸の制御
Dependencies: mbed
Fork of MPU9250AHRS by
main.cpp@3:51217f251b8d, 2018-07-13 (annotated)
- Committer:
- kurikuri
- Date:
- Fri Jul 13 07:55:27 2018 +0000
- Revision:
- 3:51217f251b8d
- Parent:
- 1:71c319f03fda
Kp-9250??;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kurikuri | 3:51217f251b8d | 1 | /* |
onehorse | 0:2e5e65a6fb30 | 2 | Demonstrate basic MPU-9250 functionality including parameterizing the register addresses, initializing the sensor, |
onehorse | 0:2e5e65a6fb30 | 3 | getting properly scaled accelerometer, gyroscope, and magnetometer data out. Added display functions to |
onehorse | 0:2e5e65a6fb30 | 4 | allow display to on breadboard monitor. Addition of 9 DoF sensor fusion using open source Madgwick and |
onehorse | 0:2e5e65a6fb30 | 5 | Mahony filter algorithms. Sketch runs on the 3.3 V 8 MHz Pro Mini and the Teensy 3.1. |
onehorse | 0:2e5e65a6fb30 | 6 | |
onehorse | 0:2e5e65a6fb30 | 7 | SDA and SCL should have external pull-up resistors (to 3.3V). |
onehorse | 0:2e5e65a6fb30 | 8 | 10k resistors are on the EMSENSR-9250 breakout board. |
onehorse | 0:2e5e65a6fb30 | 9 | |
onehorse | 0:2e5e65a6fb30 | 10 | Hardware setup: |
kurikuri | 3:51217f251b8d | 11 | MPU9250 Breakout --------- lpc1768 |
onehorse | 0:2e5e65a6fb30 | 12 | VDD ---------------------- 3.3V |
kurikuri | 3:51217f251b8d | 13 | SDA ----------------------- p28 |
kurikuri | 3:51217f251b8d | 14 | SCL ----------------------- p27 |
onehorse | 0:2e5e65a6fb30 | 15 | GND ---------------------- GND |
onehorse | 0:2e5e65a6fb30 | 16 | |
onehorse | 0:2e5e65a6fb30 | 17 | Note: The MPU9250 is an I2C sensor and uses the Arduino Wire library. |
onehorse | 0:2e5e65a6fb30 | 18 | Because the sensor is not 5V tolerant, we are using a 3.3 V 8 MHz Pro Mini or a 3.3 V Teensy 3.1. |
onehorse | 0:2e5e65a6fb30 | 19 | We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file. |
onehorse | 0:2e5e65a6fb30 | 20 | We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file. |
onehorse | 0:2e5e65a6fb30 | 21 | */ |
onehorse | 0:2e5e65a6fb30 | 22 | |
onehorse | 0:2e5e65a6fb30 | 23 | #include "mbed.h" |
onehorse | 0:2e5e65a6fb30 | 24 | #include "MPU9250.h" |
onehorse | 0:2e5e65a6fb30 | 25 | |
onehorse | 0:2e5e65a6fb30 | 26 | float sum = 0; |
onehorse | 0:2e5e65a6fb30 | 27 | uint32_t sumCount = 0; |
onehorse | 0:2e5e65a6fb30 | 28 | char buffer[14]; |
onehorse | 0:2e5e65a6fb30 | 29 | MPU9250 mpu9250; |
onehorse | 0:2e5e65a6fb30 | 30 | Timer t; |
onehorse | 0:2e5e65a6fb30 | 31 | Serial pc(USBTX, USBRX); // tx, rx |
onehorse | 0:2e5e65a6fb30 | 32 | |
onehorse | 0:2e5e65a6fb30 | 33 | int main() |
onehorse | 0:2e5e65a6fb30 | 34 | { |
onehorse | 0:2e5e65a6fb30 | 35 | pc.baud(9600); |
onehorse | 0:2e5e65a6fb30 | 36 | |
onehorse | 0:2e5e65a6fb30 | 37 | //Set up I2C |
onehorse | 0:2e5e65a6fb30 | 38 | i2c.frequency(400000); // use fast (400 kHz) I2C |
onehorse | 0:2e5e65a6fb30 | 39 | |
onehorse | 0:2e5e65a6fb30 | 40 | pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); |
onehorse | 0:2e5e65a6fb30 | 41 | |
onehorse | 0:2e5e65a6fb30 | 42 | t.start(); |
onehorse | 0:2e5e65a6fb30 | 43 | |
onehorse | 0:2e5e65a6fb30 | 44 | // Read the WHO_AM_I register, this is a good test of communication |
onehorse | 0:2e5e65a6fb30 | 45 | uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
onehorse | 0:2e5e65a6fb30 | 46 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
onehorse | 0:2e5e65a6fb30 | 47 | |
onehorse | 0:2e5e65a6fb30 | 48 | if (whoami == 0x71) // WHO_AM_I should always be 0x68 |
onehorse | 0:2e5e65a6fb30 | 49 | { |
onehorse | 0:2e5e65a6fb30 | 50 | pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
onehorse | 0:2e5e65a6fb30 | 51 | pc.printf("MPU9250 is online...\n\r"); |
onehorse | 0:2e5e65a6fb30 | 52 | sprintf(buffer, "0x%x", whoami); |
onehorse | 0:2e5e65a6fb30 | 53 | wait(1); |
onehorse | 0:2e5e65a6fb30 | 54 | |
onehorse | 0:2e5e65a6fb30 | 55 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
onehorse | 1:71c319f03fda | 56 | mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values |
onehorse | 1:71c319f03fda | 57 | pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); |
onehorse | 1:71c319f03fda | 58 | pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); |
onehorse | 1:71c319f03fda | 59 | pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); |
onehorse | 1:71c319f03fda | 60 | pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); |
onehorse | 1:71c319f03fda | 61 | pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); |
onehorse | 1:71c319f03fda | 62 | pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); |
onehorse | 0:2e5e65a6fb30 | 63 | mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
onehorse | 0:2e5e65a6fb30 | 64 | pc.printf("x gyro bias = %f\n\r", gyroBias[0]); |
onehorse | 0:2e5e65a6fb30 | 65 | pc.printf("y gyro bias = %f\n\r", gyroBias[1]); |
onehorse | 0:2e5e65a6fb30 | 66 | pc.printf("z gyro bias = %f\n\r", gyroBias[2]); |
onehorse | 0:2e5e65a6fb30 | 67 | pc.printf("x accel bias = %f\n\r", accelBias[0]); |
onehorse | 0:2e5e65a6fb30 | 68 | pc.printf("y accel bias = %f\n\r", accelBias[1]); |
onehorse | 0:2e5e65a6fb30 | 69 | pc.printf("z accel bias = %f\n\r", accelBias[2]); |
onehorse | 0:2e5e65a6fb30 | 70 | wait(2); |
onehorse | 0:2e5e65a6fb30 | 71 | mpu9250.initMPU9250(); |
onehorse | 0:2e5e65a6fb30 | 72 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
onehorse | 0:2e5e65a6fb30 | 73 | mpu9250.initAK8963(magCalibration); |
onehorse | 0:2e5e65a6fb30 | 74 | pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
onehorse | 0:2e5e65a6fb30 | 75 | pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
onehorse | 0:2e5e65a6fb30 | 76 | pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
onehorse | 0:2e5e65a6fb30 | 77 | if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
onehorse | 0:2e5e65a6fb30 | 78 | if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
onehorse | 0:2e5e65a6fb30 | 79 | if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
onehorse | 0:2e5e65a6fb30 | 80 | if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); |
onehorse | 0:2e5e65a6fb30 | 81 | wait(1); |
onehorse | 0:2e5e65a6fb30 | 82 | } |
onehorse | 0:2e5e65a6fb30 | 83 | else |
onehorse | 0:2e5e65a6fb30 | 84 | { |
onehorse | 0:2e5e65a6fb30 | 85 | pc.printf("Could not connect to MPU9250: \n\r"); |
onehorse | 0:2e5e65a6fb30 | 86 | pc.printf("%#x \n", whoami); |
onehorse | 0:2e5e65a6fb30 | 87 | sprintf(buffer, "WHO_AM_I 0x%x", whoami); |
kurikuri | 3:51217f251b8d | 88 | |
onehorse | 0:2e5e65a6fb30 | 89 | while(1) ; // Loop forever if communication doesn't happen |
onehorse | 0:2e5e65a6fb30 | 90 | } |
onehorse | 0:2e5e65a6fb30 | 91 | |
onehorse | 0:2e5e65a6fb30 | 92 | mpu9250.getAres(); // Get accelerometer sensitivity |
onehorse | 0:2e5e65a6fb30 | 93 | mpu9250.getGres(); // Get gyro sensitivity |
onehorse | 0:2e5e65a6fb30 | 94 | mpu9250.getMres(); // Get magnetometer sensitivity |
onehorse | 0:2e5e65a6fb30 | 95 | pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); |
onehorse | 0:2e5e65a6fb30 | 96 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); |
onehorse | 0:2e5e65a6fb30 | 97 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); |
onehorse | 0:2e5e65a6fb30 | 98 | magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated |
onehorse | 0:2e5e65a6fb30 | 99 | magbias[1] = +120.; // User environmental x-axis correction in milliGauss |
onehorse | 0:2e5e65a6fb30 | 100 | magbias[2] = +125.; // User environmental x-axis correction in milliGauss |
onehorse | 0:2e5e65a6fb30 | 101 | |
onehorse | 0:2e5e65a6fb30 | 102 | while(1) { |
onehorse | 0:2e5e65a6fb30 | 103 | |
onehorse | 0:2e5e65a6fb30 | 104 | // If intPin goes high, all data registers have new data |
onehorse | 0:2e5e65a6fb30 | 105 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
onehorse | 0:2e5e65a6fb30 | 106 | |
onehorse | 0:2e5e65a6fb30 | 107 | mpu9250.readAccelData(accelCount); // Read the x/y/z adc values |
onehorse | 0:2e5e65a6fb30 | 108 | // Now we'll calculate the accleration value into actual g's |
onehorse | 0:2e5e65a6fb30 | 109 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
onehorse | 0:2e5e65a6fb30 | 110 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
onehorse | 0:2e5e65a6fb30 | 111 | az = (float)accelCount[2]*aRes - accelBias[2]; |
onehorse | 0:2e5e65a6fb30 | 112 | |
onehorse | 0:2e5e65a6fb30 | 113 | mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values |
onehorse | 0:2e5e65a6fb30 | 114 | // Calculate the gyro value into actual degrees per second |
onehorse | 0:2e5e65a6fb30 | 115 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
onehorse | 0:2e5e65a6fb30 | 116 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
onehorse | 0:2e5e65a6fb30 | 117 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
onehorse | 0:2e5e65a6fb30 | 118 | |
onehorse | 0:2e5e65a6fb30 | 119 | mpu9250.readMagData(magCount); // Read the x/y/z adc values |
onehorse | 0:2e5e65a6fb30 | 120 | // Calculate the magnetometer values in milliGauss |
onehorse | 0:2e5e65a6fb30 | 121 | // Include factory calibration per data sheet and user environmental corrections |
onehorse | 0:2e5e65a6fb30 | 122 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set |
onehorse | 0:2e5e65a6fb30 | 123 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; |
onehorse | 0:2e5e65a6fb30 | 124 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; |
onehorse | 0:2e5e65a6fb30 | 125 | } |
onehorse | 0:2e5e65a6fb30 | 126 | |
onehorse | 0:2e5e65a6fb30 | 127 | Now = t.read_us(); |
onehorse | 0:2e5e65a6fb30 | 128 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
onehorse | 0:2e5e65a6fb30 | 129 | lastUpdate = Now; |
onehorse | 0:2e5e65a6fb30 | 130 | |
onehorse | 0:2e5e65a6fb30 | 131 | sum += deltat; |
onehorse | 0:2e5e65a6fb30 | 132 | sumCount++; |
onehorse | 0:2e5e65a6fb30 | 133 | |
onehorse | 0:2e5e65a6fb30 | 134 | // if(lastUpdate - firstUpdate > 10000000.0f) { |
onehorse | 0:2e5e65a6fb30 | 135 | // beta = 0.04; // decrease filter gain after stabilized |
onehorse | 0:2e5e65a6fb30 | 136 | // zeta = 0.015; // increasey bias drift gain after stabilized |
onehorse | 0:2e5e65a6fb30 | 137 | // } |
onehorse | 0:2e5e65a6fb30 | 138 | |
onehorse | 0:2e5e65a6fb30 | 139 | // Pass gyro rate as rad/s |
onehorse | 0:2e5e65a6fb30 | 140 | // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
onehorse | 0:2e5e65a6fb30 | 141 | mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
onehorse | 0:2e5e65a6fb30 | 142 | |
onehorse | 0:2e5e65a6fb30 | 143 | // Serial print and/or display at 0.5 s rate independent of data rates |
onehorse | 0:2e5e65a6fb30 | 144 | delt_t = t.read_ms() - count; |
onehorse | 0:2e5e65a6fb30 | 145 | if (delt_t > 500) { // update LCD once per half-second independent of read rate |
onehorse | 0:2e5e65a6fb30 | 146 | |
onehorse | 0:2e5e65a6fb30 | 147 | pc.printf("ax = %f", 1000*ax); |
onehorse | 0:2e5e65a6fb30 | 148 | pc.printf(" ay = %f", 1000*ay); |
onehorse | 0:2e5e65a6fb30 | 149 | pc.printf(" az = %f mg\n\r", 1000*az); |
onehorse | 0:2e5e65a6fb30 | 150 | |
onehorse | 0:2e5e65a6fb30 | 151 | pc.printf("gx = %f", gx); |
onehorse | 0:2e5e65a6fb30 | 152 | pc.printf(" gy = %f", gy); |
onehorse | 0:2e5e65a6fb30 | 153 | pc.printf(" gz = %f deg/s\n\r", gz); |
onehorse | 0:2e5e65a6fb30 | 154 | |
kurikuri | 3:51217f251b8d | 155 | pc.printf("mx = %f", mx); |
kurikuri | 3:51217f251b8d | 156 | pc.printf(" my = %f", my); |
kurikuri | 3:51217f251b8d | 157 | pc.printf(" mz = %f mG\n\r", mz); |
onehorse | 0:2e5e65a6fb30 | 158 | |
kurikuri | 3:51217f251b8d | 159 | /*//温度/* |
onehorse | 0:2e5e65a6fb30 | 160 | tempCount = mpu9250.readTempData(); // Read the adc values |
onehorse | 0:2e5e65a6fb30 | 161 | temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
onehorse | 0:2e5e65a6fb30 | 162 | pc.printf(" temperature = %f C\n\r", temperature); |
kurikuri | 3:51217f251b8d | 163 | */ |
onehorse | 0:2e5e65a6fb30 | 164 | |
onehorse | 0:2e5e65a6fb30 | 165 | pc.printf("q0 = %f\n\r", q[0]); |
onehorse | 0:2e5e65a6fb30 | 166 | pc.printf("q1 = %f\n\r", q[1]); |
onehorse | 0:2e5e65a6fb30 | 167 | pc.printf("q2 = %f\n\r", q[2]); |
onehorse | 0:2e5e65a6fb30 | 168 | pc.printf("q3 = %f\n\r", q[3]); |
onehorse | 0:2e5e65a6fb30 | 169 | |
onehorse | 0:2e5e65a6fb30 | 170 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
onehorse | 0:2e5e65a6fb30 | 171 | // In this coordinate system, the positive z-axis is down toward Earth. |
onehorse | 0:2e5e65a6fb30 | 172 | // 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. |
onehorse | 0:2e5e65a6fb30 | 173 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
onehorse | 0:2e5e65a6fb30 | 174 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
onehorse | 0:2e5e65a6fb30 | 175 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
onehorse | 0:2e5e65a6fb30 | 176 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
onehorse | 0:2e5e65a6fb30 | 177 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
onehorse | 0:2e5e65a6fb30 | 178 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
onehorse | 0:2e5e65a6fb30 | 179 | 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]); |
onehorse | 0:2e5e65a6fb30 | 180 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
onehorse | 0:2e5e65a6fb30 | 181 | 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]); |
onehorse | 0:2e5e65a6fb30 | 182 | pitch *= 180.0f / PI; |
onehorse | 0:2e5e65a6fb30 | 183 | yaw *= 180.0f / PI; |
onehorse | 0:2e5e65a6fb30 | 184 | yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 |
onehorse | 0:2e5e65a6fb30 | 185 | roll *= 180.0f / PI; |
onehorse | 0:2e5e65a6fb30 | 186 | |
onehorse | 0:2e5e65a6fb30 | 187 | pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); |
onehorse | 0:2e5e65a6fb30 | 188 | pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
onehorse | 0:2e5e65a6fb30 | 189 | // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll); |
onehorse | 0:2e5e65a6fb30 | 190 | // sprintf(buffer, "rate = %f", (float) sumCount/sum); |
kurikuri | 3:51217f251b8d | 191 | |
onehorse | 0:2e5e65a6fb30 | 192 | myled= !myled; |
onehorse | 0:2e5e65a6fb30 | 193 | count = t.read_ms(); |
onehorse | 0:2e5e65a6fb30 | 194 | |
onehorse | 0:2e5e65a6fb30 | 195 | if(count > 1<<21) { |
onehorse | 0:2e5e65a6fb30 | 196 | t.start(); // start the timer over again if ~30 minutes has passed |
onehorse | 0:2e5e65a6fb30 | 197 | count = 0; |
onehorse | 0:2e5e65a6fb30 | 198 | deltat= 0; |
onehorse | 0:2e5e65a6fb30 | 199 | lastUpdate = t.read_us(); |
onehorse | 0:2e5e65a6fb30 | 200 | } |
onehorse | 0:2e5e65a6fb30 | 201 | sum = 0; |
onehorse | 0:2e5e65a6fb30 | 202 | sumCount = 0; |
onehorse | 0:2e5e65a6fb30 | 203 | } |
onehorse | 0:2e5e65a6fb30 | 204 | } |
onehorse | 0:2e5e65a6fb30 | 205 | |
onehorse | 0:2e5e65a6fb30 | 206 | } |