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
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]}
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.
I realize the posting is old but in case others are using it: In your last response you are indicating that you still see some deviation especially under larger angles. I experienced the same but it disappeared when I added a calibration routine to capture the accelerometer's reading on a flat surface and made all subsequent readings relative to the flat surface reading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With