# Magnetometer math

Hello all,
I am trying to use a magnetometer as a reference vector for yaw drift cancellation in the direction cosine matrix.

Ok, so here's the nuts and bolts.  If I measure a three axis magnetic vector in the aircraft frame of reference, the direction cosine matrix allows me to translate that vector back to the earth frame of reference.

Once it is rotated back, what exactly am I looking at?  Do I compare the rotated vector to an Earth fixed magnetic field vector (i.e. from the NOAA tables)?  If so, I have myself convinced that the rotated vector from the aircraft should exactly match the Earth fixed vector assuming a perfect matrix and compass.  Assuming that is the case then any discrepancy between the Earth fixed vector and the rotated vector would be due to yaw drift or compass error.

And

FIGHT!

-Matt

Views: 548

### Replies to This Discussion

Matt,

If you want a solution, then I would expect there to be a robust DCM solution for the use of a magnetometer within a few weeks.

In terms of understanding: Bill Premerlani has documented the mathematics for finding offsets automatically, and created a demonstration program to test the magnetometer algorithms.

We have done an initial integration of the magnetometer code into MatrixPilot trunk, and this was flown at Sparkfun. However, we have discovered some systems issues, and Bill is currently making the code more robust.

Pete
Thanks Pete!

I am writing a DCM code for the propeller chip and love having examples to draw from. I am still conceptually curious as to how using the magnetometer works.

At a high level, if I measure a vector (of whatever) in the aircraft frame of reference, I can use the DCM matrix to rotate that vector to a fixed reference frame, usually the earth in our case. The critical piece I am missing is a mental visualization of what the vector should look like after rotating. AKA, if my DCM matrix and measurements are always 100% accurate, what would the output vector look like. What direction would it be pointing? I am thinking that it should point towards wherever I initialized the DCM matrix. Am I thinking about this correctly?

If that is the case it is a simple matter to perform vector operations to find out how to rotate the matrix to line up the output with the expected Earth vector. (It's been a long day and I am not totally sure I am making sense here...)

Also, I like the clever offset algorithm! I wrote one for the propeller using an iterative least squares regression technique. It works pretty well and can successfully map the magnetometer outputs to a sphere of chosen radius, thus accounting for hard and soft iron biases on the fly. This essentially forces the axes to be orthogonal by forcing the vector magnitude to be a set value. I have the advantage of being able to slave this to a cog so it is continually running and updating in the background though!

Thanks for all of your continued help!

-Matt
Matt,
Matt

You said, "The critical piece I am missing is a mental visualization of what the vector should look like after rotating."

If you have a vector described in the body frame of reference and rotate it so that it is described in the earth frame of reference both will actually look exactly the same when viewed from an independent location in space (.e.g a mountain top on the ground).

The vector is in reality the same, but is described in relation to a different set of Axis depending on whether it is on the ground (described relative to an earth coordinate system, or on the body (described relative to the main axis of the aircraft in our examples.).
Ah... Right... That does make sense. Got it. (Im blaming this on being stuck in a mental loop!)

So back then to one of the original questions. If I have a measured and rotated magnetometer vector, what then am I comparing it to? To find out if my yaw axis is correct I would need another magnetic vector in the Earth frame of reference to compare it to, correct? I think the sticky wicket here for me is that the vector in the Earth frame will never align with an axis of the aircraft due to the vertical component of the magnetic field and the local declination angle.

If, persay, I were to take the dot product of the measured and rotated vector with a local constant magnetic field vector (declination included) then the difference between them would be proportional to the cos of the angle, or in my case, heading correct?

Thanks again Pete for your help with this. Looks like it's back to continuum mechanics for me.

-Matt

Hi Mathias,

I am also using DCM to get correct orientation in a  smartphone application, Can you plz tell me how I can get the orientataion (three angles , yaw pitch roll) directly from DCM matrix, as if I calculate orientation using Euler angles (using atan2 function), I get gimbal lock error, I have gone through DCmdraft2.pdf  but i am not clear how to resolve it.

regards

Navigator

hi, what i understand from DCMdraft2 is that to get orientation vector direct from DCM matrix is to use rmat[6] for pitch(dot product of roll axis(first column) with ground vertical[0 0 1]), rmat[7] for roll(dot product of pitch axis with ground vertical) and rmat[4] for yaw angle (dot product of body Y axis with earth Y axis), is it right? Anybody plz can verify if i am wrong?

regards

Navigator

1

2

3

4

5

6

7

8

9

10

## Contests

Season Two of the Trust Time Trial (T3) Contest has now begun. The fourth round is an accuracy round for multicopters, which requires contestants to fly a cube. The deadline is April 14th.

A list of all T3 contests is here

24 members

1288 members

57 members

680 members

87 members