OX / Mbed 2 deprecated Ox_ver_function

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers zmu9250.h Source File

zmu9250.h

00001 #include "mbed.h"
00002 #include "MPU9250.h" 
00003 #include "math.h" 
00004 #include "kalman.h"
00005 
00006 Serial aa(USBTX,USBRX);
00007 
00008 
00009 class ZMU9250
00010 {
00011     public:
00012         ZMU9250()
00013         {
00014               
00015               //Set up I2C
00016               i2c.frequency(400000);  // use fast (400 kHz) I2C  
00017               this->t.start();        
00018               
00019               // Read the WHO_AM_I register, this is a good test of communication
00020               uint8_t whoami = this->mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
00021               
00022               
00023               if ((whoami == 0x71)||(whoami == 0x73)) // WHO_AM_I should always be 0x68
00024               {  
00025               
00026                 wait(1);
00027                 this->mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration
00028                 this->mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values
00029                 this->mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
00030                 wait(2);
00031                 this->mpu9250.initMPU9250(); 
00032                 this->mpu9250.initAK8963(magCalibration);
00033                 wait(1);
00034                }
00035                else
00036                {
00037                 while(1) ; // Loop forever if communication doesn't happen
00038                 }
00039                 this->mpu9250.getAres(); // Get accelerometer sensitivity
00040                 this->mpu9250.getGres(); // Get gyro sensitivity
00041                 this->mpu9250.getMres(); // Get magnetometer sensitivity
00042                 //magbias[0] = +470.;  // User environmental x-axis correction in milliGauss, should be automatically calculated
00043                 //magbias[1] = +120.;  // User environmental x-axis correction in milliGauss
00044                 //magbias[2] = +125.;  // User environmental x-axis correction in milliGauss
00045                 magbias[0] = +470;  // User environmental x-axis correction in milliGauss, should be automatically calculated
00046                 magbias[1] = +120;  // User environmental x-axis correction in milliGauss
00047                 magbias[2] = +125;  // User environmental x-axis correction in milliGauss
00048         }
00049         
00050         void Update()
00051         {
00052             if(this->mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {  // On interrupt, check if data ready interrupt
00053                 this->mpu9250.readAccelData(accelCount);  // Read the x/y/z adc values   
00054                 // Now we'll calculate the accleration value into actual g's
00055                 ax = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00056                 ay = (float)accelCount[1]*aRes - accelBias[1];   
00057                 az = (float)accelCount[2]*aRes - accelBias[2];  
00058                 this->mpu9250.readGyroData(gyroCount);  // Read the x/y/z adc values
00059                 // Calculate the gyro value into actual degrees per second
00060                 gx = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
00061                 gy = (float)gyroCount[1]*gRes - gyroBias[1];  
00062                 gz = (float)gyroCount[2]*gRes - gyroBias[2];   
00063                 this->mpu9250.readMagData(magCount);  // Read the x/y/z adc values   
00064                 // Calculate the magnetometer values in milliGauss
00065                 // Include factory calibration per data sheet and user environmental corrections
00066                 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]+360.0f;  // get actual magnetometer value, this depends on scale being set
00067                 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]-210.0f;  
00068                 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2];
00069                 //aa.printf("x %f\ty %f\tz %f\n",mx,my,mz);
00070                 
00071                 
00072             } // end if one
00073             Now = this->t.read_us();
00074             deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update
00075             lastUpdate = Now;
00076             this->sum += deltat;
00077             sumCount++;
00078             this->mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
00079             
00080             // Pass gyro rate as rad/s
00081             /*if((rand()%20)>=0)
00082             {
00083             this->mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f,  my,  mx, mz);
00084             }else
00085             {
00086             //this->mpu9250.MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);
00087             this->mpu9250.Mad_Update(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f);
00088             }*/
00089             
00090             
00091             // Serial print and/or display at 0.5 s rate independent of data rates
00092             delt_t = this->t.read_ms() - count;
00093             if (delt_t > 10) { // update LCD once per half-second independent of read rate
00094                 tempCount = this->mpu9250.readTempData();  // Read the adc values
00095                 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade
00096               // Define output variables from updated quaternion---these are Tait-Bryan angles, commonly used in aircraft orientation.
00097               // In this coordinate system, the positive z-axis is down toward Earth. 
00098               // 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.
00099               // Pitch is angle between sensor x-axis and Earth ground plane, toward the Earth is positive, up toward the sky is negative.
00100               // Roll is angle between sensor y-axis and Earth ground plane, y-axis up is positive roll.
00101               // These arise from the definition of the homogeneous rotation matrix constructed from quaternions.
00102               // Tait-Bryan angles as well as Euler angles are non-commutative; that is, the get the correct orientation the rotations must be
00103               // applied in the correct order which for this configuration is yaw, pitch, and then roll.
00104               // For more see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles which has additional links.
00105                 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]);   
00106                 //yaw   = atan2(2.0f * (q[0] * q[2] + q[0] * q[3]), 1 - 2 * (  q[2] * q[2] + q[3] * q[3]));   
00107                 pitch = -asin(2.0f * (q[1] * q[3] - q[0] * q[2]));
00108                 
00109                 //pitch = atan2(2.0f * (q[1] * q[3] - q[0] * q[2]),q[0]*q[0]-q[1]*q[1]+q[2]*q[2]-q[3]*q[3]);
00110                 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]);
00111                 //pitch = atan2(sin(roll)*(q[1]*q[3]-q[0]*q[2]),q[1]*q[2]+q[0]*q[3]);
00112                 pitch *= 180.0f / PI;
00113                 yaw   *= 180.0f / PI; 
00114                 //yaw   -= 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
00115                 yaw -= 0.35f;
00116                 roll  *= 180.0f / PI;
00117                 this->roll_x = roll;
00118                 this->pitch_y = pitch;
00119                 this->yaw_z = yaw;//(this->kal.getAngle(yaw*PI/180.0f,0.00,delt_t));
00120                 count = this->t.read_ms(); 
00121                 if(count > 1<<21) {
00122                     this->t.start(); // start the timer over again if ~30 minutes has passed
00123                     count = 0;
00124                     deltat= 0;
00125                     lastUpdate = this->t.read_us();
00126                 } // end if three.
00127                 this->sum = 0;
00128                 sumCount = 0; 
00129             } // end if two.
00130         }
00131         
00132         
00133         float Roll()
00134         {
00135           return roll_x;   
00136         }
00137         
00138         float Pitch()
00139         {
00140           return pitch_y;   
00141         }
00142         
00143         float Yaw()
00144         {
00145           return yaw_z;   
00146         }
00147         
00148         
00149     private:
00150         float sum;
00151         uint32_t sumCount;
00152         char buffer[14];
00153         int roll_x;
00154         kalman kal();
00155         int pitch_y;
00156         int yaw_z;
00157         MPU9250 mpu9250;
00158         Timer t;
00159         
00160            
00161 };
00162 
00163