kriswiner / MPU9250

Arduino sketches for MPU9250 9DoF with AHRS sensor fusion
1.04k stars 471 forks source link

Are the orientation angles relative to earth frame or initial IMU frame? #154

Open xiaoxuesu14 opened 7 years ago

xiaoxuesu14 commented 7 years ago

Hi Kris,

first of all, thank you so much for this amazing library!

I have a question about the Euler angles: are they relative to the earth frame (gravity and north, absolute) or to the IMU's initial frame (relative)? Using your code the roll and pitch converges to zero at the initial stage, no matter what orientation the IMU is. So I assume it is relative to the IMU frame? However, the matlab implementation of Madgwick gives absolute orientation to the earth frame. I cannot tell a difference between the two implementation thought. Could you enlighten me how this happens?

Thanks Xuesu

kriswiner commented 7 years ago

The orientation is relative to Earth frame. How are you calibrating your sensors?

On Mon, Jun 26, 2017 at 12:21 PM, xiaoxuesu14 notifications@github.com wrote:

Hi Kris,

first of all, thank you so much for this amazing library!

I have a question about the Euler angles: are they relative to the earth frame (gravity and north, absolute) or to the IMU's initial frame (relative)? Using your code the roll and pitch converges to zero at the initial stage, no matter what orientation the IMU is. So I assume it is relative to the IMU frame? However, the matlab implementation of Madgwick gives absolute orientation to the earth frame. I cannot tell a difference between the two implementation thought. Could you enlighten me how this happens?

Thanks Xuesu

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/154, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qsmAFbEufVVsp7xrSEuhnXPLRid8ks5sIAS4gaJpZM4OFvid .

xiaoxuesu14 commented 7 years ago

I only calibrated the magnetometer, nothing else. The roll and pitch converges to 0 no matter how the IMU configuration is. After roll and pitch settle down, if I rotate the IMU, the euler angles make sense, but it's just relative to the initial IMU configuration, not the earth frame.

kriswiner commented 7 years ago

Strange, not sure why. Not this way for me.

On Mon, Jun 26, 2017 at 1:17 PM, xiaoxuesu14 notifications@github.com wrote:

I only calibrated the magnetometer, nothing else. The roll and pitch converges to 0 no matter how the IMU configuration is. After roll and pitch settle down, if I rotate the IMU, the euler angles make sense, but it's just relative to the initial IMU configuration, not the earth frame.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/154#issuecomment-311169802, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qiJBuZMXkQKSIWBDw9ToURxtbd2-ks5sIBHqgaJpZM4OFvid .

xiaoxuesu14 commented 7 years ago

Orientation: 177.17 52.85 46.14 Orientation: 176.56 54.16 46.35 Orientation: 175.85 55.53 46.52 Orientation: 175.05 56.98 46.65 Orientation: 174.22 58.48 46.74 Orientation: 173.25 60.04 46.75 Orientation: 172.03 61.66 46.59 Orientation: 170.70 63.40 46.37 Orientation: 169.07 65.18 45.91 Orientation: 167.14 67.03 45.19 Orientation: 164.71 68.98 44.07 Orientation: 161.77 70.98 42.50 Orientation: 157.99 73.02 40.15 Orientation: 153.14 75.06 36.78 Orientation: 146.45 77.13 31.65 Orientation: 136.98 79.08 23.79 Orientation: 123.22 80.74 11.70 Orientation: 104.18 81.75 -5.57 Orientation: 81.51 81.74 -26.39 Orientation: 60.81 80.49 -45.18 Orientation: 45.21 78.25 -58.79 Orientation: 34.42 75.35 -67.50 Orientation: 26.69 71.95 -73.10 Orientation: 20.92 68.18 -76.67 Orientation: 16.41 64.02 -78.91 Orientation: 12.82 59.52 -80.17 Orientation: 9.80 54.70 -80.80 Orientation: 7.30 49.46 -80.89 Orientation: 5.01 43.91 -80.67 Orientation: 2.95 37.91 -80.17 Orientation: 1.14 31.51 -79.44 Orientation: -0.31 24.67 -78.43 Orientation: -1.37 17.33 -77.19 Orientation: -1.88 9.59 -75.87 Orientation: -1.09 1.77 -74.70 Orientation: -0.08 -0.24 -74.27 Orientation: -0.08 -0.53 -73.96 Orientation: 0.01 -0.39 -73.93 Orientation: -0.02 -0.48 -73.53 Orientation: 0.07 -0.27 -73.66 Orientation: -0.07 -0.39 -73.81 Orientation: -0.05 -0.33 -73.66 Orientation: 0.03 -0.24 -73.68 Orientation: -0.06 -0.39 -73.98 Orientation: -0.05 -0.51 -73.77 Orientation: 0.07 -0.46 -74.10 Orientation: -0.02 -0.19 -74.28 Orientation: -0.07 -0.26 -74.60 Orientation: -0.09 -0.22 -74.38 Orientation: -0.09 -0.46 -74.41 Orientation: 0.04 -0.35 -74.38 Orientation: -0.09 -0.50 -74.46 Orientation: -0.05 -0.38 -74.47 Orientation: -0.05 -0.43 -74.51 Orientation: 0.07 -0.09 -74.74 Orientation: -0.04 -0.57 -74.91 Orientation: -0.09 -0.44 -74.71

This looks like the output no matter how I put the IMU. Roll and pitch converges to zero anyways...

kriswiner commented 7 years ago

Looks like an error in the sensor data to me.

On Mon, Jun 26, 2017 at 2:10 PM, xiaoxuesu14 notifications@github.com wrote:

Orientation: 177.17 52.85 46.14 Orientation: 176.56 54.16 46.35 Orientation: 175.85 55.53 46.52 Orientation: 175.05 56.98 46.65 Orientation: 174.22 58.48 46.74 Orientation: 173.25 60.04 46.75 Orientation: 172.03 61.66 46.59 Orientation: 170.70 63.40 46.37 Orientation: 169.07 65.18 45.91 Orientation: 167.14 67.03 45.19 Orientation: 164.71 68.98 44.07 Orientation: 161.77 70.98 42.50 Orientation: 157.99 73.02 40.15 Orientation: 153.14 75.06 36.78 Orientation: 146.45 77.13 31.65 Orientation: 136.98 79.08 23.79 Orientation: 123.22 80.74 11.70 Orientation: 104.18 81.75 -5.57 Orientation: 81.51 81.74 -26.39 Orientation: 60.81 80.49 -45.18 Orientation: 45.21 78.25 -58.79 Orientation: 34.42 75.35 -67.50 Orientation: 26.69 71.95 -73.10 Orientation: 20.92 68.18 -76.67 Orientation: 16.41 64.02 -78.91 Orientation: 12.82 59.52 -80.17 Orientation: 9.80 54.70 -80.80 Orientation: 7.30 49.46 -80.89 Orientation: 5.01 43.91 -80.67 Orientation: 2.95 37.91 -80.17 Orientation: 1.14 31.51 -79.44 Orientation: -0.31 24.67 -78.43 Orientation: -1.37 17.33 -77.19 Orientation: -1.88 9.59 -75.87 Orientation: -1.09 1.77 -74.70 Orientation: -0.08 -0.24 -74.27 Orientation: -0.08 -0.53 -73.96 Orientation: 0.01 -0.39 -73.93 Orientation: -0.02 -0.48 -73.53 Orientation: 0.07 -0.27 -73.66 Orientation: -0.07 -0.39 -73.81 Orientation: -0.05 -0.33 -73.66 Orientation: 0.03 -0.24 -73.68 Orientation: -0.06 -0.39 -73.98 Orientation: -0.05 -0.51 -73.77 Orientation: 0.07 -0.46 -74.10 Orientation: -0.02 -0.19 -74.28 Orientation: -0.07 -0.26 -74.60 Orientation: -0.09 -0.22 -74.38 Orientation: -0.09 -0.46 -74.41 Orientation: 0.04 -0.35 -74.38 Orientation: -0.09 -0.50 -74.46 Orientation: -0.05 -0.38 -74.47 Orientation: -0.05 -0.43 -74.51 Orientation: 0.07 -0.09 -74.74 Orientation: -0.04 -0.57 -74.91 Orientation: -0.09 -0.44 -74.71

This looks like the output no matter how I put the IMU. Roll and pitch converges to zero anyways...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/154#issuecomment-311182897, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qmAKG8b-WrSYvFpCNQwtw2G_A9QIks5sIB5JgaJpZM4OFvid .

markussen145 commented 7 years ago

When you say roll and pitch converge, do you mean they change as you move the mpu but once you keep it steady, they decrease to zero? If that's the case, it may be cause by inserting raw data in madgwick/Mahoney functions with the wrong signs.

Check the data sheet of your device and see their oriention (check where x,y and z are positive).

xiaoxuesu14 commented 7 years ago

Hi,

no, that was not what I meant. I meant, at the initial stage after I started the algorithm, I kept the IMU steady, and then the roll and pitch converges to zero. It really looks like an initialization stage. After that, if I rotate the IMU, the three Euler angles all make sense. So the algorithm output is with respect to the initial configuration of the IMU (how I started with the IMU, since it initializes roll and pitch to zero), not with respect to the earth frame (gravity and north).

Thanks Xuesu

On Fri, Jul 14, 2017 at 12:36 AM, markussen145 notifications@github.com wrote:

When you say roll and pitch converge, do you mean they change as you move the mpu but once you keep it steady, they decrease to zero? If that's the case, it may be cause by inserting raw data in madgwick/Mahoney functions with the wrong signs.

Check the data sheet of your device and see their oriention (check where x,y and z are positive).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_kriswiner_MPU9250_issues_154-23issuecomment-2D315293022&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=Edy4XmO2ZgubtKhTorII4pYKgOOzCtQ0p-PIeaSbEys&m=B-KmC2x42Cjuqaflb-dJzSXSk0NnNAFcLUmu3XCsayY&s=50Z2kq6-mJFXSkn_hQBfUWKOXPl6596IRPMifxx-VAE&e=, or mute the thread https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AQ1ix-5F-5Fm1HVrZEl-5FwVrs58EkXnyttU6Cks5sNxqUgaJpZM4OFvid&d=DwMFaQ&c=ODFT-G5SujMiGrKuoJJjVg&r=Edy4XmO2ZgubtKhTorII4pYKgOOzCtQ0p-PIeaSbEys&m=B-KmC2x42Cjuqaflb-dJzSXSk0NnNAFcLUmu3XCsayY&s=_EyzKF8Ryq_C2-yFkOS8Fcl9ypjvu5IAX_jOE00RQcM&e= .

superviruz commented 7 years ago

Hi Xuesu,

I facing the same issue as you. When I laid IMU (9250) in any orientation then power it up, it will start roll and pitch to nearly zero (vary from -10 to 10). But after that, I rotate to another orientation and it send data correctly (relative to initial orientation).

Below are "some portion" of my mod code (download from sparkfun that modified from original Kris code).

I have to shorten code as most as I can to fit in Arduino mini pro (for minimize cost and size)

I know that it may be too short and make not stable as original code.

include "quaternionFilters.h"

include "MPU9250.h"

include

include

define AHRS true // Set to false for basic data read

define SerialDebug false // Set to true to get Serial output for debugging

int intPin = 3; MPU9250 myIMU;

void setup() {

Wire.begin(); // TWBR = 12; // 400 kbit/sec I2C speed if (SerialDebug) { Serial.begin(38400); } // Set up the interrupt pin, its set as active high, push-pull pinMode(intPin, INPUT); digitalWrite(intPin, LOW);

// Read the WHO_AM_I register, this is a good test of communication byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); Serial.print(" I should be "); Serial.println(0x71, HEX);

{ Serial.println("MPU9250 is online...");

// Start by performing self test and reporting values
myIMU.MPU9250SelfTest(myIMU.SelfTest);
Serial.print("x-axis self test: acceleration trim within : ");
Serial.print(myIMU.SelfTest[0],1); Serial.println("% of factory value");
Serial.print("y-axis self test: acceleration trim within : ");
Serial.print(myIMU.SelfTest[1],1); Serial.println("% of factory value");
Serial.print("z-axis self test: acceleration trim within : ");
Serial.print(myIMU.SelfTest[2],1); Serial.println("% of factory value");
Serial.print("x-axis self test: gyration trim within : ");
Serial.print(myIMU.SelfTest[3],1); Serial.println("% of factory value");
Serial.print("y-axis self test: gyration trim within : ");
Serial.print(myIMU.SelfTest[4],1); Serial.println("% of factory value");
Serial.print("z-axis self test: gyration trim within : ");
Serial.print(myIMU.SelfTest[5],1); Serial.println("% of factory value");

// Calibrate gyro and accelerometers, load biases in bias registers
myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);

myIMU.initMPU9250();
// Initialize device for active mode read of acclerometer, gyroscope, and
// temperature
Serial.println("MPU9250 initialized for active data mode....");

// Read the WHO_AM_I register of the magnetometer, this is a good test of
// communication
byte d = myIMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963);
Serial.print("AK8963 "); Serial.print("I AM "); Serial.print(d, HEX);
Serial.print(" I should be "); Serial.println(0x48, HEX);

// Get magnetometer calibration from AK8963 ROM
myIMU.initAK8963(myIMU.magCalibration);
// Initialize device for active mode read of magnetometer
Serial.println("AK8963 initialized for active data mode....");
if (SerialDebug)
{
  //  Serial.println("Calibration values: ");
  Serial.print("X-Axis sensitivity adjustment value ");
  Serial.println(myIMU.magCalibration[0], 2);
  Serial.print("Y-Axis sensitivity adjustment value ");
  Serial.println(myIMU.magCalibration[1], 2);
  Serial.print("Z-Axis sensitivity adjustment value ");
  Serial.println(myIMU.magCalibration[2], 2);
}

} attachInterrupt(digitalPinToInterrupt(intPin), pressButton, RISING); }

void loop() {

// If intPin goes high, all data registers have new data // On interrupt, check if data ready interrupt if (myIMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) {
myIMU.readAccelData(myIMU.accelCount); // Read the x/y/z adc values myIMU.getAres();

// Now we'll calculate the accleration value into actual g's
// This depends on scale being set
myIMU.ax = (float)myIMU.accelCount[0]*myIMU.aRes; // - accelBias[0];
myIMU.ay = (float)myIMU.accelCount[1]*myIMU.aRes; // - accelBias[1];
myIMU.az = (float)myIMU.accelCount[2]*myIMU.aRes; // - accelBias[2];

myIMU.readGyroData(myIMU.gyroCount);  // Read the x/y/z adc values
myIMU.getGres();

// Calculate the gyro value into actual degrees per second
// This depends on scale being set
myIMU.gx = (float)myIMU.gyroCount[0]*myIMU.gRes;
myIMU.gy = (float)myIMU.gyroCount[1]*myIMU.gRes;
myIMU.gz = (float)myIMU.gyroCount[2]*myIMU.gRes;

myIMU.readMagData(myIMU.magCount);  // Read the x/y/z adc values
myIMU.getMres();
// User environmental x-axis correction in milliGauss, should be
// automatically calculated470 201.86
myIMU.magbias[0] = cal[0];
// User environmental x-axis correction in milliGauss TODO axis??120n0.02
myIMU.magbias[1] = cal[1];
// User environmental x-axis correction in milliGauss125 -28.11
myIMU.magbias[2] = cal[2];

// Calculate the magnetometer values in milliGauss
// Include factory calibration per data sheet and user environmental
// corrections
// Get actual magnetometer value, this depends on scale being set
myIMU.mx = (float)myIMU.magCount[0]*myIMU.mRes*myIMU.magCalibration[0] -
           myIMU.magbias[0];
myIMU.my = (float)myIMU.magCount[1]*myIMU.mRes*myIMU.magCalibration[1] -
           myIMU.magbias[1];
myIMU.mz = (float)myIMU.magCount[2]*myIMU.mRes*myIMU.magCalibration[2] -
           myIMU.magbias[2];

} // if (readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)

// Must be called before updating quaternions! myIMU.updateTime();

// Sensors x (y)-axis of the accelerometer is aligned with the y (x)-axis of // the magnetometer; the magnetometer z-axis (+ down) is opposite to z-axis // (+ up) of accelerometer and gyro! We have to make some allowance for this // orientationmismatch in feeding the output to the quaternion filter. For the // MPU-9250, we have chosen a magnetic rotation that keeps the sensor forward // along the x-axis just like in the LSM9DS0 sensor. This rotation can be // modified to allow any convenient orientation convention. This is ok by // aircraft orientation standards! Pass gyro rate as rad/s // MadgwickQuaternionUpdate(ax, ay, az, gxPI/180.0f, gyPI/180.0f, gzPI/180.0f, my, mx, mz); MahonyQuaternionUpdate(myIMU.ax, myIMU.ay, myIMU.az, myIMU.gxDEG_TO_RAD, myIMU.gyDEG_TO_RAD, myIMU.gzDEG_TO_RAD, myIMU.my, myIMU.mx, myIMU.mz, myIMU.deltat);

if (!AHRS) { myIMU.delt_t = millis() - myIMU.count; if (myIMU.delt_t > 100) { if(SerialDebug) { // Print acceleration values in milligs! Serial.print("X-acceleration: "); Serial.print(1000myIMU.ax); Serial.print(" mg "); Serial.print("Y-acceleration: "); Serial.print(1000myIMU.ay); Serial.print(" mg "); Serial.print("Z-acceleration: "); Serial.print(1000*myIMU.az); Serial.println(" mg ");

    // Print gyro values in degree/sec
    Serial.print("X-gyro rate: "); Serial.print(myIMU.gx, 3);
    Serial.print(" degrees/sec ");
    Serial.print("Y-gyro rate: "); Serial.print(myIMU.gy, 3);
    Serial.print(" degrees/sec ");
    Serial.print("Z-gyro rate: "); Serial.print(myIMU.gz, 3);
    Serial.println(" degrees/sec");

    // Print mag values in degree/sec
    Serial.print("X-mag field: "); Serial.print(myIMU.mx);
    Serial.print(" mG ");
    Serial.print("Y-mag field: "); Serial.print(myIMU.my);
    Serial.print(" mG ");
    Serial.print("Z-mag field: "); Serial.print(myIMU.mz);
    Serial.println(" mG");

    myIMU.tempCount = myIMU.readTempData();  // Read the adc values
    myIMU.temperature = ((float) myIMU.tempCount)/333.87 + 21;
    // Print temperature in degrees Centigrade
    Serial.print("Temperature is ");  Serial.print(myIMU.temperature, 1);
    Serial.println(" degrees C");
  }

  myIMU.count = millis();
  //digitalWrite(myLed, !digitalRead(myLed));  // toggle led

} // if (myIMU.delt_t > 100)

} // if (!AHRS) else { // Serial print and/or display at 0.5 s rate independent of data rates myIMU.delt_t = millis() - myIMU.count;

// update LCD once per half-second independent of read rate
if (myIMU.delt_t > 100)
{
  if(SerialDebug)
  {
    Serial.print("ax = "); Serial.print((int)1000*myIMU.ax);
    Serial.print(" ay = "); Serial.print((int)1000*myIMU.ay);
    Serial.print(" az = "); Serial.print((int)1000*myIMU.az);
    Serial.println(" mg");

    Serial.print("gx = "); Serial.print( myIMU.gx, 2);
    Serial.print(" gy = "); Serial.print( myIMU.gy, 2);
    Serial.print(" gz = "); Serial.print( myIMU.gz, 2);
    Serial.println(" deg/s");

    Serial.print("mx = "); Serial.print( (int)myIMU.mx );
    Serial.print(" my = "); Serial.print( (int)myIMU.my );
    Serial.print(" mz = "); Serial.print( (int)myIMU.mz );
    Serial.println(" mG");

    Serial.print("q0 = "); Serial.print(*getQ());
    Serial.print(" qx = "); Serial.print(*(getQ() + 1));
    Serial.print(" qy = "); Serial.print(*(getQ() + 2));
    Serial.print(" qz = "); Serial.println(*(getQ() + 3));
  }

// Define output variables from updated quaternion---these are Tait-Bryan // angles, commonly used in aircraft orientation. In this coordinate system, // the positive z-axis is down toward Earth. 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. // Pitch is angle between sensor x-axis and Earth ground plane, toward the // Earth is positive, up toward the sky is negative. Roll is angle between // sensor y-axis and Earth ground plane, y-axis up is positive roll. These // arise from the definition of the homogeneous rotation matrix constructed // from quaternions. Tait-Bryan angles as well as Euler angles are // non-commutative; that is, the get the correct orientation the rotations // must be applied in the correct order which for this configuration is yaw, // pitch, and then roll. // For more see // http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles // which has additional links. myIMU.yaw = atan2(2.0f ((getQ()+1) (getQ()+2) + getQ() (getQ()+3)), getQ() getQ() + (getQ()+1) *(getQ()+1)

kriswiner commented 7 years ago

I don;t think this is right:

// MadgwickQuaternionUpdate(ax, ay, az, gxPI/180.0f, gyPI/180.0f, gz PI/180.0f, my, mx, mz); MahonyQuaternionUpdate(myIMU.ax, myIMU.ay, myIMU.az, myIMU.gxDEG_TO_RAD, myIMU.gyDEG_TO_RAD, myIMU.gzDEG_TO_RAD, myIMU.my, myIMU.mx, myIMU.mz, myIMU.deltat);

For one thing, Az is opposite to Mz.

If you want to make Ax == North, then feed in the data like this:

Ax, -Ay, -Az, Gx, -Gy, -Gz, My, -Mx, Mz

then all sensor axes are NED configuration. I ususally negate the A values, but this is not necessary for consistent results.

On Tue, Oct 3, 2017 at 3:17 AM, superviruz notifications@github.com wrote:

Hi Xuesu,

I facing the same issue as you. When I laid IMU (9250) in any orientation then power it up, it will start roll and pitch to nearly zero (vary from -10 to 10). But after that, I rotate to another orientation and it send data correctly (relative to initial orientation).

Below are "some portion" of my mod code (download from sparkfun that modified from original Kris code).

I have to shorten code as most as I can to fit in Arduino mini pro (for minimize cost and size)

I know that it may be too short and make not stable as original code.

include "quaternionFilters.h"

include "MPU9250.h"

include

include

define AHRS true // Set to false for basic data read

define SerialDebug false // Set to true to get Serial output for debugging

int intPin = 3; MPU9250 myIMU;

void setup() {

Wire.begin(); // TWBR = 12; // 400 kbit/sec I2C speed if (SerialDebug) { Serial.begin(38400); } // Set up the interrupt pin, its set as active high, push-pull pinMode(intPin, INPUT); digitalWrite(intPin, LOW);

// Read the WHO_AM_I register, this is a good test of communication byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); Serial.print(" I should be "); Serial.println(0x71, HEX);

{ Serial.println("MPU9250 is online...");

// Start by performing self test and reporting values myIMU.MPU9250SelfTest(myIMU.SelfTest); Serial.print("x-axis self test: acceleration trim within : "); Serial.print(myIMU.SelfTest[0],1); Serial.println("% of factory value"); Serial.print("y-axis self test: acceleration trim within : "); Serial.print(myIMU.SelfTest[1],1); Serial.println("% of factory value"); Serial.print("z-axis self test: acceleration trim within : "); Serial.print(myIMU.SelfTest[2],1); Serial.println("% of factory value"); Serial.print("x-axis self test: gyration trim within : "); Serial.print(myIMU.SelfTest[3],1); Serial.println("% of factory value"); Serial.print("y-axis self test: gyration trim within : "); Serial.print(myIMU.SelfTest[4],1); Serial.println("% of factory value"); Serial.print("z-axis self test: gyration trim within : "); Serial.print(myIMU.SelfTest[5],1); Serial.println("% of factory value");

// Calibrate gyro and accelerometers, load biases in bias registers myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);

myIMU.initMPU9250(); // Initialize device for active mode read of acclerometer, gyroscope, and // temperature Serial.println("MPU9250 initialized for active data mode....");

// Read the WHO_AM_I register of the magnetometer, this is a good test of // communication byte d = myIMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963); Serial.print("AK8963 "); Serial.print("I AM "); Serial.print(d, HEX); Serial.print(" I should be "); Serial.println(0x48, HEX);

// Get magnetometer calibration from AK8963 ROM myIMU.initAK8963(myIMU.magCalibration); // Initialize device for active mode read of magnetometer Serial.println("AK8963 initialized for active data mode...."); if (SerialDebug) { // Serial.println("Calibration values: "); Serial.print("X-Axis sensitivity adjustment value "); Serial.println(myIMU.magCalibration[0], 2); Serial.print("Y-Axis sensitivity adjustment value "); Serial.println(myIMU.magCalibration[1], 2); Serial.print("Z-Axis sensitivity adjustment value "); Serial.println(myIMU.magCalibration[2], 2); }

} attachInterrupt(digitalPinToInterrupt(intPin), pressButton, RISING); }

void loop() {

// If intPin goes high, all data registers have new data // On interrupt, check if data ready interrupt if (myIMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) { myIMU.readAccelData(myIMU.accelCount); // Read the x/y/z adc values myIMU.getAres();

// Now we'll calculate the accleration value into actual g's // This depends on scale being set myIMU.ax = (float)myIMU.accelCount[0]myIMU.aRes; // - accelBias[0]; myIMU.ay = (float)myIMU.accelCount[1]myIMU.aRes; // - accelBias[1]; myIMU.az = (float)myIMU.accelCount[2]*myIMU.aRes; // - accelBias[2];

myIMU.readGyroData(myIMU.gyroCount); // Read the x/y/z adc values myIMU.getGres();

// Calculate the gyro value into actual degrees per second // This depends on scale being set myIMU.gx = (float)myIMU.gyroCount[0]myIMU.gRes; myIMU.gy = (float)myIMU.gyroCount[1]myIMU.gRes; myIMU.gz = (float)myIMU.gyroCount[2]*myIMU.gRes;

myIMU.readMagData(myIMU.magCount); // Read the x/y/z adc values myIMU.getMres(); // User environmental x-axis correction in milliGauss, should be // automatically calculated470 201.86 myIMU.magbias[0] = cal[0]; // User environmental x-axis correction in milliGauss TODO axis??120n0.02 myIMU.magbias[1] = cal[1]; // User environmental x-axis correction in milliGauss125 -28.11 myIMU.magbias[2] = cal[2];

// Calculate the magnetometer values in milliGauss // Include factory calibration per data sheet and user environmental // corrections // Get actual magnetometer value, this depends on scale being set myIMU.mx = (float)myIMU.magCount[0]myIMU.mResmyIMU.magCalibration[0] - myIMU.magbias[0]; myIMU.my = (float)myIMU.magCount[1]myIMU.mResmyIMU.magCalibration[1] - myIMU.magbias[1]; myIMU.mz = (float)myIMU.magCount[2]myIMU.mResmyIMU.magCalibration[2] - myIMU.magbias[2];

} // if (readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)

// Must be called before updating quaternions! myIMU.updateTime();

// Sensors x (y)-axis of the accelerometer is aligned with the y (x)-axis of // the magnetometer; the magnetometer z-axis (+ down) is opposite to z-axis // (+ up) of accelerometer and gyro! We have to make some allowance for this // orientationmismatch in feeding the output to the quaternion filter. For the // MPU-9250, we have chosen a magnetic rotation that keeps the sensor forward // along the x-axis just like in the LSM9DS0 sensor. This rotation can be // modified to allow any convenient orientation convention. This is ok by // aircraft orientation standards! Pass gyro rate as rad/s // MadgwickQuaternionUpdate(ax, ay, az, gxPI/180.0f, gyPI/180.0f, gz PI/180.0f, my, mx, mz); MahonyQuaternionUpdate(myIMU.ax, myIMU.ay, myIMU.az, myIMU.gxDEG_TO_RAD, myIMU.gyDEG_TO_RAD, myIMU.gzDEG_TO_RAD, myIMU.my, myIMU.mx, myIMU.mz, myIMU.deltat);

if (!AHRS) { myIMU.delt_t = millis() - myIMU.count; if (myIMU.delt_t > 100) { if(SerialDebug) { // Print acceleration values in milligs! Serial.print("X-acceleration: "); Serial.print(1000

myIMU.ax); Serial.print(" mg "); Serial.print("Y-acceleration: "); Serial.print(1000myIMU.ay); Serial.print(" mg "); Serial.print("Z-acceleration: "); Serial.print(1000*myIMU.az); Serial.println(" mg ");

// Print gyro values in degree/sec
Serial.print("X-gyro rate: "); Serial.print(myIMU.gx, 3);
Serial.print(" degrees/sec ");
Serial.print("Y-gyro rate: "); Serial.print(myIMU.gy, 3);
Serial.print(" degrees/sec ");
Serial.print("Z-gyro rate: "); Serial.print(myIMU.gz, 3);
Serial.println(" degrees/sec");

// Print mag values in degree/sec
Serial.print("X-mag field: "); Serial.print(myIMU.mx);
Serial.print(" mG ");
Serial.print("Y-mag field: "); Serial.print(myIMU.my);
Serial.print(" mG ");
Serial.print("Z-mag field: "); Serial.print(myIMU.mz);
Serial.println(" mG");

myIMU.tempCount = myIMU.readTempData();  // Read the adc values
myIMU.temperature = ((float) myIMU.tempCount)/333.87 + 21;
// Print temperature in degrees Centigrade
Serial.print("Temperature is ");  Serial.print(myIMU.temperature, 1);
Serial.println(" degrees C");

}

myIMU.count = millis(); //digitalWrite(myLed, !digitalRead(myLed)); // toggle led

} // if (myIMU.delt_t > 100)

} // if (!AHRS) else { // Serial print and/or display at 0.5 s rate independent of data rates myIMU.delt_t = millis() - myIMU.count;

// update LCD once per half-second independent of read rate if (myIMU.delt_t > 100) { if(SerialDebug) { Serial.print("ax = "); Serial.print((int)1000myIMU.ax); Serial.print(" ay = "); Serial.print((int)1000myIMU.ay); Serial.print(" az = "); Serial.print((int)1000*myIMU.az); Serial.println(" mg");

Serial.print("gx = "); Serial.print( myIMU.gx, 2);
Serial.print(" gy = "); Serial.print( myIMU.gy, 2);
Serial.print(" gz = "); Serial.print( myIMU.gz, 2);
Serial.println(" deg/s");

Serial.print("mx = "); Serial.print( (int)myIMU.mx );
Serial.print(" my = "); Serial.print( (int)myIMU.my );
Serial.print(" mz = "); Serial.print( (int)myIMU.mz );
Serial.println(" mG");

Serial.print("q0 = "); Serial.print(*getQ());
Serial.print(" qx = "); Serial.print(*(getQ() + 1));
Serial.print(" qy = "); Serial.print(*(getQ() + 2));
Serial.print(" qz = "); Serial.println(*(getQ() + 3));

}

// Define output variables from updated quaternion---these are Tait-Bryan // angles, commonly used in aircraft orientation. In this coordinate system, // the positive z-axis is down toward Earth. 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. // Pitch is angle between sensor x-axis and Earth ground plane, toward the // Earth is positive, up toward the sky is negative. Roll is angle between // sensor y-axis and Earth ground plane, y-axis up is positive roll. These // arise from the definition of the homogeneous rotation matrix constructed // from quaternions. Tait-Bryan angles as well as Euler angles are // non-commutative; that is, the get the correct orientation the rotations // must be applied in the correct order which for this configuration is yaw, // pitch, and then roll. // For more see // http://en.wikipedia.org/wiki/Conversion_between_ quaternions_and_Euler_angles // which has additional links. myIMU.yaw = atan2(2.0f ((getQ()+1) (getQ()+2) + getQ() (getQ()+3)), getQ() getQ() + (getQ()+1) *(getQ()+1)

  • (getQ()+2) (getQ()+2) - (getQ()+3) (getQ()+3)); myIMU.pitch = -asin(2.0f ((getQ()+1) (getQ()+3) - getQ() (getQ()+2))); myIMU.roll = atan2(2.0f (getQ() (getQ()+1) + (getQ()+2) (getQ()+3)), getQ() getQ() - (getQ()+1) (getQ()+1)
  • (getQ()+2) (getQ()+2) + (getQ()+3) (getQ()+3)); myIMU.pitch = RAD_TO_DEG; myIMU.yaw = RAD_TO_DEG; // Declination of SparkFun Electronics (40°05'26.6"N 105°11'05.9"W) is // 8° 30' E ± 0° 21' (or 8.5°) on 2016-07-19 // - http://www.ngdc.noaa.gov/geomag-web/#declination myIMU.yaw -= 0; myIMU.roll *= RAD_TO_DEG;

    if(SerialDebug) { Serial.print("Yaw, Pitch, Roll: "); Serial.print(myIMU.yaw, 2); Serial.print(", "); Serial.print(myIMU.pitch, 2); Serial.print(", "); Serial.println(myIMU.roll, 2);

    Serial.print("rate = "); Serial.print((float)myIMU.sumCount/myIMU.sum, 2); Serial.println(" Hz");

    Serial.print("count = "); Serial.print(count); Serial.println(" times"); }

    myIMU.count = millis(); myIMU.sumCount = 0; myIMU.sum = 0; } // if (myIMU.delt_t > 100)

} // if (AHRS) //Take temp out off AHRS myIMU.tempCount = myIMU.readTempData(); // Read the adc values // Temperature in degrees Centigrade (-6000,-3),(4500,29) // http://www.webmath.com/equline1.html myIMU.temperature = ((float) myIMU.tempCount)*8/2625+107/7; // Print temperature in degrees Centigrade temp = myIMU.temperature; }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/154#issuecomment-333799973, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qnXgq5p4TIAWyQ-r8YFNTGMYwlchks5sognNgaJpZM4OFvid .