I was just reading the attitude calculation part, so I gave you a rough answer. If you have any incorrect answers, please feel free to discuss and exchange them.
There are two methods of complementary filtering, accelerometer and gyroscope accelerometer, gyroscope and magnetometer, which are the so-called IMU and AHRS.
The IMU solution uses accelerometer correction Gyroscope, regarding the accelerometer, solving the angular velocity means decomposing the gravity into the body system. The angular velocity can be solved through the inverse trigonometric function. The inability to sense the rotational motion on the z-axis can be understood in this way. When the body rotates along the z-axis, The decomposed vector has not changed from before the rotation, that is, there is no gravity component on the horizontal plane, so the accelerometer cannot sense the rotation of the z-axis. Therefore, if you use this attitude settlement method, you need to manually align the nose of the aircraft in the north direction. Therefore, there is an error in the solution of yaw.