Hey guys, I am trying to get the angles from the mpu6050.
Progress: I got the angles from the mpu6050 from 0-360 deg.
To Solve: I want to convert these to -180 to 180 deg.
Problem: The methdod i am using has some problem.
the output has a huge spike when the angle shanges from negative to positive.
This is the code i used:
if (x >= 180) {
x = x - 360;
}
else if(a<180){
x = x;
}
Serail.println(x);
Given 0 <= X < 360
-180 <= ( X - 180 ) < 180
You do know that it's 0 to 359 since 0 and 360 are the exact same direction?
-180 == +180
Yhea i know that.
BTW here is a screenshot of what i got(attached).

This is the code i used:
Nope.
Because that code didn't compile.
What is so hard about cut-and-paste?
PROBOT135:
Yhea i know that.
BTW here is a screenshot of what i got(attached).
So subtract 180 from your X = 0 to 359 and X is within -180 to 179.
To go from -180 to 179 to 0 to 359 just add 180. No need to test if X is negative or positive, just add 180.
GoForSmoke:
So subtract 180 from your X = 0 to 359 and X is within -180 to 179.
To go from -180 to 179 to 0 to 359 just add 180. No need to test if X is negative or positive, just add 180.
You can not just subtract 180. If X is a direction, X-180 is in the oppsosite direction...
To "convert" from 0 - 359 to -180 - 179 you have to subtract 360 from X if (X>=180).
if (x >= 180) {
x = x - 360;
}
PROBOT135:
Problem: The methdod i am using has some problem.
the output has a huge spike when the angle shanges from negative to positive.
This is not a problem related to the -180/180 vs 0/360 representation of a changing angle.
You will see the same "spike" when going from 359 to 0 degrees.
To handle the transition, you might have to account for the direction of change.