简体中文
English
简体中文
日本語

Arduino 上手教程

2. 设备开发 & 案例程序

5. 扩展模块

6. 应用案例

Chain Captain IMU 姿态传感器

Chain Captain 板载 BMI270 六轴 IMU 相关 API 与案例程序。

案例程序

编译要求

  • M5Stack 板管理版本 >= 3.3.10-a
  • 开发板选项 = M5ChainCaptain
  • M5Unified 库版本 >= 0.3.0
  • M5GFX 库版本 >= 0.3.0
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#include <M5Unified.h>

M5Canvas canvas(&M5.Display);

static uint32_t lastRefresh = 0;

static bool ensureImuReady()
{
  if (M5.Imu.isEnabled()) return true;

  // Retry the probe while M5PM1 and the shared I2C bus wake up.
  for (int retry = 0; retry < 5; ++retry) {
    if (M5.Imu.begin(&M5.In_I2C, M5.getBoard())) return true;
    delay(200);
  }
  return false;
}

static void drawReading()
{
  // BMI270: read accelerometer and gyroscope data.
  auto data = M5.Imu.getImuData();

  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);
  canvas.setFont(&fonts::FreeSansBold12pt7b);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.drawString("BMI270", 120, 22);

  canvas.drawRect(8, 44, 224, 120, TFT_DARKGREY);
  canvas.fillCircle(120, 104, 5, TFT_DARKGREY);
  // CoreP4X LSM6DS3TR-C and ChainCaptain BMI270 share the same fixed axis remap.
  // M5Unified applies it in getImuData(), so use the corrected axes directly.
  const int x = constrain(static_cast<int>(120 + data.accel.x * 80.0f), 18, 222);
  const int y = constrain(static_cast<int>(104 - data.accel.y * 45.0f), 54, 154);
  canvas.fillCircle(x, y, 8, TFT_GREEN);

  canvas.setFont(&fonts::FreeSans9pt7b);
  canvas.setTextDatum(top_left);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.setCursor(8, 172);
  canvas.printf("ACC  %+.2f %+.2f %+.2f\n", data.accel.x, data.accel.y, data.accel.z);
  canvas.printf("GYRO %+.2f %+.2f %+.2f", data.gyro.x, data.gyro.y, data.gyro.z);
  canvas.pushSprite(0, 0);
}

void setup()
{
  delay(500);
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  cfg.fallback_board = m5::board_t::board_M5ChainCaptain;
  M5.begin(cfg);

  if (M5.Display.getBoard() != m5::board_t::board_M5ChainCaptain) {
    M5.Display.init();
  }
  if (M5.Display.getBoard() != m5::board_t::board_M5ChainCaptain) {
    Serial.println("Display detection: no");
    return;
  }

  M5.Display.setRotation(0);
  M5.Display.setBrightness(128);

  canvas.createSprite(M5.Display.width(), M5.Display.height());

  const bool imuReady = ensureImuReady();
  Serial.printf("IMU enabled: %s\n", imuReady ? "yes" : "no");
  if (!imuReady) {
    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_RED, TFT_BLACK);
    canvas.drawString("IMU not found", 120, 120);
    canvas.pushSprite(0, 0);
    return;
  }

  drawReading();
}

void loop()
{
  if (!M5.Imu.isEnabled()) {
    delay(1000);
    return;
  }

  const uint32_t now = millis();
  if (now - lastRefresh >= 100) {
    lastRefresh = now;
    if (M5.Imu.update()) {
      drawReading();
    }
  }

  delay(10);
}

上传后,屏幕中央的绿色圆点会根据加速度方向移动,下方实时显示 BMI270 的加速度和角速度数据。

API

Chain Captain IMU 部分使用 M5Unified 库中的 IMU_Class,更多 IMU 相关的 API 可以参考下方文档:

Page Tools
PDF
On This Page