MAV-blog

Stuff related to MAV's (and UAV's) from a hobbyist's point of view. Info

Accelerometer to pitch and roll

This tutorial descibes how an accelerometer can be used to determine an aircraft’s attitude (pitch and roll).

An accelerometer measures, as it’s name hints, acceleration along a predefined axis. As you probably remember from you physics class, the earth’s gravity is also an acceleration (a falling stone keeps going faster and faster). So: with an accelerometer, we can measure the earth’s gravity! This image shows how we do it:

The red arrow represents the earth’s gravity. The blue arrow shows how the accelerometer senses gravity. Note that the axis of this accelerometer is perpendicular to the aircraft (we placed it like that in our aircraft!).
The angle theta between the actual gravity vector and the measured gravity is related to the pitch of the aircraft (pitch = theta + 90°). If we know theta, we know our pitch! Since we know the magnetude of the earth’s gravity, simple calculus gives us our pitch angle:

accelerometer = cos (theta) * gravity

theta = acos (accelerometer / gravity)

And since pitch = theta + 90°

pitch = asin (accelerometer / gravity)

Woooow, we calculated the pitch orientation of our airplane using an accelerometer. Pretty easy, huh?

Calculating the roll angle is pretty much the same. We only need an extra accelerometer with an axis perpendicular to the pitch-accelerometer.

Reality is a bit different from this simplified example. The inverse sinus can’t give you the full 360 degrees ranging pitch angle. A plane heading for the sky and one heading for the ground would both result in a 0 (zero) measurement. We’ll need an extra accelerometer to distinguish these cases. The 2-argument inverse tangens makes sure the resulting angle is in the correct quandrant. Thus:

pitch = atan2(accelerometer / gravity, z / gravity)

(more information on the atan2 function)

Now you know most about using accelerometers to calculate pitch and roll, don’t start building your own autopilot system just yet! There are more forces working on a flying airplane then just good old gravity! Just think about the centripetal force when following a circle path. We’ll need gyroscopes to correct this over short period of time (also usefull to eliminate the effect of vibrations on the accelerometer). Over a longer period of time, we’ll need some more advanced physics to estimate these other forces so we can compensate for them. I’ll write later about the different approaches you can use to do this.
The gyroscopes are covered in the next tutorial: gyroscope to roll, pitch and yaw

10 May 2006, 15:13 | Link |
  1. “The 2-argument inverse tangens does the trick here!”

    I hope that’s coming next! :)



    Luke, 10 May 2006, 16:37 | #



  2. I’m sorry:

    atan2(x,y) = atan(x/y)

    I’ll edit the text before i confuse even more people :)



    Tom, 10 May 2006, 16:57 | #



  3. Woops, its getting a bit late:

    |arctan2(x,y)| = |atan(x/y)| (when interpreting as complex numbers)

    BUT it is in the correct quandrant (see the problem described)

    More info: http://functions.wolfram.com/ElementaryFunctions/ArcTan2/



    Tom, 10 May 2006, 17:14 | #



  4. Can you describe how you get from pitch = asin(a/g) to pitch = atan2(a/g,z/g)? Why divide both arguments by g? Doesn’t atan2(a,b) = atan(b/a) which means the g’s cancel each other out.

    Thanks!



    Brian, 2 June 2006, 00:44 | #



  5. You’re right on the “g”-part, Brain. I just kept the /g there in case the 2 accelerometer’s readings might differ a bit (but in my case they don’t).

    However, you shouldn’t replace atan2(a,b) with atan(b/a): in some cases the sign would be different (atan works over 180 degrees, whereas atan2 works over 360 degrees if i recall correctly).



    Tom, 3 June 2006, 06:40 | #



  6. The formula accelerometer = cos (theta) * gravity is only correct if the plane is moving at constant velocity. If it’s moving with acceleration, there will be an additional inertial term. This is why magnetometers are needed: they determine the plane’s orientation in space, which allows to calculate gravity’s components, subtract them from accelerometer readings and obtain true accelerations ax, ay, and az of the plane.



    Yuri, 1 February 2007, 13:42 | #



  7. Your site is of great help to me! However, I find that when pitch approaches 90 degrees from the horizontal the roll flips 180 degrees, and likewise for when roll approaches 90 degrees. How could this be prevented? Thanks



    Andrew Murray, 3 March 2007, 13:14 | #



  8. Yuri could you develop more precisely what you are saying.?? Is it possible to have a kind of algorithm???



    meno, 26 March 2007, 10:00 | #



  9. Does anybody else can help me to understand the computer algorithm use by IMU with magnetometers?? I need it please.
    Thanks



    meno, 29 March 2007, 07:43 | #



  10. Hi Tom,
    what is z in
    pitch = atan2(accelerometer / gravity, z / gravity)

    thanks?



    pat, 19 September 2007, 10:52 | #



  11. the z is the accelerometer reading along the z-axis (straight up)



    Tom, 22 September 2007, 15:00 | #



  12. hey tom can u explain me why do we use gyros and accelerometers coz only 2 accelerometers are required to measure roll and pitch?
    and for yaw we require one gyro??
    hoping u re ply quick coz i’m going thru the basics of my mav



    viven, 9 October 2007, 02:56 | #



  13. sorry but want to ask u
    how the centripetal forces act on the mav
    and how the accelerometers are affected by it



    viven, 9 October 2007, 03:47 | #



  14. Hello Viven,
    When a car makes a sharp turn, you are pushed to the side of your car. This means a force calles the centripetal force is causing this. The same is valid for an MAV: when it makes a turn the centripetal force will also be measured by the accelerometers. So it no longer has an idea which part of the acceleration is caused by the gravity and which part by the centripetal force. This means we can’t really rely on the accelerometer . So our primary IMU source is the gyroscope.

    Tom



    Tom, 9 October 2007, 05:46 | #



  15. How about using a triple axis magnetometer along with the accelerometers (and gyroscopes?) to augment the system’s accuracy?

    The magnetometers measure the earth’s magnetic field in 3D space, while the accelerometers measure its gravitational acceleration. The two forces are perpendicular and the readings are absolute (not relative like the gyros).

    Kind of like what these guys here did:
    http://www.uavdev.com/



    Ali, 23 January 2008, 02:48 | #



  16. @Ali,
    This would work if you have “slow” or “stable” plane. Over a short period of time, the accelerometer can be noisy due to vibrations (motor vibration, unstable air, ...) or other shaky movements that cause sudden accelerations. You could low-pass filter this accelerometer, but you won’t be able to respond swiftly to changes in it’s attitude. That’s why in most cases, a gyro is added.



    Tom, 23 January 2008, 19:58 | #



  17. But if you have pitch and roll simultaneously, the measure of accelerometers can’t be used to get pitch and roll. Correct?
    For example, if the plane has 90º roll, the first accelerometer measures 0g.



    Bruno, 18 February 2008, 16:17 | #



  18. Hi! tom,
    just thought is there a way to calculate distance from accelerometer? I mean if my plane is moving towards the sky at an angle of 30* (from horizontal) then 3 axis accelerometer can measure the distance travelled?

    -Thanks :)



    Mickey, 13 May 2008, 20:38 | #



  19. Hey Mickey,

    It’s possible in theory, however in practice this is impossible with just an accelerometer :-) You keep summing up the ‘noise’ from your accelerometer, which means your error keeps getting bigger and bigger and bigger



    Tom, 13 May 2008, 21:07 | #



  20. Hi! Tom,
    thanks for the information. But for correct distance measurement how many accelerometer would be required? I want to calculate accurate distance. If my plane is heading in one direction parallel to horizon then can distance be measured accuratly? If yes then how many accelerometer would be required?



    Mickey, 14 May 2008, 07:51 | #



  21. hi,
    Can u tell me how one can calculate roll and pitch of a MAV using the horizon line.



    Ramya, 17 June 2008, 09:52 | #



  22. Please tell me what can be the noise level in an MEMS accelerometer? At what order of frequency ?



    Neha, 28 November 2008, 10:59 | #



  23. Hi!!, right now i’m working on my own imu prototype, but i have some questions about obtaining anlges from the accelerometer, just because in rest “with no movement” i get the angles with the accelerometer, but when i mount this on the airplane de pitch that is the x axis of the accelerometer and is pointing to the nose is changing how could i solve this. i want angles in movement but not accelerations, y have gyros to but when i correct the drift of the gyros with se acc signal the angle takes the value of the acc and is a mess

    thks!!!!



    Sergio, 3 March 2009, 17:11 | #



  24. hi tom,

    How do u differentiate between roll and pitch? the pitch accelerometer could also be used to calculate roll angle right?



    zee, 27 March 2009, 09:41 | #



  25. Hi
    How can to get the real data for input the IMU



    Mess, 26 May 2009, 18:07 | #



  26. Hi, I think this is how to calculate roll and pitch from a 3 axis accelerometer:

    static void
    calcRollAndPitch(
    float *pfRoll,
    float *pfPitch,
    float fAcclX,
    float fAcclY,
    float fAcclZ
    )
    {
    float fSumOfSquares;
    fSumOfSquares = (fAcclX * fAcclX);
    fSumOfSquares += (fAcclY * fAcclY);
    fSumOfSquares += (fAcclZ * fAcclZ); *pfRoll = -asinf(fAcclY / fSumOfSquares); *pfPitch = asinf(fAcclX / fSumOfSquares);
    }



    Richard Sole, 22 June 2009, 11:09 | #



Name
E-Mail
http://
Message
  Textile Help

|