Problem in Z-Axis motion acceleration with MPU6050
You "removed gravity" from the Z axis by subtracting a constant. Obviously, this does not work if the Z axis no longer points "up".

If the sensor cannot be maintained in a particular orientation with respect to the gravity vector, then there is no simple way to remove gravity. You'll have to do a full 6-dimensional motion solution (3 axis accelerometer, 3-axis gyro) to determine position and attitude, and then derive your non-gravity accelerations from that.

Even then, your sensors will have long-term drift, so your solution will also need to have a means for compensating for that.