Veloc

Dependencies:   mbed

Committer:
kong4580
Date:
Fri Nov 02 13:11:21 2018 +0000
Revision:
1:44a18ed16bae
Parent:
0:3af3f05e8f7d
Child:
2:1e20c0376eb6
Velocity x y z

Who changed what in which revision?

UserRevisionLine numberNew 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 1:44a18ed16bae 55 static float acc_xyz[3]={0,0,0};
kong4580 1:44a18ed16bae 56 static float v_xyz[3]={0,0,0};
kong4580 1:44a18ed16bae 57 float displacement_xyz[3]={0,0,0};
kong4580 1:44a18ed16bae 58 void AccelToVelocity(float ax,float ay,float az,float dt){
kong4580 1:44a18ed16bae 59 static float old_acc[3]={0,0,0};
kong4580 1:44a18ed16bae 60 static float old_v[3]={0,0,0};
kong4580 1:44a18ed16bae 61 float delta_ax=ax-old_acc[0];
kong4580 1:44a18ed16bae 62 float delta_ay=ay-old_acc[1];
kong4580 1:44a18ed16bae 63 float delta_az=az-old_acc[2];
kong4580 1:44a18ed16bae 64 old_acc[0]=ax;
kong4580 1:44a18ed16bae 65 old_acc[1]=ay;
kong4580 1:44a18ed16bae 66 old_acc[2]=az;
kong4580 1:44a18ed16bae 67 old_v[0]=v_xyz[0]-old_v[0];
kong4580 1:44a18ed16bae 68 old_v[1]=v_xyz[1]-old_v[1];
kong4580 1:44a18ed16bae 69 old_v[2]=v_xyz[2]-old_v[2];
kong4580 1:44a18ed16bae 70 v_xyz[0]=old_v[0]+delta_ax*dt;
kong4580 1:44a18ed16bae 71 v_xyz[1]=old_v[1]+delta_ay*dt;
kong4580 1:44a18ed16bae 72 v_xyz[2]=old_v[2]+delta_az*dt;
kong4580 1:44a18ed16bae 73
kong4580 1:44a18ed16bae 74 //pc.printf("%f %f %f \n",old_acc[]);
kong4580 1:44a18ed16bae 75
kong4580 1:44a18ed16bae 76
kong4580 1:44a18ed16bae 77 }
kong4580 0:3af3f05e8f7d 78 int main()
kong4580 1:44a18ed16bae 79 {
kong4580 0:3af3f05e8f7d 80 pc.baud(9600);
kong4580 0:3af3f05e8f7d 81
kong4580 0:3af3f05e8f7d 82 //Set up I2C
kong4580 0:3af3f05e8f7d 83 i2c.frequency(400000); // use fast (400 kHz) I2C
kong4580 0:3af3f05e8f7d 84
kong4580 0:3af3f05e8f7d 85 // pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
kong4580 0:3af3f05e8f7d 86
kong4580 0:3af3f05e8f7d 87 t.start();
kong4580 0:3af3f05e8f7d 88
kong4580 0:3af3f05e8f7d 89
kong4580 0:3af3f05e8f7d 90
kong4580 0:3af3f05e8f7d 91
kong4580 0:3af3f05e8f7d 92 // Read the WHO_AM_I register, this is a good test of communication
kong4580 0:3af3f05e8f7d 93 uint8_t whoami = mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250
kong4580 0:3af3f05e8f7d 94 // pc.printf("I AM 0x%x\n\r", whoami);
kong4580 0:3af3f05e8f7d 95 // pc.printf("I SHOULD BE 0x71\n\r");
kong4580 0:3af3f05e8f7d 96
kong4580 0:3af3f05e8f7d 97 if (whoami == 0x71) { // WHO_AM_I should always be 0x68
kong4580 0:3af3f05e8f7d 98 // pc.printf("MPU9250 WHO_AM_I is 0x%x\n\r", whoami);
kong4580 0:3af3f05e8f7d 99 // pc.printf("MPU9250 is online...\n\r");
kong4580 0:3af3f05e8f7d 100
kong4580 0:3af3f05e8f7d 101 // sprintf(buffer, "0x%x", whoami);
kong4580 0:3af3f05e8f7d 102
kong4580 0:3af3f05e8f7d 103 wait(1);
kong4580 0:3af3f05e8f7d 104
kong4580 0:3af3f05e8f7d 105 mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
kong4580 0:3af3f05e8f7d 106 mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values
kong4580 0:3af3f05e8f7d 107 /* pc.printf("x-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[0]);
kong4580 0:3af3f05e8f7d 108 pc.printf("y-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[1]);
kong4580 0:3af3f05e8f7d 109 pc.printf("z-axis self test: acceleration trim within : %f % of factory value\n\r", SelfTest[2]);
kong4580 0:3af3f05e8f7d 110 pc.printf("x-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[3]);
kong4580 0:3af3f05e8f7d 111 pc.printf("y-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[4]);
kong4580 0:3af3f05e8f7d 112 pc.printf("z-axis self test: gyration trim within : %f % of factory value\n\r", SelfTest[5]);*/
kong4580 1:44a18ed16bae 113 // if(boardbtn==0){
kong4580 0:3af3f05e8f7d 114 // wait(2);
kong4580 0:3af3f05e8f7d 115 mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
kong4580 0:3af3f05e8f7d 116 pc.printf("x gyro bias = %f\n\r", gyroBias[0]);
kong4580 0:3af3f05e8f7d 117 pc.printf("y gyro bias = %f\n\r", gyroBias[1]);
kong4580 0:3af3f05e8f7d 118 pc.printf("z gyro bias = %f\n\r", gyroBias[2]);
kong4580 0:3af3f05e8f7d 119 pc.printf("x accel bias = %f\n\r", accelBias[0]);
kong4580 0:3af3f05e8f7d 120 pc.printf("y accel bias = %f\n\r", accelBias[1]);
kong4580 0:3af3f05e8f7d 121 pc.printf("z accel bias = %f\n\r", accelBias[2]);
kong4580 0:3af3f05e8f7d 122 wait(2);
kong4580 0:3af3f05e8f7d 123
kong4580 1:44a18ed16bae 124 /*
kong4580 0:3af3f05e8f7d 125 // -----------------------------------------------------------------
kong4580 0:3af3f05e8f7d 126 }else{
kong4580 0:3af3f05e8f7d 127 gyroBias[0]=-1.244275;
kong4580 0:3af3f05e8f7d 128 gyroBias[1]=0.122137;
kong4580 0:3af3f05e8f7d 129 gyroBias[2]=-0.717557;
kong4580 0:3af3f05e8f7d 130 accelBias[0]=0.007996; // For Manual Calibate with button
kong4580 0:3af3f05e8f7d 131 accelBias[1]=0.021240;
kong4580 0:3af3f05e8f7d 132 accelBias[2]=-0.020508;
kong4580 0:3af3f05e8f7d 133 }
kong4580 0:3af3f05e8f7d 134 //------------------------------------------------------------------
kong4580 1:44a18ed16bae 135 */
kong4580 0:3af3f05e8f7d 136 mpu9250.initMPU9250();
kong4580 0:3af3f05e8f7d 137 // pc.printf("MPU9250 initialized for active data mode....\n\r"); // Initialize device for active mode read of acclerometer, gyroscope, and temperature
kong4580 0:3af3f05e8f7d 138 mpu9250.initAK8963(magCalibration);
kong4580 0:3af3f05e8f7d 139 /* pc.printf("AK8963 initialized for active data mode....\n\r"); // Initialize device for active mode read of magnetometer
kong4580 0:3af3f05e8f7d 140 pc.printf("Accelerometer full-scale range = %f g\n\r", 2.0f*(float)(1<<Ascale));
kong4580 0:3af3f05e8f7d 141 pc.printf("Gyroscope full-scale range = %f deg/s\n\r", 250.0f*(float)(1<<Gscale));
kong4580 0:3af3f05e8f7d 142 if(Mscale == 0) pc.printf("Magnetometer resolution = 14 bits\n\r");
kong4580 0:3af3f05e8f7d 143 if(Mscale == 1) pc.printf("Magnetometer resolution = 16 bits\n\r");
kong4580 0:3af3f05e8f7d 144 if(Mmode == 2) pc.printf("Magnetometer ODR = 8 Hz\n\r");
kong4580 0:3af3f05e8f7d 145 if(Mmode == 6) pc.printf("Magnetometer ODR = 100 Hz\n\r");
kong4580 0:3af3f05e8f7d 146 wait(1);
kong4580 0:3af3f05e8f7d 147 */
kong4580 0:3af3f05e8f7d 148 } else {
kong4580 0:3af3f05e8f7d 149 pc.printf("Could not connect to MPU9250: \n\r");
kong4580 0:3af3f05e8f7d 150 pc.printf("%#x \n", whoami);
kong4580 0:3af3f05e8f7d 151
kong4580 0:3af3f05e8f7d 152
kong4580 0:3af3f05e8f7d 153 sprintf(buffer, "WHO_AM_I 0x%x", whoami);
kong4580 0:3af3f05e8f7d 154
kong4580 0:3af3f05e8f7d 155
kong4580 0:3af3f05e8f7d 156 while(1) ; // Loop forever if communication doesn't happen
kong4580 0:3af3f05e8f7d 157
kong4580 0:3af3f05e8f7d 158 }
kong4580 0:3af3f05e8f7d 159
kong4580 0:3af3f05e8f7d 160 mpu9250.getAres(); // Get accelerometer sensitivity
kong4580 0:3af3f05e8f7d 161 mpu9250.getGres(); // Get gyro sensitivity
kong4580 0:3af3f05e8f7d 162 mpu9250.getMres(); // Get magnetometer sensitivity
kong4580 0:3af3f05e8f7d 163 // pc.printf("Accelerometer sensitivity is %f LSB/g \n\r", 1.0f/aRes);
kong4580 0:3af3f05e8f7d 164 // pc.printf("Gyroscope sensitivity is %f LSB/deg/s \n\r", 1.0f/gRes);
kong4580 0:3af3f05e8f7d 165 // pc.printf("Magnetometer sensitivity is %f LSB/G \n\r", 1.0f/mRes);
kong4580 0:3af3f05e8f7d 166 magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated
kong4580 0:3af3f05e8f7d 167 magbias[1] = +120.; // User environmental x-axis correction in milliGauss
kong4580 0:3af3f05e8f7d 168 magbias[2] = +125.; // User environmental x-axis correction in milliGauss
kong4580 0:3af3f05e8f7d 169
kong4580 0:3af3f05e8f7d 170 while(1) {
kong4580 0:3af3f05e8f7d 171
kong4580 0:3af3f05e8f7d 172 // If intPin goes high, all data registers have new data
kong4580 0:3af3f05e8f7d 173 if(mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt
kong4580 0:3af3f05e8f7d 174
kong4580 0:3af3f05e8f7d 175 mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
kong4580 0:3af3f05e8f7d 176 // Now we'll calculate the accleration value into actual g's
kong4580 0:3af3f05e8f7d 177 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
kong4580 0:3af3f05e8f7d 178 ay = (float)accelCount[1]*aRes - accelBias[1];
kong4580 0:3af3f05e8f7d 179 az = (float)accelCount[2]*aRes - accelBias[2];
kong4580 0:3af3f05e8f7d 180
kong4580 0:3af3f05e8f7d 181 mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values
kong4580 0:3af3f05e8f7d 182 // Calculate the gyro value into actual degrees per second
kong4580 0:3af3f05e8f7d 183 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
kong4580 0:3af3f05e8f7d 184 gy = (float)gyroCount[1]*gRes - gyroBias[1];
kong4580 0:3af3f05e8f7d 185 gz = (float)gyroCount[2]*gRes - gyroBias[2];
kong4580 0:3af3f05e8f7d 186
kong4580 0:3af3f05e8f7d 187 mpu9250.readMagData(magCount); // Read the x/y/z adc values
kong4580 0:3af3f05e8f7d 188 // Calculate the magnetometer values in milliGauss
kong4580 0:3af3f05e8f7d 189 // Include factory calibration per data sheet and user environmental corrections
kong4580 0:3af3f05e8f7d 190 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
kong4580 0:3af3f05e8f7d 191 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
kong4580 0:3af3f05e8f7d 192 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
kong4580 0:3af3f05e8f7d 193 }
kong4580 0:3af3f05e8f7d 194
kong4580 0:3af3f05e8f7d 195 Now = t.read_us();
kong4580 0:3af3f05e8f7d 196 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
kong4580 0:3af3f05e8f7d 197 lastUpdate = Now;
kong4580 0:3af3f05e8f7d 198
kong4580 0:3af3f05e8f7d 199 sum += deltat;
kong4580 0:3af3f05e8f7d 200 sumCount++;
kong4580 0:3af3f05e8f7d 201
kong4580 0:3af3f05e8f7d 202 // if(lastUpdate - firstUpdate > 10000000.0f) {
kong4580 0:3af3f05e8f7d 203 // beta = 0.04; // decrease filter gain after stabilized
kong4580 0:3af3f05e8f7d 204 // zeta = 0.015; // increasey bias drift gain after stabilized
kong4580 0:3af3f05e8f7d 205 // }
kong4580 0:3af3f05e8f7d 206
kong4580 0:3af3f05e8f7d 207 // Pass gyro rate as rad/s
kong4580 0:3af3f05e8f7d 208 // mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
kong4580 0:3af3f05e8f7d 209 mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
kong4580 0:3af3f05e8f7d 210
kong4580 0:3af3f05e8f7d 211 // Serial print and/or display at 0.5 s rate independent of data rates
kong4580 0:3af3f05e8f7d 212 delt_t = t.read_ms() - count;
kong4580 0:3af3f05e8f7d 213 if (delt_t > 100) { // update LCD once per half-second independent of read rate
kong4580 0:3af3f05e8f7d 214
kong4580 0:3af3f05e8f7d 215 //pc.printf("ax = %f", 1000*ax);
kong4580 0:3af3f05e8f7d 216 //pc.printf(" ay = %f", 1000*ay);
kong4580 0:3af3f05e8f7d 217 // pc.printf(" az = %f mg\n\r", 1000*az);
kong4580 0:3af3f05e8f7d 218
kong4580 0:3af3f05e8f7d 219 // pc.printf("gx = %f", gx);
kong4580 0:3af3f05e8f7d 220 // pc.printf(" gy = %f", gy);
kong4580 0:3af3f05e8f7d 221 // pc.printf(" gz = %f deg/s\n\r", gz);
kong4580 0:3af3f05e8f7d 222
kong4580 0:3af3f05e8f7d 223 // pc.printf("gx = %f", mx);
kong4580 0:3af3f05e8f7d 224 // pc.printf(" gy = %f", my);
kong4580 0:3af3f05e8f7d 225 // pc.printf(" gz = %f mG\n\r", mz);
kong4580 0:3af3f05e8f7d 226
kong4580 0:3af3f05e8f7d 227 tempCount = mpu9250.readTempData(); // Read the adc values
kong4580 0:3af3f05e8f7d 228 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
kong4580 0:3af3f05e8f7d 229 // pc.printf(" temperature = %f C\n\r", temperature);
kong4580 0:3af3f05e8f7d 230 //
kong4580 0:3af3f05e8f7d 231 // pc.printf("q0 = %f\n\r", q[0]);
kong4580 0:3af3f05e8f7d 232 // pc.printf("q1 = %f\n\r", q[1]);
kong4580 0:3af3f05e8f7d 233 // pc.printf("q2 = %f\n\r", q[2]);
kong4580 0:3af3f05e8f7d 234 // pc.printf("q3 = %f\n\r", q[3]);
kong4580 0:3af3f05e8f7d 235
kong4580 0:3af3f05e8f7d 236 /* lcd.clear();
kong4580 0:3af3f05e8f7d 237 lcd.printString("MPU9250", 0, 0);
kong4580 0:3af3f05e8f7d 238 lcd.printString("x y z", 0, 1);
kong4580 0:3af3f05e8f7d 239 sprintf(buffer, "%d %d %d mg", (int)(1000.0f*ax), (int)(1000.0f*ay), (int)(1000.0f*az));
kong4580 0:3af3f05e8f7d 240 lcd.printString(buffer, 0, 2);
kong4580 0:3af3f05e8f7d 241 sprintf(buffer, "%d %d %d deg/s", (int)gx, (int)gy, (int)gz);
kong4580 0:3af3f05e8f7d 242 lcd.printString(buffer, 0, 3);
kong4580 0:3af3f05e8f7d 243 sprintf(buffer, "%d %d %d mG", (int)mx, (int)my, (int)mz);
kong4580 0:3af3f05e8f7d 244 lcd.printString(buffer, 0, 4);
kong4580 0:3af3f05e8f7d 245 */
kong4580 0:3af3f05e8f7d 246 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
kong4580 0:3af3f05e8f7d 247 // In this coordinate system, the positive z-axis is down toward Earth.
kong4580 0:3af3f05e8f7d 248 // 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 249 // 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 250 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
kong4580 0:3af3f05e8f7d 251 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
kong4580 0:3af3f05e8f7d 252 // 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 253 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
kong4580 0:3af3f05e8f7d 254 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
kong4580 0:3af3f05e8f7d 255 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 256 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
kong4580 0:3af3f05e8f7d 257 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 258 pitch *= 180.0f / PI;
kong4580 0:3af3f05e8f7d 259 yaw *= 180.0f / PI;
kong4580 0:3af3f05e8f7d 260 yaw -= 85.0f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
kong4580 0:3af3f05e8f7d 261 roll *= 180.0f / PI;
kong4580 0:3af3f05e8f7d 262
kong4580 1:44a18ed16bae 263 // pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
kong4580 0:3af3f05e8f7d 264 // pc.printf("average rate = %f\n\r", (float) sumCount/sum);
kong4580 0:3af3f05e8f7d 265 // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll);
kong4580 0:3af3f05e8f7d 266 // lcd.printString(buffer, 0, 4);
kong4580 0:3af3f05e8f7d 267 // sprintf(buffer, "rate = %f", (float) sumCount/sum);
kong4580 0:3af3f05e8f7d 268 // lcd.printString(buffer, 0, 5);
kong4580 0:3af3f05e8f7d 269
kong4580 0:3af3f05e8f7d 270 myled= !myled;
kong4580 0:3af3f05e8f7d 271 count = t.read_ms();
kong4580 1:44a18ed16bae 272 acc_xyz[0]=ax;
kong4580 1:44a18ed16bae 273 acc_xyz[1]=ay;
kong4580 1:44a18ed16bae 274 acc_xyz[2]=az;
kong4580 1:44a18ed16bae 275
kong4580 1:44a18ed16bae 276 AccelToVelocity(acc_xyz[0],acc_xyz[1],acc_xyz[2],float(delt_t));//deltat*1000000
kong4580 1:44a18ed16bae 277 pc.printf("%f %f %f \n",v_xyz[0],v_xyz[1],v_xyz[2]);
kong4580 1:44a18ed16bae 278 //pc.printf("%d\n",count);
kong4580 0:3af3f05e8f7d 279 if(count > 1<<21) {
kong4580 0:3af3f05e8f7d 280 t.start(); // start the timer over again if ~30 minutes has passed
kong4580 0:3af3f05e8f7d 281 count = 0;
kong4580 0:3af3f05e8f7d 282 deltat= 0;
kong4580 0:3af3f05e8f7d 283 lastUpdate = t.read_us();
kong4580 0:3af3f05e8f7d 284 }
kong4580 0:3af3f05e8f7d 285 sum = 0;
kong4580 0:3af3f05e8f7d 286 sumCount = 0;
kong4580 1:44a18ed16bae 287 //-----------------------------------------------------------------------------------------
kong4580 1:44a18ed16bae 288
kong4580 1:44a18ed16bae 289
kong4580 0:3af3f05e8f7d 290 }
kong4580 0:3af3f05e8f7d 291 }
kong4580 1:44a18ed16bae 292
kong4580 0:3af3f05e8f7d 293 }