I began with code to hold a X/Y platform level when the base is moved and a MPU6050 and moved onto a MPU9250. The addition of magnetometer allowed the use of the MahonyQuaternionUpdate method, I then turn the Quaternions into Euler angles, finally turn the Euler angles into servo torque values to keep a platform level. To keep the magnetometer calibrated, I have a 2nd MPU9250 mounted 180 degrees, as referenced by the magnetometer, to the first MPU9250, as the platform moves around. It would not be difficult to hold the Z axis pointed N as the platform moves around.
I suggest you look over this fellows work: https://os.mbed.com/users/onehorse/
If you use the MPU9250, I found that the MahonyQuaternionUpdate method works a lot better if values are entered for Kp and Ki. These are the values, I found that work quite well for Kp and Ki for the MPU9250.
// #define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
define Kp 7.50f
define Ki 1.7f
Reads like a fun project. Good luck.