Go Down

Topic: Sensing tilt using accelerometer alone (Read 2 times) previous topic - next topic

johnwasser

When you tilt the X axis up past 90 degrees, the Z axis goes through zero and then starts going positive.  Since yrot is based on the X and Z measurements, when Z goes from negative to positive you get a different answer, even though Y is staying near zero.

To get pitch and roll, try:

Code: [Select]

pitch=atan2(ax,sqrt((ay*ay)+(az*az)))
roll=atan2(ay,sqrt((ax*ax)+(az*az)))


Note that the result is in Radians so if you want degrees you still have to convert.  You shouldn't get a problem with both arguments to atan2() being zero unless your accelerometer is in free-fall. :)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

positivelydoped23


When you tilt the X axis up past 90 degrees, the Z axis goes through zero and then starts going positive.  Since yrot is based on the X and Z measurements, when Z goes from negative to positive you get a different answer, even though Y is staying near zero.

To get pitch and roll, try:

Code: [Select]

pitch=atan2(ax,sqrt((ay*ay)+(az*az)))
roll=atan2(ay,sqrt((ax*ax)+(az*az)))


Note that the result is in Radians so if you want degrees you still have to convert.  You shouldn't get a problem with both arguments to atan2() being zero unless your accelerometer is in free-fall. :)


Great, thank you.
I think I understand the point, in case of pitch,you need to take the projection of the y and z vectors in the y-z plane so that you get the angle b/w the x vector and this projected vector.
I have not been able to try this, I am out of town for now.
I just would like to ask for 1 last favour, can you please explain how atan2 works. I have the basic understanding, but as you saw that wasn't good enough. I have never found a good answer to this question.
This would be a great help to a lot of people starting out in robotics, I've noticed even the pros do not have a clear understanding of the concept. I wish to share that understanding in my small local robotics group.
Thanks a lot

johnwasser

atan2() just saves you the trouble of dividing the Opposite side by the Adjacent side.  It also preserves quadrant information that would be lost in the division.

Tangent = Opposite over Adjacent
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

positivelydoped23


When you tilt the X axis up past 90 degrees, the Z axis goes through zero and then starts going positive.  Since yrot is based on the X and Z measurements, when Z goes from negative to positive you get a different answer, even though Y is staying near zero.

To get pitch and roll, try:

Code: [Select]

pitch=atan2(ax,sqrt((ay*ay)+(az*az)))
roll=atan2(ay,sqrt((ax*ax)+(az*az)))


Note that the result is in Radians so if you want degrees you still have to convert.  You shouldn't get a problem with both arguments to atan2() being zero unless your accelerometer is in free-fall. :)


Hi john.
Can you please explain the logic of the above code. Lets say for instance roll, here we will get the angle between which quantities?
I tried this, it does not give me right results. Roll goes from 0-45 when it should go upto 90, and then it should continue and go to 180 but it goes back to 0 from 45.

johnwasser

sqrt((x*x)+(y*y)) calculates the length of the hypotenuse of a right triangle with sides X and Y (Pythagorean Theorem)

Unfortunately it gives the absolute magnitude and destroys information about quadrant.  I guess you need the sign information for the various forces to determine pitch and roll correctly.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up