Issues reading MPU9250 data in setup

Hi Everyone.

I touched on this issue in a previous post but it has strayed quite far from the initial topic so I thought I'd separate this issue out so it can hopefully be found by anyone with similar issues in the future.

The issue is taking an average reading of IMU data from an MPU9250. Below the whole code for all context


#include "MPU9250.h"
#include <Arduino.h>
#include <ESP32Servo.h>
#include <SFE_BMP180.h>
#include <esp_now.h>
#include <WiFi.h>
#include <PID_v1.h>
#include "Filter.h"
#include "math.h"

//GlobalVariables  --------------------------------------
//Set Min/Max values for PID Output
int MAXANGLE = 90;
int MINANGLE = -90;

//Set Servo Pins
int XPin = 16;
int YPin = 17;
int ESCPin = 4;

//PID Tuning Parameters
double Kp=0.6;
double Ki=0.5;
double Kd=0.125;

float RollOutput = 0;
float PitchOutput = 0;

float RawPitch = 0;
float RawRoll = 0;
float RawAltitude = 0;

float FilteredPitch = 0;
float FilteredRoll = 0;
float FilteredAltitude = 0;
//Create PID Variables
double Setpoint1, Input1, Output1;
double Setpoint2, Input2, Output2;

double PitchAVG = 0;
double RollAVG= 0;

//Initialisers -----------------------------------------
//Create Servos
Servo XAxis;
Servo YAxis;
Servo ESC;

//Create pressure sesor variables
SFE_BMP180 pressure;

//Create IMU Variable
MPU9250 mpu;

//Filter Variables
ExponentialFilter<long> FilterPitch(25, 0);
ExponentialFilter<long> FilterRoll(25, 0);
ExponentialFilter<long> FilterAltitude(25, 0);

double baseline;

PID myPID(&Input1, &Output1, &Setpoint1, Kp, Ki, Kd, DIRECT);
PID myPID2(&Input2, &Output2, &Setpoint2, Kp, Ki, Kd, DIRECT);
//--------------------------------------------------------
                   
//RX Data struct
typedef struct struct_message {
  int val;
} struct_message;
struct_message myData;

//Recieve ESP32 Data from controller
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
}

void GimbalTest(){
  for (int i = 0 ; i <= 360 ; i++){
  int X = 90 + 20 * cos (i*M_PI/180) ;
  XAxis.write(X);
  Serial.print(X);
  int Y = 90 + 20 * sin (i*M_PI/180) ;
  YAxis.write(Y);
  Serial.print(Y);
  delay(5);
  }
}

//Pressure get funcion
double getPressure()
{
  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
  {


    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return(P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

void print_calibration() {
  Serial.println("< calibration parameters >");
  Serial.println("accel bias [g]: ");
  Serial.print(mpu.getAccBiasX() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
  Serial.print(", ");
  Serial.print(mpu.getAccBiasY() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
  Serial.print(", ");
  Serial.print(mpu.getAccBiasZ() * 1000.f / (float)MPU9250::CALIB_ACCEL_SENSITIVITY);
  Serial.println();
  Serial.println("gyro bias [deg/s]: ");
  Serial.print(mpu.getGyroBiasX() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
  Serial.print(", ");
  Serial.print(mpu.getGyroBiasY() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
  Serial.print(", ");
  Serial.print(mpu.getGyroBiasZ() / (float)MPU9250::CALIB_GYRO_SENSITIVITY);
  Serial.println();
}

void setup() {

  Serial.begin(115200);
  Wire.begin();
  delay(2000);

  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);

  if (!mpu.setup(0x68)) {  // change to your own address
  while (1) {
    Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
    delay(5000);
    }
    }
  if (pressure.begin()){
    Serial.println("BMP180 init success");
  }else{
    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while(1); // Pause forever.
    }


  // calibrate anytime you want to
  Serial.println("Accel Gyro calibration will start in 5sec.");
  Serial.println("Please leave the device still on the flat plane.");
  mpu.verbose(true);
  delay(5000);
  mpu.calibrateAccelGyro();

  print_calibration();
  mpu.verbose(false);

  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  XAxis.setPeriodHertz(50);
  XAxis.attach(XPin, 500, 2400); 
  YAxis.setPeriodHertz(50);
  YAxis.attach(YPin, 500, 2400); 
  ESC.setPeriodHertz(50);
  ESC.attach(ESCPin, 1000, 2000);

  baseline = getPressure();
  
  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");  

  float averagep [20] = {0};
  float averager [20] = {0};
  float averagep1 = 0;
  float averager1 = 0;

  Serial.println("Calculating Average pitch. This will take 20 seconds");

  for(int i = 0; i < 20; i++){
    if(mpu.update()){
      averagep[i] = mpu.getPitch();
      Serial.print("Pitch Reading ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(averagep[i]);
      delay(1000);
    }
  }

    for(int i = 10; i <20; i++){
    averagep1 += averagep[i];
  }

  PitchAVG = averagep1 / 10;

  Serial.print("Average Pitch = ");
  Serial.println(PitchAVG);

  Serial.println("Calculating Average roll. This will take 20 seconds");
  
  for(int i = 0; i < 20; i++){
    if(mpu.update()){
      averager[i] = mpu.getRoll();
      Serial.print("Roll Reading ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(averager[i]);
      delay(1000);
    }
  }

  for(int i = 10; i <20; i++){
    averager1 += averager[i];
  }
  RollAVG = averager1 / 10;

  Serial.print("Average Roll = ");
  Serial.println(RollAVG);

  Setpoint1 = PitchAVG;
  Setpoint2 = RollAVG;

  myPID.SetMode(AUTOMATIC);
  myPID2.SetMode(AUTOMATIC);

  GimbalTest();

  XAxis.write(90);
  YAxis.write(90);
}

void loop() {
  if(mpu.update()){

    double P;
    P = getPressure();

    RawPitch = mpu.getPitch();
    RawRoll = mpu.getYaw();
    RawAltitude = pressure.altitude(P, baseline);

    FilterPitch.Filter(RawPitch);
    FilterRoll.Filter(RawRoll);
    FilterAltitude.Filter(RawAltitude);

    FilteredPitch = FilterPitch.Current();
    FilteredRoll = FilterRoll.Current();
    FilteredAltitude = FilterAltitude.Current();

    Input1 = FilteredPitch;
    Input2 = FilteredRoll;

    myPID.Compute();
    myPID2.Compute();

    if (Output1 >= MAXANGLE){
      Output1 = MAXANGLE;
    }
    if (Output1 <= MINANGLE){
      Output1 = MINANGLE;
    }
    if (Output2 >= MAXANGLE){
      Output2 = MAXANGLE;
    }
    if (Output2 <= MINANGLE){
      Output2 = MINANGLE;
    }
    
    PitchOutput = Output1;
    RollOutput = Output2;

    XAxis.write(map(PitchOutput, -90, 90, 0, 180));
    YAxis.write(map(RollOutput, -90, 90, 0, 180));

    ESC.write(myData.val);


    Serial.print("RawPitch:");
    Serial.print(RawPitch);
    Serial.print("  ,  ");
    Serial.print("FilteredPitch:");
    Serial.print(FilteredPitch);
    Serial.print("  ,  ");
    // Serial.print("Pitch Output:");
    // Serial.print(map(PitchOutput, -90, 90, 0, 180));
    // Serial.print("  ,  ");
    Serial.print("RawRoll");
    Serial.print(RawRoll);
    Serial.print("  ,  ");
    Serial.print("FilteredRoll:");
    Serial.println(FilteredRoll);
    // Serial.print(",");
    // Serial.print("Roll Output:");
    // Serial.print(map(RollOutput, -90, 90, 0, 180));
    // Serial.print(",");
    // Serial.print("FilteredAltitude:");
    // Serial.println(FilteredAltitude);

  }


}

What I have found is that there tends to be a spike in the data when the readings begin. For this reason I've stored 20 readings, each a second apart, into an array and then just taken the last 10 readings to average. I did and made the timing so dramatic so that I could be sure the spiked data was not corrupting my results. See below the direct part of the code I am referencing.

  float averagep [20] = {0};
  float averager [20] = {0};
  float averagep1 = 0;
  float averager1 = 0;

  Serial.println("Calculating Average pitch. This will take 20 seconds");

  for(int i = 0; i < 20; i++){
    if(mpu.update()){
      averagep[i] = mpu.getPitch();
      Serial.print("Pitch Reading ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(averagep[i]);
      delay(1000);
    }
  }

    for(int i = 10; i <20; i++){
    averagep1 += averagep[i];
  }

  PitchAVG = averagep1 / 10;

  Serial.print("Average Pitch = ");
  Serial.println(PitchAVG);

  Serial.println("Calculating Average roll. This will take 20 seconds");
  
  for(int i = 0; i < 20; i++){
    if(mpu.update()){
      averager[i] = mpu.getRoll();
      Serial.print("Roll Reading ");
      Serial.print(i);
      Serial.print(" = ");
      Serial.println(averager[i]);
      delay(1000);
    }
  }

  for(int i = 10; i <20; i++){
    averager1 += averager[i];
  }
  RollAVG = averager1 / 10;

  Serial.print("Average Roll = ");
  Serial.println(RollAVG);

  Setpoint1 = PitchAVG;
  Setpoint2 = RollAVG;

Below is the output from running the code on my ESP32. The IMU itself was stationary while the readings where being taken so the data going into the array is quite clearly useless and I'm struggling to see where it is getting these figures from and why.


11:00:27.042 -> Accel Gyro calibration will start in 5sec.
11:00:27.076 -> Please leave the device still on the flat plane.
11:00:34.010 -> < calibration parameters >
11:00:34.010 -> accel bias [g]: 
11:00:34.010 -> 923.13, -44.01, -960.72
11:00:34.010 -> gyro bias [deg/s]: 
11:00:34.010 -> 9.37, -4.59, -8.66
11:00:34.010 -> baseline pressure: 1018.49 mb
11:00:34.044 -> Calculating Average pitch. This will take 20 seconds
11:00:34.044 -> Pitch Reading 0 = -76.81
11:00:35.018 -> Pitch Reading 1 = -37.82
11:00:36.050 -> Pitch Reading 2 = 9.50
11:00:37.032 -> Pitch Reading 3 = -24.53
11:00:38.043 -> Pitch Reading 4 = 13.12
11:00:39.033 -> Pitch Reading 5 = -19.17
11:00:40.060 -> Pitch Reading 6 = 16.60
11:00:41.052 -> Pitch Reading 7 = -17.75
11:00:42.054 -> Pitch Reading 8 = 14.59
11:00:43.062 -> Pitch Reading 9 = -12.99
11:00:44.087 -> Pitch Reading 10 = 15.26
11:00:45.065 -> Pitch Reading 11 = -17.44
11:00:46.080 -> Pitch Reading 12 = 17.40
11:00:47.098 -> Pitch Reading 13 = -17.66
11:00:48.086 -> Pitch Reading 14 = 19.77
11:00:49.077 -> Pitch Reading 15 = -22.69
11:00:50.091 -> Pitch Reading 16 = 11.03
11:00:51.101 -> Pitch Reading 17 = -8.75
11:00:52.086 -> Pitch Reading 18 = 17.74
11:00:53.123 -> Pitch Reading 19 = -10.80
11:00:54.110 -> Average Pitch = 0.38
11:00:54.110 -> Calculating Average roll. This will take 20 seconds
11:00:54.110 -> Roll Reading 0 = 167.27
11:00:55.125 -> Roll Reading 1 = -154.58
11:00:56.127 -> Roll Reading 2 = 163.50
11:00:57.122 -> Roll Reading 3 = -153.67
11:00:58.110 -> Roll Reading 4 = 165.66
11:00:59.134 -> Roll Reading 5 = -150.25
11:01:00.140 -> Roll Reading 6 = 166.97
11:01:01.129 -> Roll Reading 7 = -152.26
11:01:02.129 -> Roll Reading 8 = 164.73
11:01:03.157 -> Roll Reading 9 = -150.99
11:01:04.139 -> Roll Reading 10 = 167.46
11:01:05.145 -> Roll Reading 11 = -151.03
11:01:06.150 -> Roll Reading 12 = 165.47
11:01:07.161 -> Roll Reading 13 = -150.22
11:01:08.177 -> Roll Reading 14 = 162.75
11:01:09.158 -> Roll Reading 15 = -152.28
11:01:10.186 -> Roll Reading 16 = 162.88
11:01:11.172 -> Roll Reading 17 = -152.71
11:01:12.168 -> Roll Reading 18 = 161.86
11:01:13.199 -> Roll Reading 19 = -156.54
11:01:14.185 -> Average Roll = 5.77
11:01:14.185 -> 1109010990109901099110991109911099210992109921099310993109931099410994109941099510995109951099610896108961089710897108971089810898107981079910799107991071001071001061001061001061011061011061011051021051021051021051021051031041031041031041031041041031041031041031041031051021051021051021051021051011061011061011061001061001061001071001079910799107991079810798108981089710897108971089610896108961099510995109951099410994109941099310993109931099210992109921099110991109911099010990109901108910989109881098810988109871098710987109861098610986109851098510985109841098410984109831098310883108821088210882108811088110881107801078010780107801077910779106791067810678106781067710577105771057710576105761047610476104751047510375103751037410374102741027410274102731017310173101731007310072100721007299729972997298719871987197719771977196719670967095709570957094709470947093709370937092709270927091709170917090709070907089708970887088708870877087708770867086708670857085708570847084708470837183718371827182718271817181728172807280728072807279737973797378737873787477747774777477747675767576757675757675767576757674777477747774777478737873787379737973797279728072807280728172817181718271827182718371837183708470847084708570857085708670867086708770877087708870887088708970897090709070907091709170917092709270927093709370937094709470947095709570957096709671967197719771977198719871987299729972997210072100721007310073101731017310173102741027410274102741037410375103751037510475104761047610476105761057710577105771057710678106781067810679106791077910779107801078010780107811088110881108821088210882108831088310983109841098410984109851098510985109861098610986109871098710987109881098810988109891098911090RawPitch:30.61  ,  FilteredPitch:8.00  ,  RawRoll32.32  ,  FilteredRoll:8.00
11:01:16.080 -> RawPitch:30.68  ,  FilteredPitch:13.00  ,  RawRoll32.40  ,  FilteredRoll:14.00
11:01:16.114 -> RawPitch:30.37  ,  FilteredPitch:17.00  ,  RawRoll33.16  ,  FilteredRoll:19.00
11:01:16.148 -> RawPitch:29.39  ,  FilteredPitch:20.00  ,  RawRoll33.46  ,  FilteredRoll:22.00
11:01:16.181 -> RawPitch:28.62  ,  FilteredPitch:22.00  ,  RawRoll33.80  ,  FilteredRoll:25.00
11:01:16.215 -> RawPitch:27.54  ,  FilteredPitch:23.00  ,  RawRoll34.14  ,  FilteredRoll:27.00
11:01:16.249 -> RawPitch:26.47  ,  FilteredPitch:24.00  ,  RawRoll34.48  ,  FilteredRoll:29.00
11:01:16.283 -> RawPitch:25.33  ,  FilteredPitch:24.00  ,  RawRoll34.87  ,  FilteredRoll:30.00
11:01:16.317 -> RawPitch:24.14  ,  FilteredPitch:24.00  ,  RawRoll35.02  ,  FilteredRoll:31.00
11:01:16.352 -> RawPitch:23.07  ,  FilteredPitch:24.00  ,  RawRoll35.11  ,  FilteredRoll:32.00
11:01:16.385 -> RawPitch:21.89  ,  FilteredPitch:23.00  ,  RawRoll34.99  ,  FilteredRoll:33.00
11:01:16.418 -> RawPitch:20.53  ,  FilteredPitch:22.00  ,  RawRoll35.08  ,  FilteredRoll:33.00
11:01:16.487 -> RawPitch:19.11  ,  FilteredPitch:22.00  ,  RawRoll35.12  ,  FilteredRoll:34.00
11:01:16.524 -> RawPitch:17.80  ,  FilteredPitch:20.00  ,  RawRoll35.03  ,  FilteredRoll:34.00
11:01:16.558 -> RawPitch:16.56  ,  FilteredPitch:19.00  ,  RawRoll34.97  ,  FilteredRoll:34.00
11:01:16.596 -> RawPitch:15.36  ,  FilteredPitch:18.00  ,  RawRoll34.84  ,  FilteredRoll:34.00
11:01:16.634 -> RawPitch:14.00  ,  FilteredPitch:17.00  ,  RawRoll34.72  ,  FilteredRoll:34.00
11:01:16.672 -> RawPitch:12.52  ,  FilteredPitch:16.00  ,  RawRoll34.52  ,  FilteredRoll:34.00
11:01:16.706 -> RawPitch:11.13  ,  FilteredPitch:15.00  ,  RawRoll34.25  ,  FilteredRoll:34.00
11:01:16.740 -> RawPitch:9.86  ,  FilteredPitch:13.00  ,  RawRoll33.93  ,  FilteredRoll:34.00
11:01:16.775 -> RawPitch:8.58  ,  FilteredPitch:12.00  ,  RawRoll33.56  ,  FilteredRoll:34.00
11:01:16.808 -> RawPitch:7.15  ,  FilteredPitch:11.00  ,  RawRoll33.25  ,  FilteredRoll:33.00
11:01:16.846 -> RawPitch:5.85  ,  FilteredPitch:9.00  ,  RawRoll32.94  ,  FilteredRoll:33.00
11:01:16.879 -> RawPitch:4.74  ,  FilteredPitch:8.00  ,  RawRoll32.67  ,  FilteredRoll:33.00
11:01:16.912 -> RawPitch:3.60  ,  FilteredPitch:7.00  ,  RawRoll32.22  ,  FilteredRoll:33.00
11:01:16.946 -> RawPitch:2.76  ,  FilteredPitch:6.00  ,  RawRoll31.91  ,  FilteredRoll:32.00
11:01:16.982 -> RawPitch:2.50  ,  FilteredPitch:5.00  ,  RawRoll31.61  ,  FilteredRoll:32.00
11:01:17.015 -> RawPitch:1.66  ,  FilteredPitch:4.00  ,  RawRoll31.46  ,  FilteredRoll:32.00
11:01:17.048 -> RawPitch:1.47  ,  FilteredPitch:3.00  ,  RawRoll31.29  ,  FilteredRoll:32.00
11:01:17.117 -> RawPitch:1.22  ,  FilteredPitch:3.00  ,  RawRoll31.11  ,  FilteredRoll:31.00
11:01:17.150 -> RawPitch:0.76  ,  FilteredPitch:2.00  ,  RawRoll30.97  ,  FilteredRoll:31.00
11:01:17.183 -> RawPitch:0.52  ,  FilteredPitch:1.00  ,  RawRoll30.88  ,  FilteredRoll:31.00
11:01:17.217 -> RawPitch:0.25  ,  FilteredPitch:1.00  ,  RawRoll30.88  ,  FilteredRoll:31.00
11:01:17.251 -> RawPitch:0.21  ,  FilteredPitch:1.00  ,  RawRoll30.87  ,  FilteredRoll:30.00
11:01:17.286 -> RawPitch:0.03  ,  FilteredPitch:1.00  ,  RawRoll30.88  ,  FilteredRoll:30.00
11:01:17.319 -> RawPitch:-0.16  ,  FilteredPitch:0.00  ,  RawRoll30.84  ,  FilteredRoll:30.00
11:01:17.353 -> RawPitch:0.35  ,  FilteredPitch:0.00  ,  RawRoll30.81  ,  FilteredRoll:30.00
11:01:17.386 -> RawPitch:0.40  ,  FilteredPitch:0.00  ,  RawRoll30.74  ,  FilteredRoll:30.00
11:01:17.419 -> RawPitch:0.56  ,  FilteredPitch:0.00  ,  RawRoll30.78  ,  FilteredRoll:30.00
11:01:17.486 -> RawPitch:0.42  ,  FilteredPitch:0.00  ,  RawRoll30.71  ,  FilteredRoll:30.00
11:01:17.519 -> RawPitch:0.39  ,  FilteredPitch:0.00  ,  RawRoll30.68  ,  FilteredRoll:30.00
11:01:17.552 -> RawPitch:0.56  ,  FilteredPitch:0.00  ,  RawRoll30.64  ,  FilteredRoll:30.00
11:01:17.585 -> RawPitch:0.06  ,  FilteredPitch:0.00  ,  RawRoll30.62  ,  FilteredRoll:30.00
11:01:17.618 -> RawPitch:0.03  ,  FilteredPitch:0.00  ,  RawRoll30.53  ,  FilteredRoll:30.00
11:01:17.651 -> RawPitch:0.00  ,  FilteredPitch:0.00  ,  RawRoll30.58  ,  FilteredRoll:30.00
11:01:17.684 -> RawPitch:0.01  ,  FilteredPitch:0.00  ,  RawRoll30.53  ,  FilteredRoll:30.00
11:01:17.718 -> RawPitch:0.15  ,  FilteredPitch:0.00  ,  RawRoll30.49  ,  FilteredRoll:30.00
11:01:17.751 -> RawPitch:0.28  ,  FilteredPitch:0.00  ,  RawRoll30.58  ,  FilteredRoll:30.00
11:01:17.818 -> RawPitch:0.26  ,  FilteredPitch:0.00  ,  RawRoll30.60  ,  FilteredRoll:30.00
11:01:17.853 -> RawPitch:0.04  ,  FilteredPitch:0.00  ,  RawRoll30.56  ,  FilteredRoll:30.00
11:01:17.886 -> RawPitch:-0.01  ,  FilteredPitch:0.00  ,  RawRoll30.54  ,  FilteredRoll:30.00
11:01:17.919 -> RawPitch:0.01  ,  FilteredPitch:0.00  ,  RawRoll30.37  ,  FilteredRoll:30.00
11:01:17.952 -> RawPitch:0.18  ,  FilteredPitch:0.00  ,  RawRoll30.40  ,  FilteredRoll:30.00
11:01:17.987 -> RawPitch:0.09  ,  FilteredPitch:0.00  ,  RawRoll30.37  ,  FilteredRoll:30.00
11:01:18.020 -> RawPitch:-0.05  ,  FilteredPitch:0.00  ,  RawRoll30.34  ,  FilteredRoll:30.00
11:01:18.053 -> RawPitch:0.00  ,  FilteredPitch:0.00  ,  RawRoll30.31  ,  FilteredRoll:30.00
11:01:18.086 -> RawPitch:0.01  ,  FilteredPitch:0.00  ,  RawRoll30.32  ,  FilteredRoll:30.00
11:01:18.157 -> RawPitch:0.17  ,  FilteredPitch:0.00  ,  RawRoll30.31  ,  FilteredRoll:30.00
11:01:18.191 -> RawPitch:0.20  ,  FilteredPitch:0.00  ,  RawRoll30.39  ,  FilteredRoll:30.00
11:01:18.224 -> RawPitch:-0.01  ,  FilteredPitch:0.00  ,  RawRoll30.25  ,  FilteredRoll:30.00
11:01:18.258 -> RawPitch:0.11  ,  FilteredPitch:0.00  ,  RawRoll30.19  ,  FilteredRoll:30.00
11:01:18.291 -> RawPitch:-0.10  ,  FilteredPitch:0.00  ,  RawRoll30.26  ,  FilteredRoll:30.00
11:01:18.329 -> RawPitch:-0.11  ,  FilteredPitch:0.00  ,  RawRoll30.38  ,  FilteredRoll:30.00
11:01:18.362 -> RawPitch:-0.25  ,  FilteredPitch:0.00  ,  RawRoll30.42  ,  FilteredRoll:30.00
11:01:18.396 -> RawPitch:-0.18  ,  FilteredPitch:0.00  ,  RawRoll30.36  ,  FilteredRoll:30.00
11:01:18.431 -> RawPitch:-0.29  ,  FilteredPitch:0.00  ,  RawRoll30.44  ,  FilteredRoll:30.00
11:01:18.464 -> RawPitch:-0.03  ,  FilteredPitch:0.00  ,  RawRoll30.56  ,  FilteredRoll:30.00
11:01:18.500 -> RawPitch:0.18  ,  FilteredPitch:0.00  ,  RawRoll30.40  ,  FilteredRoll:30.00
11:01:18.534 -> RawPitch:0.30  ,  FilteredPitch:0.00  ,  RawRoll30.32  ,  FilteredRoll:30.00
11:01:18.602 -> RawPitch:0.30  ,  FilteredPitch:0.00  ,  RawRoll30.39  ,  FilteredRoll:30.00
11:01:18.638 -> RawPitch:0.19  ,  FilteredPitch:0.00  ,  RawRoll30.43  ,  FilteredRoll:30.00
11:01:18.672 -> RawPitch:0.27  ,  FilteredPitch:0.00  ,  RawRoll30.50  ,  FilteredRoll:30.00
11:01:18.708 -> RawPitch:0.11  ,  FilteredPitch:0.00  ,  RawRoll30.41  ,  FilteredRoll:30.00
11:01:18.743 -> RawPitch:0.15  ,  FilteredPitch:0.00  ,  RawRoll30.44  ,  FilteredRoll:30.00
11:01:18.776 -> RawPitch:0.15  ,  FilteredPitch:0.00  ,  RawRoll30.43  ,  FilteredRoll:30.00
11:01:18.810 -> RawPitch:0.16  ,  FilteredPitch:0.00  ,  RawRoll30.46  ,  FilteredRoll:30.00
11:01:18.846 -> RawPitch:0.20  ,  FilteredPitch:0.00  ,  RawRoll30.57  ,  FilteredRoll:30.00
11:01:18.880 -> RawPitch:0.05  ,  FilteredPitch:0.00  ,  RawRoll30.57  ,  FilteredRoll:30.00
11:01:18.914 -> RawPitch:0.08  ,  FilteredPitch:0.00  ,  RawRoll30.48  ,  FilteredRoll:30.00
11:01:18.948 -> RawPitch:-0.03  ,  FilteredPitch:0.00  ,  RawRoll30.53  ,  FilteredRoll:30.00
11:01:18.982 -> RawPitch:-0.09  ,  FilteredPitch:0.00  ,  RawRoll30.55  ,  FilteredRoll:30.00
11:01:19.015 -> RawPitch:-0.14  ,  FilteredPitch:0.00  ,  RawRoll30.48  ,  FilteredRoll:30.00
11:01:19.049 -> RawPitch:-0.17  ,  FilteredPitch:0.00  ,  RawRoll30.49  ,  FilteredRoll:30.00
11:01:19.115 -> RawPitch:-0.28  ,  FilteredPitch:0.00  ,  RawRoll30.45  ,  FilteredRoll:30.00
11:01:19.148 -> RawPitch:-0.24  ,  FilteredPitch:0.00  ,  RawRoll30.42  ,  FilteredRoll:30.00
11:01:19.182 -> RawPitch:-0.12  ,  FilteredPitch:0.00  ,  RawRoll30.46  ,  FilteredRoll:30.00
11:01:19.216 -> RawPitch:0.03  ,  FilteredPitch:0.00  ,  RawRoll30.47  ,  FilteredRoll:30.00
11:01:19.250 -> RawPitch:-0.03  ,  FilteredPitch:0.00  ,  RawRoll30.49  ,  FilteredRoll:30.00
11:01:19.284 -> RawPitch:0.09  ,  FilteredPitch:0.00  ,  RawRoll30.59  ,  FilteredRoll:30.00
11:01:19.317 -> RawPitch:0.16  ,  FilteredPitch:0.00  ,  RawRoll30.60  ,  FilteredRoll:30.00
11:01:19.350 -> RawPitch:0.18  ,  FilteredPitch:0.00  ,  RawRoll30.66  ,  FilteredRoll:30.00
11:01:19.383 -> RawPitch:0.29  ,  FilteredPitch:0.00  ,  RawRoll30.68  ,  FilteredRoll:30.00
11:01:19.420 -> RawPitch:0.16  ,  FilteredPitch:0.00  ,  RawRoll30.63  ,  FilteredRoll:30.00
11:01:19.456 -> RawPitch:0.26  ,  FilteredPitch:0.00  ,  RawRoll30.62  ,  FilteredRoll:30.00
11:01:19.526 -> RawPitch:0.35  ,  FilteredPitch:0.00  ,  RawRoll30.65  ,  FilteredRoll:30.00
11:01:19.564 -> RawPitch:0.30  ,  FilteredPitch:0.00  ,  RawRoll30.71  ,  FilteredRoll:30.00
11:01:19.597 -> RawPitch:0.20  ,  FilteredPitch:0.00  ,  RawRoll30.74  ,  FilteredRoll:30.00
11:01:19.635 -> RawPitch:-0.03  ,  FilteredPitch:0.00  ,  RawRoll30.82  ,  FilteredRoll:30.00
11:01:19.672 -> RawPitch:0.07  ,  FilteredPitch:0.00  ,  RawRoll30.71  ,  FilteredRoll:30.00
11:01:19.708 -> RawPitch:0.32  ,  FilteredPitch:0.00  ,  RawRoll30.59  ,  FilteredRoll:30.00
11:01:19.742 -> RawPitch:0.12  ,  FilteredPitch:0.00  ,  RawRoll30.53  ,  FilteredRoll:30.00
11:01:19.775 -> RawPitch:-0.03  ,  FilteredPitch:0.00  ,  RawRoll30.50  ,  FilteredRoll:30.00
11:01:19.808 -> RawPitch:-0.06  ,  FilteredPitch:0.00  ,  RawRoll30.41  ,  FilteredRoll:30.00
11:01:19.846 -> RawPitch:0.09  ,  FilteredPitch:0.00  ,  RawRoll30.34  ,  FilteredRoll:30.00
11:01:19.880 -> RawPitch:0.34  ,  FilteredPitch:0.00  ,  RawRoll30.44  ,  FilteredRoll:30.00
11:01:19.914 -> RawPitch:0.02  ,  FilteredPitch:0.00  ,  RawRoll30.42  ,  FilteredRoll:30.00
11:01:19.947 -> RawPitch:0.07  ,  FilteredPitch:0.00  ,  RawRoll30.39  ,  FilteredRoll:30.00
11:01:19.982 -> RawPitch:0.01  ,  FilteredPitch:0.00  ,  RawRoll30.43  ,  FilteredRoll:30.00
11:01:20.016 -> RawPitch:0.03  ,  FilteredPitch:0.00  ,  RawRoll30.46  ,  FilteredRoll:30.00
11:01:20.049 -> RawPitch:0.14  ,  FilteredPitch:0.00  ,  RawRoll30.38  ,  FilteredRoll:30.00
11:01:20.117 -> RawPitch:0.13  ,  FilteredPitch:0.00  ,  RawRoll30.38  ,  FilteredRoll:30.00
11:01:20.150 -> RawPitch:-0.05  ,  FilteredPitch:0.00  ,  RawRoll30.39  ,  FilteredRoll:30.00

You can see from the above data dump the pitch and roll readings taken in the setup and then you can see the spike in roll and pitch that happens through the first second or so of the main loop. The fluctuations of the data being read in setup are so varied and seemingly random, and don't correlate to the subsequent readings from the main loop and I have no idea why.... Help?!

Thanks in advanced!

A

#include "MPU9250.h"

which MPU9250 library are you using?

Hideakitai library

do you see that same weird behavior when you use their example

Yep, just tested this. I'm using the simple with calibration example. Below is what is returned.

12:24:17.452 -> Accel Gyro calibration will start in 5sec.
12:24:17.452 -> Please leave the device still on the flat plane.
12:24:24.385 -> Mag calibration will start in 5sec.
12:24:24.385 -> Please Wave device in a figure eight until done.
12:24:29.442 -> Mag Factory Calibration Values: 
12:24:29.442 -> X-Axis sensitivity offset value 1.21
12:24:29.442 -> Y-Axis sensitivity offset value 1.21
12:24:29.442 -> Z-Axis sensitivity offset value 1.16
12:24:29.442 -> Mag Calibration: Wave device in a figure eight until done!
12:24:52.952 -> mag x min/max:
12:24:52.952 -> 148
12:24:52.952 -> 119
12:24:52.952 -> mag y min/max:
12:24:52.952 -> 364
12:24:52.952 -> 335
12:24:52.952 -> mag z min/max:
12:24:52.952 -> 328
12:24:52.952 -> 302
12:24:52.952 -> Mag Calibration done!
12:24:52.952 -> AK8963 mag biases (mG)
12:24:52.952 -> 240.70, 633.67, 549.80
12:24:52.952 -> AK8963 mag scale (mG)
12:24:52.952 -> 0.98, 0.98, 1.05
12:24:52.987 -> Mag Factory Calibration Values: 
12:24:52.987 -> X-Axis sensitivity offset value 1.21
12:24:52.987 -> Y-Axis sensitivity offset value 1.21
12:24:52.987 -> Z-Axis sensitivity offset value 1.16
12:24:52.987 -> < calibration parameters >
12:24:52.987 -> accel bias [g]: 
12:24:52.987 -> 923.56, -47.75, -961.08
12:24:53.022 -> gyro bias [deg/s]: 
12:24:53.022 -> 9.36, -4.65, -8.65
12:24:53.022 -> mag bias [mG]: 
12:24:53.022 -> 240.70, 633.67, 549.80
12:24:53.022 -> mag scale []: 
12:24:53.022 -> 0.98, 0.98, 1.05
12:24:53.022 -> Yaw, Pitch, Roll: -133.56, -9.45, -166.62
12:24:53.058 -> Yaw, Pitch, Roll: -133.72, -8.92, -167.47
12:24:53.095 -> Yaw, Pitch, Roll: -133.58, -8.50, -168.22
12:24:53.128 -> Yaw, Pitch, Roll: -133.80, -8.22, -168.96
12:24:53.128 -> Yaw, Pitch, Roll: -133.83, -7.76, -169.49
12:24:53.166 -> Yaw, Pitch, Roll: -134.16, -7.41, -170.06
12:24:53.200 -> Yaw, Pitch, Roll: -134.43, -7.18, -170.71
12:24:53.236 -> Yaw, Pitch, Roll: -134.50, -6.78, -171.17
12:24:53.236 -> Yaw, Pitch, Roll: -134.88, -6.34, -171.49
12:24:53.269 -> Yaw, Pitch, Roll: -134.84, -5.97, -171.99
12:24:53.306 -> Yaw, Pitch, Roll: -134.81, -5.61, -172.49
12:24:53.340 -> Yaw, Pitch, Roll: -134.92, -5.28, -173.01
12:24:53.373 -> Yaw, Pitch, Roll: -134.90, -5.11, -173.47
12:24:53.373 -> Yaw, Pitch, Roll: -134.77, -4.82, -173.83
12:24:53.408 -> Yaw, Pitch, Roll: -134.70, -4.56, -174.14
12:24:53.445 -> Yaw, Pitch, Roll: -134.77, -4.01, -174.29
12:24:53.478 -> Yaw, Pitch, Roll: -135.11, -3.82, -174.56
12:24:53.511 -> Yaw, Pitch, Roll: -135.33, -3.70, -174.88
12:24:53.511 -> Yaw, Pitch, Roll: -135.36, -3.62, -175.28
12:24:53.550 -> Yaw, Pitch, Roll: -135.31, -3.55, -175.51
12:24:53.587 -> Yaw, Pitch, Roll: -135.23, -3.44, -175.79
12:24:53.623 -> Yaw, Pitch, Roll: -135.75, -3.28, -175.89
12:24:53.623 -> Yaw, Pitch, Roll: -135.76, -3.19, -176.13
12:24:53.656 -> Yaw, Pitch, Roll: -135.81, -3.03, -176.32
12:24:53.689 -> Yaw, Pitch, Roll: -136.36, -2.97, -176.42
12:24:53.723 -> Yaw, Pitch, Roll: -136.32, -2.67, -176.48
12:24:53.756 -> Yaw, Pitch, Roll: -136.64, -2.47, -176.53
12:24:53.756 -> Yaw, Pitch, Roll: -137.02, -2.38, -176.66
12:24:53.790 -> Yaw, Pitch, Roll: -137.05, -2.40, -176.78
12:24:53.823 -> Yaw, Pitch, Roll: -137.35, -2.24, -176.93
12:24:53.857 -> Yaw, Pitch, Roll: -137.67, -2.20, -176.99
12:24:53.890 -> Yaw, Pitch, Roll: -138.04, -2.01, -176.95
12:24:53.923 -> Yaw, Pitch, Roll: -138.39, -1.75, -176.87
12:24:53.923 -> Yaw, Pitch, Roll: -138.33, -1.56, -177.04
12:24:53.957 -> Yaw, Pitch, Roll: -138.65, -1.42, -177.09
12:24:53.990 -> Yaw, Pitch, Roll: -138.52, -1.60, -176.99
12:24:54.023 -> Yaw, Pitch, Roll: -138.78, -1.64, -177.33
12:24:54.057 -> Yaw, Pitch, Roll: -138.93, -1.68, -177.38
12:24:54.057 -> Yaw, Pitch, Roll: -139.25, -1.74, -177.43
12:24:54.090 -> Yaw, Pitch, Roll: -138.89, -1.96, -177.48
12:24:54.125 -> Yaw, Pitch, Roll: -138.95, -2.11, -177.70
12:24:54.158 -> Yaw, Pitch, Roll: -138.95, -2.44, -178.09
12:24:54.191 -> Yaw, Pitch, Roll: -139.02, -2.41, -178.33
12:24:54.224 -> Yaw, Pitch, Roll: -139.37, -2.33, -178.48
12:24:54.224 -> Yaw, Pitch, Roll: -139.82, -2.23, -178.31
12:24:54.257 -> Yaw, Pitch, Roll: -140.09, -2.29, -178.34
12:24:54.293 -> Yaw, Pitch, Roll: -140.29, -2.45, -178.63
12:24:54.327 -> Yaw, Pitch, Roll: -140.60, -2.38, -178.54
12:24:54.360 -> Yaw, Pitch, Roll: -140.44, -2.40, -178.48

As you can see the pitch and roll still have the spike in the first few readings.

Right so I found one massive derp mistake...

I had been initialising the RawRoll data to read Yaw..not Roll. Nonetheless the results are still nonsensical and the issue remains.

So I've managed to find my way around having to average out the data and got a dataset that works. The issue of the spike still remains but I believe this may have to do with the filter the library uses...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.