# How to add a complementary filter for the gyro z axis using a magnetometer?

I'm using the MPU9250, it measures acceleration, magnetometer, gyro, etc. Complementary filter tutorial for this is here, however this tutorial does not have a magnetometer and I do, thus I thought I could eliminate the gyro drift.
Seen here:

My complementary filters for Gyro X and Gyro Y are shown below respectively.

``````float angle_x = alpha*gyro_angle_x + (1.0 - alpha)*accel_angle_x;
``````
``````float angle_y = alpha * gyro_angle_y + (1.0 - alpha) * accel_angle_y;
``````

I have my local inclination angle which I have been previously told I need and I also have my magnetometer value.

My question is; How do I combine all my values into an equation for 'angle_z'?

I have Code Close to Perfection Well not quite there but I have A PID Calibration routine that does a great gob at zeroing out drift for the mpu6050/MPU6500 or Equivalent MPU9150/MPU9250 I have now have easy access to the compass with the same code and I am using DMP V 6.12 and I also have access to every register through simple macro requests that use the same naming structure as the Registry Map documents.

My Library is Simple_MPU6050 and so to be clear I am VERY interested in the same goal you are attempting. I have not yet achieved it but I am interested in working with you towards gyro / Compass integration or 9 Degrees of freedom.

I can share with you a library that is precompiled that is supposed to do this but I am afraid it is for ARM processors but I could be wrong and you may be able to get it working.

Invensense Developer corner is where these are located. The attached files are only the 9Degree of Freedom library and .h associated files they created. If you are able to get this to successfully compile these with an Arduino UNO I will be most grateful (This is a hobby and my skill set is limited when it comes to pre-compiled libraries) With this library compiling we should shortly achieve 9 Degrees of freedom with magnetometer calibration and more:

The Motion Driver 6.12 contains a binary library which contains InvenSense proprietary algorithms for sensor fusion and dynamic calibration. The MD 6.12 driver pushes the sensor data into the MPL and the MPL will handle the 9-axis sensor fusion including the compass integration.

Z
The libmplmpu.lib file failed to attach. Forum error you will need to get a login for https://www.invensense.com/developers/login/
look for Embedded MotionDriver 6.12

Added it with the .txt extension. Change it to .lib to get the file.

accel_auto_cal.h (1.01 KB)

compass_vec_cal.h (804 Bytes)

fast_no_motion.h (1.29 KB)

fusion_9axis.h (1022 Bytes)

gyro_tc.h (888 Bytes)

inv_math.h (184 Bytes)