I bought a 3-axis magnetometer (Similar to this one ) And plugged into an Arduino in order to read the heading value. I mounted it on my robot and I drove with the robot for around 30 meters, turned 180 degrees and drove back to my starting position. I plotted the heading value and it shows inconsistent values. The 180 degrees turn started at sec 55, the rest is driving in one direction using a joystick and following a wall as reference so small deviations are expected but not that big as in the image.
When the robot is turning in-place, there is no such problem and the heading variation follows the rotation of the robot. The robot (Turtlebot) is a little bit shaky such that the magnetometer doesn't always have the x and y axes parallel to the floor but I don't think few degrees of offset can cause such a huge difference. I calculate the heading as follows:
heading = atan2(y field intensity, x field intensity)
Why does this happen? Could it be form some metals or electric wires under the floor? Can you suggest a more robust method/sensor for estimating the heading in indoor environments?
I drove the same path again and the pattern similarity is making it even weirder