Tuesday, August 27, 2013

Arduino IMU: Pitch & Roll from an ADXL345

Introduction

The ADXL345 accelerometer measures the X,Y,Z components of the device's acceleration but in order to use this information we need to manipulate the data to a more convenient format. First of all, even if it's not mandatory, I prefer to scale the raw data from the sensor in a International System measure (g). There is this simple equation that binds the two kind of measurements:


Moreover, since I want to use the IMU for the construction of a quadcopter, I will need the estimation of pitch and roll. As you can easily imagine, there's no way to determine the yaw just trough the accelerometer's data. Indeed, if you imagine an airplane laying on the flat surface of the airport with the Z-axis perpendicular to its wing-plane, there are no change in the gravity (static acceleration) if you rotate it. But you can calculate its tilt!  That's what we're gonna do.




With a little bit of math you can see how pitch and roll can be estimated with just the three x,y and z "native" accelerometer's outputs. Indeed: 


Where φ is the roll angle and θ is the pitch angle. On Arduino you can use atan2() to semplify your integrity check (denominator must not be zero of course) which eliminate the ambiguity of the angle depending on the quadrant.


Roll & Pitch estimation

In this tutorial I'll use the same configuration as the last post but the ADXL345 is mounted on a GY80 made-in-china IMU I bought to bring on my test without spend a lot. It mounts 4 different sensors, of which I'll write on my next post.




In the snippet above I've inserted just the code relative to the post's arguments, if you want the whole code leave a comment with a valid email. The serial output is shown in the image below.


15 comments:

  1. How do you calculate the yaw?

    ReplyDelete
  2. I'm sorry to answer you with a such delay but I'm currently relocating for a new job so I had to left some stuff home. I'll soon bring all my little lab to Turin (Italy) and continue with my playground!

    Stay tuned.

    ReplyDelete
  3. Hello, could I look at your whole code?!
    j.hankle@yahoo.com

    Thanks,
    Jason

    ReplyDelete
  4. Hello guy, could you send to my email the whole code?
    marceloprintes@hotmail.com

    Thanks

    ReplyDelete
  5. Hi, your arcticle was helpful!
    Can you email me your whole code?

    ponemperaetor@mail.com

    Thank you!

    ReplyDelete
  6. Hello, could you send to my email the whole code?
    good811209@yahoo.com.tw

    ReplyDelete
  7. numbersbabe941@gmail.com
    Hi, im just wondering if it is possible to make the same measurement by using hmc 5883?

    ReplyDelete
  8. I am using HMC5883L for my project. I have interfaced it with the arduino & executed the program given in the website. But the problem is it is showing the same angle (66.68) when I rotate & if I do it in other places I am facingthe same problem. I have also added declination angle of that place. So please help me how to make it work properly

    ReplyDelete
  9. I am using HMC5883L for my project. I have interfaced it with the arduino & executed the program given in the website. But the problem is it is showing the same angle (66.68) when I rotate & if I do it in other places I am facingthe same problem. I have also added declination angle of that place. So please help me how to make it work properly

    ReplyDelete
  10. I am using HMC5883L for my project. I have interfaced it with the arduino & executed the program given in the website. But the problem is it is showing the same angle (66.68) when I rotate & if I do it in other places I am facingthe same problem. I have also added declination angle of that place. So please help me how to make it work properly

    ReplyDelete
  11. I am using HMC5883L for my project. I have interfaced it with the arduino & executed the program given in the website. But the problem is it is showing the same angle (66.68) when I rotate & if I do it in other places I am facingthe same problem. I have also added declination angle of that place. So please help me how to make it work properly

    ReplyDelete
  12. Would you be able to send the full code?

    My email is vpennkast@gmail.com

    Thanks!

    ReplyDelete
  13. Can you please send me the full code to sebastian@franye.at.

    Thank you very much in advance!
    Cheers

    ReplyDelete
  14. Would you please to send the full code?

    My email is htphuong16693@gmail.com

    Thanks!

    ReplyDelete
  15. Hello, can you please send me the full code to schoefch@web.de. Furthermore I have a question to you. It would be very helfully for me if you can answer me. Thank you so much!

    ReplyDelete