Monday, January 14, 2013

Gyroscope calibration helper, for AVR Atmega, Arduino and other micro

update to version 02

A gyroscope is a device for measuring or maintaining orientation, based on the principles of angular momentum.
Almost every gyroscope chip has to be calibrated, because every device brings a manufacturing error.



In case of gyroscope the correction parameters we need are gain and offset.

Once you obtain those values you can get calibrated data by applying this formula:
x_calibrated = (x_raw-offsetx) / gainx
y_calibrated = (y_raw-offsety) / gainy
z_calibrated = (z_raw-offsetz) / gainz



This Python script implements a simple Gyroscope calibration routine to estimate offset and gain calibration values.

To calibrate offset we just leave gyroscope in a stable position.

To correct gain, this script do not use a calibrated rotation platform. I use an integration method. User must rotate it in 6 differenct way (see sheet provided) for a fixed angle, i use 90 degrees, cause i can do it with a simple calibration platiform (a cube), but greater angles (3600) would bring to better calibration.
The rotation plane of the gyroscope have to be parallel to the requested rotation, so try to be precise placing the sensor over a rotation platform, and rotating it.
During every rotation the script collect raw values. Integrating those values gives use the raw total values for the angle formed. Because we fixed the angle we should estimate the gain factor for the raw values. I sense the rotation stop evaluating raw values, a better way should be adding stop sensors. I suggest to repeat calibration more than one time, cause small rotation error can lead to calibration error.


To obtain values, run this script and follow the instructions, a calibration sheet is provived to help you directing the sensor.

If you run your sensor in a big range of temperature you should also consider a calibration dependent to temperature too.
You can run the script function that compute temperature compensation values.
Cool down you chip, then launch the script, while the sensor come back to ambient temperature, this script collect values, then compute linear regression to find suitable temperature compensation values.
Now, given a tempdelta, that is a temperature delta difference from the sensor start time
tempdelta = actualtemp - starttimetemp
The calibrated axis would became:
x_calibrated = (x_raw-((tempcompx*tempdelta) + offsetx)) / gainx
y_calibrated = (y_raw-((tempcompy*tempdelta) + offsety)) / gainy
z_calibrated = (z_raw-((tempcompz*tempdelta) + offsetz)) / gainz

You may repeat those tests a few times to get better results.

On the microcontroller side you have to setup a function that print out to UART raw values read from your chip.
Given 2 bytes (int16_t) variables for every axis, output the LSB and then MSB byte ((uint8_t)(int16_t>>0) + (uint8_t)(int16_t>>8)), follow by a '\n' char .

Snippets are provided for AVR Atmega and Arduino, but it can be setup for other micro too.


Changelog

  • 02: small fix on the gyro_docalibration.txt readme file for the tempcomp function for x_calibrated, y_calibrated, z_calibrated
  • 01: first version

Code


Notes
  • read risk disclaimer
  • excuse my bad english

31 comments:

  1. very good guide how to use gyro and accelerometer sensors. I add this tutorial on my article where a long list with sensors and tutorials about how to interface and programming accelerometer, gyro and IMU sensors http://www.intorobotics.com/accelerometer-gyroscope-and-imu-sensors-tutorials/

    ReplyDelete
  2. thank you for this calibration method
    can you give me your reference for this method? because I want calibrate mpu6050 for my master's thesis and I need to reference.
    please help me!

    ReplyDelete
    Replies
    1. my email is "alinezhad66@yahoo.com"
      please send me your reference.
      thank you.

      Delete
    2. Could you send me the reference too? I'm going to build a Lego Mindstrom robot using MPU6050.
      Thank you.
      My email is : peppepoma@virgilio.it

      Delete
    3. could you send me too ?
      My email : chongkamat@gmail.com
      Thank you very much
      Regards

      Delete
    4. Please send me also the references for mpu6050 calibration
      to : nedvedmuhammad@gmail.com
      Thank you very much
      Regards

      Delete
    5. for all who needs references:

      you can find it googling for : "gyroscope calibration by integration"

      Delete
  3. hi, Davide, I send you an E-Mail. Have you seen it?
    My E-Mail is: bow5152@gmail.com

    ReplyDelete
  4. Please send me also the references for mpu6050 calibration Sir.
    please send to nedvedmuhammad@gmail.com
    Thank's

    ReplyDelete
    Replies
    1. you can find references googling for : "gyroscope calibration by integration"

      Delete
  5. can you share code read and calibration mpu6050. i can't calibration mpu6050 with avr !
    My E-Mail is: sinhvien901@gmail.com
    thank you!

    ReplyDelete
    Replies
    1. Hello, you find it in the "Code" paragraph above.

      Delete
  6. Thank for suppot. But don't understand , we will user code "avr_lib_mpu6050_03.zip" and use "#define MPU6050_GETATTITUDE 2" for calibration mpu6050 ? And now "#define MPU6050_CALIBRATEDACCGYRO" 1 or 0?
    And question i can't run gyro_docalibration.py, you can share library python(all modun) use on project.
    Thank you very much.

    ReplyDelete
    Replies
    1. To run the python code, you have to install python to your system. This one uses python 2.7.*.
      Firmware side, you have to add the snippet avrgcc provided above in your project main function. You can also use your own mpu6050 library. This works even with mine mpu6050 library, but you have to insert the snippet for sending raw values to the calibration script. Obviouslly, you have to change the raw readin function (read_x_axis_raw_data.. etc..) according to your library.

      Delete
  7. hello Davide !
    i can calib gyro, accel
    read values:
    bias for X 56.65
    bias for Y -29.05
    bias for Z -39.50
    scale factor for X clockwise is 0.0604
    scale factor for X anticlockwise is -0.0663
    scale factor for Y clockwise is 0.0634
    scale factor for Y anticlockwise is -0.0613
    scale factor for Z clockwise is 0.0686
    scale factor for Z anticlockwise is -0.0594
    suggested calibration values
    offset X 56.65
    offset Y -29.05
    offset Z -39.50
    scale fator X 0.0633
    scale fator Y 0.0623
    scale fator Z 0.0640

    And change to code calibration mpu6050
    #define MPU6050_GXOFFSET 56.65
    #define MPU6050_GYOFFSET -29.05
    #define MPU6050_GZOFFSET -39.50

    #define MPU6050_GXGAIN 16.4
    #define MPU6050_GYGAIN 16.4
    #define MPU6050_GZGAIN 16.4

    How to calculator MPU6050_GXGAIN, MPU6050_GYGAIN, MPU6050_GZGAIN
    and MPU6050_AXGAIN, MPU6050_AYGAIN, MPU6050_AZGAIN ?
    My value Roll, Pitch, Yaw always drifts :(
    Thank you suppot !

    ReplyDelete
    Replies
    1. Hello, the scale factor should be your gain. Anyway your values seams a little out of range to me.
      A reasonable gain for a mpu6050 set to MPU6050_GYRO_FS_2000 should be something like 16.4

      Delete
    2. Hi ! i understand GXGAIN,GYGAIN ...sorry i don't read datasheet ^^
      but, you have the problem like me drift yaw? i have fix offset X, Y, Z
      follow you. I think calibration gyro, accel unresolved issues thoroughly. you can just me way fixed?
      thank you !

      Delete
    3. Drift problem is a common problem in gyro. That's the reason why a gyroscope has to be calibrated.
      Accellermeter calibration is not for drift error fixing, but it helps.
      Once you have calibrated, recompile project with calibration enabled, because the method for uncalibrated and calibrated estimation changed. Take a look at your code to understaind where the calibration values comes in play.
      Also check your hardware and gyroscope with other firmware, to be sure that your hardware works in the correct way.

      Delete
    4. Sorry for the delay, "stylusc84" let me think about this calibration method again, and about the value you ("angels demons") obtained.
      Please take a look at the below comment (July 15, 2014 at 1:27 PM to me). Hope this helps, Davide.

      Delete
  8. Hi Davide, A question, in the video, what is it the reference of the Gyro of the calibration? It is the MPU6050?
    Thank You

    ReplyDelete
    Replies
    1. Hello, no is a ITG-3200 mounted on a 10dof imu. But i've calibrated some mpu6050 with this method without problems.

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello. The fact that when you remove the read_byte 0x21 line affect the problem is a symptom that something wrong happens in the comunication. you have to double check you uart connection. Debug the python to micro uart line. Also try to understaind how the communication happens, you will so know why the 0x21 byte send exists,

      Delete
    2. Thank you for your answer, i just figured it out - you were right. The problem was because i didn't turned on the receive complete interrupt bit. Now i have done the gyro calibration but the results are strange - i have the same problem as 'angels demons' who commented above, The scale factor is to small. Here you can see my results: http://imgur.com/FV7kq4G Do you have any ide what can be the cause of that?

      Delete
    3. So sorry.
      I have not noticed that the gain in my mpu6050 library is expressed as LSB/(°/s), because the library conversion use gyroscope gain as divisor value. What you get from this calibration is 1/LSB.
      So to obtain your mpu6050 gain, useable with my library conversion, you have to divide 1 by the scale factor obtained. In other words: you obtain 0.0633, the scale factor for my mpu6050 library will be 1/0.0633 = 15.797, which is pretty reasonable value considere the 14.6 uncalibrated value.
      Let me know it this works.

      Delete