8字磁力计较准的原理

The 8/S shaped pattern is used to calibrate magnetometers in mobile phones and other devices.

Background

Typical mobile phone era magnetometers measure the magnetic field strength along three orthogonal axes, e.g.:

m=mxı^+myȷ^+mzk^m=mxı^+myȷ^+mzk^

With the magnitude of the field given by,

m=m2x+m2y+m2z‖m‖=mx2+my2+mz2

and the angle of rotation from each axis as

θk=cos1mkm, where k[x,y,z]θk=cos−1⁡mk‖m‖, where k∈[x,y,z]

Calibration

Since the magenetic field of the earth is relatively constant, the magnitude of the as field measured by the magnetometer should also be constant, regardless of the orientation of the sensor. i.e. if one were to rotate the sensor around and plot mxmxmymy, and mzmz in 3D, the paths should plot out the surface of a sphere with constant radius.

Ideally it should look somthing like this:

However due to hard and soft iron effects and other distortions, it ends up looking like a deformed sphere:

This is because the magnitude of the magnetic field as measured by the sensor is changing with orientation. The result being that the direction of the magnetic field when calculated according to the formulas above is different from the true direction.

Calibration must be performed to adjust each of the three axis readings so that the magnitude is constant regardless of orientation - you can think of it as the deformed sphere must be warped into a perfect sphere. The LSM303 application note has lots of detailed instructions on how to perform this.

So what about the figure 8 pattern!?

Performing the figure 8 pattern 'traces out' part of the deformed sphere above. From the coordinates obtained, the deformation of the sphere can be estimated, and the calibration coefficients obtained. A good pattern is one that traces through the greatest range of orientations and therefore estimates the greatest deviation from the true constant magnitude.

To estimate the shape of the deformed sphere, least squares ellipse fitting can be used. The LSM303 application note also has information on this.

A simple method for a basic calibration

According to the app note if you assume no soft-iron distortion, the deformed sphere will not be tilted. Therefore a simple method for a basic calibration may be possible:

  • Find the maximum and minimum value for each axis, and get the 1/2 range and zero point

rk=12(max(mk)min(mk))rk=12(max(mk)−min(mk))

zk=max(mk)rkzk=max(mk)−rk

  • Shift and scale each axis measurement

mk=mkzkrkmk′=mk−zkrk

  • Calculate values as before except using mkmk′

This is based off the code found here.

Solving using least squares

MATLAB code to solve using least squares is shown below. The code assumes a variable mag where the columns are the x y z values.

H = [mag(:,1), mag(:,2), mag(:,3), - mag(:,2).^2, - mag(:,3).^2, ones(size(mag(:,1)))];
w = mag(:,1).^2;
X = (H'*H)\H'*w;
offX = X(1)/2;
offY = X(2)/(2*X(4));
offZ = X(3)/(2*X(5));
temp = X(6) + offX^2 + X(4)*offY^2 + X(5)*offZ^2;
scaleX = sqrt(temp);
scaleY = sqrt(temp / X(4));
scaleZ= sqrt(temp / X(5));

To do a dynamic figure 8 calibration, you could run the least squares routine with every new reading and terminate when the offset and scale factors have stabilised.

你可能感兴趣的:(8字磁力计较准的原理)