🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Compass Data from M5stack Fire

    Arduino
    1
    1
    2.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hetzer
      last edited by

      I have the M5Stack fire (Version:2018.2A) but having difficulties in getting magnometer data.
      Previously uiflow had the block get Z but it is removed at a later stage.
      I tried below Arduino code but readings are incorrect.
      Could anyone assist?

      #include <M5Stack.h>
      #include "utility/MPU9250.h"

      MPU9250 IMU;

      void setup()
      {
      M5.begin();
      Wire.begin();

      IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
      IMU.initMPU9250();
      IMU.initAK8963(IMU.magCalibration);
      }

      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)
      {
      IMU.readAccelData(IMU.accelCount);
      IMU.getAres();

      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];
      
      IMU.readGyroData(IMU.gyroCount);  // Read the x/y/z adc values
      IMU.getGres();
      
      // Calculate the gyro value into actual degrees per second
      // This depends on scale being set
      IMU.gx = (float)IMU.gyroCount[0] * IMU.gRes;
      IMU.gy = (float)IMU.gyroCount[1] * IMU.gRes;
      IMU.gz = (float)IMU.gyroCount[2] * IMU.gRes;
      
      IMU.readMagData(IMU.magCount);  // Read the x/y/z adc values
      IMU.getMres();
      // User environmental x-axis correction in milliGauss, should be
      // automatically calculated
      //IMU.magbias[0] = +470.;
      // User environmental x-axis correction in milliGauss TODO axis??
      //IMU.magbias[1] = +120.;
      // User environmental x-axis correction in milliGauss
      //IMU.magbias[2] = +125.;
      
      // Calculate the magnetometer values in milliGauss
      // Include factory calibration per data sheet and user environmental
      // corrections
      // Get actual magnetometer value, this depends on scale being set
      IMU.mx = (float)IMU.magCount[0] * IMU.mRes * IMU.magCalibration[0] -
               IMU.magbias[0];
      IMU.my = (float)IMU.magCount[1] * IMU.mRes * IMU.magCalibration[1] -
               IMU.magbias[1];
      IMU.mz = (float)IMU.magCount[2] * IMU.mRes * IMU.magCalibration[2] -
               IMU.magbias[2];
      
      
      
      int x=64+10;
      int y=128+20;
      int z=192+30;
      
      M5.Lcd.fillScreen(BLACK);
      M5.Lcd.setTextColor(GREEN , BLACK);
      M5.Lcd.setTextSize(2);
      M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250/AK8963");
      M5.Lcd.setCursor(0, 32); M5.Lcd.print("x");
      M5.Lcd.setCursor(x, 32); M5.Lcd.print("y");
      M5.Lcd.setCursor(y, 32); M5.Lcd.print("z");
      
      
      
      M5.Lcd.setCursor(0, 64 * 2); M5.Lcd.print((int)(IMU.gx));
      M5.Lcd.setCursor(x, 64 * 2); M5.Lcd.print((int)(IMU.gy));
      M5.Lcd.setCursor(y, 64 * 2); M5.Lcd.print((int)(IMU.gz));
      M5.Lcd.setCursor(z, 64 * 2); M5.Lcd.print("o/s");
      
      M5.Lcd.setCursor(0, 80 * 2); M5.Lcd.print((int)(IMU.mx));
      M5.Lcd.setCursor(x, 80 * 2); M5.Lcd.print((int)(IMU.my));
      M5.Lcd.setCursor(y, 80 * 2); M5.Lcd.print((int)(IMU.mz));
      M5.Lcd.setCursor(z, 80 * 2); M5.Lcd.print("mG");
      
      float headingRadians = atan2((IMU.my), (IMU.mx));
      float headingDegrees = headingRadians * 180 / PI;
      

      if (headingDegrees < 0) {
      headingDegrees += 360;
      }
      M5.Lcd.setTextColor(YELLOW , BLACK);
      M5.Lcd.setCursor(0, 48 * 2); M5.Lcd.print(headingDegrees);

      delay(100);
      

      }

      }

      1 Reply Last reply Reply Quote 0
      • First post
        Last post