views:

776

answers:

3

Hi, I know this question is definitely solved somewhere many times already, please enlighten me if you know of their existence, thanks.

Quick rundown: I want to compute from a 3 axis accelerometer the gravity component on each of these 3 axes. I have used 2 axes free body diagrams to work out the accelerometer's gravity component in the world X-Z, Y-Z and X-Y axes. But the solution seems slightly off, it's acceptable for extreme cases when only 1 accelerometer axis is exposed to gravity, but for a pitch and roll of both 45 degrees, the combined total magnitude is greater than gravity (obtained by Xa^2+Ya^2+Za^2=g^2; Xa, Ya and Za are accelerometer readings in its X, Y and Z axis).

More detail: The device is a Nexus One, and have a magnetic field sensor for azimuth, pitch and roll in addition to the 3-axis accelerometer.

In the world's axis (with Z in the same direction as gravity, and either X or Y points to the north pole, don't think this matters much?), I assumed my device has a pitch (P) on the Y-Z axis, and a roll (R) on the X-Z axis. With that I used simple trig to get: Sin(R)=Ax/Gxz Cos(R)=Az/Gxz Tan(R)=Ax/Az

There is another set for pitch, P.

Now I defined gravity to have 3 components in the world's axis, a Gxz that is measurable only in the X-Z axis, a Gyz for Y-Z, and a Gxy for X-Y axis. Gxz^2+Gyz^2+Gxy^2=2*G^2 the 2G is because gravity is effectively included twice in this definition.

Oh and the X-Y axis produce something more exotic... I'll explain if required later.

From these equations I obtained a formula for Az, and removed the tan operations because I don't know how to handle tan90 calculations (it's infinity?).

So my question is, anyone know whether I did this right/wrong or able to point me to the right direction?

Thanks! Dvd

+1  A: 

I wish I knew because I am also interested in this problem.

A good place to start researching is at http://www.diydrones.com/ . The folks over there have already solved this problem in the context of aircraft autopilots. There is a ton of high quality, open source code linked to from that site, as well as discussions of the math involved.

NXT
+2  A: 

As I understand your question, you know the pitch and yaw of your device (from the magnetometer) and want to use this information to calculate the component of gravity along each of your (device) coordinate axes?

As a physicist I'm brought up with Euler angles instead of pitch-yaw-roll, but looking at http://en.wikipedia.org/wiki/Yaw,_pitch,_and_roll I would calculate this as follows: Assume that your device is initially oriented along the global coordinate frame, so that gravity is gvec:={0,0,-g} (in the local frame). Now we have to calculate the local coordinates of gvec as we go through the yaw-pitch-roll (yaw doesn't do anything as you mention). To me this is easiest with rotation matrices: we have to change the sign of the angles since gvec stays put. I'll do this with Mathematica because that's my hammer and this is a nail

yaw = RotationMatrix[-yawangle,{0,0,1}];
pitch = RotationMatrix[-pitchangle, {0,1,0}];
roll = RotationMatrix[-rollangle,{1,0,0}];
gvec={0,0,-g}
yaw.gvec
pitch.yaw.gvec
roll.pitch.yaw.gvec

The output is the local coordinates for gvec before yaw, and after yaw, pitch, and roll (so last line below should be your answer):

{0,0,-g}
{0,0,-g}
{g Sin[pitchangle],0,-g Cos[pitchangle]}
{g Sin[pitchangle],-g Cos[pitchangle] Sin[rollangle],-g Cos[pitchangle] Cos[rollangle]}
Janus
+1  A: 

Thanks Janus! Your explanation kinda enlightened me regarding the rotation matrix. And the final line did solve my problem!

Now I just need to rework my free body diagrams to find out what I've did wrong... I already found that I should not have had a X-Y component of gravity, since gravity is orthonormal to the X-Y axis...

THanks again!

Edit: follow up on this, the last line: {g Sin[pitchangle],-g Cos[pitchangle] Sin[rollangle],-g Cos[pitchangle] Cos[rollangle]}

I've found instead of -g Cos[pitchangle] Sin[rollangle] Sin[roll] from my free body diagram more closely resembles the actual acceleration.

What I can't understand now is the last component -g Cos[pitchangle] Cos[rollangle] now it is perfect for small pitch and roll angles, and it works fine for either a pitch or roll angle while the other stays at 0, but a deviation becomes significant when both pitch and roll are no longer a small angle (say 40 degrees). Actually I also realised to achieve a 45 roll and 45 pitch on the nexus one, the phone would have a 0 Z axis reading, with X and Y both on 6.8ish acceleration. While the resulting formula from the rotation matrix multiplication at 45 roll and 45 pitch would be 0.5 gravity.

Is there something wrong with the orientation sensor output? or is this how pitch and roll are supposed to work?

Does anyone know how to account for this?

Thanks!

dvd
I am looking for similar problem. So did you got an exact formula to remove gravity factor from all axis, taking account of device orientation values at that instant. I want only pure acceleration on all axis without gravity components.
mob_king