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