bamlor nuttymaisuay

Dependencies:   mbed

Committer:
jaybehandsome
Date:
Mon Dec 11 01:12:18 2017 +0000
Revision:
4:9cc307f25dc9
Parent:
3:46cc9d386ff4
Child:
5:738285670edf
fucking damn

Who changed what in which revision?

UserRevisionLine numberNew 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 4:9cc307f25dc9 36 float x = 10,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 4:9cc307f25dc9 49 BusOut B(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), C(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 4:9cc307f25dc9 53
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 4:9cc307f25dc9 129 // mpu9250.readAccelData(accelCount); // Read the x/y/z adc values
jaybehandsome 4:9cc307f25dc9 130 // // Now we'll calculate the accleration value into actual g's
jaybehandsome 4:9cc307f25dc9 131 // ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
jaybehandsome 4:9cc307f25dc9 132 // ay = (float)accelCount[1]*aRes - accelBias[1];
jaybehandsome 4:9cc307f25dc9 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 4:9cc307f25dc9 141 // mpu9250.readMagData(magCount); // Read the x/y/z adc values
jaybehandsome 4:9cc307f25dc9 142 // // Calculate the magnetometer values in milliGauss
jaybehandsome 4:9cc307f25dc9 143 // // Include factory calibration per data sheet and user environmental corrections
jaybehandsome 4:9cc307f25dc9 144 // mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set
jaybehandsome 4:9cc307f25dc9 145 // my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1];
jaybehandsome 4:9cc307f25dc9 146 // mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
jaybehandsome 4:9cc307f25dc9 147 // }
jaybehandsome 0:1e46c1a32764 148
jaybehandsome 4:9cc307f25dc9 149 // Now = t.read_us();
jaybehandsome 4:9cc307f25dc9 150 // deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
jaybehandsome 4:9cc307f25dc9 151 // lastUpdate = Now;
jaybehandsome 0:1e46c1a32764 152
jaybehandsome 4:9cc307f25dc9 153 //sum += deltat;
jaybehandsome 4:9cc307f25dc9 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 4:9cc307f25dc9 166 delt_t = t.read_us() - count;
jaybehandsome 4:9cc307f25dc9 167 if (delt_t > 1) { // update LCD once per half-second independent of read rate
jaybehandsome 4:9cc307f25dc9 168 // pc.printf("delt_t = %f\n",delt_t);
jaybehandsome 4:9cc307f25dc9 169
jaybehandsome 4:9cc307f25dc9 170 // if (gz < 1)
jaybehandsome 4:9cc307f25dc9 171 // {
jaybehandsome 4:9cc307f25dc9 172 // x = 0;
jaybehandsome 4:9cc307f25dc9 173 // }
jaybehandsome 4:9cc307f25dc9 174 // else if (gz < 50)
jaybehandsome 4:9cc307f25dc9 175 // {
jaybehandsome 4:9cc307f25dc9 176 // x = 19;
jaybehandsome 4:9cc307f25dc9 177 // }
jaybehandsome 4:9cc307f25dc9 178 // else
jaybehandsome 4:9cc307f25dc9 179 // {
jaybehandsome 4:9cc307f25dc9 180 // x = 13;
jaybehandsome 4:9cc307f25dc9 181 // }
jaybehandsome 4:9cc307f25dc9 182 // x=35;
jaybehandsome 4:9cc307f25dc9 183 if(gz<0)
jaybehandsome 3:46cc9d386ff4 184 {
jaybehandsome 4:9cc307f25dc9 185 x = 0;
jaybehandsome 4:9cc307f25dc9 186 }
jaybehandsome 4:9cc307f25dc9 187 x = 89.1 ;
jaybehandsome 4:9cc307f25dc9 188 posA += (gz*delt_t*x/1000000);
jaybehandsome 4:9cc307f25dc9 189 posA = fmod(posA,360);
jaybehandsome 4:9cc307f25dc9 190 posB = fmod(posA + 240,360);
jaybehandsome 4:9cc307f25dc9 191 posC = fmod(posA + 120,360);
jaybehandsome 4:9cc307f25dc9 192 //pc.printf("x = %f", x);
jaybehandsome 4:9cc307f25dc9 193
jaybehandsome 2:af822f5a5120 194
jaybehandsome 3:46cc9d386ff4 195 // if (posA >= 360 && posA < 720)
jaybehandsome 3:46cc9d386ff4 196 // {
jaybehandsome 2:af822f5a5120 197 // A = 0x7F;
jaybehandsome 2:af822f5a5120 198 // B = 0x7F;
jaybehandsome 2:af822f5a5120 199 // C = 0x7F;
jaybehandsome 2:af822f5a5120 200 // }
jaybehandsome 3:46cc9d386ff4 201 // else if (posA >= 720 && posA < 1080)
jaybehandsome 3:46cc9d386ff4 202 // {
jaybehandsome 3:46cc9d386ff4 203 // A = 0x7F;
jaybehandsome 3:46cc9d386ff4 204 // B = 0;
jaybehandsome 3:46cc9d386ff4 205 // C = 0;
jaybehandsome 3:46cc9d386ff4 206 // }
jaybehandsome 3:46cc9d386ff4 207 // else if (posA >= 1080 && posA < 1440)
jaybehandsome 3:46cc9d386ff4 208 // {
jaybehandsome 3:46cc9d386ff4 209 // A = 0x7F;
jaybehandsome 3:46cc9d386ff4 210 // B = 0x7F;
jaybehandsome 3:46cc9d386ff4 211 // C = 0x7F;
jaybehandsome 3:46cc9d386ff4 212 // }
jaybehandsome 3:46cc9d386ff4 213 // else if (posA >= 1440 && posA < 1800)
jaybehandsome 3:46cc9d386ff4 214 // {
jaybehandsome 3:46cc9d386ff4 215 // A = 0x7F;
jaybehandsome 3:46cc9d386ff4 216 // B = 0;
jaybehandsome 3:46cc9d386ff4 217 // C = 0;
jaybehandsome 3:46cc9d386ff4 218 // }
jaybehandsome 3:46cc9d386ff4 219 // else if (posA >= 1800)
jaybehandsome 3:46cc9d386ff4 220 // {
jaybehandsome 3:46cc9d386ff4 221 // A = 0x7F;
jaybehandsome 3:46cc9d386ff4 222 // B = 0x7F;
jaybehandsome 3:46cc9d386ff4 223 // C = 0x7F;
jaybehandsome 3:46cc9d386ff4 224 // }
jaybehandsome 2:af822f5a5120 225 // else
jaybehandsome 2:af822f5a5120 226 // {
jaybehandsome 2:af822f5a5120 227 // A = 0;
jaybehandsome 2:af822f5a5120 228 // B = 0;
jaybehandsome 2:af822f5a5120 229 // C = 0;
jaybehandsome 2:af822f5a5120 230 // }
jaybehandsome 2:af822f5a5120 231 // if (posA >= 360)
jaybehandsome 2:af822f5a5120 232 // {
jaybehandsome 2:af822f5a5120 233 // break;
jaybehandsome 2:af822f5a5120 234 // }
jaybehandsome 4:9cc307f25dc9 235
jaybehandsome 4:9cc307f25dc9 236 if (posA > 0 && posA < 12)
jaybehandsome 3:46cc9d386ff4 237 {
jaybehandsome 4:9cc307f25dc9 238 A = 0x70;
jaybehandsome 4:9cc307f25dc9 239 }
jaybehandsome 4:9cc307f25dc9 240 else if(posA > 12 && posA < 24 )
jaybehandsome 4:9cc307f25dc9 241 {
jaybehandsome 4:9cc307f25dc9 242 A = 0x08;
jaybehandsome 3:46cc9d386ff4 243 }
jaybehandsome 4:9cc307f25dc9 244 else if(posA > 24 && posA < 36 )
jaybehandsome 4:9cc307f25dc9 245 {
jaybehandsome 4:9cc307f25dc9 246 A = 0x07;
jaybehandsome 4:9cc307f25dc9 247 }
jaybehandsome 4:9cc307f25dc9 248 else if(posA > 36 && posA < 48 )
jaybehandsome 3:46cc9d386ff4 249 {
jaybehandsome 4:9cc307f25dc9 250 A = 0x08;
jaybehandsome 4:9cc307f25dc9 251 }
jaybehandsome 4:9cc307f25dc9 252 else if(posA > 48 && posA < 60 )
jaybehandsome 4:9cc307f25dc9 253 {
jaybehandsome 4:9cc307f25dc9 254 A = 0x70;
jaybehandsome 4:9cc307f25dc9 255 }
jaybehandsome 3:46cc9d386ff4 256 else
jaybehandsome 3:46cc9d386ff4 257 {
jaybehandsome 3:46cc9d386ff4 258 A = 0x00;
jaybehandsome 3:46cc9d386ff4 259 }
jaybehandsome 3:46cc9d386ff4 260 if (posB > 0 && posB < 30)
jaybehandsome 3:46cc9d386ff4 261 {
jaybehandsome 4:9cc307f25dc9 262 B = 0x70;
jaybehandsome 4:9cc307f25dc9 263 }
jaybehandsome 4:9cc307f25dc9 264 else if(posB > 12 && posB < 24 )
jaybehandsome 4:9cc307f25dc9 265 {
jaybehandsome 4:9cc307f25dc9 266 B = 0x08;
jaybehandsome 3:46cc9d386ff4 267 }
jaybehandsome 4:9cc307f25dc9 268 else if(posB > 24 && posB < 36 )
jaybehandsome 4:9cc307f25dc9 269 {
jaybehandsome 4:9cc307f25dc9 270 B = 0x07;
jaybehandsome 4:9cc307f25dc9 271 }
jaybehandsome 4:9cc307f25dc9 272 else if(posB > 36 && posB < 48 )
jaybehandsome 4:9cc307f25dc9 273 {
jaybehandsome 4:9cc307f25dc9 274 B = 0x08;
jaybehandsome 4:9cc307f25dc9 275 }
jaybehandsome 4:9cc307f25dc9 276 else if(posB > 48 && posB < 60 )
jaybehandsome 4:9cc307f25dc9 277 {
jaybehandsome 4:9cc307f25dc9 278 B = 0x70;
jaybehandsome 4:9cc307f25dc9 279 }
jaybehandsome 3:46cc9d386ff4 280 else
jaybehandsome 3:46cc9d386ff4 281 {
jaybehandsome 3:46cc9d386ff4 282 B = 0x00;
jaybehandsome 3:46cc9d386ff4 283 }
jaybehandsome 4:9cc307f25dc9 284
jaybehandsome 3:46cc9d386ff4 285 if (posC > 0 && posC < 30)
jaybehandsome 3:46cc9d386ff4 286 {
jaybehandsome 4:9cc307f25dc9 287 C = 0x70;
jaybehandsome 4:9cc307f25dc9 288 }
jaybehandsome 4:9cc307f25dc9 289 else if(posC > 12 && posC < 24 )
jaybehandsome 4:9cc307f25dc9 290 {
jaybehandsome 4:9cc307f25dc9 291 C = 0x08;
jaybehandsome 3:46cc9d386ff4 292 }
jaybehandsome 4:9cc307f25dc9 293 else if(posC > 24 && posC < 36 )
jaybehandsome 4:9cc307f25dc9 294 {
jaybehandsome 4:9cc307f25dc9 295 C = 0x07;
jaybehandsome 4:9cc307f25dc9 296 }
jaybehandsome 4:9cc307f25dc9 297 else if(posC > 36 && posC < 48 )
jaybehandsome 4:9cc307f25dc9 298 {
jaybehandsome 4:9cc307f25dc9 299 C = 0x08;
jaybehandsome 4:9cc307f25dc9 300 }
jaybehandsome 4:9cc307f25dc9 301 else if(posC > 48 && posC < 60 )
jaybehandsome 4:9cc307f25dc9 302 {
jaybehandsome 4:9cc307f25dc9 303 C = 0x70;
jaybehandsome 4:9cc307f25dc9 304 }
jaybehandsome 3:46cc9d386ff4 305 else
jaybehandsome 3:46cc9d386ff4 306 {
jaybehandsome 3:46cc9d386ff4 307 C = 0x00;
jaybehandsome 3:46cc9d386ff4 308 }
jaybehandsome 0:1e46c1a32764 309 // pc.printf("ax = %f", 1000*ax);
jaybehandsome 0:1e46c1a32764 310 // pc.printf(" ay = %f", 1000*ay);
jaybehandsome 0:1e46c1a32764 311 // pc.printf(" az = %f mg\n\r", 1000*az);
jaybehandsome 0:1e46c1a32764 312
jaybehandsome 2:af822f5a5120 313 // pc.printf("gx = %f", gx);
jaybehandsome 2:af822f5a5120 314 // pc.printf(" gy = %f", gy);
jaybehandsome 4:9cc307f25dc9 315 pc.printf(" %.2f\r", gz);
jaybehandsome 4:9cc307f25dc9 316 pc.printf(" %.2f %.2f %.2f\n", posA, posB, posC);
jaybehandsome 0:1e46c1a32764 317
jaybehandsome 0:1e46c1a32764 318 // pc.printf("gx = %f", mx);
jaybehandsome 0:1e46c1a32764 319 // pc.printf(" gy = %f", my);
jaybehandsome 0:1e46c1a32764 320 // pc.printf(" gz = %f mG\n\r", mz);
jaybehandsome 0:1e46c1a32764 321
jaybehandsome 4:9cc307f25dc9 322 // tempCount = mpu9250.readTempData(); // Read the adc values
jaybehandsome 4:9cc307f25dc9 323 // temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
jaybehandsome 1:c102abc55118 324 // pc.printf(" temperature = %f C\n\r", temperature);
jaybehandsome 0:1e46c1a32764 325
jaybehandsome 0:1e46c1a32764 326 // pc.printf("q0 = %f\n\r", q[0]);
jaybehandsome 0:1e46c1a32764 327 // pc.printf("q1 = %f\n\r", q[1]);
jaybehandsome 0:1e46c1a32764 328 // pc.printf("q2 = %f\n\r", q[2]);
jaybehandsome 0:1e46c1a32764 329 // pc.printf("q3 = %f\n\r", q[3]);
jaybehandsome 0:1e46c1a32764 330
jaybehandsome 0:1e46c1a32764 331
jaybehandsome 0:1e46c1a32764 332
jaybehandsome 0:1e46c1a32764 333 // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
jaybehandsome 0:1e46c1a32764 334 // In this coordinate system, the positive z-axis is down toward Earth.
jaybehandsome 0:1e46c1a32764 335 // 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 336 // 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 337 // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
jaybehandsome 0:1e46c1a32764 338 // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
jaybehandsome 0:1e46c1a32764 339 // 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 340 // applied in the correct order which for this configuration is yaw, pitch, and then roll.
jaybehandsome 0:1e46c1a32764 341 // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
jaybehandsome 4:9cc307f25dc9 342 // 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 4:9cc307f25dc9 343 // pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
jaybehandsome 4:9cc307f25dc9 344 // 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 4:9cc307f25dc9 345 // pitch *= 180.0f / PI;
jaybehandsome 4:9cc307f25dc9 346 // yaw *= 180.0f / PI;
jaybehandsome 4:9cc307f25dc9 347 // yaw -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
jaybehandsome 4:9cc307f25dc9 348 // roll *= 180.0f / PI;
jaybehandsome 0:1e46c1a32764 349
jaybehandsome 0:1e46c1a32764 350 // pc.printf("Yaw, Pitch, Roll: %f %f %f\n\r", yaw, pitch, roll);
jaybehandsome 0:1e46c1a32764 351 // pc.printf("average rate = %f\n\r", (float) sumCount/sum);
jaybehandsome 0:1e46c1a32764 352 // sprintf(buffer, "YPR: %f %f %f", yaw, pitch, roll);
jaybehandsome 0:1e46c1a32764 353 // sprintf(buffer, "rate = %f", (float) sumCount/sum);
jaybehandsome 0:1e46c1a32764 354 //
jaybehandsome 0:1e46c1a32764 355
jaybehandsome 4:9cc307f25dc9 356 count = t.read_us();
jaybehandsome 0:1e46c1a32764 357
jaybehandsome 4:9cc307f25dc9 358 // if(count > 1<<21) {
jaybehandsome 4:9cc307f25dc9 359 // t.start(); // start the timer over again if ~30 minutes has passed
jaybehandsome 4:9cc307f25dc9 360 // count = 0;
jaybehandsome 4:9cc307f25dc9 361 // deltat= 0;
jaybehandsome 4:9cc307f25dc9 362 // lastUpdate = t.read_us();
jaybehandsome 4:9cc307f25dc9 363 // }
jaybehandsome 4:9cc307f25dc9 364 // sum = 0;
jaybehandsome 4:9cc307f25dc9 365 // sumCount = 0;
jaybehandsome 0:1e46c1a32764 366 }
jaybehandsome 0:1e46c1a32764 367 }
jaybehandsome 3:46cc9d386ff4 368
jaybehandsome 4:9cc307f25dc9 369
jaybehandsome 4:9cc307f25dc9 370 }
jaybehandsome 0:1e46c1a32764 371 }