mirror of
https://github.com/m5stack/m5-docs.git
synced 2026-05-20 10:23:01 -07:00
6.1 KiB
6.1 KiB
IMU Sensor MPU9250
IMUとして搭載されているMPU9250は2種類のチップを1つのモジュールに統合しています。ひとつはMPU6500という3軸のジャイロセンサーと3軸の加速度センサーのチップです。もうひとつはAK8963という3軸地磁気センサーのチップです。
initMPU9250()
構文:
void initMPU9250();
説明:
MPU6500チップを初期化します。
使用例:
#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() {
}
initAK8963()
構文:
void initAK8963(float * destination);
説明:
AK8963チップを初期化します。
| 引数 | 説明 | 型 |
|---|---|---|
| destination | AK8963補正用の値を保存する変数 | float * |
使用例:
#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() {
}
calibrateMPU9250()
構文:
void calibrateMPU9250(float * gyroBias, float * accelBias);
説明:
ジャイロと加速度センサーの補正値を計算します。
| 引数 | 説明 | 型 |
|---|---|---|
| gyroBias | gyro オフセット | float * |
| accelBias | accel オフセット | float * |
使用例:
#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() {
}
readByte()
構文:
uint8_t readByte(uint8_t address, uint8_t subAddress);
説明:
MPU9250の指定のレジスタから1バイト分のデータを取得します。
| 引数 | 説明 | 型 |
|---|---|---|
| address | (MPU9250/AK8963) I2Cアドレス | uint8_t |
| subAddress | レジスタアドレス | uint8_t |
使用例:
#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() {
}
readGyroData()
構文:
void readGyroData(int16_t * destination);
説明:
3軸ジャイロセンサーの値を取得します。
| 引数 | 説明 | 型 |
|---|---|---|
| destination | ジャイロセンサーの値 | int16_t * |
使用例:
#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);
}
readAccelData()
構文:
void readAccelData(int16_t * destination);
説明:
3軸加速度センサーから値を取得します。
| 引数 | 説明 | 型 |
|---|---|---|
| destination | 加速度センサーの値 | int16_t * |
使用例:
#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);
}