Files

263 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2019-02-28 15:56:00 +08:00
# IMU Sensor MPU9250
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
*IMUとして搭載されているMPU9250は2種類のチップを1つのモジュールに統合しています。ひとつはMPU6500という3軸のジャイロセンサーと3軸の加速度センサーのチップです。もうひとつはAK8963という3軸地磁気センサーのチップです。*
2019-02-27 11:57:45 +08:00
## initMPU9250()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>void initMPU9250();</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
MPU6500チップを初期化します。
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250(); // this line must be after Wire.begin()
}
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
void loop() {
2019-02-27 11:57:45 +08:00
}
```
## initAK8963()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>void initAK8963(float * destination);</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
AK8963チップを初期化します。
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
| 引数 | 説明 | 型 |
| --- | --- | -- |
| destination | AK8963補正用の値を保存する変数 | float * |
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
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() {
2019-02-27 11:57:45 +08:00
}
```
## calibrateMPU9250()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>void calibrateMPU9250(float * gyroBias, float * accelBias);</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
ジャイロと加速度センサーの補正値を計算します。
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
| 引数 | 説明 | 型 |
| --- | --- | -- |
| gyroBias | gyro オフセット | float * |
| accelBias | accel オフセット | float * |
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
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 ");
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
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");
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
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");
2019-02-27 11:57:45 +08:00
}
2019-02-28 15:56:00 +08:00
void loop() {
2019-02-27 11:57:45 +08:00
}
```
## readByte()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>uint8_t readByte(uint8_t address, uint8_t subAddress);</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
MPU9250の指定のレジスタから1バイト分のデータを取得します。
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
| 引数 | 説明 | 型 |
| --- | --- | -- |
| address | (MPU9250/AK8963) I2Cアドレス | uint8_t |
| subAddress | レジスタアドレス | uint8_t |
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
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() {
2019-02-27 11:57:45 +08:00
}
```
## readGyroData()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>void readGyroData(int16_t * destination);</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
3軸ジャイロセンサーの値を取得します。
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
| 引数 | 説明 | 型 |
| --- | --- | -- |
| destination | ジャイロセンサーの値 | int16_t * |
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250();
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
2019-02-27 11:57:45 +08:00
}
2019-02-28 15:56:00 +08:00
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);
2019-02-27 11:57:45 +08:00
}
```
## readAccelData()
2019-02-28 15:56:00 +08:00
**構文:**
2019-02-27 11:57:45 +08:00
2019-03-01 18:57:09 +08:00
<mark>void readAccelData(int16_t * destination);</mark>
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
**説明:**
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
3軸加速度センサーから値を取得します。
2019-02-27 11:57:45 +08:00
2019-02-28 15:56:00 +08:00
| 引数 | 説明 | 型 |
| --- | --- | -- |
| destination | 加速度センサーの値 | int16_t * |
**使用例:**
2019-02-27 11:57:45 +08:00
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
2019-02-28 15:56:00 +08:00
void setup() {
M5.begin();
Wire.begin();
IMU.initMPU9250();
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
2019-02-27 11:57:45 +08:00
}
2019-02-28 15:56:00 +08:00
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);
2019-02-27 11:57:45 +08:00
}
```