composate heading using mpu6050+HMC5883L

Hello there i'm working on a project, that i use 10 DOF mpu6050 /HMC5883L/BMP180.
i used this code mpu_6050/hmc5883l

i'm searching on the internet to find the formula about the tilt composation so i find this formula

Code: [Select]
// tilt compensated magnetic sensor measurements
float magxcomp = Magxcos(Pitch)+Magzsin(Pitch);
float magycomp = Magxsin(Roll)sin(Pitch)+Magycos(Roll)-Magzsin(Roll)*cos(Pitch);

problem is that the heading is not comosated. when i did a roll or a pitch the value of the heading change, so i want to fix this prblem, i want the value of heading stay correct event i did a pitch or a roll.
thanks

is there something wrong with the formula ??
i'mo working on autonomous flight so i need the correct value of heading, please helpe

Please help :frowning:

The answer was given in your earlier, identical post on the topic.

i tried to use this formula "m for the magnet field"

float Yh = my * cos(pitch) - mz * sin(pitch);
float Xh = mx* cos(roll) + mz * sin(pitch) * sin(roll) + mz* (pitch) * sin(roll);
heading = atan2(Yh, Xh);
if (heading < 0) heading += 2 * M_PI;
i tried a lot of test but idid'nt recognize the probleme

here it is my test

when the heading is between 250 and 300 degrees. the tilt composation works fine when i do a negative ROLL. but if a do a positive ROLL or positive and negative PITCH it does'nt composate.
when the heading is on 0 degrees, the tilt composaton works fine when i do a negative PITCH. but if a do a positive PITCH or positive and negative ROLL it does'nt composate.

is there something wrong with the formula
please help

Maybe you have pitch and roll defined differently than as used in the formula. Study the links that I posted in your other thread.

You MUST calibrate the magnetometer for tilt compensation (note the spelling) to work.

You MUST calibrate the magnetometer for tilt compensation

how do i to calibrate the magnetometer?? please

Here is the best method. Google will show you others, but I'll bet you didn't try.

thanks you for the idea. i did some change and now the heading is a litle bit componsated but still getting error and i think is due to the calibartion. i didn't finde a code that calibarte my magnetometer.

could you help me how to code the calibartion of hmc5883l please