# Gyro Yaw Drift Compensation With The Aid of Magnetomer

I'm getting data From an IMU and a Magnetometer synchronously, my roll and pitch drifts are corrected with accelerometer inside IMU and now I have the DCM matrix during my sensor reading i also have the true heading from Magnetometer as a simple H degrees from north. what is the most straightforward way of correcting the my DCM with this H angle, i don't want to go through kalman filtering, just i want the formulation to simply compensate my yaw angles from this H value.

Based on the answered and comment i think my question was not clear so i try to clarify it more

more detail:

I have an IMU witch has an accelerometer and gyroscope that internally compensates for pitch and roll drift; this IMU not only provides me compensated pitch and roll angles as well as uncompensated yaw angle but also provides all raw data of accelerometer and gyroscope. by roll, pitch and yaw angle i mean three Euler angles as φ, θ, ψ (witch are result of 3->2->1 Rotation).

now i have added a magnetometer sensor to my system. i read raw data from magnetometer and using atan2 i convert it to heading. now i want to use a simple complementary filter to compensate for yaw drift and i ask here for it's formulation. my main problem arises from the fact that the ψ (yaw) angle from the IMU is not directly only related to Heading. i mean for instance i cannot replace the yaw angle (ψ) with the heading value! so i want to know witch witch formulation i can simply relate this heading to yaw angle.

my roll and pitch drifts are corrected with accelerometer inside IMU

You mean they're being correct by the IMU, or you're correcting the readings yourself using the accelerometer from the IMU?

i want the formulation to simply compensate my yaw angles from this H value.

If you're correcting the other angles yourself, just use the same method to correct headings.

i don't want to go through kalman filtering

Well, you're going to have to do something if you want better results. Madgwick filter would be the best, the Kalman, then you could even try complementary filtering. The Madgwick filter is freely available (bottom of page) - highly recommended.

• i added more detail Jun 11 '18 at 10:42

A magnetometer in my experience is not a reliable source of heading. They are effected by local magnetic fields caused by large metal objects and electrical currents. Using it as a simple offset will probably make your heading worse. You will definately need some type of filter.

First of all: Filtering would be the approach here. However, there are more complected and less complicated - in the sense of effort - ways to achieve it.

I assume you get the orientation (quaternion or euler angles does not matter at this point) form the IMU. When you have that is really complicated to integrate the magnetic measurements. It would need a sophisticated dynamics model of your system. I.e. a lot of effort do develop and verify. But once you have that, a kalman (or others probabilistic approaches like particle filter) would lead to very good results.

Furthermore, I assume you can also get the raw measurements of the IMU, i.e. the angular velocity, linear acceleration and magnetic field. Also here a proper dynamics model would lead to a kalman or similar in order to achieve good performance. In my opinion this would lead to the best result but needs a lot of effort to get there (knowledge of the system and sensor noise).

A more straight forward way is to use a Madgwick or Complementary filter as they are implemented in a nice library (check it out). They use the raw data and you can also use them with or without the magnetic field measurements. This library is very straight forward to implement and easy to use.

In general for magnetic field measurements it is advantageous to have some kind of calibration. This should be provided by the manufacturer. The calibration compensates for static magnetic substances in the environment of the sensor. When properly calibrated, using the magnetic field measurement - together with the angular velocity and linear acceleration - will clearly improve the orientation estimation - especially the yaw drift. Moreover, magnetic field measurement indoors mostly performs not as good as outdoors.

• i added more detail, i will appreciate if you read it Jun 11 '18 at 10:41
• Use the library i provided the link for in my answer above. Feed the complementary filter the measurements form the IMU (yaw, pitch, and roll - this estimation is independent from magnetometer) and the ones from the magnetometer. These do not have to come from the same sensor(!). But take care of the different frames the magnetometer and the IMU are in. They have to be in the same frame if you feed them to the filter. Furthermore, make sure that us these measurements that are the closest to each other on the timeline - i.e. have the smallest time difference when they were measured. Jun 14 '18 at 11:51