pdf-icon

Attitude Sensor MPU9250

*The attitude sensor MPU9250 is a multi-chip module that integrates two chips. One chip is the MPU6500, consisting of a 3-axis gyroscope and a 3-axis accelerometer. The other is the AK8963, a 3-axis magnetometer.*

Note: Before using, an MPU9250 IMU object should be created.

initMPU9250()

Description:

Initializes the MPU6500 chip.

Syntax:

void initMPU9250()

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    IMU.initMPU9250();
    byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
    Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
}

initAK8963()

Description:

Initializes the AK8963 chip.

Syntax:

void initAK8963(float * destination)

Parameter Description
destination Pointer to the array of factory preset default values inside the AK8963 chip

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    // "magCalibration[3] = {0};" was defined at file "MPU9250.h"
    IMU.initAK8963(IMU.magCalibration);
    Serial.println("AK8963 initialized for active data mode....");
    if (Serial)
    {
      //  Serial.println("Calibration values: ");
      Serial.print("X-Axis sensitivity adjustment value ");
      Serial.println(IMU.magCalibration[0], 2);
      Serial.print("Y-Axis sensitivity adjustment value ");
      Serial.println(IMU.magCalibration[1], 2);
      Serial.print("Z-Axis sensitivity adjustment value ");
      Serial.println(IMU.magCalibration[2], 2);
    }
}

calibrateMPU9250()

Description:

After initializing the chip, this function reads and saves the gyroscope and accelerometer offset values in their respective offset registers when in a stationary state.

Syntax:

void calibrateMPU9250(float * gyroBias, float * accelBias)

Parameter Description
gyroBias Address of the array to save the gyroscope offset values
accelBias Address of the array to save the accelerometer offset values

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    IMU.initMPU9250();
    byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
    Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
    // "gyroBias[3] = {0};" was defined at file "MPU9250.h"
    // "accelBias[3] = {0};" was defined at file "MPU9250.h"
    IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextSize(1);
    M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250 bias");
    M5.Lcd.setCursor(0, 16); M5.Lcd.print(" x   y   z  ");

    M5.Lcd.setCursor(0,  32); M5.Lcd.print((int)(1000*IMU.accelBias[0]));
    M5.Lcd.setCursor(32, 32); M5.Lcd.print((int)(1000*IMU.accelBias[1]));
    M5.Lcd.setCursor(64, 32); M5.Lcd.print((int)(1000*IMU.accelBias[2]));
    M5.Lcd.setCursor(96, 32); M5.Lcd.print("mg");

    M5.Lcd.setCursor(0,  48); M5.Lcd.print(IMU.gyroBias[0], 1);
    M5.Lcd.setCursor(32, 48); M5.Lcd.print(IMU.gyroBias[1], 1);


    M5.Lcd.setCursor(64, 48); M5.Lcd.print(IMU.gyroBias[2], 1);
    M5.Lcd.setCursor(96, 48); M5.Lcd.print("o/s");
}

void loop(){

}

readByte()

Description:

Reads and returns the value from a specified register; value length is 8 bits.

Syntax:

uint8_t readByte(uint8_t address, uint8_t subAddress)

Parameter Description
address Chip address (MPU9250/AK8963)
subAddress Internal register address of the chip

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
    Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
}

readGyroData()

Description:

Reads the gyroscope values in three directions.

Syntax:

void readGyroData(int16_t * destination)

Parameter Description
destination Address of the array to save the gyroscope values

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    IMU.initMPU9250();
    byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
    Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
    IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}

void loop(){
    // If intPin goes high, all data registers have new data
    // On interrupt, check if data ready interrupt
    if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
    {
        // "gyroCount[3] = {0};" was defined at file "MPU9250.h"
        IMU.readGyroData(IMU.gyroCount);  // Read the x/y/z adc values
        IMU.getGres(); // get Gyro scales saved to "gRes"
        IMU.gx = (float)IMU.gyroCount[0]*IMU.gRes;
        IMU.gy = (float)IMU.gyroCount[1]*IMU.gRes;
        IMU.gz = (float)IMU.gyroCount[2]*IMU.gRes;
        Serial.print("X-gyro rate: "); Serial.print(IMU.gx, 3);
        Serial.print(" degrees/sec ");
        Serial.print("Y-gyro rate: "); Serial.print(IMU.gy, 3);
        Serial.print(" degrees/sec ");
        Serial.print("Z-gyro rate: "); Serial.print(IMU.gz, 3);
        Serial.println(" degrees/sec");
    }
}

readAccelData()

Description:

Reads the accelerometer values in three directions.

Syntax:

void readAccelData(int16_t * destination)

Parameter Description
destination Address of the array to save the accelerometer values

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup(){
    M5.begin();
    Serial.begin(115200);

    IMU.initMPU9250();
    byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
    Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
    IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}

void loop(){
    // If intPin goes high, all data registers have new data
    // On interrupt, check if data ready interrupt
    if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
    {
        // "accelCount[3] = {0};" was defined at file "MPU9250.h"
        IMU.readAccelData(IMU.accelCount);
        IMU.getAres(); // get accelerometer scales saved to "aRes"
        IMU.ax = (float)IMU.accelCount[0]*

IMU.aRes; // - accelBias[0];
        IMU.ay = (float)IMU.accelCount[1]*IMU.aRes; // - accelBias[1];
        IMU.az = (float)IMU.accelCount[2]*IMU.aRes; // - accelBias[2];
        Serial.print("X-acceleration: "); Serial.print(1000*IMU.ax);
        Serial.print(" mg ");
        Serial.print("Y-acceleration: "); Serial.print(1000*IMU.ay);
        Serial.print(" mg ");
        Serial.print("Z-acceleration: "); Serial.print(1000*IMU.az);
        Serial.println(" mg ");
    }
}

readTempData()

Description:

Reads the temperature.

Syntax:

int16_t readTempData(void)

Example:

#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup()
{
  M5.begin();
  Wire.begin();

  IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
  IMU.initMPU9250();
  IMU.initAK8963(IMU.magCalibration);
}

void loop()
{
   IMU.tempCount = IMU.readTempData();
   IMU.temperature = ((float) IMU.tempCount) / 333.87 + 21.0;
   M5.Lcd.setCursor(0, 0);
   M5.Lcd.print("MPU9250 Temperature is ");
   M5.Lcd.print(IMU.temperature, 1);
   delay(500);
}
On This Page