- the code looks ok, and you only need the heading i guess
- i think the magnetometer measures strongest magnet around, so when there's nothing around, the mag should pick the earth magnetic field and give you your heading relative to the earth's magnetic field.
- when the object's magnetic field is stronger than the earth, it will take over, and your mag is going to measure the strength of that.
- im not sure how to do this, but i think it is pretty much impossible since the mag only give heading
- as i said it gives u your heading, but don't expect it to be really accurate, you can try a 9dof IMU which has the mag+gyro+accel and through AHRS algorithm, it should give you better result.