pdf-icon

姿态传感器 MPU9250

姿态传感器 MPU9250 是一款多芯片模块,集成两个芯片,一个芯片是 MPU6500,由 3 轴陀螺仪和 3 轴加速度计组成,另外一个是 AK8963 是 3 轴磁力计

注意: 在使用前应使用 MPU9250 IMU 创建实体

initMPU9250()

功能:

初始化 MPU6500 芯片

函数原型:

void initMPU9250()

使用示例:

#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()

功能:

初始化 AK8963 芯片

函数原型:

void initAK8963(float * destination)

参数 描述
destination 出厂预设于 AK8963 芯片内缺省值的数组指针

使用示例:

#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()

功能:

初始化芯片后,执行该函数以读取静止状态下的陀螺仪和加速度值偏移,并保存到各自的偏移寄存器

函数原型:

void calibrateMPU9250(float * gyroBias, float * accelBias)

参数 描述
gyroBias 保存静止下陀螺仪值的数组的地址
accelBias 保存静止下加速度值的数组的地址

使用示例:

#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()

功能:

读取指定寄存器内数据返回值:寄存器值,长度为8位

函数原型:

uint8_t readByte(uint8_t address, uint8_t subAddress)

参数 描述
address 芯片 (MPU9250/AK8963)地址
subAddress 芯片内寄存器地址

使用示例:

#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()

功能:

读取三个方向的陀螺仪值

函数原型:

void readGyroData(int16_t * destination)

参数 描述
destination 保存陀螺仪值的数组的地址

使用示例:

#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()

功能:

读取三个方向的加速度值

函数原型:

void readAccelData(int16_t * destination)

参数 描述
destination 保存加速度值的数组的地址

使用示例:

#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()

功能:

读取温度

函数原型:

int16_t readTempData(void)

使用示例:

#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