Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of greenimugetithothot by
GETMPU9250.h
00001 #include "mbed.h" 00002 #include "MPU9250.h" 00003 #include "math.h" 00004 00005 Serial aa(D1, D0); 00006 class ZMU9250 00007 { 00008 public: 00009 ZMU9250() 00010 { 00011 00012 //Set up I2C 00013 i2c.frequency(400000); // use fast (400 kHz) I2C 00014 this->t.start(); 00015 00016 // Read the WHO_AM_I register, this is a good test of communication 00017 uint8_t whoami = this->mpu9250.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 00018 if ((whoami == 0x71)||(whoami == 0x73)) // WHO_AM_I should always be 0x68 00019 { 00020 wait(1); 00021 this->mpu9250.resetMPU9250(); // Reset registers to default in preparation for device calibration 00022 this->mpu9250.MPU9250SelfTest(SelfTest); // Start by performing self test and reporting values 00023 this->mpu9250.calibrateMPU9250(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers 00024 wait(2); 00025 this->mpu9250.initMPU9250(); 00026 this->mpu9250.initAK8963(magCalibration); 00027 wait(1); 00028 } 00029 else 00030 { 00031 while(1) ; // Loop forever if communication doesn't happen 00032 } 00033 this->mpu9250.getAres(); // Get accelerometer sensitivity 00034 this->mpu9250.getGres(); // Get gyro sensitivity 00035 this->mpu9250.getMres(); // Get magnetometer sensitivity 00036 magbias[0] = +470; // User environmental x-axis correction in milliGauss, should be automatically calculated 00037 magbias[1] = +120; // User environmental x-axis correction in milliGauss 00038 magbias[2] = +125; // User environmental x-axis correction in milliGauss 00039 } 00040 00041 void Update() 00042 { 00043 if(this->mpu9250.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { // On interrupt, check if data ready interrupt 00044 this->mpu9250.readAccelData(accelCount); // Read the x/y/z adc values 00045 // Now we'll calculate the accleration value into actual g's 00046 ax = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set 00047 ay = (float)accelCount[1]*aRes - accelBias[1]; 00048 az = (float)accelCount[2]*aRes - accelBias[2]; 00049 this->mpu9250.readGyroData(gyroCount); // Read the x/y/z adc values 00050 // Calculate the gyro value into actual degrees per second 00051 gx = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set 00052 gy = (float)gyroCount[1]*gRes - gyroBias[1]; 00053 gz = (float)gyroCount[2]*gRes - gyroBias[2]; 00054 this->mpu9250.readMagData(magCount); // Read the x/y/z adc values 00055 // Calculate the magnetometer values in milliGauss 00056 // Include factory calibration per data sheet and user environmental corrections 00057 mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]+360.0f; // get actual magnetometer value, this depends on scale being set 00058 my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]-210.0f; 00059 mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; 00060 //aa.printf("mx %f\tmy %f\tmz %fgx %f\tgy %f\tgz %fax %f\tay %f\taz %f\n\n\n",mx,my,mz,gx,gy,gz,ax,ay,az); 00061 //wait(0.5); 00062 00063 00064 } 00065 Now = this->t.read_us(); 00066 deltat = (float)((Now - lastUpdate)/1000000.0f) ; // set integration time by time elapsed since last filter update 00067 lastUpdate = Now; 00068 this->sum += deltat; 00069 sumCount++; 00070 this->mpu9250.MadgwickQuaternionUpdate(ax, ay, az, gx, gy, gz, my, mx, mz); 00071 delt_t = this->t.read_ms() - count; 00072 if (delt_t > 10) { // update LCD once per half-second independent of read rate 00073 tempCount = this->mpu9250.readTempData(); // Read the adc values 00074 temperature = ((float) tempCount) / 333.87f + 21.0f; // Temperature in degrees Centigrade 00075 this->axis_gx =gx; 00076 this->axis_gy =gy; 00077 this->axis_gz =gz; 00078 this->axis_ax =ax; 00079 this->axis_ay =ay; 00080 this->axis_az =az; 00081 count = this->t.read_ms(); 00082 if(count > 1<<21) { 00083 this->t.start(); // start the timer over again if ~30 minutes has passed 00084 count = 0; 00085 deltat= 0; 00086 lastUpdate = this->t.read_us(); 00087 } 00088 this->sum = 0; 00089 sumCount = 0; 00090 } 00091 } 00092 00093 00094 float g_ax() 00095 { 00096 return axis_ax; 00097 } 00098 float g_ay() 00099 { 00100 return axis_ay; 00101 } 00102 float g_az() 00103 { 00104 return axis_az; 00105 } 00106 float g_gx() 00107 { 00108 return axis_gx; 00109 } 00110 float g_gy() 00111 { 00112 return axis_gy; 00113 } 00114 float g_gz() 00115 { 00116 return axis_gz; 00117 } 00118 00119 00120 00121 private: 00122 float sum; 00123 uint32_t sumCount; 00124 char buffer[14]; 00125 MPU9250 mpu9250; 00126 Timer t; 00127 float axis_az,axis_ay,axis_ax,axis_gz,axis_gy,axis_gx; 00128 00129 00130 }; 00131 00132
Generated on Sat Jul 16 2022 11:37:55 by
