Quaternions from ICM 20948 DMP to calculate yaw

Hi!
I’m using ICM 20948 with DMP enabled to calculate quaternions. With those quaternions, I’ve been using the calculations in the code in the image attached to derive yaw (from OpenLog_Artemis/Firmware/Test Sketches/IMU_DMP_Quat9/IMU_DMP_Quat9.ino at main · sparkfun/OpenLog_Artemis · GitHub). In the attached image, I took the IMU for multiple laps around a rugby field and you can see that for two of the turns something strange is happening… as I make the 90 deg turn in red the yaw changes by close to 180 deg, and then on the subsequent turn in blue the yaw changes by 90 deg but is then quickly corrected back, as though it realises the overestimation in change of yaw from the previous turn. Does anyone have any ideas as to why this might be happening? Here is the raw quaternion data: rugbyfield.txt - Google Drive
Thank you!

My first guess is that it is maybe mounted on the wrong axis? Double check the sensor orientation and/or share a photo of its placement

Possibly. But the sensor was oriented flat the whole time, with Z pointing down (as in the picture below, X being east/west).

That looks right
Make sure you’ve done a calibration, and maybe test it by going in circles each way for a sec and reading the data to see if it is still ‘blipping’