## problem about Seven-Parameter Magnetic Calibration Model

cancel
Showing results for
Search instead for
Did you mean:

## problem about Seven-Parameter Magnetic Calibration Model

925 Views Contributor I

Hi all, in application note AN5019, section 5.4(Inverse Soft-iron Matrix), formula(93), what if one of beta is negative? Then perform the sqrt is not allowable.

We make beta_0*beta_1*beta_2 to be positive in section 5.2, but we can not make sure beta_0,beta_1 and beta_2 all be positive, right?

What am I missing?

Thanks

Labels (1)
• ### SensorFusion

Tags (1)
15 Replies
256 Views  NXP Employee

Here's the answer. Equation (50) of AN5019 models not only ellipsoids but also hyperboloids. Because you have so few measurements, and particularly none positioned to defined the ends of the ellipsoid, the least squares fit is actually a hyperboloid and not an ellipse. This is demonstrated by the presence of two negative eigenvalues of A and one positive eigenvalue.

So, yes, in theory the eigensolver can produce mathematically correct but physically nonsensical solutions to the magnetic calibration. We've never seen this in practice since we tend to use a large number of measurements to minimise noise in the calibration fit.

If you want to ensure that you detect and avoid the hyperboloid solution, just check that the eigenvalues of A are all positive which means that the fit is an ellipsoid.

Mark Pedley

256 Views  NXP Employee

Pengcheng

I've re-calculated everything you've done and I agree with your ellispoid matrix A in the 10x10 model.

A= -0.4894    1.1329    0.1683

1.1329   -0.5614    0.0563

0.1683    0.0563   -0.9528

The fundamental problem is that two of the three eigenvalues (below) of this A matrix are negative:

-1.667198538104477

-0.960726510832039

0.624328131475283

Since A=(invW^T).invW the eigenvalues of A are the square of the eigenvalues of invW and must be non-negative. This non-negativity is used in equations (125) and (126) of AN5019 to take the square root of the diagonal eigenvalue matrix.

So your A matrix cannot be decomposed into the product (invW^T).invW with real invW.

My belief is therefore that numerical rounding errors plus the use of too few measurements (particularly since those measurements lie mostly on two circles) has resulted in an ellipsoid matrix which is physically non-realistic.

Mark

256 Views  NXP Employee

I see you provided the datasets earlier. I think I know what the problem is and it's simply the way the data lies mostly on two slices through the ellipsoid. The 7 element calibration fits an ellipsoid aligned along the Cartesian axes and the 10 element calibration adds off axis components that allows a fit to an arbitrary oriented ellipsoid. But with just two slices through the ellipsoid there is not enough information to constrain the ellipsoid's orientation. I saw this previously with a customer who was carefully orienting his magnetometer by rotating about two axes only. When he moved to a random movement that filled in more of the ellipsoid he got a sensible 10 element fit. In the sensor fusion software we try to use about 300 measurements before fitting the 10 parameter just just to avoid numerical errors from the magnetometer measurements. So more measurements will give you a more stable measurement matrix X^T.X and ellipsoid matrix.

256 Views  NXP Employee

Pencheng

Could you please list the components of your7x7 and the 10x10 measurement matrices X^T.X as in equations (89 and (119) of AN5019.

Thank you

Mark Pedley

256 Views Contributor I

7x7:

25186834.89   30578896.35   20653165.03   -1072235.61      -1731.45    -132449.65      50389.71

30578896.35   73103570.61   30020440.07   -1239393.65     996602.64     276761.28      63586.61

20653165.03   30020440.07   61669192.13   -1042239.40     492929.18     387195.87      59867.66

-1072235.61   -1239393.65   -1042239.40      50389.71      -1339.52       7171.94      -2170.62

-1731.45     996602.64     492929.18      -1339.52      63586.61       4960.34       -549.54

-132449.65     276761.28     387195.87       7171.94       4960.34      59867.66       -263.34

50389.71      63586.61      59867.66      -2170.62       -549.54       -263.34        131.00

10x10:

25186834.89   -3431275.97    5527923.69   30578896.35    9078545.19   20653165.03   -1072235.61      -1731.45    -132449.65      50389.71

-3431275.97  122315585.41   18157090.37  -60521850.18  -22042453.43  -27161251.37      -3462.89   -2478787.31    -378846.71      -2679.04

5527923.69   18157090.37   82612660.12  -11021226.72  -54322502.75  -13549282.91    -264899.29    -378846.71   -2084478.80      14343.88

30578896.35  -60521850.18  -11021226.72   73103570.61   21954354.37   30020440.07   -1239393.65     996602.64     276761.28      63586.61

9078545.19  -22042453.43  -54322502.75   21954354.37  120081760.27   20302163.60    -378846.71     553522.56     985858.37       9920.69

20653165.03  -27161251.37  -13549282.91   30020440.07   20302163.60   61669192.13   -1042239.40     492929.18     387195.87      59867.66

-1072235.61      -3462.89    -264899.29   -1239393.65    -378846.71   -1042239.40      50389.71      -1339.52       7171.94      -2170.62

-1731.45   -2478787.31    -378846.71     996602.64     553522.56     492929.18      -1339.52      63586.61       4960.34       -549.54

-132449.65    -378846.71   -2084478.80     276761.28     985858.37     387195.87       7171.94       4960.34      59867.66       -263.34

50389.71      -2679.04      14343.88      63586.61       9920.69      59867.66      -2170.62       -549.54       -263.34        131.00

256 Views Contributor I

Now I do have some datasets that works for seven-parameter model but fail for ten-parameter model. Please see the attachment(each line consists of x,y and z component of one measurement).

I obtained the datasets using the magnetic sensor in my Android smartphone(sensor TYPE_MAGNETIC_FIELD). I rotated the smartphone about its two axis sequentially.

The locus and magnitude is also included as attachment as I could not insert a picture because when I insert a picture the system says "That image type is forbidden".

When I use the seven-parameter model, ellipsoid fit matrix A is:

0.8265         0         0

0    1.1416         0

0         0    1.0598

The diagonal elements are all positive.

However, when I use the ten-parameter model, A is:

-0.4894    1.1329    0.1683

1.1329   -0.5614    0.0563

0.1683    0.0563   -0.9528

And the inverse soft-iron matrix is:

0.4061 + 0.6275i   0.3907 - 0.6384i   0.0573 - 0.0946i

0.3907 - 0.6384i   0.3759 + 0.6700i   0.0551 - 0.0445i

0.0573 - 0.0946i   0.0551 - 0.0445i   0.0081 + 0.9738i

You see, there are complex numbers.

So my question is: is this datasets just too problematic to be use for calibration? I tried other datasets, it seems the calibration algorithm works most of the time.

ps: I didn't use the Freescale sensor fusion library implementations to calculate the result. I just read the AN5019 document and implement the algorithm using MATLAB. I think my code is correct.

256 Views  NXP Employee

Check that the determinant of your ellipsoid matrix A is positive in the 10 parameter case. If it's negative then you need to negate the eigenvector as described in the application note. Your Matlab matrix square root function is returning a complex valued square root matrix which is completely physically meaningless. The issues here are in your Matlab implementation and not in the NXP Sensor Fusion C code.

256 Views Contributor I

Sorry for the late reply. I have checked that det(A)=1. I have also tried the NXP Sensor Fusion C code and got the same result. I think the reason is that the calibration algorithm can not guarantee a ellipsoid fitting. Please refier to this paper: http://dl.acm.org/citation.cfm?id=302950 . Although it is for 2D, it can be generalized to 3D case.

256 Views  NXP Employee

Pengcheng,

I am confident it is not the sensor fusion calibration algorithm.  We do a full least squares fit in 3 dimensions.   The original algorithm was from a published paper in academica, we've scrubbed the algorithm multiple times, and it has been very stable over the last five years.

Here are a couple ideas:

1. My first guess is simply that you don't have enough datapoints for a decent fit.  I plotted your data from the earlier post and it is a very sparse dataset.  You need more points about the ellipsoid.
2. When you say you tried the NXP C code and got the same result, do you mean you applied the NXP code to the same captured dataset?  If so, I'm wondering if there is a HAL (hardware abstraction layer) error in your sensor dirver.  In other words, maybe the data set is inconsistent?   Going back to (1), if we had points over the full ellipsoid, we could be surer of the self-consistency of the data.

Mike

256 Views Contributor I

Yes, I extracted the NXP core code and applied to the same dataset. The magnetometer measurements are obtained from Android OS and I think the data quality is OK. Maybe the amount of datapoints is not enough as needed by the algorithm. However, I can also say the algorithm is not robust with respect to data. In my opinion, a better algorithm may be equation(17) of this paper, i.e. the constraint is β^TCβ=1 instead of β^Tβ=1. This leads to a generalized eigenvalues problem instead of an  eigenvalues problem.

256 Views  NXP Employee

Pengcheng,

Thanks for the pointer to the paper by Liu, et. al.  It looks interesting, and I note that it builds on some of the same papers we used as basis for the current NXP implementation.   I will take the time to study it, but that will have to wait for a week or two, as I'm tied up this week.

Regards,

Mike

256 Views  NXP Employee

The eigensolver returns solution eigenvectors which are normalized but undetermined within a multiplier of +1 or -1. Both a valid eigenvectors but we check that we have the correct one by examining the determinant and negating the vector is needed. This is described in the paragraph after equation (90) in AN5019. Since the values of the gain terms are near 1.0 in value this guarantees a positive square root.

As an additional check, all square roots in the sensor fusion code are written as sqrtf(fabs(x)) to ensure a positive argument. This is mostly defensive against rounding errors near zero resulting in a small positive argument being computed as slightly negative. It also protects are gross axis misalignment where one magnetometer axis is accidentally negated in direction.

256 Views  NXP Employee

The betas should always be positive.  These are terms that distort the ideal magnetic sphere into an ellipsoid.  Negative values simply don't make sense in this context.

256 Views Contributor III

Hello,
I'm trying to implement eCompass. It seems strange a negative Beta. Double check the matrix method.

Can you pass me the AN5019 document link because I can not find it?

I used the AN4246 document for the calculation of the beta and AN4248 for the algorithm.
Did you also considered you?

I downloaded a compass app on my tablet. Before starting the app tells you to move the tablet following a circuit like the infinity symbol with the screen facing up to eight times. This is to initialize the sensors.
We must also do it for the algorithm of freescale? I'll have found it written down somewhere.

How many samples you take to calculate the beta and what you send frequency magnetometer?

I think I have too low ...

256 Views Contributor I

you can download the sensor fusion library. Sensor Fusion|NXP

AN5019 is included there.  