Data processing from MPU6050

I found a code on the internet that converts MPU6050 to degrees. I don't understand what int minVal and int maxVal does and how we got them. Another thing I don't understand is why the PI value is added to the final result. Does it help calculate the deviation?

#include<Wire.h>
 
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 
int minVal=265;
int maxVal=402;
 
double x;
double y;
double z;
 
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
 
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);


Serial.print("AngleX= ");
Serial.println(x);
 
Serial.print("AngleY= ");
Serial.println(y);
 
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
delay(400);
}

If you just "found" the code, does the code work properly? IF it does not, then this may be someone's failed try at programming and there is no way to tell what they were trying nor why.

The person who wrote that code might remember the answer to your questions. Or maybe not.

It works well.

Is there any other option, to convert the output of accelerometer to degrees?

You can measure tilt angles correctly, which the "found code" does not, by following this tutorial: How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing-DFRobot

Okay, I will try it. Thanks

Acceleration is not measured in degrees (or radians)

I know, but it can be converted. At least I found it can.

BUT, it always has an acceleration component. So degrees must be degrees per second or radians per second. You cannot eliminate the time component.

Are you talking about using the accelerometer as an inclinometer?
Plenty of example code for that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.