I moved your topic to an appropriate forum category @awais110565.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Without any of the map()s or contstrain()s, this line in the new code:
...would give you a 0-360°--direction of tilt. Constraining that measurement of a direction of tilt to a single 90° quadrant seems nonsensical. For example, constraining the direction of measured tilt to the 90° between rightwards & forward means that tipping backwards gives a result of straight rightwards tilt???
If you want to measure tilt and roll, use the code in the link.
To be completely clear, these are mathematically correct expressions for the only two angles you can measure with the MPU-6050, where x,y,z are the three RAW acceleration values.
// minimal MPU-6050 tilt and roll (sjr). Works with MPU-9250 too.
// works perfectly with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
//
// Tested with 3.3V eBay Pro Mini with no external pullups on I2C bus (worked with internal pullups)
// Add 4.7K pullup resistors to 3.3V if required. A4 = SDA, A5 = SCL
#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;
void setup() {
Wire.begin(); //begin the wire communication
Wire.beginTransmission(MPU_addr1); //begin, send the slave adress (in this case 68)
Wire.write(0x6B); //make the reset (place a 0 into the 6B register)
Wire.write(0);
Wire.endTransmission(true); //end the transmission
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(MPU_addr1);
Wire.write(0x3B); //send starting register address, accelerometer high byte
Wire.endTransmission(false); //restart for read
Wire.requestFrom(MPU_addr1, 6); //get six bytes accelerometer data
int t = Wire.read();
xa = (t << 8) | Wire.read();
t = Wire.read();
ya = (t << 8) | Wire.read();
t = Wire.read();
za = (t << 8) | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
roll = atan2(ya , za) * 180.0 / PI;
pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied
Serial.print("roll = ");
Serial.print(roll,1);
Serial.print(", pitch = ");
Serial.println(pitch,1);
delay(400);
}
So your z-axis and y-axis are horizontal, while your +y-axis is vertical.
What is the way that link moves in 3d space? It looks like theta pivots around z, Does the link also pivot around y? Is the MPU6050 mounted on the link? Is the pivot parallel to the z-axis and the MPU6050's Y-axis along the link? Like:
Then accZ' will always be near zero (because Z' is horizontal), and when theta = 0° accY'=0 because Y' is horizontal and accX = -1g because X' points downwards. And theta = 90° when accY=1g because the Y'-axis points up, and accX'=0 because the X' axis is horizontal, To get atan2(y',x') to give the proper angle, you have to be very careful with the axes, the signs and direction of gravity. If you mount it in a non-standard way, the standard formulas aren't going to work