bamlor nuttymaisuay
Dependencies: mbed
main.cpp@3:46cc9d386ff4, 2017-12-10 (annotated)
- Committer:
- jaybehandsome
- Date:
- Sun Dec 10 11:45:47 2017 +0000
- Revision:
- 3:46cc9d386ff4
- Parent:
- 2:af822f5a5120
- Child:
- 4:9cc307f25dc9
rectangle;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaybehandsome | 0:1e46c1a32764 | 1 | /* MPU9250 Basic Example Code |
jaybehandsome | 0:1e46c1a32764 | 2 | by: Kris Winer |
jaybehandsome | 0:1e46c1a32764 | 3 | date: April 1, 2014 |
jaybehandsome | 0:1e46c1a32764 | 4 | license: Beerware - Use this code however you'd like. If you |
jaybehandsome | 0:1e46c1a32764 | 5 | find it useful you can buy me a beer some time. |
jaybehandsome | 0:1e46c1a32764 | 6 | |
jaybehandsome | 0:1e46c1a32764 | 7 | Demonstrate basic MPU-9250 functionality including parameterizing the register addresses, initializing the sensor, |
jaybehandsome | 0:1e46c1a32764 | 8 | getting properly scaled accelerometer, gyroscope, and magnetometer data out. Added display functions to |
jaybehandsome | 0:1e46c1a32764 | 9 | allow display to on breadboard monitor. Addition of 9 DoF sensor fusion using open source Madgwick and |
jaybehandsome | 0:1e46c1a32764 | 10 | Mahony filter algorithms. Sketch runs on the 3.3 V 8 MHz Pro Mini and the Teensy 3.1. |
jaybehandsome | 0:1e46c1a32764 | 11 | |
jaybehandsome | 0:1e46c1a32764 | 12 | SDA and SCL should have external pull-up resistors (to 3.3V). |
jaybehandsome | 0:1e46c1a32764 | 13 | 10k resistors are on the EMSENSR-9250 breakout board. |
jaybehandsome | 0:1e46c1a32764 | 14 | |
jaybehandsome | 0:1e46c1a32764 | 15 | Hardware setup: |
jaybehandsome | 0:1e46c1a32764 | 16 | MPU9250 Breakout --------- Arduino |
jaybehandsome | 0:1e46c1a32764 | 17 | VDD ---------------------- 3.3V |
jaybehandsome | 0:1e46c1a32764 | 18 | VDDI --------------------- 3.3V |
jaybehandsome | 0:1e46c1a32764 | 19 | SDA ----------------------- A4 |
jaybehandsome | 0:1e46c1a32764 | 20 | SCL ----------------------- A5 |
jaybehandsome | 0:1e46c1a32764 | 21 | GND ---------------------- GND |
jaybehandsome | 0:1e46c1a32764 | 22 | |
jaybehandsome | 0:1e46c1a32764 | 23 | Note: The MPU9250 is an I2C sensor and uses the Arduino Wire library. |
jaybehandsome | 0:1e46c1a32764 | 24 | 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. |
jaybehandsome | 0:1e46c1a32764 | 25 | We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file. |
jaybehandsome | 0:1e46c1a32764 | 26 | We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file. |
jaybehandsome | 0:1e46c1a32764 | 27 | */ |
jaybehandsome | 0:1e46c1a32764 | 28 | |
jaybehandsome | 0:1e46c1a32764 | 29 | //#include "ST_F401_84MHZ.h" |
jaybehandsome | 0:1e46c1a32764 | 30 | //F401_init84 myinit(0); |
jaybehandsome | 0:1e46c1a32764 | 31 | #include "mbed.h" |
jaybehandsome | 0:1e46c1a32764 | 32 | #include "MPU9250.h" |
jaybehandsome | 0:1e46c1a32764 | 33 | |
jaybehandsome | 0:1e46c1a32764 | 34 | |
jaybehandsome | 0:1e46c1a32764 | 35 | |
jaybehandsome | 3:46cc9d386ff4 | 36 | float x = 9,y = 0; |
jaybehandsome | 0:1e46c1a32764 | 37 | float sum = 0; |
jaybehandsome | 0:1e46c1a32764 | 38 | uint32_t sumCount = 0; |
jaybehandsome | 0:1e46c1a32764 | 39 | char buffer[14]; |
jaybehandsome | 0:1e46c1a32764 | 40 | |
jaybehandsome | 0:1e46c1a32764 | 41 | MPU9250 mpu9250; |
jaybehandsome | 0:1e46c1a32764 | 42 | |
jaybehandsome | 0:1e46c1a32764 | 43 | Timer t; |
jaybehandsome | 0:1e46c1a32764 | 44 | |
jaybehandsome | 0:1e46c1a32764 | 45 | Serial pc(PA_15, PB_7); // tx, rx |
jaybehandsome | 0:1e46c1a32764 | 46 | |
jaybehandsome | 0:1e46c1a32764 | 47 | |
jaybehandsome | 0:1e46c1a32764 | 48 | |
jaybehandsome | 3:46cc9d386ff4 | 49 | BusOut C(PA_0,PA_1,PA_4,PB_0,PC_1,PC_2,PC_3), A(PC_10,PC_12,PA_13,PA_14,PC_13,PC_11,PD_2), B(PA_5,PA_6,PA_7,PC_7,PB_2,PB_1,PB_15); |
jaybehandsome | 2:af822f5a5120 | 50 | float posA,posB,posC; |
jaybehandsome | 0:1e46c1a32764 | 51 | int main() |
jaybehandsome | 0:1e46c1a32764 | 52 | { |
jaybehandsome | 3:46cc9d386ff4 | 53 | A = 0x7F; |
jaybehandsome | 0:1e46c1a32764 | 54 | pc.baud(9600); |
jaybehandsome | 0:1e46c1a32764 | 55 | //Set up I2C |
jaybehandsome | 0:1e46c1a32764 | 56 | i2c.frequency(400000); // use fast (400 kHz) I2C |
jaybehandsome | 0:1e46c1a32764 | 57 | |
jaybehandsome | 0:1e46c1a32764 | 58 | pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock); |
jaybehandsome | 0:1e46c1a32764 | 59 | |
jaybehandsome | 0:1e46c1a32764 | 60 | t.start(); |
jaybehandsome | 0:1e46c1a32764 | 61 | |
jaybehandsome | 0:1e46c1a32764 | 62 | |
jaybehandsome | 0:1e46c1a32764 | 63 | // Read the WHO_AM_I register, this is a good test of communication |
jaybehandsome | 0:1e46c1a32764 | 64 | uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
jaybehandsome | 0:1e46c1a32764 | 65 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 66 | |
jaybehandsome | 0:1e46c1a32764 | 67 | if (whoami == 0x71) // WHO_AM_I should always be 0x68 |
jaybehandsome | 0:1e46c1a32764 | 68 | { |
jaybehandsome | 0:1e46c1a32764 | 69 | pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami); |
jaybehandsome | 0:1e46c1a32764 | 70 | pc.printf("MPU9250 is online...\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 71 | sprintf(buffer, "0x%x", whoami); |
jaybehandsome | 0:1e46c1a32764 | 72 | |
jaybehandsome | 0:1e46c1a32764 | 73 | wait(1); |
jaybehandsome | 0:1e46c1a32764 | 74 | |
jaybehandsome | 0:1e46c1a32764 | 75 | mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration |
jaybehandsome | 0:1e46c1a32764 | 76 | mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values |
jaybehandsome | 0:1e46c1a32764 | 77 | pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]); |
jaybehandsome | 0:1e46c1a32764 | 78 | pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]); |
jaybehandsome | 0:1e46c1a32764 | 79 | pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]); |
jaybehandsome | 0:1e46c1a32764 | 80 | pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]); |
jaybehandsome | 0:1e46c1a32764 | 81 | pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]); |
jaybehandsome | 0:1e46c1a32764 | 82 | pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]); |
jaybehandsome | 0:1e46c1a32764 | 83 | mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
jaybehandsome | 0:1e46c1a32764 | 84 | pc.printf("x gyro bias = %f\n\r", gyroBias[0]); |
jaybehandsome | 0:1e46c1a32764 | 85 | pc.printf("y gyro bias = %f\n\r", gyroBias[1]); |
jaybehandsome | 0:1e46c1a32764 | 86 | pc.printf("z gyro bias = %f\n\r", gyroBias[2]); |
jaybehandsome | 0:1e46c1a32764 | 87 | pc.printf("x accel bias = %f\n\r", accelBias[0]); |
jaybehandsome | 0:1e46c1a32764 | 88 | pc.printf("y accel bias = %f\n\r", accelBias[1]); |
jaybehandsome | 0:1e46c1a32764 | 89 | pc.printf("z accel bias = %f\n\r", accelBias[2]); |
jaybehandsome | 0:1e46c1a32764 | 90 | wait(2); |
jaybehandsome | 0:1e46c1a32764 | 91 | mpu9250.initMPU9250(); |
jaybehandsome | 0:1e46c1a32764 | 92 | pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
jaybehandsome | 0:1e46c1a32764 | 93 | mpu9250.initAK8963(magCalibration); |
jaybehandsome | 0:1e46c1a32764 | 94 | pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer |
jaybehandsome | 0:1e46c1a32764 | 95 | pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale)); |
jaybehandsome | 0:1e46c1a32764 | 96 | pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale)); |
jaybehandsome | 0:1e46c1a32764 | 97 | if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 98 | if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 99 | if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 100 | if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r"); |
jaybehandsome | 0:1e46c1a32764 | 101 | wait(1); |
jaybehandsome | 0:1e46c1a32764 | 102 | } |
jaybehandsome | 0:1e46c1a32764 | 103 | else |
jaybehandsome | 0:1e46c1a32764 | 104 | { |
jaybehandsome | 0:1e46c1a32764 | 105 | pc.printf("Could not connect to MPU9250: \n\r"); |
jaybehandsome | 0:1e46c1a32764 | 106 | pc.printf("%#x \n", whoami); |
jaybehandsome | 0:1e46c1a32764 | 107 | |
jaybehandsome | 0:1e46c1a32764 | 108 | sprintf(buffer, "WHO_AM_I 0x%x", whoami); |
jaybehandsome | 0:1e46c1a32764 | 109 | |
jaybehandsome | 0:1e46c1a32764 | 110 | |
jaybehandsome | 0:1e46c1a32764 | 111 | while(1) ; // Loop forever if communication doesn't happen |
jaybehandsome | 0:1e46c1a32764 | 112 | } |
jaybehandsome | 0:1e46c1a32764 | 113 | |
jaybehandsome | 0:1e46c1a32764 | 114 | mpu9250.getAres(); // Get accelerometer sensitivity |
jaybehandsome | 0:1e46c1a32764 | 115 | mpu9250.getGres(); // Get gyro sensitivity |
jaybehandsome | 0:1e46c1a32764 | 116 | mpu9250.getMres(); // Get magnetometer sensitivity |
jaybehandsome | 0:1e46c1a32764 | 117 | pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes); |
jaybehandsome | 0:1e46c1a32764 | 118 | pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes); |
jaybehandsome | 0:1e46c1a32764 | 119 | pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes); |
jaybehandsome | 0:1e46c1a32764 | 120 | magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated |
jaybehandsome | 0:1e46c1a32764 | 121 | magbias[1] = +120.; // User environmental x-axis correction in milliGauss |
jaybehandsome | 0:1e46c1a32764 | 122 | magbias[2] = +125.; // User environmental x-axis correction in milliGauss |
jaybehandsome | 0:1e46c1a32764 | 123 | |
jaybehandsome | 0:1e46c1a32764 | 124 | while(1) { |
jaybehandsome | 0:1e46c1a32764 | 125 | |
jaybehandsome | 0:1e46c1a32764 | 126 | // If intPin goes high, all data registers have new data |
jaybehandsome | 0:1e46c1a32764 | 127 | if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt |
jaybehandsome | 0:1e46c1a32764 | 128 | |
jaybehandsome | 0:1e46c1a32764 | 129 | mpu9250.readAccelData(accelCount); // Read the x/y/z adc values |
jaybehandsome | 0:1e46c1a32764 | 130 | // Now we'll calculate the accleration value into actual g's |
jaybehandsome | 0:1e46c1a32764 | 131 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
jaybehandsome | 0:1e46c1a32764 | 132 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
jaybehandsome | 0:1e46c1a32764 | 133 | az = (float)accelCount[2]*aRes - accelBias[2]; |
jaybehandsome | 0:1e46c1a32764 | 134 | |
jaybehandsome | 0:1e46c1a32764 | 135 | mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values |
jaybehandsome | 0:1e46c1a32764 | 136 | // Calculate the gyro value into actual degrees per second |
jaybehandsome | 0:1e46c1a32764 | 137 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set |
jaybehandsome | 0:1e46c1a32764 | 138 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; |
jaybehandsome | 0:1e46c1a32764 | 139 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; |
jaybehandsome | 0:1e46c1a32764 | 140 | |
jaybehandsome | 0:1e46c1a32764 | 141 | mpu9250.readMagData(magCount); // Read the x/y/z adc values |
jaybehandsome | 0:1e46c1a32764 | 142 | // Calculate the magnetometer values in milliGauss |
jaybehandsome | 0:1e46c1a32764 | 143 | // Include factory calibration per data sheet and user environmental corrections |
jaybehandsome | 0:1e46c1a32764 | 144 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set |
jaybehandsome | 0:1e46c1a32764 | 145 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; |
jaybehandsome | 0:1e46c1a32764 | 146 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; |
jaybehandsome | 0:1e46c1a32764 | 147 | } |
jaybehandsome | 0:1e46c1a32764 | 148 | |
jaybehandsome | 0:1e46c1a32764 | 149 | Now = t.read_us(); |
jaybehandsome | 0:1e46c1a32764 | 150 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
jaybehandsome | 0:1e46c1a32764 | 151 | lastUpdate = Now; |
jaybehandsome | 0:1e46c1a32764 | 152 | |
jaybehandsome | 0:1e46c1a32764 | 153 | sum += deltat; |
jaybehandsome | 0:1e46c1a32764 | 154 | sumCount++; |
jaybehandsome | 0:1e46c1a32764 | 155 | |
jaybehandsome | 0:1e46c1a32764 | 156 | // if(lastUpdate - firstUpdate > 10000000.0f) { |
jaybehandsome | 0:1e46c1a32764 | 157 | // beta = 0.04; // decrease filter gain after stabilized |
jaybehandsome | 0:1e46c1a32764 | 158 | // zeta = 0.015; // increasey bias drift gain after stabilized |
jaybehandsome | 0:1e46c1a32764 | 159 | // } |
jaybehandsome | 0:1e46c1a32764 | 160 | |
jaybehandsome | 0:1e46c1a32764 | 161 | // Pass gyro rate as rad/s |
jaybehandsome | 0:1e46c1a32764 | 162 | // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
jaybehandsome | 0:1e46c1a32764 | 163 | mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz); |
jaybehandsome | 0:1e46c1a32764 | 164 | |
jaybehandsome | 0:1e46c1a32764 | 165 | // Serial print and/or display at 0.5 s rate independent of data rates |
jaybehandsome | 0:1e46c1a32764 | 166 | delt_t = t.read_ms() - count; |
jaybehandsome | 2:af822f5a5120 | 167 | if (delt_t > 5) { // update LCD once per half-second independent of read rate |
jaybehandsome | 2:af822f5a5120 | 168 | pc.printf("delt_t = %f\n",delt_t); |
jaybehandsome | 3:46cc9d386ff4 | 169 | pc.printf(" x = %f", x); |
jaybehandsome | 3:46cc9d386ff4 | 170 | y += 1; |
jaybehandsome | 3:46cc9d386ff4 | 171 | if (y == 2000) |
jaybehandsome | 3:46cc9d386ff4 | 172 | { |
jaybehandsome | 3:46cc9d386ff4 | 173 | x+=0.5; |
jaybehandsome | 3:46cc9d386ff4 | 174 | y = 0; |
jaybehandsome | 3:46cc9d386ff4 | 175 | } |
jaybehandsome | 3:46cc9d386ff4 | 176 | posA += (gz*delt_t*x/1000); |
jaybehandsome | 2:af822f5a5120 | 177 | posB = posA + 120; |
jaybehandsome | 2:af822f5a5120 | 178 | posC = posA + 240; |
jaybehandsome | 2:af822f5a5120 | 179 | |
jaybehandsome | 3:46cc9d386ff4 | 180 | // if (posA >= 360 && posA < 720) |
jaybehandsome | 3:46cc9d386ff4 | 181 | // { |
jaybehandsome | 2:af822f5a5120 | 182 | // A = 0x7F; |
jaybehandsome | 2:af822f5a5120 | 183 | // B = 0x7F; |
jaybehandsome | 2:af822f5a5120 | 184 | // C = 0x7F; |
jaybehandsome | 2:af822f5a5120 | 185 | // } |
jaybehandsome | 3:46cc9d386ff4 | 186 | // else if (posA >= 720 && posA < 1080) |
jaybehandsome | 3:46cc9d386ff4 | 187 | // { |
jaybehandsome | 3:46cc9d386ff4 | 188 | // A = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 189 | // B = 0; |
jaybehandsome | 3:46cc9d386ff4 | 190 | // C = 0; |
jaybehandsome | 3:46cc9d386ff4 | 191 | // } |
jaybehandsome | 3:46cc9d386ff4 | 192 | // else if (posA >= 1080 && posA < 1440) |
jaybehandsome | 3:46cc9d386ff4 | 193 | // { |
jaybehandsome | 3:46cc9d386ff4 | 194 | // A = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 195 | // B = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 196 | // C = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 197 | // } |
jaybehandsome | 3:46cc9d386ff4 | 198 | // else if (posA >= 1440 && posA < 1800) |
jaybehandsome | 3:46cc9d386ff4 | 199 | // { |
jaybehandsome | 3:46cc9d386ff4 | 200 | // A = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 201 | // B = 0; |
jaybehandsome | 3:46cc9d386ff4 | 202 | // C = 0; |
jaybehandsome | 3:46cc9d386ff4 | 203 | // } |
jaybehandsome | 3:46cc9d386ff4 | 204 | // else if (posA >= 1800) |
jaybehandsome | 3:46cc9d386ff4 | 205 | // { |
jaybehandsome | 3:46cc9d386ff4 | 206 | // A = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 207 | // B = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 208 | // C = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 209 | // } |
jaybehandsome | 2:af822f5a5120 | 210 | // else |
jaybehandsome | 2:af822f5a5120 | 211 | // { |
jaybehandsome | 2:af822f5a5120 | 212 | // A = 0; |
jaybehandsome | 2:af822f5a5120 | 213 | // B = 0; |
jaybehandsome | 2:af822f5a5120 | 214 | // C = 0; |
jaybehandsome | 2:af822f5a5120 | 215 | // } |
jaybehandsome | 2:af822f5a5120 | 216 | // if (posA >= 360) |
jaybehandsome | 2:af822f5a5120 | 217 | // { |
jaybehandsome | 2:af822f5a5120 | 218 | // break; |
jaybehandsome | 2:af822f5a5120 | 219 | // } |
jaybehandsome | 3:46cc9d386ff4 | 220 | if (posA >= 360) |
jaybehandsome | 3:46cc9d386ff4 | 221 | { |
jaybehandsome | 3:46cc9d386ff4 | 222 | posA-=360; |
jaybehandsome | 3:46cc9d386ff4 | 223 | } |
jaybehandsome | 3:46cc9d386ff4 | 224 | if (posA > 0 && posA < 30) |
jaybehandsome | 3:46cc9d386ff4 | 225 | { |
jaybehandsome | 3:46cc9d386ff4 | 226 | A = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 227 | } |
jaybehandsome | 3:46cc9d386ff4 | 228 | else |
jaybehandsome | 3:46cc9d386ff4 | 229 | { |
jaybehandsome | 3:46cc9d386ff4 | 230 | A = 0x00; |
jaybehandsome | 3:46cc9d386ff4 | 231 | } |
jaybehandsome | 3:46cc9d386ff4 | 232 | if (posB >= 360) |
jaybehandsome | 3:46cc9d386ff4 | 233 | { |
jaybehandsome | 3:46cc9d386ff4 | 234 | posB-=360; |
jaybehandsome | 3:46cc9d386ff4 | 235 | } |
jaybehandsome | 3:46cc9d386ff4 | 236 | if (posB > 0 && posB < 30) |
jaybehandsome | 3:46cc9d386ff4 | 237 | { |
jaybehandsome | 3:46cc9d386ff4 | 238 | B = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 239 | } |
jaybehandsome | 3:46cc9d386ff4 | 240 | else |
jaybehandsome | 3:46cc9d386ff4 | 241 | { |
jaybehandsome | 3:46cc9d386ff4 | 242 | B = 0x00; |
jaybehandsome | 3:46cc9d386ff4 | 243 | } |
jaybehandsome | 3:46cc9d386ff4 | 244 | if (posC >= 360) |
jaybehandsome | 3:46cc9d386ff4 | 245 | { |
jaybehandsome | 3:46cc9d386ff4 | 246 | posC-=360; |
jaybehandsome | 3:46cc9d386ff4 | 247 | } |
jaybehandsome | 3:46cc9d386ff4 | 248 | if (posC > 0 && posC < 30) |
jaybehandsome | 3:46cc9d386ff4 | 249 | { |
jaybehandsome | 3:46cc9d386ff4 | 250 | C = 0x7F; |
jaybehandsome | 3:46cc9d386ff4 | 251 | } |
jaybehandsome | 3:46cc9d386ff4 | 252 | else |
jaybehandsome | 3:46cc9d386ff4 | 253 | { |
jaybehandsome | 3:46cc9d386ff4 | 254 | C = 0x00; |
jaybehandsome | 3:46cc9d386ff4 | 255 | } |
jaybehandsome | 0:1e46c1a32764 | 256 | // pc.printf("ax = %f", 1000*ax); |
jaybehandsome | 0:1e46c1a32764 | 257 | // pc.printf(" ay = %f", 1000*ay); |
jaybehandsome | 0:1e46c1a32764 | 258 | // pc.printf(" az = %f mg\n\r", 1000*az); |
jaybehandsome | 0:1e46c1a32764 | 259 | |
jaybehandsome | 2:af822f5a5120 | 260 | // pc.printf("gx = %f", gx); |
jaybehandsome | 2:af822f5a5120 | 261 | // pc.printf(" gy = %f", gy); |
jaybehandsome | 0:1e46c1a32764 | 262 | pc.printf(" gz = %f deg/s\n\r", gz); |
jaybehandsome | 2:af822f5a5120 | 263 | pc.printf(" posA = %f\n", posA); |
jaybehandsome | 0:1e46c1a32764 | 264 | |
jaybehandsome | 0:1e46c1a32764 | 265 | // pc.printf("gx = %f", mx); |
jaybehandsome | 0:1e46c1a32764 | 266 | // pc.printf(" gy = %f", my); |
jaybehandsome | 0:1e46c1a32764 | 267 | // pc.printf(" gz = %f mG\n\r", mz); |
jaybehandsome | 0:1e46c1a32764 | 268 | |
jaybehandsome | 0:1e46c1a32764 | 269 | tempCount = mpu9250.readTempData(); // Read the adc values |
jaybehandsome | 0:1e46c1a32764 | 270 | temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade |
jaybehandsome | 1:c102abc55118 | 271 | // pc.printf(" temperature = %f C\n\r", temperature); |
jaybehandsome | 0:1e46c1a32764 | 272 | |
jaybehandsome | 0:1e46c1a32764 | 273 | // pc.printf("q0 = %f\n\r", q[0]); |
jaybehandsome | 0:1e46c1a32764 | 274 | // pc.printf("q1 = %f\n\r", q[1]); |
jaybehandsome | 0:1e46c1a32764 | 275 | // pc.printf("q2 = %f\n\r", q[2]); |
jaybehandsome | 0:1e46c1a32764 | 276 | // pc.printf("q3 = %f\n\r", q[3]); |
jaybehandsome | 0:1e46c1a32764 | 277 | |
jaybehandsome | 0:1e46c1a32764 | 278 | |
jaybehandsome | 0:1e46c1a32764 | 279 | |
jaybehandsome | 0:1e46c1a32764 | 280 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
jaybehandsome | 0:1e46c1a32764 | 281 | // In this coordinate system, the positive z-axis is down toward Earth. |
jaybehandsome | 0:1e46c1a32764 | 282 | // 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. |
jaybehandsome | 0:1e46c1a32764 | 283 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
jaybehandsome | 0:1e46c1a32764 | 284 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
jaybehandsome | 0:1e46c1a32764 | 285 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
jaybehandsome | 0:1e46c1a32764 | 286 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
jaybehandsome | 0:1e46c1a32764 | 287 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
jaybehandsome | 0:1e46c1a32764 | 288 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
jaybehandsome | 0:1e46c1a32764 | 289 | 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]); |
jaybehandsome | 0:1e46c1a32764 | 290 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
jaybehandsome | 0:1e46c1a32764 | 291 | 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]); |
jaybehandsome | 0:1e46c1a32764 | 292 | pitch *= 180.0f / PI; |
jaybehandsome | 0:1e46c1a32764 | 293 | yaw *= 180.0f / PI; |
jaybehandsome | 0:1e46c1a32764 | 294 | yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 |
jaybehandsome | 0:1e46c1a32764 | 295 | roll *= 180.0f / PI; |
jaybehandsome | 0:1e46c1a32764 | 296 | |
jaybehandsome | 0:1e46c1a32764 | 297 | // pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); |
jaybehandsome | 0:1e46c1a32764 | 298 | // pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
jaybehandsome | 0:1e46c1a32764 | 299 | // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll); |
jaybehandsome | 0:1e46c1a32764 | 300 | // sprintf(buffer, "rate = %f", (float) sumCount/sum); |
jaybehandsome | 0:1e46c1a32764 | 301 | // |
jaybehandsome | 0:1e46c1a32764 | 302 | |
jaybehandsome | 0:1e46c1a32764 | 303 | count = t.read_ms(); |
jaybehandsome | 0:1e46c1a32764 | 304 | |
jaybehandsome | 0:1e46c1a32764 | 305 | if(count > 1<<21) { |
jaybehandsome | 0:1e46c1a32764 | 306 | t.start(); // start the timer over again if ~30 minutes has passed |
jaybehandsome | 0:1e46c1a32764 | 307 | count = 0; |
jaybehandsome | 0:1e46c1a32764 | 308 | deltat= 0; |
jaybehandsome | 0:1e46c1a32764 | 309 | lastUpdate = t.read_us(); |
jaybehandsome | 0:1e46c1a32764 | 310 | } |
jaybehandsome | 0:1e46c1a32764 | 311 | sum = 0; |
jaybehandsome | 0:1e46c1a32764 | 312 | sumCount = 0; |
jaybehandsome | 0:1e46c1a32764 | 313 | } |
jaybehandsome | 0:1e46c1a32764 | 314 | } |
jaybehandsome | 3:46cc9d386ff4 | 315 | |
jaybehandsome | 2:af822f5a5120 | 316 | pc.printf("fuck you"); |
jaybehandsome | 0:1e46c1a32764 | 317 | } |