# Rotating a gyroscope's output values into an earth-relative reference frame

For whatever reason, I seem to have gotten myself twisted into quite a confusion on how to process data from my 3 axis gyro and 3 axis accelerometer to get gyro rotation values related to the earth's reference frame. i.e. Given my accelerometer measures gravity (we assume it's stable) along the sensors (x,y,z) reference frame, how do I adjust the values of my gyro, measured along the sensors same (x,y,z) reference frame such that I am actually measuring the rotating about the Earth's frame x (North), y (West), and z (downwards) direction?

I just can't seem to wrap my head around the use of the rotation matrix. Do I simply employ a 3d rotation matrix, and multiply the vector, $[gyro_x, gyro_y, gyro_z]^T$, by a rotation matrix, R? I then assume the rotation angles, are somehow derived from the filtered accelerometer outputs, but I'm having trouble deciding on what those should be.

I have yet to find a concise reference on how to move the rotation and acceleration values from the sensor reference frame into the earth global reference frame. Perhaps someone can help? Thanks

-
What do you mean by "Earth's x (forward motion), y (port direction), and z (downwards) direction" ? In what way is the earth itself moving forward? Where is earth's portward direction? (Am I safe in assuming you're not modelling earth moving in its orbit?) These sound non-sensical. A system of forward/port/down sounds more like the frame on a moving vehicle. I'd expect an earth frame to have axes like east/north/up, or north/east/down, etc. –  JustJeff Apr 7 '12 at 2:41
Jeff, you're right. It is confusing. I'll clarify. –  gallamine Apr 7 '12 at 15:20
Well, that's an improvement, but now (not trying to be a troll, honestly) I feel compelled to point out that North/West/Down is a left-handed coordinate system. Most of the time the systems you encounter will be right-handed ones. Using the directions you supplied as the positive axes, these would be North/West/Up or West/North/Down, or even Up/North/West. I think systems involving North and East are more common, though. –  JustJeff Apr 7 '12 at 18:27

It's possible that your confusion stems from the fact that there are multiple solutions to the problem. While your accelerometer can tell you which way is up, it cannot distinguish between North and West. I.E. If you rotate the device about the vertical axis, the outputs of the accelerometers won't change.

How can you distinguish between North and West? The best way is probably to add a digital compass to the mix. Alternatively, you may not care to know the difference between real North and real West. You may only want two orthogonal horizontal axes. I'll assume the latter.

Define our frames first. The device's frame is (X, Y, Z). The earth's frame is (V, H1, H2).

Lets assume your accelerometer readings (Ax, Ay, Az) are in the range -1 .. +1, where +1 means straight up. Immediately you know which direction is up: it's simply (Ax, Ay, Az). But how do we obtain a horizontal axis? There's a function called the Cross Product, which takes any two vectors as inputs, and returns another vector at right angles to both. Therefore, we can easily find a vector at right angles to Up. In C:

Vector3D V  = (Ax, Ay, Az);
Vector3D H1 = RANDOM_VECTOR;

Vector3D H2 = CrossProduct(V, H1);
H1 = CrossProduct(V, H2);

Normalise(H1);
Normalise(H2);


So, now we have a vertical vector V, and two horizontal vectors H1 and H2. Now we just need to rotate the gyroscope readings into the same frame.

Let's call the gyroscope readings (Gx, Gy, Gz). We're going to convert them into earth frame rotation coordinates (GV, GH1, GH2). All you have to do is to think about the gyro readings like a single 3D vector. Which way is it pointing in the device's frame. Which way is it pointing in the Earth's frame?

GV  = (Gx*V.x)  + (Gy*V.y)  + (Gz*V.z);
GH1 = (Gx*H1.x) + (Gy*H1.y) + (Gz*H1.z);
GH2 = (Gx*H2.x) + (Gy*H2.y) + (Gz*H2.z);


(I hope that's right)...

-
Thanks Rocketmagnet! That does help. Let me see if I've got this: Effectively you're creating a matrix M, [V.x V.y V.z; H1.x H1.y H1.z; H2.x H2.y H2.z] and multiplying that by the gyro vector, [Gx Gy Gz]^T, which gives you the gyro vector referenced in the NEW coordinate frame, [GV GH1 GH2]? The vertical vector, V, is given because we have an accelerometer that outputs the (negative) gravity vector . The other two vectors are normal to the gravity vector, and hence define the "earth" coordinate frame. –  gallamine Apr 8 '12 at 12:32
@gallamine: Yes, that's right. I hope the code example was clear. If not, let me know and I'll try to clarify it. –  Rocketmagnet Apr 8 '12 at 13:23
I assume that the .x, .y, and .z components are the 1st, 2nd, and 3rd components of the different vectors? Conceptually, this just seems so easy. Why would you ever opt to do a complicated rotation matrix as opposed to just projecting the vectors? –  gallamine Apr 9 '12 at 0:08
@gallamine: well, both of those things are mathematically identical, and neither is more complex. –  Rocketmagnet Apr 9 '12 at 10:14
Good answer. But got some questions. What should be random vector in this case? does it matter? And did this work as expected? –  Kevin Peter Nov 29 '12 at 22:01
show 1 more comment