I have an IMU FXOS8700CQR1 which has an accelerometer and magnetometer. I'm trying to calculate roll, pitch from the accelerometer and yaw from the magnetometer.
roll = atan2(-accelerometer,accelerometer);
pitch = atan2(accelerometer,sqrt(accelerometer*accelerometer+(accelerometer*accelerometer)));
magx = magnetometer*cos(pitch)+magnetometer*sin(roll)*sin(pitch) + magnetometer*sin(pitch)*cos(roll);
magy = -magnetometer*cos(roll) + magnetometer*sin(roll);
yaw = atan2(-magy,magx)*180/M_PI;
I'm getting incorrect yaw data but I'm not sure about the equations which I found on the internet. Does anybody have any idea in this regard?