IMU The MPU9250 is a multi-chip module that integrates two chips. One chip is the MPU6500, which consists of a 3-axis gyroscope and a 3-axis accelerometer. The other is the AK8963 is a 3-axis magnetometer.
Description:
This function initialize MPU6500 chip.
Syntax:
void initMPU9250()
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250(); // this line must be after Wire.begin()
}
void loop() {
}
Description:
This function initialize AK8963 chip.
Syntax:
void initAK8963(float * destination)
Argument | Description | Type |
---|---|---|
destination | for AK8963 Calibration Value | float * |
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
IMU.initAK8963(IMU.magCalibration);
M5.Lcd.println("AK8963 initialized for active data mode....");
if (Serial)
{
M5.Lcd.println("Calibration values: ");
M5.Lcd.print("X-Axis sensitivity adjustment value ");
M5.Lcd.println(IMU.magCalibration[0], 2);
M5.Lcd.print("Y-Axis sensitivity adjustment value ");
M5.Lcd.println(IMU.magCalibration[1], 2);
M5.Lcd.print("Z-Axis sensitivity adjustment value ");
M5.Lcd.println(IMU.magCalibration[2], 2);
}
}
void loop() {
}
Description:
This function calcurate offset values of gyro and accelerometer.
Syntax:
void calibrateMPU9250(float * gyroBias, float * accelBias)
Argument | Description | Type |
---|---|---|
gyroBias | gyro offset | float * |
accelBias | accel offset | float * |
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250();
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() {
}
Description:
This function reads a byte data from specified register of MPU9250.
Syntax:
uint8_t readByte(uint8_t address, uint8_t subAddress)
;
Argument | Description | Type |
---|---|---|
address | (MPU9250/AK8963) I2C address | uint8_t |
subAddress | register address | uint8_t |
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
uint8_t id = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
M5.Lcd.print("MPU9250 I AM 0x"); M5.Lcd.print(id, HEX);
}
void loop() {
}
Description:
This function reads the value of 3-axis gyro sensor.
Syntax:
void readGyroData(int16_t * destination)
Argument | Description | Type |
---|---|---|
destination | read gyro values | int16_t * |
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250();
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)
{
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
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;
M5.Lcd.print("X-gyro rate: "); M5.Lcd.print(IMU.gx, 3);
M5.Lcd.println(" degrees/sec ");
M5.Lcd.print("Y-gyro rate: "); M5.Lcd.print(IMU.gy, 3);
M5.Lcd.println(" degrees/sec ");
M5.Lcd.print("Z-gyro rate: "); M5.Lcd.print(IMU.gz, 3);
M5.Lcd.println(" degrees/sec");
}
delay(500);
}
Description:
This function reads the value of 3-axis accelerate sensor.
Syntax:
void readAccelData(int16_t * destination)
Argument | Description | Type |
---|---|---|
destination | read accelerate values | int16_t * |
Example:
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250();
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)
{
M5.Lcd.clear();
M5.Lcd.setCursor(0, 0);
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];
M5.Lcd.print("X-acceleration: "); M5.Lcd.print(1000 * IMU.ax);
M5.Lcd.println(" mg ");
M5.Lcd.print("Y-acceleration: "); M5.Lcd.print(1000 * IMU.ay);
M5.Lcd.println(" mg ");
M5.Lcd.print("Z-acceleration: "); M5.Lcd.print(1000 * IMU.az);
M5.Lcd.println(" mg ");
}
delay(500);
}
Description:
This function 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);
}