MPU6050 data into tilt/inclination angles?

Hi. So I found a website that turns acceleration data into tilt angles: https://www.instructables.com/id/How-to-Measure-Angle-With-MPU-6050GY-521/

I'm wondering if anyone can explain how this is done? Namely, what's going on with the arduino map function and with the geometry/trigonometry? What formula is being used??

I want to use this code for a project except I want to understand what's going on first. I tried posting this in the math stack exchange but couldn't get an answer, so hopefully someone here can help!

Here's the code I use, which is basically the same as on the site except the method for getting the acceleration data:

mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

int xAng = map(ax, minAcel, maxAcel, -90, 90);
int yAng = map(ay, minAcel, maxAcel, -90, 90);
int zAng = map(az, minAcel, maxAcel, -90, 90);

//tilt angles
double x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
double y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);

Best.

That code is simply wrong. Instructables are almost always complete crap, presented by people who have no idea what they are doing.

The theory is explained and the correct code is presented on https://www.dfrobot.com/wiki/index.php/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing.

Well....the code was pretty accurate though. It gives me the expected tilt angles

It gives me the expected tilt angles

Only for simple rotations about either the X or Y axis. As soon as you compound them, it will fail. Keep in mind that the order of rotations matters -- rotating about X then Y does not produce the same result as rotating about Y then X.

The map function calls do nothing useful, so they can be simply taken out.

For the complete theory, see this reference. Good luck with your project!

Ohh ok.

I can test this later, but just to make sure I'm understanding right...because I think I'm a bit confused on calculating the tilt angle about the x axis vs. about the y axis.

Given acceleration of xAc, yAc, and zAc, I can find the tilt angle:

x angle = xAc / sqrt(xAc^2 + yAc^2 + zAc^2) y angle = yAc / sqrt(xAc^2 + yAc^2 + zAc^2)

?

Those two equations give you the [u]cosine[/u] of the angle that the X or Y accelerometer axis makes with the gravity vector (or Down), respectively.

It really would be a good idea to study the material in the two links that I posted, especially the latter. It is complete and correct.