English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X IMU 6-Axis Attitude Sensor

APIs and example programs related to the IMU attitude sensor of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1

CoreP4X has an onboard LSM6DS3TR-C 6-axis attitude sensor, including a 3-axis accelerometer and a 3-axis gyroscope.

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 99 100 101 102 103 104 105 106 107 108 109 110 111
#include <M5Unified.h>

M5Canvas canvas(&M5.Display);

static void drawImu(const m5::imu_data_t& data)
{
  constexpr int boxX = 40;
  constexpr int boxY = 126;
  constexpr int boxW = 400;
  constexpr int boxH = 210;
  constexpr int centerX = boxX + boxW / 2;
  constexpr int centerY = boxY + boxH / 2;
  constexpr float ballScale = 70.0f;
  constexpr float ballXSign = 1.0f;
  constexpr float ballYSign = -1.0f;

  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);
  canvas.setTextColor(TFT_CYAN, TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold24pt7b);
  canvas.drawString("CoreP4X IMU", 240, 54);

  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString("LSM6DS3TR-C", 240, 94);

  canvas.drawRect(boxX, boxY, boxW, boxH, TFT_DARKGREY);
  canvas.drawFastVLine(centerX, boxY, boxH, 0x2124);
  canvas.drawFastHLine(boxX, centerY, boxW, 0x2124);
  canvas.fillCircle(centerX, centerY, 5, TFT_DARKGREY);

  // Screen X follows accel.x; screen Y is inverted because Y grows downward.
  const int ballX = constrain(
      centerX + static_cast<int>(data.accel.x * ballScale * ballXSign),
      boxX + 24, boxX + boxW - 24);
  const int ballY = constrain(
      centerY + static_cast<int>(data.accel.y * ballScale * ballYSign),
      boxY + 24, boxY + boxH - 24);
  canvas.fillCircle(ballX, ballY, 16, TFT_GREEN);

  char accLine[64];
  char gyroLine[64];
  snprintf(accLine, sizeof(accLine), "ACC  %+5.2f  %+5.2f  %+5.2f",
           data.accel.x, data.accel.y, data.accel.z);
  snprintf(gyroLine, sizeof(gyroLine), "GYRO %+5.2f  %+5.2f  %+5.2f",
           data.gyro.x, data.gyro.y, data.gyro.z);

  canvas.setTextDatum(middle_center);
  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.drawString(accLine, 240, 384);
  canvas.drawString(gyroLine, 240, 428);
  canvas.pushSprite(0, 0);
}

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

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

void setup()
{

  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setRotation(0);
  M5.Display.setBrightness(160);
  canvas.createSprite(M5.Display.width(), M5.Display.height());

  if (!ensureImuReady()) {
    canvas.fillSprite(TFT_BLACK);
    canvas.setTextDatum(middle_center);
    canvas.setTextColor(TFT_RED, TFT_BLACK);
    canvas.setFont(&fonts::FreeSansBold18pt7b);
    canvas.drawString("IMU not found", 240, 240);
    canvas.pushSprite(0, 0);
    Serial.println("IMU enabled: no");
    return;
  }

  Serial.println("IMU enabled: yes");
  drawImu(M5.Imu.getImuData());
}

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

  if (M5.Imu.update()) {
    const auto data = M5.Imu.getImuData();
    drawImu(data);
    Serial.printf("A: %.3f %.3f %.3f | G: %.3f %.3f %.3f\n",
                  data.accel.x, data.accel.y, data.accel.z,
                  data.gyro.x, data.gyro.y, data.gyro.z);
  }

  delay(100);
}

After uploading, tilt the CoreP4X to move the green ball in the center; the lower part of the screen shows the 3-axis acceleration and 3-axis angular velocity in real time.

API

The IMU section of CoreP4X uses the IMU_Class in the M5Unified library. For more APIs, refer to:

Page Tools
PDF
On This Page