A bit ago someone raised questions about orientation angles produced by the Sparkfun MPU-9250 breakout board, using the provided AHRS code (Sparkfun made its own modifications to the code posted by Kris Winer on Github).
I decided to buy the Sparkfun MPU-9250 board to test it and discovered a major bug in both sets of software that produces nonsensical yaw, pitch and roll angles. It has been there for years, and others have noticed it (see issue #368 on Kris Winer's Github page), but has not been fixed. The code has never produced useful results.
Sparkfun's code (MPU2590BasicAHRS_I2C) doesn't work out of the box, either, because it assumes the wrong default I2C address for their own breakout board.
Sloppy work, all the way around.
The main issue is that the incorrect code inverts the handedness of the magnetometer coordinate system (which, due to the chip construction, has all axes swapped relative to the accelerometer/gyro axes).
A simple fix is to negate the magnetometer Z axis data, as SHOWN CORRECTED in the line below. Note the term "-myIMU.mz".
MahonyQuaternionUpdate(myIMU.ax, myIMU.ay, myIMU.az, myIMU.gx * DEG_TO_RAD,
myIMU.gy * DEG_TO_RAD, myIMU.gz * DEG_TO_RAD, myIMU.my,
myIMU.mx, -myIMU.mz, myIMU.deltat);
Of course, to get useful data at all, the magnetometer MUST be correctly calibrated. So, do activate the built in calibration code, as in the modified Sparkfun example attached to this post. The calibration procedure is far from the best, and applies only the bias offsets, but works well enough to produce somewhat sensible results.
In the code attached, yaw = 0 corresponds to the accelerometer/gyro X axis pointing to magnetic North, with Z up, after proper magnetometer calibration.
UPDATE 3/24/2020: The stupid Sparkfun gyro/accelerometer calibration routine assumes that the sensor is kept still and horizontal, with Z up during initialization. The accelerometer should be recalibrated after this step or the pitch and roll angles will be wrong. Since the MPU-9250 has been discontinued, I recommend that people don't waste their time with it.
UPDATE 3/31/2020: I rewrote everything using a different MPU-9250 library and attached the new code at the end of reply #18, this thread. It all works well, provided that BOTH the accelerometer and the magnetometer are properly calibrated. See comments in .zip file.
MPU9250BasicAHRS_I2C.zip (4.86 KB)