How to find correct values in complementary filter for MPU-6050?

Hi,

I am using an MPU-6050 on a GY-521 breakout board connected to an Arduino UNO R3 to calculate the tilt angle from the MEMS accelerometer and the corresponding angular velocity from the MEMS gyroscope. Then, these two data are fed into a complementary filter to generate an accurate and reliable tilt angle without drift.

Problem: I cannot understand why the following code works. I don't understand the negative signs in front of atan2 function and in front of the gx variable. The variables ay and az contain the horizontal and vertical components of the gravity vector when the IMU is tilted in the Y-Z plane. The variable gx contains the angular velocity along the X axis.

Attempt:
I tried to visualise it using this figure from the datasheet:
OrientationOfAxes.png
I looked at my MPU-6050 GY-521 hardware and i found some orientation markings on there. The MPU-6050 is being tilted in the Y-Z plane, and at the same time, there is rotational motion along the X-axis. But i can't understand the negative signs.
My guess is that the tilt is being assumed to be forwards in the Y-Z plane, based on the figure, so it is in the opposite direction to the default polarity of rotation on the X-axis, meaning, clockwise. In that case, the negative in front of the gx makes sense. However, the negative in front of atan2 is still a mystery.

Solution: (can't understand why it works)

void accelgyro(int16_t ax, int16_t ay, int16_t az, int16_t gx, int16_t gy, int16_t gz)
{
  accelAngle = -atan2(ay, az) * (180/ PI);
  gyroAngularVelocity = -gx / 131.0;
  complementaryFilter(accelAngle, gyroAngularVelocity);
}

OrientationOfAxes.png

The negative signs are needed to conform to a particular convention defining a positive rotation angle and the exact form of the equations (one of several possible) depends on the order of applying successive rotations.

Theory here and introduction to Euler orientation angles here.

to generate an accurate and reliable tilt angle

Better: "to generate an improved estimate of the tilt angle".

jremington:
The negative signs are needed to conform to a particular convention defining a positive rotation angle and the exact form of the equations (one of several possible) depends on the order of applying successive rotations.

Theory here and introduction to Euler orientation angles here.
Better: "to generate an improved estimate of the tilt angle".

Thanks for the feedback. I'm honestly feeling quite lost in this, so i'll be going through the suggested reading material.

To analyse and (hopefully) understand the equations in the code from my first post, i will try to go through it step by step and in my opinion, maybe a good starting point could be this line from the code:

gyroAngularVelocity = -gx / 131.0;

which seems to indicate that the rotation direction applied here is in the opposite direction to the orientation markings. So, in order to get positive angular velocity readings, a negative sign is needed in front of gx.
Here is an edited version of the figure from my first post to show what i am thinking, with the rotation direction indicated by the red arrow:
actualrotation.png
So, from this assumption (i hope it's correct?) the resulting tilt measured by the accelerometer will therefore also be in the same direction. The resulting tilt is represented as follows (again, i hope i'm correct with this visualisation) with a side view of the MPU-6050 in the YZ-plane:
gravity vector.jpg
From the figure above, g is the gravity vector which is measured as a positive value equal to 1. gy is the gravity component along the Y-axis and gz is the gravity component along the Z-axis. θ is the rotation about the X-axis or the tilt angle and it is measured in radians.

In the code, the atan2 function is used to find θ. From the geometric visualisation, the horizontal component of the gravity vector is given by gy = -gsinθ where the returned value is negative and the vertical component of the gravity vector is given by gz = gcosθ where the returned value is positive. Therefore, tanθ = -gy/gz and θ = atan (-gy/gz).

Considering an arbitrary value of the tilt angle: 5 degrees rotation clockwise. Then, according to convention, since positive angle has counterclockwise rotation about the origin and a negative angle has clockwise rotation, converting to radians, -5° × PI/180 = -0.0872665 rad. Then, gy = -(1)(sin (-0.0872665)) = 0.08715578 and gz = (1)(cos (-0.0872665)) = 0.99619469, then using this online calculator for atan2: https://www.medcalc.org/manual/atan2_function.php the answer is: 0.087266500416 rad which is 5.000 deg. This explains why a negative sign is needed in front of the resulting calculated angle, so as to get positive angle readings.
I think i might have solved it? Since this explains why tilt angle from the accelerometer as well as the angular velocity from the gyroscope both have negative signs to give positive values.

P.S. After doing more research, i found this to be very helpful: Arduino IMU: Pitch & Roll from an Accelerometer – The C Continuum

actualrotation.png

gravity vector.jpg

There is no generally accepted definition of a "positive" rotation.

That is why the diagram in your first post shows arrows wrapped around the axes -- to show the rotation sense that the manufacturer defines as positive.