MPU6050 Accel/Gyro no-interrupt gives erratic accel readings

what2use
Posts: 20
Joined: Fri Jul 10, 2020 5:27 pm

MPU6050 Accel/Gyro no-interrupt gives erratic accel readings

Postby what2use » Thu Aug 20, 2020 8:23 pm

I am using the Electronic Cats MPU6050 lib to interface with an MPU6050 on an ESP32 without interrupt pin. The basic code reads data from the MPU6050 and then displays it on the OLED

However the readings are very erratic. Even with the MPU taped to a table to give stability, the readings are erratic and occasionally read close to 2g without any movement at all.

I was told that an interrupt may cause the sketch to drop GPS data so I started tinkering with the MPU6050 without an interrupt

This is a test sketch that gets the acceleration data and displays it on the OLED

Any help is greatly appreciated
Thanks

Code: Select all

#include "heltec.h"
#include "SSD1306Ascii.h"

#include "SSD1306AsciiWire.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

SSD1306AsciiWire oled;

MPU6050 mpu;

bool dmpReady = false;
uint8_t devStatus;
uint16_t packetSize;
uint16_t fifoCount;
uint8_t fifoBuffer[64];
float MaxGx = 0, MaxGy = 0, MaxGz = 0;

Quaternion q; // [w, x, y, z] quaternion container

void setup() {
Heltec.begin(true /DisplayEnable Enable/, false /LoRa Enable/, true /Serial Enable/);

  oled.begin(&Adafruit128x64, 0x3C, 16);
  oled.setFont(System5x7);
  oled.clear();

  Wire.begin();  //pin 21 and 22 for ESP32
  Wire.setClock(400000); 

  Serial.begin(115200);

  Serial.println(F("Initializing I2C devices..."));
  mpu.initialize();

  Serial.println(F("Testing device connections..."));
  Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  Serial.println(F("Initializing DMP..."));
  oled.setCursor(0,0);
  oled.println("Calibrate Acc/Gyro");
  devStatus = mpu.dmpInitialize();

  mpu.setXGyroOffset(220);
  mpu.setYGyroOffset(76);
  mpu.setZGyroOffset(-85);
  mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

if (devStatus == 0) {
    mpu.CalibrateAccel(6);
    mpu.CalibrateGyro(6);
    mpu.PrintActiveOffsets();
    Serial.println(F("Enabling DMP..."));
    oled.print("Accel/Gyro Ready");
    delay(1000);
    oled.clear();
    mpu.setDMPEnabled(true);

    Serial.println(F("DMP ready!"));
    dmpReady = true;

    packetSize = mpu.dmpGetFIFOPacketSize();
} else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));
    Serial.print(devStatus);
    Serial.println(F(")"));
}

}

void loop() {
// if MPU6050nprogramming failed, don't do anything
if (!dmpReady) return;

while (fifoCount < packetSize) {
    if (fifoCount < packetSize) {
      // try to get out of the infinite loop 
      fifoCount = mpu.getFIFOCount();
    }  
}

fifoCount = mpu.getFIFOCount();
if(fifoCount < packetSize){
      }
else if (fifoCount >= 1024) {
    // reset so we can continue cleanly
    mpu.resetFIFO();
  //  fifoCount = mpu.getFIFOCount();  // will be zero after reset
    Serial.println(F("FIFO overflow!"));

}

while(fifoCount >= packetSize){
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
}

        mpu.dmpGetQuaternion(&q, fifoBuffer);

         if (abs(q.x) > abs(MaxGx))
          {
            MaxGx = q.x;
          } else{
            MaxGx = MaxGx;
          }

        if (abs(q.y) > abs(MaxGy))
          {
            MaxGy = q.y;
          } else{
            MaxGy = MaxGy;
          }

    if (abs(q.z) > abs(MaxGz))
      {
        MaxGz = q.z;
       } else{
        MaxGz = MaxGz;
       }

      oled.setCursor(0,0);
      oled.clearToEOL();
      oled.println("ACCEL  X     Y     Z");
      oled.setCursor(0,1);
      oled.clearToEOL();
      oled.setCursor(20,1);
      oled.print(q.x,2);
      oled.setCursor(60,1);
      oled.print(q.y,2);
      oled.setCursor(95,1);
      oled.println(q.z,2);

      oled.setCursor(0,4);
      oled.println("MaxG   X     Y     Z");
      oled.setCursor(0,5);
      oled.clearToEOL();
      oled.setCursor(20,5);
      oled.print(MaxGx);
      oled.setCursor(60,5);
      oled.print(MaxGy);
      oled.setCursor(95,5);
      oled.println(MaxGz);

Who is online

Users browsing this forum: Google [Bot] and 84 guests