Converting the Raw Magnetometer Counts to Calibrated Engineering Units
The ideal case
Three magnetometer sensors place on three orthogonal axis X, Y and Z.
Each magnetometer produces an output of 0.0 when exposed to a zero magnetic field and the same values when each axis is point at the Earth’s magnetic pole.
The real world magnetometer chip
Each axis has to have an offset value applied to the raw counts to obtain a zero.
For a given magnetic field each axis may produce a slightly different value.
The axis may not be perfectly orthogonal.
Some math:
Let X, Y and Z be the raw readings obtained from the magnetometer.
Let Xoffset, Yoffset and Zoffset be the zero offset that is applied to each axis X, Y and Z to obtain
X1 = X – Xoffset, Y1= Y - Yoffset, and Z1 = Z – Zoffset.
Let X2 = XA*X1 + XB*Y1 + XC*Z1,
Let Y2 = YA*X1 + YB*Y1 + YC*Z1,
Let Z2 = ZA*X1 + ZB*Y1 + ZC*Z1, where
XA, XB, XC,
YA, YB, YC, and
ZA, ZB, ZC are calibration values
Let R = sqrt ( X2 ^2 + Y2^2 + Z2^2), and
For a large number of samples let avgR = sumof( Rn)/N, the average radius.
Let Error(n) = R(n)- avgR, and Error^2(n)= Error(n) * Error(n).
Finally SumError^2 = sumof(Error^2(n))
All this is done in the attached spreadsheet.
Collect some calibration data using the Magtest.js code.
Start the program and point each axis of the magnetometer toward the North magnetic pole.
At my location the pole is 60 degrees below the horizon. Each axis has to be point at and away from the pole to get a good calibration sample. When calibrated the magnetic radius R should be a constant ( at least statistically.
Finding the zero offsets and calibration values
The raw data are then pasted into the spreadsheet X, Y , and Z columns.
raw
raw
raw
N
X
Y
Z
0
-735
478
118
1
-700
555
38
2
-660
645
-26
...
More data
The X1 etc. columns have to be copied and pasted such that each XYZ sample is calculated.
raw-offset
X1
Y1
Z1
X2
Y2
Z2
R
Error^2
-1795.01
461.26
-438.90
-1756.92
235.48
-478.70
1836.13
35.58
-1760.01
538.26
-518.90
-1721.45
315.47
-557.92
1836.90
45.29
The formula view:
raw-offset
X1
Y1
Z1
=+B11-$B$3
=+C11-$B$4
=+D11-$B$5
=+B12-$B$3
=+C12-$B$4
=+D12-$B$5
X2
Y2
Z2
=+$E11$C$3+$F11$D$3+$G11*$E$3
=+$E11$C$4+$F11$D$4+$G11*$E$4
=+$E11$C$5+$F11$D$5+$G11*$E$5
=+$E12$C$3+$F12$D$3+$G12*$E$3
=+$E12$C$4+$F12$D$4+$G12*$E$4
=+$E12$C$5+$F12$D$5+$G12*$E$5
R
Error^2
=+SQRT(H11*H11+I11*I11+J11*J11)
=+(K11-$K$8)^2
=+SQRT(H12*H12+I12*I12+J12*J12)
=+(K12-$K$8)^2
The calculation range in Cells K8 and L7 have to be adjusted to fit the data range.
J
K
L
Sumerr^2
243533.8512
Avg R=
1830.168415
J
K
L
Sumerr^2
=+SUM(L11:L163)
Avg R=
=+AVERAGE(K11:K163)
We start with the offsets in B3 ,B4, and B5 set to zero and the calibration values
Cells C3,D4, and E5 equal to 1
Cells C4, C5, D3, D5, E3, and E4 equal to 0.0.
Offset
A
B
C
X
0
1
0
0
Y
0
0
1
0
Z
0
0
0
1
The Solver function of Excel (part of the analysis tool pack add in) is used to find the offsets and calibration coefficients.
Track your progress by copying the value in cell L8 into a progress column.
First use the solver to minimize cell L8 by changing cells B3, B4, and B5.
The value in cell L8 should be smaller and cells B3, B4, and B5 should have changed.
Offset
A
B
C
X
956.5255249
1
0
0
Y
57.48926514
0
1
0
Z
564.9867808
0
0
1
Apply solver once again this time minimizing cell L8 by changing cells
C3, C4, C5, D3, D4, D5, E3, and E4. Note that cell E5 is left to have a value of 1.0.
At this point the error squared should been reduced by a lot.
Offset
A
B
C
X
1060.011759
0.982416927
0.014146407
0
Y
16.73968712
0.121577401
0.983634984
0
Z
556.9017955
0.02217041
0
1
I use the Descriptive Statistics function in the analysis tool pack to analyze the X2, Y2, Z2, and R values.
R
X2
Y2
Z2
Mean
1829.74
-364.59
391.43
632.47
Standard Error
3.23
67.06
76.42
84.77
Median
1829.47
-437.90
486.95
869.72
Standard Deviation
39.96
829.43
945.26
1048.54
Sample Variance
1597.08
687950.89
893521.30
1099436.03
Kurtosis
13.36
-0.80
0.03
-0.81
Skewness
-1.24
0.03
-0.81
-0.70
Range
400.83
3116.84
3511.30
3364.00
Minimum
1589.21
-1839.88
-1767.80
-1533.31
Maximum
1990.03
1276.96
1743.49
1830.68
Sum
279950.92
-55782.91
59888.09
96768.49
Count
153.00
153.00
153.00
153.00
Additional measures that may be applied is to remove data that produces an R value more than two standard deviations from the average R, and then recalculate the calibration values.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Converting the Raw Magnetometer Counts to Calibrated Engineering Units
The ideal case
Three magnetometer sensors place on three orthogonal axis X, Y and Z.
Each magnetometer produces an output of 0.0 when exposed to a zero magnetic field and the same values when each axis is point at the Earth’s magnetic pole.
The real world magnetometer chip
Each axis has to have an offset value applied to the raw counts to obtain a zero.
For a given magnetic field each axis may produce a slightly different value.
The axis may not be perfectly orthogonal.
Some math:
Let X, Y and Z be the raw readings obtained from the magnetometer.
Let Xoffset, Yoffset and Zoffset be the zero offset that is applied to each axis X, Y and Z to obtain
X1 = X – Xoffset, Y1= Y - Yoffset, and Z1 = Z – Zoffset.
Let X2 = XA*X1 + XB*Y1 + XC*Z1,
Let Y2 = YA*X1 + YB*Y1 + YC*Z1,
Let Z2 = ZA*X1 + ZB*Y1 + ZC*Z1, where
XA, XB, XC,
YA, YB, YC, and
ZA, ZB, ZC are calibration values
Let R = sqrt ( X2 ^2 + Y2^2 + Z2^2), and
For a large number of samples let avgR = sumof( Rn)/N, the average radius.
Let Error(n) = R(n)- avgR, and Error^2(n)= Error(n) * Error(n).
Finally SumError^2 = sumof(Error^2(n))
All this is done in the attached spreadsheet.
Collect some calibration data using the Magtest.js code.
Start the program and point each axis of the magnetometer toward the North magnetic pole.
At my location the pole is 60 degrees below the horizon. Each axis has to be point at and away from the pole to get a good calibration sample. When calibrated the magnetic radius R should be a constant ( at least statistically.
Finding the zero offsets and calibration values
The raw data are then pasted into the spreadsheet X, Y , and Z columns.
The X1 etc. columns have to be copied and pasted such that each XYZ sample is calculated.
raw-offset
The formula view:
raw-offset
The calculation range in Cells K8 and L7 have to be adjusted to fit the data range.
We start with the offsets in B3 ,B4, and B5 set to zero and the calibration values
Cells C3,D4, and E5 equal to 1
Cells C4, C5, D3, D5, E3, and E4 equal to 0.0.
The Solver function of Excel (part of the analysis tool pack add in) is used to find the offsets and calibration coefficients.
Track your progress by copying the value in cell L8 into a progress column.
First use the solver to minimize cell L8 by changing cells B3, B4, and B5.
The value in cell L8 should be smaller and cells B3, B4, and B5 should have changed.
Apply solver once again this time minimizing cell L8 by changing cells
C3, C4, C5, D3, D4, D5, E3, and E4. Note that cell E5 is left to have a value of 1.0.
At this point the error squared should been reduced by a lot.
I use the Descriptive Statistics function in the analysis tool pack to analyze the X2, Y2, Z2, and R values.
Additional measures that may be applied is to remove data that produces an R value more than two standard deviations from the average R, and then recalculate the calibration values.
2 Attachments