Jump to content
I2Cdevlib Forums
luisrodenas

Arduino Sketch to automatically calculate MPU6050 offsets

Recommended Posts

Hello!
First of all thank you for your sketch. Really makes things easier. But i have still some problems with it...

I tried to make it a little bit faster by commenting out some delay() but the sketch didn't like that. it stops at some (maybe random) point.
I also tried to leave the delay() where they were and just commented out all the Serial.print... because i won't need those messages in my project. Your sketch didn't like that either. same behaviour as when i don't use the delay().

and last but not least i tried to get rid of the delays AND the serial output (except for the finish message). guess what....sketch STOPS.

my question is:
why doesn't it work when someone tries to make it faster / get rid of the serial output?

i really changed nothing else. just commented out some delays and some serial output.

 

i would appreciate if someone could explain this behaviour to me

 

greets,

wambo

Share this post


Link to post
Share on other sites

So I am running with the issue from the begining of this thread. I can see just "..." and calibration can't finished. I use Arduino YUN currently and 1.1v of the sketch. I have below outpus of means values..

 

Send any character to start sketch.
 

MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up.
Don't touch it until you see a finish message.
 
MPU6050 connection successful

Reading sensors for first time...

Calculating offsets...
6744    713    17482    54    -693    220
-2092    -1526    15153    54    1354    -802
119    710    17488    -969    -1717    1245
116    714    15154    1078    1354    -1826
118    -1524    17488    -969    -693    2270
121    712    15149    1078    330    -2850
121    714    17487    -969    331    3293
117    718    15151    1078    -694    -2850
121    -1527    17485    -969    330    2269
117    713    15149    1078    330    -1826
119    716    17483    -969    -692    1245
117    -1527    15150    1078    330    -801
118    713    17491    -1993    329    221
120    714    17490    2102    -693    221
120    -1523    15153    -1994    330    -802
120    714    17487    2102    330    1245
-2085    713    15151    -1993    -693    -802
120    -1525    17484    2101    330    221
122    712    15154    -1994    330    221
118    714    17491    2102    -693    -802
118    -1522    15150    -1993    1354    1244
119    714    17484    1078    -1717    -1826
117    714    15157    -969    1355    2269
115    -1523    17489    1078    -693    -1826
116    713    15150    -969    330    1245
115    713    17485    1078    -693    -802
118    -1525    15152    -969    1354    221
117    714    17484    1078    -1717    221
117    718    15150    -969    1353    -802
121    713    17484    1078    -693    1245
115    -1525    15149    -1993    330    -802
118    713    17486    2101    -693    221
117    713    17486    -1993    1354    221
118    -1525    15153    2102    -1717    -802
116    713    17485    -1993    1353    1245
-2089    715    15149    2102    -693    -1826
119    -1524    17485    -1993    330    2269
117    714    15152    2102    330    -1826
113    713    17488    -1994    -693    1245
120    -1524    15152    2101    330    -802
117    712    17484    -3017    330    221
116    714    15147    3126    -694    221
119    -1524    17493    -3017    329    -802
120    715    15147    3125    330    1245
120    717    17488    -3018    -694    -802
117    -1527    15154    3126    330    221
116    713    17485    -3017    330    222
117    714    15152    3126    -693    -802
115    -1524    17489    -3017    330    1245
117    717    17486    2102    330    -1826

 

Any ideas ?

Share this post


Link to post
Share on other sites

I just have tried UNO and YUN again. Calibration not working on both. Gotta do something wrong here.

 

A5 -> SCL

A4 -> SDA

3.3V

GND

D2 -> INT

 

Connects just fine with 0x68 address, I have not changed sketch. using 1.1v.

Any ideas ?

Thanks.

Share this post


Link to post
Share on other sites

Hello, can someone please assist me in implementing ths into a project of mine?  I'm building a lateral G-Force meter for a Supra and I want to either be able to set the zero at the press of a momentary button and have it save permanently until the button is pressed again even if the device is powered off and back on, or have it reset the zero every time the device is powered on. Preferably the momentary button method. Would I have to use EEPROM or the PROGMEM features in the code? 

Share this post


Link to post
Share on other sites

Interesting... tested a GY51 module connected to an Arduino Nano. Works good so far without any editing. Thanks for your effort.

Here's my result :
Send any character to start sketch.

Send any character to start sketch.

Send any character to start sketch.


MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up.
Don't touch it until you see a finish message.

MPU6050 connection successful

Reading sensors for first time...

Calculating offsets...
...
...
...
...
...
...

FINISHED!

Sensor readings with offsets:    0    4    16379    0    -1    1
Your offsets:    -1081    -907    1483    127    30    11

Data is printed as: acelX acelY acelZ giroX giroY giroZ
Check that your sensor readings are close to 0 0 16384 0 0 0
If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)
 

Share this post


Link to post
Share on other sites

I downloaded the calibration sketch today, 5-Mar-2016, v1.1, and used it to calibrate a new MPU6050 breakout board using an Arduino Uno.  The sketch worked perfectly.  Thank you, luisrodenas, for the development and updates.

Share this post


Link to post
Share on other sites

Hello everyone,

 

Just wanted to share some help for those of you who are having problems with the auto-calibration not working, such as in posts #11 and #29 and others.

 

I had the same problem, and it turned out to be caused by an outdated version of the i2cdev library.

 

go here

 

https://github.com/jrowberg/i2cdevlib

 

and download the latest version of the i2cdev library.  Replace your old version of the i2cdev library in your Arduino libraries folder with the new one.

 

Voila!

 

 

Share this post


Link to post
Share on other sites

Hi everyone,

 

Even after updating my I2C library like StrayVoltage suggested in#35, the sketch does not converge for me.

 

I think I've found the underlying problem in my setup: setXGyroOffset (and the Y,Z functions too) cannot handle negative inputs.

In my code, I declare:

 

  accelgyro.setXAccelOffset(-1);
  accelgyro.setYAccelOffset(-1);
  accelgyro.setZAccelOffset(-1);
  accelgyro.setXGyroOffset(-1);
  accelgyro.setYGyroOffset(-1);
  accelgyro.setZGyroOffset(-1);

 

 

But then when I read them out with 

 

 Serial.print(accelgyro.getXAccelOffset()); 
  Serial.print(accelgyro.getYAccelOffset());
  Serial.print(accelgyro.getZAccelOffset());
  Serial.print(accelgyro.getXGyroOffset()); 
  Serial.print(accelgyro.getYGyroOffset()); 
  Serial.print(accelgyro.getZGyroOffset()); 
 
I see
 
-1 ; -1; -1 ; 63 ; 63 ; 63
 
That is, the accelerometer is able to handle negative inputs, but the Gyroscope functions freak out and change -1 to 63. I think this has to do with negative representation (63 = 111111 in binary) but what's odd is that 64 is a 6 bit number, not 8 or 16
 
That's why my sketch doesn't converge. It seems like the sketch is trying to add negative numbers when the readings are positive, so that the sum is 0.
 
But instead of adding a negative number, it is positive; thus the indefinite cycling.
 
I am looking into how to fix this, but my programming background is still crappy:
 
edited to add my system:
 
I'm using a Teensy 3.2 with the SparkFun 9150 breakout board, with both the old and new I2C dev libraries. ( I need to comment out Wire.h in my calibration.ino code because of conflicting libraries)
 
I still see the same problem when I use my Arduino Uno instead of the Teensy

Share this post


Link to post
Share on other sites

I think I got it!

 

Looking at the MPU6050.cpp code in the library, there are 2 Gyrofunctions:

 

getXGyroOffset and getXGyroOffsetUser

 

after re-declaring the offset inputs as int16_t  and using setXGyroOffsetUser made the difference. It converges now!

Share this post


Link to post
Share on other sites

>Put the MPU6050 as horizontal as possible and leave it there, don't touch it.

 

My MPU6050 is oriented vertically (e.g. its z-axis is really my x-axis). Will this calibration program still work?

Share this post


Link to post
Share on other sites

Cerin, i actually didn't go back and look at the code again so take this with a grain of salt, but im pretty sure it will not correctly calculate acceleration (gyro will probably be ok) offsets if the orientation is changed.  The program is expecting to see the force of gravity on one axis, and accounts for this when calculating the offsets.  If you do not edit the code to change what axis is expecting to see gravity, the offsets probably wont converge, and if they do will be very wrong.

 

It might be possible to re-write the program to intelligently detect which axis is detecting gravity (reads ~16000 at level rest) and run a smarter calibration routine.

 

When i needed to reorient my sensors i just fooled the code into switching my axis for me, something like:

 

axisX = -readAxis(y);

 

if you go this route you will need to think carefully about how to switch up your axes to maintain a valid coordinate system.

 

Happy coding!

Share this post


Link to post
Share on other sites

Cerin, i actually didn't go back and look at the code again so take this with a grain of salt, but im pretty sure it will not correctly calculate acceleration (gyro will probably be ok) offsets if the orientation is changed.  The program is expecting to see the force of gravity on one axis, and accounts for this when calculating the offsets.  If you do not edit the code to change what axis is expecting to see gravity, the offsets probably wont converge, and if they do will be very wrong.

 

It might be possible to re-write the program to intelligently detect which axis is detecting gravity (reads ~16000 at level rest) and run a smarter calibration routine.

 

When i needed to reorient my sensors i just fooled the code into switching my axis for me, something like:

 

axisX = -readAxis(y);

 

if you go this route you will need to think carefully about how to switch up your axes to maintain a valid coordinate system.

 

Happy coding!

 

Thanks for the warning. That's what I was afraid of. I decided to simply reorient my sensor to the default. I just calibrated it and it seems to have converged correctly.

Share this post


Link to post
Share on other sites

Hi..thanks for sharing this information. As per my knowledge the one issue that I have is that the I2C address of the MPU6050 object is incorrectly set to 0x69 by default, this is NOT the default address of the IMU. It took me the longest time to figure out why my sensor wasn't connecting

 

pcb assembly

Share this post


Link to post
Share on other sites

Hi..thanks for sharing this information. As per my knowledge the one issue that I have is that the I2C address of the MPU6050 object is incorrectly set to 0x69 by default, this is NOT the default address of the IMU. It took me the longest time to figure out why my sensor wasn't connecting

 

Where does it say 0x69 is the default? All the example code I've found says 0x68 is the default, which works perfectly for me.

Share this post


Link to post
Share on other sites

Hi..thanks for sharing this information. As per my knowledge the one issue that I have is that the I2C address of the MPU6050 object is incorrectly set to 0x69 by default, this is NOT the default address of the IMU. It took me the longest time to figure out why my sensor wasn't connecting

 

The address is not incorrect, the mpu6050 has an address select pin 'AD0' that selects address between 0x68 and 0x69.  Your electrical connections must match your code.  This is important in case you run into another i2c device which uses the same address, or more likely you wish to use 2 mpu6050 modules at once in order to get better data, which may be useful for solving gyro drift problems in applications which require low gyro drift. 

Share this post


Link to post
Share on other sites

Hi! I've been trying to calibrate my accelerometer with luis rodenas code however, the code isn't uploaded on the arduino and the same message comes back again and again: 

"no 'uint8_t MPU6050::dmpInitialize()' member function declared in class 'MPU6050' "

Anybody has an idea why?

Share this post


Link to post
Share on other sites

Hello, I am having some trouble with offsets....

 

I can run the calibration script, have it converge, get some sensible offsets, plug them into the RAW sketch and then get good results (i.e. close to zero on X, Y accels and on all 3 gyros) using getMotion6(). So far so good.

 

But my real application is interrupt driven, and I'm basing it closely on the DMP6 sketch. I plug my offsets into my version of that sketch, which loads the FIFO on interrupt with getFIFOBytes(), and then uses dmpGetAccel() and dmpGetGyro() to read the raw values from the FIFO packet. And when I do that, the results are rubbish, my X accel goes from close to zero to ~200. Same sensor, same Arduino, firmly fixed and unmoved on the same desk. Switch back to the RAW script, all is good again. Any ideas what I might be doing wrong?

 

(What I want to get to eventually is a sketch which runs a self-calibration of the X axis accel offset at startup, and sets the offset accordingly, because in my eventual use case setting up the sensor and clamping it accurately horizontal will be difficult. I only really care about the X-axis accel and gyro.).

 

Thanks for any tips!

 

Andrew

Share this post


Link to post
Share on other sites

Hi luisrodenas I have a question : Do I use the same values for whatever full scale I'm using at the gyro and accel ? What I mean is, if I have MPU9255_GYRO_FULL_SCALE_2000DPS or MPU9255_GYRO_FULL_SCALE_250DPS, the function works without changing anything ? I ask that since values oscilate within different scales at least for the Accelerometer.

Also I have a suggestion : When you do the average, you are adding all the samples and dividing by the number of samples at the end. That may lead to overflows easy if the sample number is too large. What I suggest is why not take a first read, and then, inside the loop, take another read, add it to the first and divide by 2, that result adds to another read and divides by 2 so on and so forth, that way you don't get to higher numbers to operate with.

 

a=readAccelX;

for (int n=0;n<samples;n++)

{

   a=a+readAccelX;

   a >> 1 ;

}

Share this post


Link to post
Share on other sites

I connected mpu-6050 to Arduino pro micro 5v. 

I run the calibration script, and after 15min didn't have a result. So i updated I2Cdev, and added some lines to debug script. I print means for every iteration, and it looks crazzy.

What is wrong?

All script parameters are default. Unfortunatelly before calibration it works quite good... Maby you know default parameters?

Vcc - 4,7

Ad0 - LOW

SDA - 2 

SCL - 3

1.PNG

523a1765757b7f5c6e8b4567.png

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...