Perhaps a bug inside the mag 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
User avatar
JayBee
Posts: 12
Joined: Tue Mar 13, 2012 9:33 pm
Location: Bremerhaven (Germany)

Perhaps a bug inside the mag calibration

Post by JayBee »

Hello,

I use the freescale compass ic mag3110 and have problems with the mag calibration.
The reason:
when I move the compass in all directions I get the following values:
2880 < X < 3740
-3780 < Y > -2830
540 < Z < 1510

1LSB = 0.1 uT, so the deltas of all axis are between 86 and 97 uT, I think that's ok but the problem is the offset on all axis.
No axis goes throught zero!

So for the X and Z axis the startvalue of magZeroTempMin never changed and for the Z axis the startvalue of magZeroTempMax never changed.
For a correct calibration I have init the startvalues instead of zero with the actual values.

Code: Select all

...
  magADC[ROLL]  = magADC[ROLL]  * magCal[ROLL];
  magADC[PITCH] = magADC[PITCH] * magCal[PITCH];
  magADC[YAW]   = magADC[YAW]   * magCal[YAW];
  if (calibratingM == 1) {
    tCal = t;
//    for(axis=0;axis<3;axis++) {magZero[axis] = 0;magZeroTempMin[axis] = 0; magZeroTempMax[axis] = 0;}
    for(axis=0;axis<3;axis++) {magZero[axis] = 0;magZeroTempMin[axis] = magADC[axis]; magZeroTempMax[axis] = magADC[axis];}
    calibratingM = 0;
  }
  if (magInit) { // we apply offset only once mag calibration is done
...


With this modification the compass works fine.

Have someone of your made similar experience with it's compass ic ?


JayBee

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

Re: Perhaps a bug inside the mag calibration

Post by Alexinparis »

Hi,

It's strange your range is very far from 0
But yes, it's perfectly right.
thank you for pointing this ;)

User avatar
JayBee
Posts: 12
Joined: Tue Mar 13, 2012 9:33 pm
Location: Bremerhaven (Germany)

Re: Perhaps a bug inside the mag calibration

Post by JayBee »

Hello Alex,

thanks for the fast feedback.

If you are interrested in to implemented the MAG3110, here is the code

Code: Select all

// ************************************************************************************************************
// I2C Compass MAG3110
// ************************************************************************************************************
// I2C adress: 0x1C (8bit)   0x0E (7bit)
// ************************************************************************************************************
#if defined(MAG3110)
  #define MAG_ADDRESS 0x1C
  #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,0x12);  //Trigger immediate with 20 Hz (64 samples) and then return to standby mode
    delay(100);
    magInit = 1;
  }
 
  #if not defined(MPU6050_EN_I2C_BYPASS)
    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]) );
      //Start a new meassurement
      i2c_writeReg(MAG_ADDRESS,MAG_CTRL_REG1,0x12);  //Trigger immediate with 20 Hz (64 samples) and then return to standby mode
    }
  #endif
#endif


JayBee

User avatar
JayBee
Posts: 12
Joined: Tue Mar 13, 2012 9:33 pm
Location: Bremerhaven (Germany)

Re: Perhaps a bug inside the mag calibration

Post by JayBee »

Hello Alex,

thank's for implementation of the MAG3110.

If you finally can change line 605 of def.h to

#if defined(HMC5883) || defined(HMC5843) || defined(AK8975) || defined(MAG3110)

I would be very happy :P


Have a nice weekend,
Joerg

User avatar
ciskje
Posts: 34
Joined: Sat Mar 26, 2011 12:24 am

Re: Perhaps a bug inside the mag calibration

Post by ciskje »

I think that the best will be to introduce a define for MAG_PRESENT non for every hardware supported.

User avatar
JayBee
Posts: 12
Joined: Tue Mar 13, 2012 9:33 pm
Location: Bremerhaven (Germany)

Re: Perhaps a bug inside the mag calibration

Post by JayBee »

Ciskje,
this define still exits, it's the result of #if defined... line, the definition MAG.
I think the idea from Alex was ONLY the owner of the master Source code finally adds new sensors and modify all necessary define lines.
So the 'user' can select his prefered sensor by only uncommand the define of this sensor (that's all).
Otherwise he need to uncommand a second define, the master define (MAG, GYRO BARO etc.).

JayBee

User avatar
ciskje
Posts: 34
Joined: Sat Mar 26, 2011 12:24 am

Re: Perhaps a bug inside the mag calibration

Post by ciskje »

in only one place:

#if defined(HMC5883) || defined(HMC5843) || defined(AK8975) || defined(MAG3110) || defined(LASTUBERALLESMAG) .... x100 .... || defined(LASTKNOWNMAGINTHEWHOLEWORLD)
#define MAG
#endif

all over the program:

#if defined(MAG)
blablabla calibration
#endif

#if defined(MAG)
blablabla common code
#endif

#if defined(MAG)
blablabla uh uh there is one MAG! COOL!
#endif

#if defined(LASTUBERALLESMAG)
...implementation...
#endif

One line to bring them all and in the darkness bind them :)

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: Perhaps a bug inside the mag calibration

Post by timecop »

That's how its already done.

User avatar
ciskje
Posts: 34
Joined: Sat Mar 26, 2011 12:24 am

Re: Perhaps a bug inside the mag calibration

Post by ciskje »

There is also another good method:

#if defined(MAG3110) && !defined(MAG)

...blabla...
#else

User avatar
ciskje
Posts: 34
Joined: Sat Mar 26, 2011 12:24 am

Re: Perhaps a bug inside the mag calibration

Post by ciskje »

MMmmmmm, this is a solution to the problem:



#if defined(MAG3110) && !defined(MAG)
...blabla... init read etc.
#define MAG
#else
#error There are 2 or more MAG defined
#endif

So every programmer doesn't modify a common part (and the maintainer can only cut&copy the part) , and also you can check if there are 2 ore more mag defined.

I will implement this way, thanks Jaybee!

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

Re: Perhaps a bug inside the mag calibration

Post by Alexinparis »

The purpose of the current code is to define the MAG sensor you like, on then work only with the "MAG" definition via generic functions shared among all MAG sensors.
This way, when you add a new mag device, it's very easy to update the code without changing everything.

User avatar
ciskje
Posts: 34
Joined: Sat Mar 26, 2011 12:24 am

Re: Perhaps a bug inside the mag calibration

Post by ciskje »

Alexinparis wrote:The purpose of the current code is to define the MAG sensor you like, on then work only with the "MAG" definition via generic functions shared among all MAG sensors.
This way, when you add a new mag device, it's very easy to update the code without changing everything.


Clearly, but this is not the problem of Jaybee.
If you remove the define MAG in the common part and let the programmers of the device drivers to activate it, you don't need to modify other parts, but only cut&paste the Jaybee part, so you can't forget other line to modify. (and also you can check a multiple activation of devices).

------------------------------------------------------------------------------------
cut&paste and also 1 line to modify in a common part
------------------------------------------------------------------------------------

#if defined(MAG1) || defined(MAG2) ....x100.... defined(MAG102)
#define MAG
#endif

#if define(MAG1)
..specific device driver...
#endif

#if define(MAG)
...common part...
#endif

------------------------------------------------------------------------------------
my solution (only cut&paste):
------------------------------------------------------------------------------------

#if define(MAG1)
..specific device driver...
#define MAG
#endif

#if define(MAG)
...common part...
#endif

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

Re: Perhaps a bug inside the mag calibration

Post by Alexinparis »

Ok, I understand now.
it's effectively clearer like this.

Post Reply