Software Magnetometer calibration

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
urkadurka
Posts: 21
Joined: Wed Jan 11, 2012 10:12 pm
Contact:

Software Magnetometer calibration

Post by urkadurka »

Hi, in the multiwii code I found this code about Magnetometer

Code: Select all

if (tCal != 0) {
    if ((t - tCal) < 30000000) { // 30s: you have 30s to turn the multi in all directions
      LEDPIN_TOGGLE;
      for(axis=0;axis<3;axis++) {
        if (magADC[axis] < magZeroTempMin[axis]) magZeroTempMin[axis] = magADC[axis];
        if (magADC[axis] > magZeroTempMax[axis]) magZeroTempMax[axis] = magADC[axis];
      }
    } else {
      tCal = 0;
      for(axis=0;axis<3;axis++)
        global_conf.magZero[axis] = (magZeroTempMin[axis] + magZeroTempMax[axis])/2;
      writeGlobalSet(1);
    }
  } else {
    #if defined(SENSORS_TILT_45DEG_LEFT)
      int16_t temp = ((magADC[PITCH] - magADC[ROLL] )*7)/10;
      magADC[ROLL] = ((magADC[ROLL]  + magADC[PITCH])*7)/10;
      magADC[PITCH] = temp;
    #endif
    #if defined(SENSORS_TILT_45DEG_RIGHT)
      int16_t temp = ((magADC[PITCH] + magADC[ROLL] )*7)/10;
      magADC[ROLL] = ((magADC[ROLL]  - magADC[PITCH])*7)/10;
      magADC[PITCH] = temp;
    #endif
  }
  return 1;
}
#endif

// ************************************************************************************************************
// I2C Compass MAG3110
// ************************************************************************************************************
// I2C adress: 0x0E (7bit)
// ************************************************************************************************************
#if defined(MAG3110)
  #define MAG_ADDRESS 0x0E
  #define MAG_DATA_REGISTER 0x01
  #define MAG_CTRL_REG1 0x10
  #define MAG_CTRL_REG2 0x11
 
  void Mag_init() {
    delay(100);
    i2c_writeReg(MAG_ADDRESS,MAG_CTRL_REG2,0x80);  //Automatic Magnetic Sensor Reset
    delay(100);
    i2c_writeReg(MAG_ADDRESS,MAG_CTRL_REG1,0x11); // DR = 20Hz ; OS ratio = 64 ; mode = Active
    delay(100);
    magInit = 1;
  }
 
  #if !defined(MPU6050_I2C_AUX_MASTER)
    void Device_Mag_getADC() {
      i2c_getSixRawADC(MAG_ADDRESS,MAG_DATA_REGISTER);
      MAG_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,         
                       ((rawADC[2]<<8) | rawADC[3]) ,     
                       ((rawADC[4]<<8) | rawADC[5]) );
    }
  #endif


The calibration method is to found a MIN & MAX value of magntic field on each axis, then an average is done and used.

Experience >>

1. with a simple magnetometer calibration at the airfield in other words : start calibration and a simple quad rotating around each axis, then wait the calibration finishes. Then the magnetometer seems working, with the GUI I see a promptly answer for any change of quad orientation and "N", "S", "W" "E" are right.
BUT :( :( GPS POS HOLD work well only if the quad is faced to NORD, if I face it to "W" the HOLD doesn't work and the quad start a large circle.

2. More sophisticated procedure (as readed somewhere in this forum). Quad faced to nord, pitch 45° looking to the ground (I live in Italy Piemonte), the start the magnetometer calibration and for each axis I turn 360° forth and back. Then I wait for the calibration end.
in this way GPS HOLD work well and I face the quad to every direction.

NOTE : during the tests there isn't wind, and each calibration has done in the middle of the air field and far from any metallic structure, the airfiels is grass.
I'm using AIOP V1.1 ALL IN ONE PRO Flight Controller, the sensor are MPU6050 6 axis gyro/accel with Motion Processing Unit, HMC5883L 3-axis digital magnetometer
MS5611-01BA03 highprecision altimeter.

So there is a techinical explanetion about this behaviour ?
Some body could link this post to other with explanations ?

Many thanks
Urkadurka
http://www.gte-elicotteri.it

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: Software Magnetometer calibration

Post by Alexinparis »

The Earth's magnetic lines of force are not parallel to the surface.
http://en.wikipedia.org/wiki/Magnetic_dip

If you rely only on the 3 earth axis to proceed the calibration, the MIN-MAX for each axis could not be reached.
As you mentioned, a perfect calibration should follow the magnetic axis on your local field.

To avoid confusion, the best way is to try to point in *every* direction of the sphere during the 30s delay.

scrat
Posts: 925
Joined: Mon Oct 15, 2012 9:47 am
Location: Slovenia

Re: Software Magnetometer calibration

Post by scrat »

Alexinparis wrote:The Earth's magnetic lines of force are not parallel to the surface.
http://en.wikipedia.org/wiki/Magnetic_dip

If you rely only on the 3 earth axis to proceed the calibration, the MIN-MAX for each axis could not be reached.
As you mentioned, a perfect calibration should follow the magnetic axis on your local field.

To avoid confusion, the best way is to try to point in *every* direction of the sphere during the 30s delay.


Could you somehow extend this 30s timing for mag calibration to let's say 50s or at least 40s?

Gimbal
Posts: 146
Joined: Tue Jul 24, 2012 7:14 pm
Location: Sweden

Re: Software Magnetometer calibration

Post by Gimbal »

Could you somehow extend this 30s timing for mag calibration to let's say 50s or at least 40s?[/quote]

Code: Select all

if ((t - tCal) < 50000000) { // 50s: you have 50s to turn the multi in all directions


User avatar
shikra
Posts: 783
Joined: Wed Mar 30, 2011 7:58 pm

Re: Software Magnetometer calibration

Post by shikra »

Agree - if look at the usual place for declination, it also highlights the inclination...
http://magnetic-declination.com/

I point min to north down to the ground at the inclination angle.
Rotate around that axis keeping board in line as best as I can.

It is noticeably better. Think it should be highlighted more.

urkadurka wrote:More sophisticated procedure (as readed somewhere in this forum). Quad faced to nord, pitch 45° looking to the ground (I live in Italy Piemonte), the start the magnetometer calibration and for each axis I turn 360° forth and back. Then I wait for the calibration end.
in this way GPS HOLD work well and I face the quad to every direction.

scrat
Posts: 925
Joined: Mon Oct 15, 2012 9:47 am
Location: Slovenia

Re: Software Magnetometer calibration

Post by scrat »

Gimbal wrote:Could you somehow extend this 30s timing for mag calibration to let's say 50s or at least 40s?


Code: Select all

if ((t - tCal) < 50000000) { // 50s: you have 50s to turn the multi in all directions

[/quote]


Great. Thanks for info. Could you be so kind and tell me which file to edit?

urkadurka
Posts: 21
Joined: Wed Jan 11, 2012 10:12 pm
Contact:

Re: Software Magnetometer calibration

Post by urkadurka »

The file to edit is SENSOR.INO, roughly around the 1050th code line you can found where to change the value.

Bye
Urkadurka

User avatar
Quadraf
Posts: 68
Joined: Fri Jan 21, 2011 12:55 am
Location: Deurne Holland
Contact:

Re: Software Magnetometer calibration

Post by Quadraf »

Or you can use MWii-Preflight to do all the settings including mag callibration time up to 99 sec :mrgreen: .
http://code.google.com/p/mwii-preflight/

Hans
Last edited by Quadraf on Thu Jan 17, 2013 1:08 am, edited 1 time in total.

LenzGr
Posts: 166
Joined: Wed Nov 23, 2011 10:50 am
Location: Hamburg, Germany
Contact:

Re: Software Magnetometer calibration

Post by LenzGr »

Quadraf wrote:Or you can use MWii-Preflight to do all the settings including mag callibration time up to 60 sec :mrgreen: .
http://code.google.com/p/mwii-preflight/

Holy cow, that is the most overloaded application window I have seen in a while. No offense :)

tovrin
Posts: 705
Joined: Tue Sep 20, 2011 4:08 pm

Re: Software Magnetometer calibration

Post by tovrin »

wow, yes, overloaded, but fricking awesome! i will remember that fort he next guy who cant figure out how to compile an arduino sketch for MW

urkadurka
Posts: 21
Joined: Wed Jan 11, 2012 10:12 pm
Contact:

Re: Software Magnetometer calibration

Post by urkadurka »

After good mag calibration, done with the quadcopter far from any magnetics, irony structures cellular phones with front oriented to nord and down to grass roughly 60° doing 2 complete turn along each axis, the mag work fine and also GPS Hold and RTH. :idea:

I done another test, erasing first at all the eeprom (via utility available in arduino examples) and if I do the calibration in my home without regarding to the previous tricks and with only 1 turn the mag seems to work but GPS hold and RTH don't work !!

Then the mag calibration is simple but must be done with attention.

If you look at DJI help, I tried the DJI software and hardware with one of friend of mine, the routine on DJI boards is simple on the paper but we need to repeat the calibration job more times before a success :o , so calibration seems simple but is difficult. :shock:

So mag calibration is important and necessary :P

Urkadurka
http://www.gte-elicotteri.com

Post Reply