Skip to content

Files

Latest commit

42ffbc3 · Aug 3, 2024

History

History
This branch is 51 commits behind ArduPilot/WebTools:main.

MAGFit

MAGFit

A tool for calibrating compass from a flight log. Measured mag field from the log is compared to the expected field for the given location and attitude. A number of fits are done with varying degrees of freedom to allow the user to select the best.

Calibration

This tool calculates the calibration parameters these are (first compass):

Offsets

COMPASS_OFS_X: o x
COMPASS_OFS_Y: o y
COMPASS_OFS_Z: o z

Scale

COMPASS_SCALE: s

Iron correction

COMPASS_DIA_X: I x x
COMPASS_DIA_Y: I y y
COMPASS_DIA_Z: I z z

COMPASS_ODI_X: I x y
COMPASS_ODI_Y: I x z
COMPASS_ODI_Z: I y z

These form the symmetrical iron correction matrix:

I = [ I x x I x y I x z I x y I y y I y z I x z I y z I z z ]

Motor

COMPASS_MOT_X: m x
COMPASS_MOT_Y: m y
COMPASS_MOT_Z: m z

Application

Raw readings from the compass are given by:

r x , r y , r z

Motor calibration values from either battery current or throttle level are given by:

t

The calibration is then applied:

[ r x + o x r y + o y r z + o z ] s I + [ m x m y m z ] t

Maths

This tool assembles the measured and expected earth field into a matrix in the form A x = B . This allows a fast least squares fit for the calibration parameters x using matrix decomposition.

Is solved resulting in the value of x such that:

m i n A x B 2 2

This can also be written as:

m i n i = 1 n ( A i x B i ) 2

At each logged compass data point the vehicles attitude is interpolated from the selected attitude source. This is then used to calculate the expected earth field in body frame.

x, y and z axis measurements:

r x 1 , r x 2   . . .   r x n

r y 1 , r y 2   . . .   r y n

r z 1 , r z 2   . . .   r z n

x, y and z axis expected:

e x 1 , e x 2   . . .   e x n

e y 1 , e y 2   . . .   e y n

e z 1 , e z 2   . . .   e z n

Motor calibration value, either battery current or throttle is also interpolated:

t 1 , t 2   . . .   t n

The matrices for A and B are given for each point, these are then combined and solved as one.

[ A 1 A 2 . . . A n ] x = [ B 1 B 2 . . . B n ]

Offsets only

If only the offsets are being fitted the formulation is as follows.

Solution array x :

x = [ o x o y o z ]

Matrix A for each point i :

A i = [ 1 0 0 0 1 0 0 0 1 ]

Array B for each point i :

B i = [ e x i r x i e y i r y i e z i r z i ]

Offsets and scale

If offsets and scale are being fitted the formulation is as follows.

Solution array x :

x = [ o x s o y s o z s s ]

The offsets are extracted by removing the scale factor given by the last item in the array.

Matrix A for each point i:

A i = [ 1 0 0 r x i 0 1 0 r y i 0 0 1 r z i ]

Array B for each point i :

B i = [ e x i e y i e z i ]

Offsets and iron

If offsets and iron matrix are being fitted the formulation is as follows.

x = [ x 1 x 2 x 3 I x x I y y I z z I x y I x z I y z ]

The values x 1 , x 2 and x 3 are the offsets with the iron matrix applied. The inverse of the iron matrix is used to recover the offset values.

[ o x o y o z ] = [ x 1 x 2 x 3 ] I 1

The iron matrix is then normalized to give a value for the scale.

s = I x x + I y y + I z z 3

I = I s

Matrix A for each point i :

A i = [ 1 0 0 r x i 0 0 r y i r z i 0 0 1 0 0 r y i 0 r x i 0 r z i 0 0 1 0 0 r z i 0 r x i r y i ]

As before the array B for each point i is:

B i = [ e x i e y i e z i ]

Motor

Motor calibration is added by extending the x and A matrices from the previous methods:

x = [ . . . m x m y m z ]

A i = [ . . . t i 0 0 . . . 0 t i 0 . . . 0 0 t i ]

Weights

Each data point is assigned a weighting based on the the vehicles attitude. Attitudes that occur less frequently get a larger weighting that those which are seen frequently.

w 1 , w 2 , . . . w n

The equation then becomes:

m i n i = 1 n w i ( A i x B i ) 2

To restore the standard A x = B form the weightings are pre-applied to the A and B matrices:

m i n i = 1 n ( w i A i x w i B i ) 2