test mpu6050 for fibo
Dependencies: mbed
Fork of MPU6050IMU by
main.cpp@3:4b0e7e8a06ae, 2015-02-28 (annotated)
- Committer:
- masterholy
- Date:
- Sat Feb 28 15:36:41 2015 +0000
- Revision:
- 3:4b0e7e8a06ae
- Parent:
- 1:cea9d83b8636
test mpu6050 for fibo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
onehorse | 0:65aa78c10981 | 1 | |
onehorse | 0:65aa78c10981 | 2 | /* MPU6050 Basic Example Code |
onehorse | 0:65aa78c10981 | 3 | by: Kris Winer |
onehorse | 0:65aa78c10981 | 4 | date: May 1, 2014 |
onehorse | 0:65aa78c10981 | 5 | license: Beerware - Use this code however you'd like. If you |
onehorse | 0:65aa78c10981 | 6 | find it useful you can buy me a beer some time. |
onehorse | 0:65aa78c10981 | 7 | |
onehorse | 0:65aa78c10981 | 8 | Demonstrate MPU-6050 basic functionality including initialization, accelerometer trimming, sleep mode functionality as well as |
onehorse | 0:65aa78c10981 | 9 | parameterizing the register addresses. Added display functions to allow display to on breadboard monitor. |
onehorse | 0:65aa78c10981 | 10 | No DMP use. We just want to get out the accelerations, temperature, and gyro readings. |
onehorse | 0:65aa78c10981 | 11 | |
onehorse | 0:65aa78c10981 | 12 | SDA and SCL should have external pull-up resistors (to 3.3V). |
onehorse | 0:65aa78c10981 | 13 | 10k resistors worked for me. They should be on the breakout |
onehorse | 0:65aa78c10981 | 14 | board. |
onehorse | 0:65aa78c10981 | 15 | |
onehorse | 0:65aa78c10981 | 16 | Hardware setup: |
onehorse | 0:65aa78c10981 | 17 | MPU6050 Breakout --------- Arduino |
onehorse | 0:65aa78c10981 | 18 | 3.3V --------------------- 3.3V |
onehorse | 0:65aa78c10981 | 19 | SDA ----------------------- A4 |
onehorse | 0:65aa78c10981 | 20 | SCL ----------------------- A5 |
onehorse | 0:65aa78c10981 | 21 | GND ---------------------- GND |
onehorse | 0:65aa78c10981 | 22 | |
onehorse | 0:65aa78c10981 | 23 | Note: The MPU6050 is an I2C sensor and uses the Arduino Wire library. |
onehorse | 0:65aa78c10981 | 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. |
onehorse | 0:65aa78c10981 | 25 | We have disabled the internal pull-ups used by the Wire library in the Wire.h/twi.c utility file. |
onehorse | 0:65aa78c10981 | 26 | We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file. |
onehorse | 0:65aa78c10981 | 27 | */ |
onehorse | 0:65aa78c10981 | 28 | |
onehorse | 1:cea9d83b8636 | 29 | #include "mbed.h" |
onehorse | 1:cea9d83b8636 | 30 | #include "MPU6050.h" |
masterholy | 3:4b0e7e8a06ae | 31 | //#include "N5110.h" |
onehorse | 0:65aa78c10981 | 32 | |
onehorse | 0:65aa78c10981 | 33 | // Using NOKIA 5110 monochrome 84 x 48 pixel display |
onehorse | 0:65aa78c10981 | 34 | // pin 9 - Serial clock out (SCLK) |
onehorse | 0:65aa78c10981 | 35 | // pin 8 - Serial data out (DIN) |
onehorse | 0:65aa78c10981 | 36 | // pin 7 - Data/Command select (D/C) |
onehorse | 0:65aa78c10981 | 37 | // pin 5 - LCD chip select (CS) |
onehorse | 0:65aa78c10981 | 38 | // pin 6 - LCD reset (RST) |
onehorse | 0:65aa78c10981 | 39 | //Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6); |
onehorse | 0:65aa78c10981 | 40 | |
masterholy | 3:4b0e7e8a06ae | 41 | AnalogIn analog_value0(A0); |
masterholy | 3:4b0e7e8a06ae | 42 | AnalogIn analog_value1(A1); |
masterholy | 3:4b0e7e8a06ae | 43 | AnalogIn analog_value2(A2); |
masterholy | 3:4b0e7e8a06ae | 44 | AnalogIn analog_value3(A3); |
masterholy | 3:4b0e7e8a06ae | 45 | AnalogIn analog_value4(A4); |
masterholy | 3:4b0e7e8a06ae | 46 | AnalogIn analog_value5(A5); |
masterholy | 3:4b0e7e8a06ae | 47 | |
masterholy | 3:4b0e7e8a06ae | 48 | |
masterholy | 3:4b0e7e8a06ae | 49 | |
masterholy | 3:4b0e7e8a06ae | 50 | |
onehorse | 1:cea9d83b8636 | 51 | float sum = 0; |
onehorse | 1:cea9d83b8636 | 52 | uint32_t sumCount = 0; |
onehorse | 1:cea9d83b8636 | 53 | |
masterholy | 3:4b0e7e8a06ae | 54 | int time_us = 0; |
masterholy | 3:4b0e7e8a06ae | 55 | unsigned int yawH,yawL,pitchH,pitchL,rowH,rowL; |
masterholy | 3:4b0e7e8a06ae | 56 | unsigned int axH,axL,ayH,ayL,azH,azL; |
masterholy | 3:4b0e7e8a06ae | 57 | unsigned int gxH,gxL,gyH,gyL,gzH,gzL; |
masterholy | 3:4b0e7e8a06ae | 58 | unsigned int f0,f1,f2; |
masterholy | 3:4b0e7e8a06ae | 59 | unsigned int baroH,baroL; |
masterholy | 3:4b0e7e8a06ae | 60 | |
masterholy | 3:4b0e7e8a06ae | 61 | |
onehorse | 1:cea9d83b8636 | 62 | MPU6050 mpu6050; |
onehorse | 1:cea9d83b8636 | 63 | |
onehorse | 1:cea9d83b8636 | 64 | Timer t; |
onehorse | 1:cea9d83b8636 | 65 | |
onehorse | 1:cea9d83b8636 | 66 | Serial pc(USBTX, USBRX); // tx, rx |
onehorse | 1:cea9d83b8636 | 67 | |
onehorse | 1:cea9d83b8636 | 68 | // VCC, SCE, RST, D/C, MOSI,S CLK, LED |
masterholy | 3:4b0e7e8a06ae | 69 | //N5110 lcd(PA_8, PB_10, PA_9, PA_6, PA_7, PA_5, PC_7); |
masterholy | 3:4b0e7e8a06ae | 70 | |
masterholy | 3:4b0e7e8a06ae | 71 | void callbackPCSerial() { |
masterholy | 3:4b0e7e8a06ae | 72 | pc.putc(255); |
masterholy | 3:4b0e7e8a06ae | 73 | pc.putc(rowH); |
masterholy | 3:4b0e7e8a06ae | 74 | pc.putc(rowL); |
masterholy | 3:4b0e7e8a06ae | 75 | pc.putc(pitchH); |
masterholy | 3:4b0e7e8a06ae | 76 | pc.putc(pitchL); |
masterholy | 3:4b0e7e8a06ae | 77 | pc.putc(yawH); |
masterholy | 3:4b0e7e8a06ae | 78 | pc.putc(yawL); |
masterholy | 3:4b0e7e8a06ae | 79 | |
masterholy | 3:4b0e7e8a06ae | 80 | //pc.putc(axH); |
masterholy | 3:4b0e7e8a06ae | 81 | //pc.putc(axL); |
masterholy | 3:4b0e7e8a06ae | 82 | //pc.putc(ayH); |
masterholy | 3:4b0e7e8a06ae | 83 | //pc.putc(ayL); |
masterholy | 3:4b0e7e8a06ae | 84 | //pc.putc(azH); |
masterholy | 3:4b0e7e8a06ae | 85 | //pc.putc(azL); |
masterholy | 3:4b0e7e8a06ae | 86 | |
masterholy | 3:4b0e7e8a06ae | 87 | //pc.putc(gxH); |
masterholy | 3:4b0e7e8a06ae | 88 | //pc.putc(gxL); |
masterholy | 3:4b0e7e8a06ae | 89 | //pc.putc(gyH); |
masterholy | 3:4b0e7e8a06ae | 90 | //pc.putc(gyL); |
masterholy | 3:4b0e7e8a06ae | 91 | //pc.putc(gzH); |
masterholy | 3:4b0e7e8a06ae | 92 | //pc.putc(gzL); |
masterholy | 3:4b0e7e8a06ae | 93 | |
masterholy | 3:4b0e7e8a06ae | 94 | //pc.putc(baroH); |
masterholy | 3:4b0e7e8a06ae | 95 | //pc.putc(baroL); |
masterholy | 3:4b0e7e8a06ae | 96 | |
masterholy | 3:4b0e7e8a06ae | 97 | //pc.putc(f0); |
masterholy | 3:4b0e7e8a06ae | 98 | //pc.putc(f1); |
masterholy | 3:4b0e7e8a06ae | 99 | //pc.putc(f2); |
masterholy | 3:4b0e7e8a06ae | 100 | |
masterholy | 3:4b0e7e8a06ae | 101 | } |
onehorse | 1:cea9d83b8636 | 102 | |
onehorse | 1:cea9d83b8636 | 103 | int main() |
onehorse | 1:cea9d83b8636 | 104 | { |
masterholy | 3:4b0e7e8a06ae | 105 | pc.baud(115200); |
masterholy | 3:4b0e7e8a06ae | 106 | pc.attach(&callbackPCSerial); |
onehorse | 1:cea9d83b8636 | 107 | //Set up I2C |
onehorse | 1:cea9d83b8636 | 108 | i2c.frequency(400000); // use fast (400 kHz) I2C |
onehorse | 1:cea9d83b8636 | 109 | |
onehorse | 1:cea9d83b8636 | 110 | t.start(); |
onehorse | 1:cea9d83b8636 | 111 | |
masterholy | 3:4b0e7e8a06ae | 112 | //lcd.init(); |
masterholy | 3:4b0e7e8a06ae | 113 | //lcd.setBrightness(0.05); |
onehorse | 1:cea9d83b8636 | 114 | |
onehorse | 1:cea9d83b8636 | 115 | |
onehorse | 1:cea9d83b8636 | 116 | // Read the WHO_AM_I register, this is a good test of communication |
onehorse | 1:cea9d83b8636 | 117 | uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050); // Read WHO_AM_I register for MPU-6050 |
onehorse | 1:cea9d83b8636 | 118 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x68\n\r"); |
onehorse | 1:cea9d83b8636 | 119 | |
onehorse | 1:cea9d83b8636 | 120 | if (whoami == 0x68) // WHO_AM_I should always be 0x68 |
onehorse | 1:cea9d83b8636 | 121 | { |
onehorse | 1:cea9d83b8636 | 122 | pc.printf("MPU6050 is online..."); |
onehorse | 1:cea9d83b8636 | 123 | wait(1); |
masterholy | 3:4b0e7e8a06ae | 124 | //lcd.clear(); |
masterholy | 3:4b0e7e8a06ae | 125 | //lcd.printString("MPU6050 OK", 0, 0); |
onehorse | 0:65aa78c10981 | 126 | |
onehorse | 1:cea9d83b8636 | 127 | |
onehorse | 1:cea9d83b8636 | 128 | mpu6050.MPU6050SelfTest(SelfTest); // Start by performing self test and reporting values |
onehorse | 1:cea9d83b8636 | 129 | pc.printf("x-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[0]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 130 | pc.printf("y-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[1]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 131 | pc.printf("z-axis self test: acceleration trim within : "); pc.printf("%f", SelfTest[2]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 132 | pc.printf("x-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[3]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 133 | pc.printf("y-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[4]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 134 | pc.printf("z-axis self test: gyration trim within : "); pc.printf("%f", SelfTest[5]); pc.printf("% of factory value \n\r"); |
onehorse | 1:cea9d83b8636 | 135 | wait(1); |
onehorse | 0:65aa78c10981 | 136 | |
onehorse | 1:cea9d83b8636 | 137 | 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) |
onehorse | 1:cea9d83b8636 | 138 | { |
onehorse | 1:cea9d83b8636 | 139 | mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration |
onehorse | 1:cea9d83b8636 | 140 | mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers |
onehorse | 1:cea9d83b8636 | 141 | mpu6050.initMPU6050(); pc.printf("MPU6050 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature |
onehorse | 0:65aa78c10981 | 142 | |
masterholy | 3:4b0e7e8a06ae | 143 | //lcd.clear(); |
masterholy | 3:4b0e7e8a06ae | 144 | //lcd.printString("MPU6050", 0, 0); |
masterholy | 3:4b0e7e8a06ae | 145 | //lcd.printString("pass self test", 0, 1); |
masterholy | 3:4b0e7e8a06ae | 146 | //lcd.printString("initializing", 0, 2); |
onehorse | 1:cea9d83b8636 | 147 | wait(2); |
onehorse | 1:cea9d83b8636 | 148 | } |
onehorse | 1:cea9d83b8636 | 149 | else |
onehorse | 1:cea9d83b8636 | 150 | { |
onehorse | 1:cea9d83b8636 | 151 | pc.printf("Device did not the pass self-test!\n\r"); |
onehorse | 0:65aa78c10981 | 152 | |
masterholy | 3:4b0e7e8a06ae | 153 | //lcd.clear(); |
masterholy | 3:4b0e7e8a06ae | 154 | //lcd.printString("MPU6050", 0, 0); |
masterholy | 3:4b0e7e8a06ae | 155 | //lcd.printString("no pass", 0, 1); |
masterholy | 3:4b0e7e8a06ae | 156 | //lcd.printString("self test", 0, 2); |
onehorse | 1:cea9d83b8636 | 157 | } |
onehorse | 1:cea9d83b8636 | 158 | } |
onehorse | 1:cea9d83b8636 | 159 | else |
onehorse | 1:cea9d83b8636 | 160 | { |
onehorse | 1:cea9d83b8636 | 161 | pc.printf("Could not connect to MPU6050: \n\r"); |
onehorse | 1:cea9d83b8636 | 162 | pc.printf("%#x \n", whoami); |
onehorse | 1:cea9d83b8636 | 163 | |
masterholy | 3:4b0e7e8a06ae | 164 | //lcd.clear(); |
masterholy | 3:4b0e7e8a06ae | 165 | //lcd.printString("MPU6050", 0, 0); |
masterholy | 3:4b0e7e8a06ae | 166 | //lcd.printString("no connection", 0, 1); |
masterholy | 3:4b0e7e8a06ae | 167 | //lcd.printString("0x", 0, 2); lcd.setXYAddress(20, 2); lcd.printChar(whoami); |
onehorse | 1:cea9d83b8636 | 168 | |
onehorse | 1:cea9d83b8636 | 169 | while(1) ; // Loop forever if communication doesn't happen |
onehorse | 0:65aa78c10981 | 170 | } |
onehorse | 0:65aa78c10981 | 171 | |
onehorse | 0:65aa78c10981 | 172 | |
onehorse | 0:65aa78c10981 | 173 | |
onehorse | 1:cea9d83b8636 | 174 | while(1) { |
onehorse | 0:65aa78c10981 | 175 | |
onehorse | 1:cea9d83b8636 | 176 | // If data ready bit set, all data registers have new data |
onehorse | 1:cea9d83b8636 | 177 | if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt |
onehorse | 1:cea9d83b8636 | 178 | mpu6050.readAccelData(accelCount); // Read the x/y/z adc values |
onehorse | 1:cea9d83b8636 | 179 | mpu6050.getAres(); |
onehorse | 0:65aa78c10981 | 180 | |
onehorse | 0:65aa78c10981 | 181 | // Now we'll calculate the accleration value into actual g's |
onehorse | 0:65aa78c10981 | 182 | ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set |
onehorse | 0:65aa78c10981 | 183 | ay = (float)accelCount[1]*aRes - accelBias[1]; |
masterholy | 3:4b0e7e8a06ae | 184 | az = (float)accelCount[2]*aRes - accelBias[2]; |
masterholy | 3:4b0e7e8a06ae | 185 | ax = (ax+5)*100; |
masterholy | 3:4b0e7e8a06ae | 186 | ay = (ay+5)*100; |
masterholy | 3:4b0e7e8a06ae | 187 | az = (az+5)*100; |
masterholy | 3:4b0e7e8a06ae | 188 | axH = ((int)ax)/256 ; |
masterholy | 3:4b0e7e8a06ae | 189 | axL = ((int)ax)%256; |
masterholy | 3:4b0e7e8a06ae | 190 | ayH = ((int)ay)/256 ; |
masterholy | 3:4b0e7e8a06ae | 191 | ayL = ((int)ay)%256; |
masterholy | 3:4b0e7e8a06ae | 192 | azH = ((int)az)/256 ; |
masterholy | 3:4b0e7e8a06ae | 193 | azL = ((int)az)%256; |
onehorse | 0:65aa78c10981 | 194 | |
onehorse | 1:cea9d83b8636 | 195 | mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values |
onehorse | 1:cea9d83b8636 | 196 | mpu6050.getGres(); |
onehorse | 0:65aa78c10981 | 197 | |
onehorse | 0:65aa78c10981 | 198 | // Calculate the gyro value into actual degrees per second |
onehorse | 1:cea9d83b8636 | 199 | gx = (float)gyroCount[0]*gRes; // - gyroBias[0]; // get actual gyro value, this depends on scale being set |
onehorse | 1:cea9d83b8636 | 200 | gy = (float)gyroCount[1]*gRes; // - gyroBias[1]; |
onehorse | 1:cea9d83b8636 | 201 | gz = (float)gyroCount[2]*gRes; // - gyroBias[2]; |
masterholy | 3:4b0e7e8a06ae | 202 | gx = (gx+500)*10; |
masterholy | 3:4b0e7e8a06ae | 203 | gy = (gy+500)*10; |
masterholy | 3:4b0e7e8a06ae | 204 | gz = (gz+500)*10; |
masterholy | 3:4b0e7e8a06ae | 205 | gxH = ((int)gx)/256 ; |
masterholy | 3:4b0e7e8a06ae | 206 | gxL = ((int)gx)%256; |
masterholy | 3:4b0e7e8a06ae | 207 | gyH = ((int)gy)/256 ; |
masterholy | 3:4b0e7e8a06ae | 208 | gyL = ((int)gy)%256; |
masterholy | 3:4b0e7e8a06ae | 209 | gzH = ((int)gz)/256 ; |
masterholy | 3:4b0e7e8a06ae | 210 | gzL = ((int)gz)%256; |
onehorse | 0:65aa78c10981 | 211 | |
onehorse | 1:cea9d83b8636 | 212 | tempCount = mpu6050.readTempData(); // Read the x/y/z adc values |
onehorse | 0:65aa78c10981 | 213 | temperature = (tempCount) / 340. + 36.53; // Temperature in degrees Centigrade |
onehorse | 0:65aa78c10981 | 214 | } |
onehorse | 0:65aa78c10981 | 215 | |
onehorse | 0:65aa78c10981 | 216 | Now = t.read_us(); |
onehorse | 1:cea9d83b8636 | 217 | deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update |
onehorse | 0:65aa78c10981 | 218 | lastUpdate = Now; |
onehorse | 1:cea9d83b8636 | 219 | |
onehorse | 1:cea9d83b8636 | 220 | sum += deltat; |
onehorse | 1:cea9d83b8636 | 221 | sumCount++; |
onehorse | 1:cea9d83b8636 | 222 | |
onehorse | 0:65aa78c10981 | 223 | if(lastUpdate - firstUpdate > 10000000.0f) { |
onehorse | 1:cea9d83b8636 | 224 | beta = 0.04; // decrease filter gain after stabilized |
onehorse | 1:cea9d83b8636 | 225 | zeta = 0.015; // increasey bias drift gain after stabilized |
onehorse | 0:65aa78c10981 | 226 | } |
onehorse | 1:cea9d83b8636 | 227 | |
onehorse | 0:65aa78c10981 | 228 | // Pass gyro rate as rad/s |
onehorse | 1:cea9d83b8636 | 229 | mpu6050.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f); |
onehorse | 0:65aa78c10981 | 230 | |
onehorse | 0:65aa78c10981 | 231 | // Serial print and/or display at 0.5 s rate independent of data rates |
onehorse | 0:65aa78c10981 | 232 | delt_t = t.read_ms() - count; |
masterholy | 3:4b0e7e8a06ae | 233 | if (delt_t > 50) { // update LCD once per half-second independent of read rate |
onehorse | 1:cea9d83b8636 | 234 | |
masterholy | 3:4b0e7e8a06ae | 235 | //pc.printf("ax = %f", 1000*ax); |
masterholy | 3:4b0e7e8a06ae | 236 | //pc.printf(" ay = %f", 1000*ay); |
masterholy | 3:4b0e7e8a06ae | 237 | //pc.printf(" az = %f mg\n\r", 1000*az); |
onehorse | 0:65aa78c10981 | 238 | |
masterholy | 3:4b0e7e8a06ae | 239 | //pc.printf("gx = %f", gx); |
masterholy | 3:4b0e7e8a06ae | 240 | //pc.printf(" gy = %f", gy); |
masterholy | 3:4b0e7e8a06ae | 241 | //pc.printf(" gz = %f deg/s\n\r", gz); |
onehorse | 1:cea9d83b8636 | 242 | |
masterholy | 3:4b0e7e8a06ae | 243 | //pc.printf(" temperature = %f C\n\r", temperature); |
onehorse | 0:65aa78c10981 | 244 | |
masterholy | 3:4b0e7e8a06ae | 245 | //pc.printf("q0 = %f\n\r", q[0]); |
masterholy | 3:4b0e7e8a06ae | 246 | //pc.printf("q1 = %f\n\r", q[1]); |
masterholy | 3:4b0e7e8a06ae | 247 | //pc.printf("q2 = %f\n\r", q[2]); |
masterholy | 3:4b0e7e8a06ae | 248 | //pc.printf("q3 = %f\n\r", q[3]); |
onehorse | 1:cea9d83b8636 | 249 | |
masterholy | 3:4b0e7e8a06ae | 250 | //lcd.clear(); |
masterholy | 3:4b0e7e8a06ae | 251 | //lcd.printString("MPU6050", 0, 0); |
masterholy | 3:4b0e7e8a06ae | 252 | //lcd.printString("x y z", 0, 1); |
masterholy | 3:4b0e7e8a06ae | 253 | //lcd.setXYAddress(0, 2); lcd.printChar((char)(1000*ax)); |
masterholy | 3:4b0e7e8a06ae | 254 | //lcd.setXYAddress(20, 2); lcd.printChar((char)(1000*ay)); |
masterholy | 3:4b0e7e8a06ae | 255 | //lcd.setXYAddress(40, 2); lcd.printChar((char)(1000*az)); lcd.printString("mg", 66, 2); |
onehorse | 1:cea9d83b8636 | 256 | |
onehorse | 0:65aa78c10981 | 257 | |
onehorse | 0:65aa78c10981 | 258 | // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation. |
onehorse | 0:65aa78c10981 | 259 | // In this coordinate system, the positive z-axis is down toward Earth. |
onehorse | 0:65aa78c10981 | 260 | // 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:65aa78c10981 | 261 | // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative. |
onehorse | 0:65aa78c10981 | 262 | // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll. |
onehorse | 0:65aa78c10981 | 263 | // These arise from the definition of the homogeneous rotation matrix constructed from quaternions. |
onehorse | 0:65aa78c10981 | 264 | // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be |
onehorse | 0:65aa78c10981 | 265 | // applied in the correct order which for this configuration is yaw, pitch, and then roll. |
onehorse | 0:65aa78c10981 | 266 | // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links. |
onehorse | 0:65aa78c10981 | 267 | 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:65aa78c10981 | 268 | pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2])); |
onehorse | 0:65aa78c10981 | 269 | 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:65aa78c10981 | 270 | pitch *= 180.0f / PI; |
onehorse | 0:65aa78c10981 | 271 | yaw *= 180.0f / PI; |
onehorse | 0:65aa78c10981 | 272 | roll *= 180.0f / PI; |
onehorse | 0:65aa78c10981 | 273 | |
onehorse | 1:cea9d83b8636 | 274 | // pc.printf("Yaw, Pitch, Roll: \n\r"); |
onehorse | 1:cea9d83b8636 | 275 | // pc.printf("%f", yaw); |
onehorse | 1:cea9d83b8636 | 276 | // pc.printf(", "); |
onehorse | 1:cea9d83b8636 | 277 | // pc.printf("%f", pitch); |
onehorse | 1:cea9d83b8636 | 278 | // pc.printf(", "); |
onehorse | 1:cea9d83b8636 | 279 | // pc.printf("%f\n\r", roll); |
onehorse | 1:cea9d83b8636 | 280 | // pc.printf("average rate = "); pc.printf("%f", (sumCount/sum)); pc.printf(" Hz\n\r"); |
onehorse | 0:65aa78c10981 | 281 | |
onehorse | 1:cea9d83b8636 | 282 | pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll); |
masterholy | 3:4b0e7e8a06ae | 283 | //pc.printf("average rate = %f\n\r", (float) sumCount/sum); |
onehorse | 1:cea9d83b8636 | 284 | |
onehorse | 1:cea9d83b8636 | 285 | myled= !myled; |
onehorse | 1:cea9d83b8636 | 286 | count = t.read_ms(); |
onehorse | 1:cea9d83b8636 | 287 | sum = 0; |
onehorse | 1:cea9d83b8636 | 288 | sumCount = 0; |
onehorse | 0:65aa78c10981 | 289 | } |
onehorse | 0:65aa78c10981 | 290 | } |
onehorse | 1:cea9d83b8636 | 291 | |
onehorse | 1:cea9d83b8636 | 292 | } |