Compass demo problem


I am using matrix creator and I am working with microphones in HAL. I wanted to include compass into my project and I am facing issue with compass demo (matrix-creator-hal/demos/compass_demo.cpp). I tried to rotate with matrix-c and at some point the leds are going crazy (because data in imu_data.yaw are wrong). I know that this problem description is nonsense so I have made a video to better understand what’s the problem

Do you have any tips how to solve this?

Hi @rostidlo,

You might be experiencing a compass calibration problem. First check here about Soft and Hard Iron Effect and the basics of calibration. Simple explanation:

  • Hard Iron: A hard-iron distortion can be visibly identified by an offset of the origin of the ideal circle from 0,0. Like below:
  • Soft Iron: Unlike hard-iron distortion where the magnetic field is additive to the earth’s field, soft-iron distortion is the result of material that influences, or distorts, a magnetic field—but does not necessarily generate a magnetic field itself. Like this:

Following that guide should not be hard to implement a Hard iron calibraiton. This is the simplest one and you should get pretty good result after this. For more precise measurements you can try to implement the Soft Iron calibraiton.

You can also see here for implementation reference.

Let me know how it goes.

1 Like

I played around with the mag a little bit and implemented a simple python calibration / tilt compensation example:

You have to slowly wave the creator around in an 8 form, covering almost every direction, even upside down.

maybe it would make sense to make the calibration feature available in the MCU (LSM9DS1):

# creator/lsm9ds1.cpp
void LSM9DS1::calibrateMag(bool loadIn) 

i also addded tilt correction, but i’m not sure if i implemented it correctly, the accel also needs calibration, but it works quite fine.

@loom @yoelrc88

Thanks guys so much, good to know that it can be calibrated.

Thanks for the script @loom
I found also this one from @yoelrc88

I will look into it and will let you know.

yc/compass calib is even better implemented as it writes the calibration data to the chip itself and uses more fancy other stuff, but the branch is a little bit outdated and needs to be rebased to the latest master branch. and i’m not sure if it works with the current mcu firmware.

I have merged the code from yc/compasscalib to current driver source code on my r-pi and succesfully run the calibration. But it didn’t change anything. Compass-demo was still showing same results.

So I’ve been looking at the variables during running/debugging the calibration. In xy_angle_index variable there was ± correct angle aroung Z axis. So based on this code I have deleted things which I don’t need and I have compass which is showing north on LEDs. Bad thing on this code is that before every run I have to turn the device at least once around Z axis to initialize min/max etc. If someone is interested there is code

It is not 100% accurate, accuracy is bad when the device is not 100% in horizontal position. But for my usage (I have R-pi with matrix-c on chassis - so it is still in horizontal position) its good enough.

Thanks again for help :slightly_smiling_face:

You are right @loom. This tries to store the calibration values in the chip itself.

You could use this for now just as a reference, the branch is a bit outdated and currently does not fully work as intended.

Let me know if you have any additional questions on this.

you could try to adept my tilt correction in the python example using the accel. and be aware on the sensor the the x and z is flipped to the accel. so it’s better to use -mx and not mx int calculating the heading.