0

I've been trying to make an accelerometer calibration tool for my Arduino Nano and MPU6050, but the arduino seems to randomly do incorrect addition. If you look at the output of the console, it works fine for the first 4 iterations, then on the fifth, decides that -31904+-7964 is equal to 25668. It is supposed to be -39868. It does this every time I run it, usually between the 3rd to 6th iteration, but with different numbers.

bool calix = true; //X calibrated or not
      while(calix)
      {
        accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
        Serial.print("PrevAvgX: "); Serial.println(axo/i);
        Serial.print("Axo: "); Serial.println(axo);
        Serial.print("RawX: "); Serial.println(ax);
        axo = (float)ax + (float)axo;
        i += 1;
        Serial.print("Axo2: "); Serial.println(axo);
        Serial.print("RawX: "); Serial.println(ax);
        float cax = axo/i; //Current Average (x)
        Serial.print("AverageX: "); Serial.println(cax);
        delay(100);
        accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
        Serial.print("NewX: "); Serial.println(ax);
        float offsettocurrent = ax-(axo/i);
        Serial.print("AverageX-RawX: "); Serial.println(offsettocurrent);
        if(offsettocurrent>5 or offsettocurrent<-5)
        {
          Serial.println("Offset not accurate enough. Continuing.");
        }
        else
        {
          Serial.println("Offset accurate enough. Finishing calibration");
          break;
        }
        delay(100);
      }

Serial console output:

Initializing I2C devices...
Testing device connections...
Connected to MPU6050
Attempting full calibration
PrevAvgX: -1
Axo: 0
RawX: -8048
Axo2: -8048
RawX: -8048
AverageX: -8048.00
NewX: -8028
AverageX-RawX: 20.00
Offset not accurate enough. Continuing.
PrevAvgX: -8048
Axo: -8048
RawX: -7940
Axo2: -15988
RawX: -7940
AverageX: -7994.00
NewX: -7964
AverageX-RawX: 30.00
Offset not accurate enough. Continuing.
PrevAvgX: -7994
Axo: -15988
RawX: -7972
Axo2: -23960
RawX: -7972
AverageX: -7986.00
NewX: -8016
AverageX-RawX: -30.00
Offset not accurate enough. Continuing.
PrevAvgX: -7986
Axo: -23960
RawX: -7944
Axo2: -31904
RawX: -7944
AverageX: -7976.00
NewX: -8016
AverageX-RawX: -40.00
Offset not accurate enough. Continuing.
PrevAvgX: -7976
Axo: -31904
RawX: -7964
Axo2: 25668
RawX: -7964
AverageX: 5133.00
NewX: -7944
AverageX-RawX: -13077.00
Offset not accurate enough. Continuing.
2
  • What do you want to calibrate ? Do you use the sensor for fast 3D motion with accelerometer/gyro fusion or as a spirit level ? For a spirit level you only need the accelerometer. For 3D motion there is an alternative using the AHRS calculations. This code has also a selftest and calibration: github.com/kriswiner/MPU-6050/blob/master/MPU6050IMU.ino
    – Jot
    Commented Mar 5, 2017 at 14:33
  • I'm going to use the MPU6050 for a quadcopter, so gyro and accel. I'll check out that link, thanks.
    – Matthew
    Commented Mar 5, 2017 at 15:00

1 Answer 1

2

int only goes from -32768 to 32768, and rolls-over, as you've seen. Use a long data type instead and you can go MUCH further from zero. Use double instead of float likewise, when you need more room. Be aware however that larger types use more RAM, and in the case of double, execute a bit slower as well.

3
  • 2
    Double doesn't give more room - it gives more precision. Also, on Arduino, double and float are exactly the same thing.
    – Majenko
    Commented Mar 5, 2017 at 14:18
  • Changing all the variables to doubles and longs fixed it. Thanks!
    – Matthew
    Commented Mar 5, 2017 at 14:58
  • 1
    Although an overflowed int usually rolls overs this way, you cannot even count on that, as in C and C++ it's undefined behavior. Use unsigned integer types if you want predictable roll-over. Commented Mar 5, 2017 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.