pdf-icon

Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

StopWatch IMU

StopWatch IMU姿勢センサー入力関連APIとサンプルプログラム。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーバージョン >= 3.3.7
  • ボードオプション = M5StopWatch
  • M5Unified ライブラリバージョン >= 0.2.15
  • M5GFX ライブラリバージョン >= 0.2.21
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <M5Unified.h>

void setup(void) {
    auto cfg = M5.config();
    M5.begin(cfg);
    M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
    Serial.begin(115200);
}

void loop(void) {
    auto imu_update = M5.Imu.update();
    if (imu_update) {
        auto data = M5.Imu.getImuData();

        // The data obtained by getImuData can be used as follows.
        data.accel.x;      // accel x-axis value.
        data.accel.y;      // accel y-axis value.
        data.accel.z;      // accel z-axis value.
        data.accel.value;  // accel 3values array [0]=x / [1]=y / [2]=z.

        data.gyro.x;      // gyro x-axis value.
        data.gyro.y;      // gyro y-axis value.
        data.gyro.z;      // gyro z-axis value.
        data.gyro.value;  // gyro 3values array [0]=x / [1]=y / [2]=z.

        data.value;  // all sensor 9values array [0~2]=accel / [3~5]=gyro / [6~8]=mag

        Serial.printf("ax: %f  ay: %f  az: %f\r\n", data.accel.x, data.accel.y, data.accel.z);
        Serial.printf("gx: %f  gy: %f  gz: %f\r\n", data.gyro.x, data.gyro.y, data.gyro.z);
        M5.Display.setCursor(40, 120);
        M5.Display.clear();  // Delay 100ms
        M5.Display.printf("IMU:\r\n");
        M5.Display.printf("       ax: %f\n       ay: %f\n       az: %f\r\n", data.accel.x, data.accel.y, data.accel.z);
        M5.Display.printf("       gx: %f\n       gy: %f\n       gz: %f\r\n", data.gyro.x, data.gyro.y, data.gyro.z);
    }
    delay(300);
}

書き込み後、シリアルモニタと画面上で IMU センサーの加速度およびジャイロスコープデータを確認できます。

API

StopWatch の IMU 部分では M5Unified ライブラリの IMU_Class を使用しています。IMU 関連APIの詳細は以下のドキュメントを参照してください:

On This Page