How is the magnetometer calibration done in software?

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
User avatar
McNugget
Posts: 3
Joined: Fri Dec 06, 2013 3:52 pm

How is the magnetometer calibration done in software?

Post by McNugget »

Hi SW-developers,

I'm working my way through the code of MultiWii and MultiWii-Conf at the moment and I'm not able to find out how compass calibration is done within the software.
Is it done on the AVR alone, or is the GUI required to measure and calculate the necessary values from telemetry data? Where is the code that calculates it?
I found the flags to initiate compass calibration but the actual process was nowhere to be found...

I'm using the Crius All in one pro V1 as a basis.

Thanks in advance for pointing me to the right spot!

Best regards,
Timo

User avatar
howardhb
Posts: 189
Joined: Tue Oct 11, 2011 7:10 pm
Location: Port Elizabeth, South Africa

Re: How is the magnetometer calibration done in software?

Post by howardhb »

The GUI is required to initiate the calibration process.
( Can be done with sticks when disarmed - Throttle hi & Yaw Right & Pitch back)
When you click "Calibrate" (or use the sticks) the software requires you to rotate your copter (sensor) through all possible angles so that the sensor can read the maximum / minimum magnetic fields in all planes of rotation.
From this the "span" or "range" is stored and used as a reference to calculate the "heading", given the influences of the specifics of your particular installation and location.
This is why the Mag sensor should be kept away from magnetic influences such as ESC wiring and other metallic / magnetic influences.

Note: A properly calibrated Mag sensor is imperative for good POS Hold and GPS Navigation.

Once calibrated, the "Heading" (in the GUI) should, when the copter is tilted, remain constant.

H.
Edit: Also be sure to enter your specific locations' Magnetic Declination in Config.h #define MAG_DECLINATION xx.xx
Use this online tool: http://magnetic-declination.com
The deviation is given in degrees,minutes (minutes are 0 to 60) but MultiWii requires decimal minutes so to get decimal minutes, divide minutes by 60.
Eg: -27degrees 30minutes = -27.5 degrees
Declination is each locations variation from true magnetic North, dependant on where you are on the earth.
If you are in the Southern Hemisphere, (south of the Equator, declination is NEGATIVE, so put a "-" in front of the declination value in Config.h

User avatar
McNugget
Posts: 3
Joined: Fri Dec 06, 2013 3:52 pm

Re: How is the magnetometer calibration done in software?

Post by McNugget »

Hey Howard,

thanks a lot for describing the process and what happens while calibrating the compass. But unfortunately, that was not the information I was searching for. It was still helpful, though!

I was asking for the .h and .c file and line number where the 30second calibration process actually takes place.
I checked Protocol.cpp, line 559:

Code: Select all

   case MSP_MAG_CALIBRATION:
     if(!f.ARMED) f.CALIBRATE_MAG = 1;
     headSerialReply(0);
     break;


As well as Sensors.cpp, line 1114:

Code: Select all

  if (f.CALIBRATE_MAG) {
    tCal = t;
    for(axis=0;axis<3;axis++) {
      global_conf.magZero[axis] = 0;
      magZeroTempMin[axis] = imu.magADC[axis];
      magZeroTempMax[axis] = imu.magADC[axis];
    }
    f.CALIBRATE_MAG = 0;
  }


But I couldn't find the actual 30 second min/max checking... :(

Any hints?

User avatar
McNugget
Posts: 3
Joined: Fri Dec 06, 2013 3:52 pm

Re: How is the magnetometer calibration done in software?

Post by McNugget »

Sorry... I just found it. Its 10 lines below... Thanks a lot!

Post Reply