kriswiner / MPU9250

Arduino sketches for MPU9250 9DoF with AHRS sensor fusion
1.03k stars 472 forks source link

MPU9250 Arduino Pro Mini calibration #323

Closed gojunnyo closed 5 years ago

gojunnyo commented 5 years ago

Hi Kris! I am honestly tired due to 5 days and night of straight browsing, reading and basically doing anything to calibrate my MPU9250, I have read the 0x73 "issue" and that my sensor is MPU9255, I am new to these sensors and knew basics in Arduino. I had read somewhere that MPU6050 code "theoretically" is the same with MPU9250 but without the magnetometer, so what I did is I used MPU6050 calibration sketches. Although it says MPU6050 connection failed, the sketch managed to spit out data which confuses me (it failed so it shouldn't continue but why did it? or it says it failed because I used MPU9250). The problem is, it takes literally forever to calibrate as if it doesn't want to calibrate at all. I tried modifying your MPU9250_AHRS sketch and got it working but I have no idea what to do with the data. From what I observed on the serial plotter, the graph seems back and forth; more like a triangle wave. And still, I have no idea what was happening, although I observed changes if I move the sensors (obviously). Any help is much appreciated!

MPU6050 CALIBRATION SOURCE: http://wired.chillibasket.com/2015/01/calibrating-mpu6050/

``

include "I2Cdev.h"

include "MPU6050.h"

include "Wire.h"

/////////////////////////////////// CONFIGURATION ///////////////////////////// //Change this 3 variables if you want to fine tune the skecth to your needs. int buffersize=1000; //Amount of readings used to average, make it higher to get more precision but sketch will be slower (default:1000) int acel_deadzone=8; //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge (default:8) int giro_deadzone=1; //Giro error allowed, make it lower to get more precision, but sketch may not converge (default:1)

// default I2C address is 0x68 // specific I2C addresses may be passed as a parameter here // AD0 low = 0x68 (default for InvenSense evaluation board) // AD0 high = 0x69 //MPU6050 accelgyro; MPU6050 accelgyro(0x68); // <-- use for AD0 high

int16_t ax, ay, az,gx, gy, gz;

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0; int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

/////////////////////////////////// SETUP //////////////////////////////////// void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) Wire.begin(); // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

// initialize serial communication Serial.begin(115200);

// initialize device accelgyro.initialize();

// wait for ready while (Serial.available() && Serial.read()); // empty buffer while (!Serial.available()){ Serial.println(F("Send any character to start sketch.\n")); delay(1500); }
while (Serial.available() && Serial.read()); // empty buffer again

// start message Serial.println("\nMPU6050 Calibration Sketch"); delay(2000); Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n"); delay(3000); // verify connection Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); delay(1000); // reset offsets accelgyro.setXAccelOffset(0); accelgyro.setYAccelOffset(0); accelgyro.setZAccelOffset(0); accelgyro.setXGyroOffset(0); accelgyro.setYGyroOffset(0); accelgyro.setZGyroOffset(0); }

/////////////////////////////////// LOOP //////////////////////////////////// void loop() { if (state==0){ Serial.println("\nReading sensors for first time..."); meansensors(); state++; delay(1000); }

if (state==1) { Serial.println("\nCalculating offsets..."); calibration(); state++; delay(1000); }

if (state==2) { meansensors(); Serial.println("\nFINISHED!"); Serial.print("\nSensor readings with offsets:\t"); Serial.print(mean_ax); Serial.print("\t"); Serial.print(mean_ay); Serial.print("\t"); Serial.print(mean_az); Serial.print("\t"); Serial.print(mean_gx); Serial.print("\t"); Serial.print(mean_gy); Serial.print("\t"); Serial.println(mean_gz); Serial.print("Your offsets:\t"); Serial.print(ax_offset); Serial.print("\t"); Serial.print(ay_offset); Serial.print("\t"); Serial.print(az_offset); Serial.print("\t"); Serial.print(gx_offset); Serial.print("\t"); Serial.print(gy_offset); Serial.print("\t"); Serial.println(gz_offset); Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ"); Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0"); Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)"); while (1); } }

/////////////////////////////////// FUNCTIONS //////////////////////////////////// void meansensors(){ long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

while (i<(buffersize+101)){ // read raw accel/gyro measurements from device accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
  buff_ax=buff_ax+ax;
  buff_ay=buff_ay+ay;
  buff_az=buff_az+az;
  buff_gx=buff_gx+gx;
  buff_gy=buff_gy+gy;
  buff_gz=buff_gz+gz;
}
if (i==(buffersize+100)){
  mean_ax=buff_ax/buffersize;
  mean_ay=buff_ay/buffersize;
  mean_az=buff_az/buffersize;
  mean_gx=buff_gx/buffersize;
  mean_gy=buff_gy/buffersize;
  mean_gz=buff_gz/buffersize;
}
i++;
delay(2); //Needed so we don't get repeated measures

} }

void calibration(){ ax_offset=-mean_ax/8; ay_offset=-mean_ay/8; az_offset=(16384-mean_az)/8;

gx_offset=-mean_gx/4; gy_offset=-mean_gy/4; gz_offset=-mean_gz/4; while (1){ int ready=0; accelgyro.setXAccelOffset(ax_offset); accelgyro.setYAccelOffset(ay_offset); accelgyro.setZAccelOffset(az_offset);

accelgyro.setXGyroOffset(gx_offset);
accelgyro.setYGyroOffset(gy_offset);
accelgyro.setZGyroOffset(gz_offset);

meansensors();
Serial.println("...");

if (abs(mean_ax)<=acel_deadzone) ready++;
else ax_offset=ax_offset-mean_ax/acel_deadzone;

if (abs(mean_ay)<=acel_deadzone) ready++;
else ay_offset=ay_offset-mean_ay/acel_deadzone;

if (abs(16384-mean_az)<=acel_deadzone) ready++;
else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

if (abs(mean_gx)<=giro_deadzone) ready++;
else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

if (abs(mean_gy)<=giro_deadzone) ready++;
else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

if (abs(mean_gz)<=giro_deadzone) ready++;
else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

if (ready==6) break;

} } ``

I also tried Jeff Rowberg's MPU6050 Library - IMU_Zero Sketch and ran it for 30 minutes, no offset were shown on my serial monitor :'(

IMU_Zero Sketch ``

include "I2Cdev.h"

include "MPU6050.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h

if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

#include "Wire.h"

endif

// class default I2C address is 0x68 // specific I2C addresses may be passed as a parameter here // AD0 low = 0x68 (default for InvenSense evaluation board) // AD0 high = 0x69 MPU6050 accelgyro; //MPU6050 accelgyro(0x69); // <-- use for AD0 high

const char LBRACKET = '['; const char RBRACKET = ']'; const char COMMA = ','; const char BLANK = ' '; const char PERIOD = '.';

const int iAx = 0; const int iAy = 1; const int iAz = 2; const int iGx = 3; const int iGy = 4; const int iGz = 5;

const int usDelay = 3150; // empirical, to hold sampling to 200 Hz const int NFast = 1000; // the bigger, the better (but slower) const int NSlow = 10000; // .. const int LinesBetweenHeaders = 5; int LowValue[6]; int HighValue[6]; int Smoothed[6]; int LowOffset[6]; int HighOffset[6]; int Target[6]; int LinesOut; int N;

void ForceHeader() { LinesOut = 99; }

void GetSmoothed() { int16_t RawValue[6]; int i; long Sums[6]; for (i = iAx; i <= iGz; i++) { Sums[i] = 0; } // unsigned long Start = micros();

for (i = 1; i <= N; i++)
  { // get sums
    accelgyro.getMotion6(&RawValue[iAx], &RawValue[iAy], &RawValue[iAz], 
                         &RawValue[iGx], &RawValue[iGy], &RawValue[iGz]);
    if ((i % 500) == 0)
      Serial.print(PERIOD);
    delayMicroseconds(usDelay);
    for (int j = iAx; j <= iGz; j++)
      Sums[j] = Sums[j] + RawValue[j];
  } // get sums

// unsigned long usForN = micros() - Start; // Serial.print(" reading at "); // Serial.print(1000000/((usForN+N/2)/N)); // Serial.println(" Hz"); for (i = iAx; i <= iGz; i++) { Smoothed[i] = (Sums[i] + N/2) / N ; } } // GetSmoothed

void Initialize() { // join I2C bus (I2Cdev library doesn't do this automatically)

if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

    Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
    Fastwire::setup(400, true);
#endif

Serial.begin(9600);

// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();

// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

} // Initialize

void SetOffsets(int TheOffsets[6]) { accelgyro.setXAccelOffset(TheOffsets [iAx]); accelgyro.setYAccelOffset(TheOffsets [iAy]); accelgyro.setZAccelOffset(TheOffsets [iAz]); accelgyro.setXGyroOffset (TheOffsets [iGx]); accelgyro.setYGyroOffset (TheOffsets [iGy]); accelgyro.setZGyroOffset (TheOffsets [iGz]); } // SetOffsets

void ShowProgress() { if (LinesOut >= LinesBetweenHeaders) { // show header Serial.println("\tXAccel\t\t\tYAccel\t\t\t\tZAccel\t\t\tXGyro\t\t\tYGyro\t\t\tZGyro"); LinesOut = 0; } // show header Serial.print(BLANK); for (int i = iAx; i <= iGz; i++) { Serial.print(LBRACKET); Serial.print(LowOffset[i]), Serial.print(COMMA); Serial.print(HighOffset[i]); Serial.print("] --> ["); Serial.print(LowValue[i]); Serial.print(COMMA); Serial.print(HighValue[i]); if (i == iGz) { Serial.println(RBRACKET); } else { Serial.print("]\t"); } } LinesOut++; } // ShowProgress

void PullBracketsIn() { boolean AllBracketsNarrow; boolean StillWorking; int NewOffset[6];

Serial.println("\nclosing in:");
AllBracketsNarrow = false;
ForceHeader();
StillWorking = true;
while (StillWorking) 
  { StillWorking = false;
    if (AllBracketsNarrow && (N == NFast))
      { SetAveraging(NSlow); }
    else
      { AllBracketsNarrow = true; }// tentative
    for (int i = iAx; i <= iGz; i++)
      { if (HighOffset[i] <= (LowOffset[i]+1))
          { NewOffset[i] = LowOffset[i]; }
        else
          { // binary search
            StillWorking = true;
            NewOffset[i] = (LowOffset[i] + HighOffset[i]) / 2;
            if (HighOffset[i] > (LowOffset[i] + 10))
              { AllBracketsNarrow = false; }
          } // binary search
      }
    SetOffsets(NewOffset);
    GetSmoothed();
    for (int i = iAx; i <= iGz; i++)
      { // closing in
        if (Smoothed[i] > Target[i])
          { // use lower half
            HighOffset[i] = NewOffset[i];
            HighValue[i] = Smoothed[i];
          } // use lower half
        else
          { // use upper half
            LowOffset[i] = NewOffset[i];
            LowValue[i] = Smoothed[i];
          } // use upper half
      } // closing in
    ShowProgress();
  } // still working

} // PullBracketsIn

void PullBracketsOut() { boolean Done = false; int NextLowOffset[6]; int NextHighOffset[6];

Serial.println("expanding:");
ForceHeader();

while (!Done)
  { Done = true;
    SetOffsets(LowOffset);
    GetSmoothed();
    for (int i = iAx; i <= iGz; i++)
      { // got low values
        LowValue[i] = Smoothed[i];
        if (LowValue[i] >= Target[i])
          { Done = false;
            NextLowOffset[i] = LowOffset[i] - 1000;
          }
        else
          { NextLowOffset[i] = LowOffset[i]; }
      } // got low values

    SetOffsets(HighOffset);
    GetSmoothed();
    for (int i = iAx; i <= iGz; i++)
      { // got high values
        HighValue[i] = Smoothed[i];
        if (HighValue[i] <= Target[i])
          { Done = false;
            NextHighOffset[i] = HighOffset[i] + 1000;
          }
        else
          { NextHighOffset[i] = HighOffset[i]; }
      } // got high values
    ShowProgress();
    for (int i = iAx; i <= iGz; i++)
      { LowOffset[i] = NextLowOffset[i];   // had to wait until ShowProgress done
        HighOffset[i] = NextHighOffset[i]; // ..
      }
 } // keep going

} // PullBracketsOut

void SetAveraging(int NewN) { N = NewN; Serial.print("averaging "); Serial.print(N); Serial.println(" readings each time"); } // SetAveraging

void setup() { Initialize(); for (int i = iAx; i <= iGz; i++) { // set targets and initial guesses Target[i] = 0; // must fix for ZAccel HighOffset[i] = 0; LowOffset[i] = 0; } // set targets and initial guesses Target[iAz] = 16384; SetAveraging(NFast);

PullBracketsOut();
PullBracketsIn();

Serial.println("-------------- done --------------");

} // setup

void loop() { } // loop ``

gojunnyo commented 5 years ago

By the way, my hardware connection is MPU9250/MPU9255 ------------------ ARDUINO PRO MINI VCC ------------------ 3.3 Volts GND ------------------- GND SCL ------------------- SCL SDA ------------------- SDA

Although I can say that this doesn't help, but I want to clarify that I have done my connections properly.

frx9paaiow3xmx7 large

kriswiner commented 5 years ago

Just check that SDA/SCL aren;t reverswed. I can;t remember which way is which off the top of my head. You do need a GND connection, of course. Which MPU9250 breakout are you using? And what do you mean "does not work"?

On Fri, Nov 9, 2018 at 8:35 AM gojunnyo notifications@github.com wrote:

By the way, my hardware connection is MPU9250/MPU9255 ------------------ ARDUINO PRO MINI VCC ------------------ 3.3 Volts GND ------------------- GND SCL ------------------- SCL SDA ------------------- SDA

Although I can say that this doesn't help, but I want to clarify that I have done my connections properly.

[image: frx9paaiow3xmx7 large] https://user-images.githubusercontent.com/18816265/48275463-826e1780-e480-11e8-822b-df04c74d5e70.jpg

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437416715, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qq4FBn8xHDPh50rLIaL67P5Qpyzpks5uta7rgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Thank you for your fast reply sir!

I forgot to mention, in I2C scanner sketch, the sensor is acknowledged. What will happen if the SDA and SCL are reversed? (Enlighten me with your knowledge) Just as I checked, my connections are correct.

I just wanted to know the offsets of the gyroscope and accelerometer of my MPU9250. Because I cannot find any sketches that could help me. Unless if I have MPU6050. I cannot find any MPU9250 calibration sketches that would spit up gyroscope and accelerometer offsets.

9250-1

kriswiner commented 5 years ago

Really, no sketches at all?

https://github.com/kriswiner/MPU9250

On Fri, Nov 9, 2018 at 10:02 AM gojunnyo notifications@github.com wrote:

Thank you for your fast reply sir!

I forgot to mention, in I2C scanner sketch, the sensor is acknowledged. What will happen if the SDA and SCL are reversed? (Enlighten me with your knowledge) Just as I checked, my connections are correct.

I just wanted to know the offsets of the gyroscope and accelerometer of my MPU9250. Because I cannot find any sketches that could help me. Unless if I have MPU6050. I cannot find any MPU9250 calibration sketches that would spit up gyroscope and accelerometer offsets.

[image: 9250-1] https://user-images.githubusercontent.com/18816265/48279456-a3883580-e48b-11e8-892c-829bfda03627.jpg

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437443481, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1quUF9mj1oQy9IZ7F6ctfd7I1p7NKks5utcMigaJpZM4YW3-K .

gojunnyo commented 5 years ago

I already saw this https://github.com/kriswiner/MPU9250/blob/master/MPU9250BasicAHRS.ino

but I am not sure about what part of the code spits out the offset of the sensor.

I honestly don't understand the whole process of the sketch, but I do know it outputs actual data of the orientation of the sensor. I am eager to know about where in the code that integrates values or the like (as long as it outputs an offset).

I just wanted to eliminate the runtime calibration everytime I power on the sensor.

kriswiner commented 5 years ago

I am afraid you are going to have to understand the sketches to understand how to manage the offset calibrations...

On Fri, Nov 9, 2018 at 10:30 AM gojunnyo notifications@github.com wrote:

I already saw this https://github.com/kriswiner/MPU9250/blob/master/MPU9250BasicAHRS.ino

but I am not sure about what part of the code spits out the offset of the sensor.

I honestly don't understand the whole process of the sketch, but I do know it outputs actual data of the orientation of the sensor. I am eager to know about where in the code that integrates values or the like (as long as it outputs an offset).

I just wanted to eliminate the runtime calibration everytime I power on the sensor.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437451970, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qgfZTpmoBRBKqxM3FMlxjsd68clGks5utcnLgaJpZM4YW3-K .

gojunnyo commented 5 years ago

where do I start?

kriswiner commented 5 years ago

Line one.

Maybe use this https://github.com/kriswiner/ESP32/blob/master/MPU9250_MS5637/MPU9250_MS5637_AHRS.ino as a template for your own sketch.

On Fri, Nov 9, 2018 at 10:37 AM gojunnyo notifications@github.com wrote:

where do I start?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437454098, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qoAzwTIOr5TxGXdZ8cR3WTxo1SXgks5utctwgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Thank you sir!

I will post my adaptation to the code, (maybe tomorrow, I'm from the Philippines by the way and I am really sleepy, I am just so hopeless that I got myself involved in this forum). Hopefully, I can get things right! Thank you for your guidance.

gojunnyo commented 5 years ago

Hi! it's me again.

I found a sketch which would output the offsets of the accelerometer and gyroscope of my sensor. Will this suffice?

/*
offset cancellation example
This example automatically finds offset values for each axis of the
accelerometer and gyroscope.

NOTE: Please don't move the sensor while offset is adjusted.

NOTE2: All offset settings are stored in volatile memory so they
have to be set every time the module is reset.
*/

#include <MPU9255.h>//include MPU9255 library

MPU9255 mpu;

void print_data()//read and print raw data from the sensors
{
  for(int i=0;i<=10;i++)
  {
    mpu.read_acc();
    mpu.read_gyro();

    Serial.print("AX: ");
    Serial.print(mpu.ax);
    Serial.print(" AY: ");
    Serial.print(mpu.ay);
    Serial.print(" AZ: ");
    Serial.print(mpu.az);
    Serial.print("    GX: ");
    Serial.print(mpu.gx);
    Serial.print(" GY: ");
    Serial.print(mpu.gy);
    Serial.print(" GZ: ");
    Serial.println(mpu.gz);
    delay(500);
  }
}

void adjust_offset()
{
  //set bandwidths to 5Hz to reduce the noise
  mpu.set_acc_bandwidth(acc_5Hz);
  mpu.set_gyro_bandwidth(gyro_5Hz);

  int16_t gX_offset = 0;//gyroscope X axis offset
  int16_t gY_offset = 0;//gyroscope Y axis offset
  int16_t gZ_offset = 0;//gyroscope Z axis offset

  int16_t aX_offset = 0;//accelerometer X axis offset
  int16_t aY_offset = 0;//accelerometer Y axis offset
  int16_t aZ_offset = 0;//accelerometer Z axis offset

  //update flags

  //gyroscope
  bool update_gX = true;
  bool update_gY = true;
  bool update_gZ = true;

  //accelerometer
  bool update_aX = true;
  bool update_aY = true;
  bool update_aZ = true;

  //discard the first reading
  mpu.read_acc();
  mpu.read_gyro();
  delay(10);

  while(1)//offset adjusting loop
  {
    mpu.read_acc();
    mpu.read_gyro();

    //-------- adjust accelerometer X axis offset ----------

    if(mpu.ax>0 && update_aX==true)//if X axis readings are greater than 0
    {
      aX_offset --;//decrement offset
    }

    if(mpu.ax<0 && update_aX==true)
    {
      aX_offset ++;//increment offset
    }

    //-------- adjust accelerometer Y axis offset ----------

    if(mpu.ay>0 && update_aY==true)//if X axis readings are greater than 0
    {
      aY_offset --;//decrement offset
    }

    if(mpu.ay<0 && update_aY==true)
    {
      aY_offset ++;//increment offset
    }

    //-------- adjust accelerometer Z axis offset ----------

    if(mpu.az>0 && update_aZ==true)//if X axis readings are greater than 0
    {
      aZ_offset --;//decrement offset
    }

    if(mpu.az<0 && update_aZ==true)
    {
      aZ_offset ++;//increment offset
    }

        //-------- adjust gyroscope X axis offset ----------

    if(mpu.gx>0 && update_gX==true)//if X axis readings are greater than 0
    {
      gX_offset --;//decrement offset
    }

    if(mpu.gx<0 && update_gX==true)
    {
      gX_offset ++;//increment offset
    }

    //-------- adjust gyroscope Y axis offset ----------

    if(mpu.gy>0 && update_gY==true)//if X axis readings are greater than 0
    {
      gY_offset --;//decrement offset
    }

    if(mpu.gy<0 && update_gY==true)
    {
      gY_offset ++;//increment offset
    }

    //-------- adjust gyroscope Z axis offset ----------

    if(mpu.gz>0 && update_gZ==true)//if X axis readings are greater than 0
    {
      gZ_offset --;//decrement offset
    }

    if(mpu.gz<0 && update_gZ==true)
    {
      gZ_offset ++;//increment offset
    }

    //print data
    Serial.print("AX: ");
    Serial.print(mpu.ax);
    Serial.print(" (Offset: ");
    Serial.print(aX_offset);
    Serial.print(" ) ");
    Serial.print(" AY: ");
    Serial.print(mpu.ay);
    Serial.print(" (Offset: ");
    Serial.print(aY_offset);
    Serial.print(" ) ");
    Serial.print(" AZ: ");
    Serial.print(mpu.az);
    Serial.print(" (Offset: ");
    Serial.print(aZ_offset);
    Serial.print(" ) ");
    Serial.print("    GX: ");
    Serial.print(mpu.gx);
    Serial.print(" (Offset: ");
    Serial.print(gX_offset);
    Serial.print(" ) ");
    Serial.print(" GY: ");
    Serial.print(" (Offset: ");
    Serial.print(gY_offset);
    Serial.print(" ) ");
    Serial.print(mpu.gy);
    Serial.print(" GZ: ");
    Serial.print(mpu.gz);
    Serial.print(" (Offset: ");
    Serial.print(gZ_offset);
    Serial.println(" ) ");

    //set new offset
    if(update_gX==true)
    {
      mpu.set_gyro_offset(X_axis,gX_offset);
    }

    if(update_gY==true)
    {
      mpu.set_gyro_offset(Y_axis,gY_offset);
    }

    if(update_gZ==true)
    {
      mpu.set_gyro_offset(Z_axis,gZ_offset);
    }

    if(update_aX==true)
    {
      mpu.set_acc_offset(X_axis,aX_offset);
    }

    if(update_aY==true)
    {
      mpu.set_acc_offset(Y_axis,aY_offset);
    }

    if(update_aZ==true)
    {
      mpu.set_acc_offset(Z_axis,aZ_offset);
    }

    //------ Check if each axis is adjusted -----
    const short maximum_error = 5;//set maximum deviation to 5 LSB
    if((mpu.ax-maximum_error) <= 0)
    {

    }

    if((abs(mpu.ax)-maximum_error) <= 0)
    {
      update_aX = false;
    }

    if((abs(mpu.ay)-maximum_error) <= 0)
    {
      update_aY = false;
    }

    if((abs(mpu.az)-maximum_error) <= 0)
    {
      update_aZ = false;
    }

    if((abs(mpu.gx)-maximum_error) <= 0)
    {
      update_gX = false;
    }

    if((abs(mpu.gy)-maximum_error) <= 0)
    {
      update_gY = false;
    }

    if((abs(mpu.gz)-maximum_error) <= 0)
    {
      update_gZ = false;
    }

    //------ Adjust procedure end condition ------
    if(update_gX==false && update_gY==false && update_gZ==false && update_aX==false && update_aY==false && update_aZ==false)
    {
      break;
    }

    delay(10);
  }
  //print the output values :
  Serial.println("Offset cancellation complete!");
  Serial.println();
  Serial.print("Accelerometer offset:  X: ");
  Serial.print(aX_offset);
  Serial.print("  Y: ");
  Serial.print(aY_offset);
  Serial.print("  Z: ");
  Serial.println(aZ_offset);
  Serial.print("Gyroscope offset:   X: ");
  Serial.print(gX_offset);
  Serial.print("  Y: ");
  Serial.print(gY_offset);
  Serial.print("  Z: ");
  Serial.println(gZ_offset);
  Serial.println();
}

void setup() {
  Serial.begin(115200);//initialize Serial port

  if(mpu.init())
  {
  Serial.println("initialization failed");
  }
  else
  {
  Serial.println("initialization succesful!");
  }

  //print some control readings
  print_data();

  //adjust offset
  Serial.println("Adjusting offset...");
  adjust_offset();
  delay(10000);

}

void loop() {
  print_data();//print data forever
}

I am planning to modify this to get the average of 1000 samples because it is quick. It took less than 20 seconds to spit out offsets. Do you think it is worth the effort? or should I stick with the original code? Your help will be much appreciated!

kriswiner commented 5 years ago

Don't know. Why not use the gyro and accel calibration in the sketches in the repository I linked to?

On Fri, Nov 9, 2018 at 6:24 PM gojunnyo notifications@github.com wrote:

Hi! it's me again.

I found a sketch which would output the offsets of the accelerometer and gyroscope of my sensor. Will this suffice?

/* offset cancellation example This example automatically finds offset values for each axis of the accelerometer and gyroscope.

NOTE: Please don't move the sensor while offset is adjusted.

NOTE2: All offset settings are stored in volatile memory so they have to be set every time the module is reset. */

include //include MPU9255 library

MPU9255 mpu;

void print_data()//read and print raw data from the sensors { for(int i=0;i<=10;i++) { mpu.read_acc(); mpu.read_gyro();

Serial.print("AX: ");
Serial.print(mpu.ax);
Serial.print(" AY: ");
Serial.print(mpu.ay);
Serial.print(" AZ: ");
Serial.print(mpu.az);
Serial.print("    GX: ");
Serial.print(mpu.gx);
Serial.print(" GY: ");
Serial.print(mpu.gy);
Serial.print(" GZ: ");
Serial.println(mpu.gz);
delay(500);

} }

void adjust_offset() { //set bandwidths to 5Hz to reduce the noise mpu.set_acc_bandwidth(acc_5Hz); mpu.set_gyro_bandwidth(gyro_5Hz);

int16_t gX_offset = 0;//gyroscope X axis offset int16_t gY_offset = 0;//gyroscope Y axis offset int16_t gZ_offset = 0;//gyroscope Z axis offset

int16_t aX_offset = 0;//accelerometer X axis offset int16_t aY_offset = 0;//accelerometer Y axis offset int16_t aZ_offset = 0;//accelerometer Z axis offset

//update flags

//gyroscope bool update_gX = true; bool update_gY = true; bool update_gZ = true;

//accelerometer bool update_aX = true; bool update_aY = true; bool update_aZ = true;

//discard the first reading mpu.read_acc(); mpu.read_gyro(); delay(10);

while(1)//offset adjusting loop { mpu.read_acc(); mpu.read_gyro();

//-------- adjust accelerometer X axis offset ----------

if(mpu.ax>0 && update_aX==true)//if X axis readings are greater than 0
{
  aX_offset --;//decrement offset
}

if(mpu.ax<0 && update_aX==true)
{
  aX_offset ++;//increment offset
}

//-------- adjust accelerometer Y axis offset ----------

if(mpu.ay>0 && update_aY==true)//if X axis readings are greater than 0
{
  aY_offset --;//decrement offset
}

if(mpu.ay<0 && update_aY==true)
{
  aY_offset ++;//increment offset
}

//-------- adjust accelerometer Z axis offset ----------

if(mpu.az>0 && update_aZ==true)//if X axis readings are greater than 0
{
  aZ_offset --;//decrement offset
}

if(mpu.az<0 && update_aZ==true)
{
  aZ_offset ++;//increment offset
}

    //-------- adjust gyroscope X axis offset ----------

if(mpu.gx>0 && update_gX==true)//if X axis readings are greater than 0
{
  gX_offset --;//decrement offset
}

if(mpu.gx<0 && update_gX==true)
{
  gX_offset ++;//increment offset
}

//-------- adjust gyroscope Y axis offset ----------

if(mpu.gy>0 && update_gY==true)//if X axis readings are greater than 0
{
  gY_offset --;//decrement offset
}

if(mpu.gy<0 && update_gY==true)
{
  gY_offset ++;//increment offset
}

//-------- adjust gyroscope Z axis offset ----------

if(mpu.gz>0 && update_gZ==true)//if X axis readings are greater than 0
{
  gZ_offset --;//decrement offset
}

if(mpu.gz<0 && update_gZ==true)
{
  gZ_offset ++;//increment offset
}

//print data
Serial.print("AX: ");
Serial.print(mpu.ax);
Serial.print(" (Offset: ");
Serial.print(aX_offset);
Serial.print(" ) ");
Serial.print(" AY: ");
Serial.print(mpu.ay);
Serial.print(" (Offset: ");
Serial.print(aY_offset);
Serial.print(" ) ");
Serial.print(" AZ: ");
Serial.print(mpu.az);
Serial.print(" (Offset: ");
Serial.print(aZ_offset);
Serial.print(" ) ");
Serial.print("    GX: ");
Serial.print(mpu.gx);
Serial.print(" (Offset: ");
Serial.print(gX_offset);
Serial.print(" ) ");
Serial.print(" GY: ");
Serial.print(" (Offset: ");
Serial.print(gY_offset);
Serial.print(" ) ");
Serial.print(mpu.gy);
Serial.print(" GZ: ");
Serial.print(mpu.gz);
Serial.print(" (Offset: ");
Serial.print(gZ_offset);
Serial.println(" ) ");

//set new offset
if(update_gX==true)
{
  mpu.set_gyro_offset(X_axis,gX_offset);
}

if(update_gY==true)
{
  mpu.set_gyro_offset(Y_axis,gY_offset);
}

if(update_gZ==true)
{
  mpu.set_gyro_offset(Z_axis,gZ_offset);
}

if(update_aX==true)
{
  mpu.set_acc_offset(X_axis,aX_offset);
}

if(update_aY==true)
{
  mpu.set_acc_offset(Y_axis,aY_offset);
}

if(update_aZ==true)
{
  mpu.set_acc_offset(Z_axis,aZ_offset);
}

//------ Check if each axis is adjusted -----
const short maximum_error = 5;//set maximum deviation to 5 LSB
if((mpu.ax-maximum_error) <= 0)
{

}

if((abs(mpu.ax)-maximum_error) <= 0)
{
  update_aX = false;
}

if((abs(mpu.ay)-maximum_error) <= 0)
{
  update_aY = false;
}

if((abs(mpu.az)-maximum_error) <= 0)
{
  update_aZ = false;
}

if((abs(mpu.gx)-maximum_error) <= 0)
{
  update_gX = false;
}

if((abs(mpu.gy)-maximum_error) <= 0)
{
  update_gY = false;
}

if((abs(mpu.gz)-maximum_error) <= 0)
{
  update_gZ = false;
}

//------ Adjust procedure end condition ------
if(update_gX==false && update_gY==false && update_gZ==false && update_aX==false && update_aY==false && update_aZ==false)
{
  break;
}

delay(10);

} //print the output values : Serial.println("Offset cancellation complete!"); Serial.println(); Serial.print("Accelerometer offset: X: "); Serial.print(aX_offset); Serial.print(" Y: "); Serial.print(aY_offset); Serial.print(" Z: "); Serial.println(aZ_offset); Serial.print("Gyroscope offset: X: "); Serial.print(gX_offset); Serial.print(" Y: "); Serial.print(gY_offset); Serial.print(" Z: "); Serial.println(gZ_offset); Serial.println(); }

void setup() { Serial.begin(115200);//initialize Serial port

if(mpu.init()) { Serial.println("initialization failed"); } else { Serial.println("initialization succesful!"); }

//print some control readings print_data();

//adjust offset Serial.println("Adjusting offset..."); adjust_offset(); delay(10000);

}

void loop() { print_data();//print data forever }

I am planning to modify this to get the average of 1000 samples because it is quick. It took less than 20 seconds to spit out offsets. Do you think it is worth the effort? or should I stick with the original code? Your help will be much appreciated!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437551060, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qmHSVFnlT4MimLXzfZBVp0Sp8CNaks5utjjegaJpZM4YW3-K .

gojunnyo commented 5 years ago

Sorry for my rookie mistakes.

I got your MPU9250_AHRS modified, and I got these data

Is this what I should expect? Is it supposed to oscillate?

capture

kriswiner commented 5 years ago

I don't think so, what are you plotting?

On Fri, Nov 9, 2018 at 9:33 PM gojunnyo notifications@github.com wrote:

Sorry for my rookie mistakes.

I got your MPU9250_AHRS modified, and I got these data

Is this what I should expect? Is it supposed to oscillate?

[image: capture] https://user-images.githubusercontent.com/18816265/48297860-01009e80-e4ed-11e8-8e64-5bfec9fad6e3.PNG

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437560058, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qv5QDA-NSri4sXfNVryAnz642gJsks5utmUzgaJpZM4YW3-K .

gojunnyo commented 5 years ago

I got it working now! What I did is I excluded most Serial.print and modified to print only YPR values.

Any tips to make this fast? I'm planning to implement this to our PID controller (Part of our "crude" VTOL tailsitter flight controller)

Your help again sir is highly appreciated!

If ever you are willing to waste your time with us, to know more and contribute to our research as our advisor, contact me through this email nino.verame@gmail.com

kriswiner commented 5 years ago

For fast fusion use an MCU with a floating point unit like this https://www.tindie.com/products/TleraCorp/dragonfly-stm32l47696-development-board/ one. Or this https://www.tindie.com/products/TleraCorp/ladybug-stm32l432-development-board/ one.

On Sat, Nov 10, 2018 at 6:28 AM gojunnyo notifications@github.com wrote:

I got it working now! What I did is I excluded most Serial.print and modified to print only YPR values.

Any tips to make this fast? I'm planning to implement this to our PID controller (Part of our "crude" VTOL tailsitter flight controller)

Your help again sir is highly appreciated!

If ever you are willing to waste your time with us, contact me through this email nino.verame@gmail.com

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437587650, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1ql0vW1prQdP6LcO8fy7EMkMBoa-Eks5utuJ1gaJpZM4YW3-K .

gojunnyo commented 5 years ago

With these boards sir? Can I connect the same configuration as the pro mini Arduino board? or there are slight changes?

kriswiner commented 5 years ago

What do you mean by same configuration??

On Sat, Nov 10, 2018 at 10:58 PM gojunnyo notifications@github.com wrote:

With these boards sir? Can I connect the same configuration as the pro mini Arduino board? or there are slight changes?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437648243, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qiCWAcTmR0yCpKrr0e3aD9XEZ6k5ks5ut8qbgaJpZM4YW3-K .

gojunnyo commented 5 years ago

How accurate is the selftest function in the sketch? or it depends on the orientation of the sensor during calibration?

gojunnyo commented 5 years ago

More like, if I have connected the sensor to pin 5 (pro mini), the same configuration applies to the boards you have

gojunnyo commented 5 years ago

Sorry by the way if I am so annoying on this thread

gojunnyo commented 5 years ago

Need your help again sir!

I based my code to yours and what this is what happened. Yaw is fairly consistent. Pitch and roll are not. I am confused why this happens.

TRIAL 1: (Set of 5 samples) Each trial is determined after resetting the pro mini.

Yaw, Pitch, Roll: 41.77, 1.34, -1.17 Yaw, Pitch, Roll: 40.89, 1.47, -1.34 Yaw, Pitch, Roll: 40.98, 1.44, -1.30 Yaw, Pitch, Roll: 41.31, 1.47, -1.30 Yaw, Pitch, Roll: 40.19, 1.49, -1.35

TRIAL 2 Yaw, Pitch, Roll: 41.92, 6.78, -1.23 Yaw, Pitch, Roll: 42.35, 6.66, -1.12 Yaw, Pitch, Roll: 41.27, 6.82, -1.24 Yaw, Pitch, Roll: 42.39, 6.73, -1.18 Yaw, Pitch, Roll: 42.07, 6.77, -1.23

TRIAL 3 Yaw, Pitch, Roll: 41.12, 1.36, -6.65 Yaw, Pitch, Roll: 41.76, 1.37, -6.58 Yaw, Pitch, Roll: 41.63, 1.36, -6.59 Yaw, Pitch, Roll: 41.35, 1.42, -6.64 Yaw, Pitch, Roll: 40.72, 1.36, -6.68

kriswiner commented 5 years ago

No idea. Poor calibration, MCU too slow?

On Sun, Nov 11, 2018 at 8:24 AM gojunnyo notifications@github.com wrote:

Need your help again sir!

I based my code to yours and what this is what happened. Yaw is fairly consistent. Pitch and roll are not. I am confused why this happens.

TRIAL 1: (Set of 5 samples) Each trial is determined after resetting the pro mini.

Yaw, Pitch, Roll: 41.77, 1.34, -1.17 Yaw, Pitch, Roll: 40.89, 1.47, -1.34 Yaw, Pitch, Roll: 40.98, 1.44, -1.30 Yaw, Pitch, Roll: 41.31, 1.47, -1.30 Yaw, Pitch, Roll: 40.19, 1.49, -1.35

TRIAL 2 Yaw, Pitch, Roll: 41.92, 6.78, -1.23 Yaw, Pitch, Roll: 42.35, 6.66, -1.12 Yaw, Pitch, Roll: 41.27, 6.82, -1.24 Yaw, Pitch, Roll: 42.39, 6.73, -1.18 Yaw, Pitch, Roll: 42.07, 6.77, -1.23

TRIAL 3 Yaw, Pitch, Roll: 41.12, 1.36, -6.65 Yaw, Pitch, Roll: 41.76, 1.37, -6.58 Yaw, Pitch, Roll: 41.63, 1.36, -6.59 Yaw, Pitch, Roll: 41.35, 1.42, -6.64 Yaw, Pitch, Roll: 40.72, 1.36, -6.68

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437683758, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qqUJvB6opVGUgnFfKkukEke6TGzmks5uuE8mgaJpZM4YW3-K .

gojunnyo commented 5 years ago

And by calibration you specifically mean?

kriswiner commented 5 years ago

offset biases for the accel/gyro/mag at a minimum.

On Sun, Nov 11, 2018 at 12:40 PM gojunnyo notifications@github.com wrote:

And by calibration you specifically mean?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437702833, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1ql5_kwTn8oV6vt_Wlfzroac62ik-ks5uuIs2gaJpZM4YW3-K .

gojunnyo commented 5 years ago

How will I do that?

kriswiner commented 5 years ago

Are you just using the sketches in the repository as a magic black box or are you reading the sketch to see what it does?

Calibration is included in the sketches if you have it properly implemented. Since I don;t know what you are doing (but hope you do!) all I can advise is to look in the sketches to see how calibration is managed.

On Sun, Nov 11, 2018 at 1:02 PM gojunnyo notifications@github.com wrote:

How will I do that?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437704596, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qrWFU8zs284LFSEWXY_BbEag6ENKks5uuJBcgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Good point. Just wanted to clarify things, because I'm still stuck at why I acquired inconsistent pitch and roll. I seriously don't know what caused it. Recently though, putting the sensor at close to perfect horizontal orientation, I managed to acquire close to zero pitch and roll with a deviation of approximately 0.5 on first power up. During my third power up, the values of pitch and roll jumped to +4 even on the same setup.

I might have not known this but I managed to acquire close to zero pitch and roll after first power on! If I reset the pro mini, the values will jump positive or negative 4. If I want to acquire the close to zero values, I just have to drain the power, and turn it back on!

Can you confirm sir? that even though I reset my MCU, the sensor still stores the value within its volatile memory? and that during reset, the sensor is unaffected?

kriswiner commented 5 years ago

If you cut power to the sensor it will power on to its default state.

On Sun, Nov 11, 2018 at 1:32 PM gojunnyo notifications@github.com wrote:

Good point. Just wanted to clarify things, because I'm still stuck at why I acquired inconsistent pitch and roll. I seriously don't know what caused it. Recently though, putting the sensor at close to perfect horizontal orientation, I managed to acquire close to zero pitch and roll with a deviation of approximately 0.5 on first power up. During my third power up, the values of pitch and roll jumped to +4 even on the same setup.

I might have not known this but I managed to acquire close to zero pitch and roll if after first power on! If I reset the pro mini, the values will jump positive or negative 4. If I want to acquire the close to zero values, I just have to drain the power, and turn it back on!

Can you confirm sir? that even though I reset my MCU, the sensor still stores the value within its volatile memory? and that during reset, the sensor is unaffected?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437706891, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qmVRrLwLQMBD0TLM64RAPrag_m-vks5uuJdggaJpZM4YW3-K .

gojunnyo commented 5 years ago

But during MCU reset, the sensor will not go back to its default state?

kriswiner commented 5 years ago

No idea

On Sun, Nov 11, 2018 at 1:35 PM gojunnyo notifications@github.com wrote:

But during MCU reset, the sensor will not go back to its default state?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437707122, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qnGlKd0uyTZyOUekLk32A36EqO9Xks5uuJgUgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Okay sir! Thank you for everything! I'll just stick with the power cut as the reset mechanism of the system, through it the beauty and functionality of the sketch are properly demonstrated. Still wanted to find out what really happened.

Hoping to buy one of your boards but currently, I'm forced to optimize and utilize what the pro mini can do. As much as I really wanted to buy beefer MCU's as you suggested, but due to financial constraints, we cannot. Thus, I will do whatever it takes (even though I become annoying more than ever on this thread).

gojunnyo commented 5 years ago

Will this development board suffice? https://shopee.ph/32-bit-ARM-Cortex-Compatible-with-Arduino-Zero-Form-Mini-i.77433702.1363223813

gojunnyo commented 5 years ago

Lastly, any tips on how to make the sensor know absolute horizon? In which, I do not have to do runtime calibration every time.

I might have missed it from the sketch, but it would be a great help if you'll give me a hint on how to achieve it.

kriswiner commented 5 years ago

That's a Cortex M0. You want a Cortex M4F. Ladybug is a better choice. Is it that much more expensive?

On Sun, Nov 11, 2018 at 3:22 PM gojunnyo notifications@github.com wrote:

Will this development board suffice?

https://shopee.ph/32-bit-ARM-Cortex-Compatible-with-Arduino-Zero-Form-Mini-i.77433702.1363223813

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-437715067, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qgvqlKmoyIUwDSY3ryncIc80EdFHks5uuLEbgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Too bad in our country, yes, it is indeed expensive. The shipping costs around 15 dollars. And I'm having troubles in finding similar products which cost less.

gojunnyo commented 5 years ago

How can I make the calibration permanent?

kriswiner commented 5 years ago

Store it in your sketch.

On Tue, Nov 13, 2018 at 12:19 AM gojunnyo notifications@github.com wrote:

How can I make the calibration permanent?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438176440, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qj9vTPiO9Txh9ccOra-dnUmHoRWEks5uuoCngaJpZM4YW3-K .

gojunnyo commented 5 years ago

Correct me if I comprehended your sketch wrong.

The code below is responsible in storing the offset to the registers. And that data[0] is just another form of gyro_bias[0]

  writeByte(MPU9250_ADDRESS, XG_OFFSET_H, data[0]);
  writeByte(MPU9250_ADDRESS, XG_OFFSET_L, data[1]);
  writeByte(MPU9250_ADDRESS, YG_OFFSET_H, data[2]);
  writeByte(MPU9250_ADDRESS, YG_OFFSET_L, data[3]);
  writeByte(MPU9250_ADDRESS, ZG_OFFSET_H, data[4]);
  writeByte(MPU9250_ADDRESS, ZG_OFFSET_L, data[5]);

So in my sketch instead of

void accelgyrocalMPU9250(float * dest1, float * dest2)
{  
  uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data
  uint16_t ii, packet_count, fifo_count;
  int32_t gyro_bias[3]  = {0, 0, 0}, accel_bias[3] = {0, 0, 0};

 // reset device
  writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device
  delay(100);

I will change the values within int32_t gyro_bias[3] = { into the 3 Values I got from the sketch that output raw gyro_bias without the dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; } the same goes to accelerometer.

kriswiner commented 5 years ago

Yes, then don't run the calibration anymore. But the bias is temperature dependent so can drift.

On Tue, Nov 13, 2018 at 9:36 AM gojunnyo notifications@github.com wrote:

Correct me if I comprehended your sketch wrong.

The code below is responsible in storing the offset to the registers. And that data[0] is just another form of gyro_bias[0]

writeByte(MPU9250_ADDRESS, XG_OFFSET_H, data[0]); writeByte(MPU9250_ADDRESS, XG_OFFSET_L, data[1]); writeByte(MPU9250_ADDRESS, YG_OFFSET_H, data[2]); writeByte(MPU9250_ADDRESS, YG_OFFSET_L, data[3]); writeByte(MPU9250_ADDRESS, ZG_OFFSET_H, data[4]); writeByte(MPU9250_ADDRESS, ZG_OFFSET_L, data[5]);

So in my sketch instead of

void accelgyrocalMPU9250(float dest1, float dest2) { uint8_t data[12]; // data array to hold accelerometer and gyro x, y, z, data uint16_t ii, packet_count, fifo_count; int32_t gyro_bias[3] = {0, 0, 0}, accel_bias[3] = {0, 0, 0};

// reset device writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x80); // Write a one to bit 7 reset bit; toggle reset device delay(100);

I will change the int32_t gyro_bias[3] = {3 Values I got from the sketch that output raw gyro_bias without the dest1[0] = (float) gyro_bias[0]/(float) gyrosensitivity; } the same goes to accelerometer.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438365110, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qnZOXoM5rhT4QveukcWDJsH09--Iks5uuwMlgaJpZM4YW3-K .

gojunnyo commented 5 years ago

the temperature was added to both accel and gyro biases, correct?

so is it good, if my calibration would only be getting temperature data and doing the corrections to the hard-coded biases?

The downside is I get a delay, especially on aircraft recovery, correct?

kriswiner commented 5 years ago

How was the temperature added to the biases?

On Tue, Nov 13, 2018 at 9:59 AM gojunnyo notifications@github.com wrote:

the temperature was added to both accel and gyro biases, correct?

so is it good, if my calibration would only be getting temperature data and doing the corrections to the hard-coded biases?

The downside is I get a delay, especially on aircraft recovery, correct?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438374087, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qs4Ra6bcfG8QRrP8X6NcywEvyO7Yks5uuwiSgaJpZM4YW3-K .

gojunnyo commented 5 years ago

I just broke the momentum hahaha

    accel_bias[0] += (int32_t) accel_temp[0]; 
    accel_bias[1] += (int32_t) accel_temp[1];
    accel_bias[2] += (int32_t) accel_temp[2];
    gyro_bias[0]  += (int32_t) gyro_temp[0];
    gyro_bias[1]  += (int32_t) gyro_temp[1];
    gyro_bias[2]  += (int32_t) gyro_temp[2];

What does this mean exactly?

kriswiner commented 5 years ago

temp means temporary

On Tue, Nov 13, 2018 at 10:24 AM gojunnyo notifications@github.com wrote:

I just broke the momentum hahaha

accel_bias[0] += (int32_t) accel_temp[0];
accel_bias[1] += (int32_t) accel_temp[1];
accel_bias[2] += (int32_t) accel_temp[2];
gyro_bias[0]  += (int32_t) gyro_temp[0];
gyro_bias[1]  += (int32_t) gyro_temp[1];
gyro_bias[2]  += (int32_t) gyro_temp[2];

What does this mean exactly?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438382499, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qhhEIS1WudfEO_IOeYWDJJY_0jO0ks5uuw5TgaJpZM4YW3-K .

gojunnyo commented 5 years ago

facepalm

So is there any part of the sketch that tackles with the temperature compensation? or it was the commented part?

kriswiner commented 5 years ago

No

On Tue, Nov 13, 2018 at 10:41 AM gojunnyo notifications@github.com wrote:

facepalm

So is there any part of the sketch that tackles with the temperature compensation? or it was the commented part?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438388507, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qu8n8iJlVu8jUYux8C-4cr9-yXasks5uuxJ2gaJpZM4YW3-K .

gojunnyo commented 5 years ago

Sorry to bother you again sir,

I encounter this "unusual problem"

ax = 0.00 ay = 0.00 az = 0.00 mg gx = 0.00 gy = 0.00 gz = 0.00 deg/s mx = 0 my = 0 mz = 0 mG q0 = 1.00 qx = 0.00 qy = 0.00 qz = 0.00 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 13.80, 0.00, 0.00 Grav_x, Grav_y, Grav_z: 0.00, 0.00, 1000.00 mg Lin_ax, Lin_ay, Lin_az: 0.00, 0.00, -1000.00 mg sumCount = 0 sum = 0.00 rate = nan Hz

This is the only data that comes up, again and again even if I move the sensor.

kriswiner commented 5 years ago

Are you using the interrupt?

On Wed, Nov 14, 2018 at 10:00 AM gojunnyo notifications@github.com wrote:

Sorry to bother you again sir,

I encounter this "unusual problem"

ax = 0.00 ay = 0.00 az = 0.00 mg gx = 0.00 gy = 0.00 gz = 0.00 deg/s mx = 0 my = 0 mz = 0 mG q0 = 1.00 qx = 0.00 qy = 0.00 qz = 0.00 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 13.80, 0.00, 0.00 Grav_x, Grav_y, Grav_z: 0.00, 0.00, 1000.00 mg Lin_ax, Lin_ay, Lin_az: 0.00, 0.00, -1000.00 mg sumCount = 0 sum = 0.00 rate = nan Hz

This is the only data that comes up, again and again even if I move the sensor.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438758531, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1quPGlbqqJhvg7CC7-_qRcbA6DolCks5uvFpDgaJpZM4YW3-K .

gojunnyo commented 5 years ago

Yes, why?

kriswiner commented 5 years ago

If it is not set up properly, meaning not getting the interrupt, this is the output i would expect.

On Wed, Nov 14, 2018 at 10:27 AM gojunnyo notifications@github.com wrote:

Yes, why?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/323#issuecomment-438767403, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qqQoyy7Sn0U76m6YXYeAs9k23Jveks5uvGCNgaJpZM4YW3-K .

gojunnyo commented 5 years ago

If I disable the interrupt, will it be worse?