LSM303DLHC - Calibration, Pitch, Roll and Tilt Compensated Heading

Hi there,

I've attempted calibrate my Adafruit LMS303DLHC several times using the instructions above, however I have not yet been able to get the tilt-compensation to actually work.

I've done some research on the topic, which involved thoroughly reading through this post, as well as Ed Mallon's calibration blog post, and sixD's tilt compensation thread on the Pololu forums (which has not yet been resolved). While it appears I'm calibrating properly, when I run the Pitch, Roll and Heading script, the heading will change significantly when any pitch or roll is experienced by the sensor.

Using Magneto and plot.ly for visualization, my uncalibrated values appeared as below:


After calibration, they improved to the following:


Accelerometer calibration was quite good to start off with, but after calibration bias was even closer to 0:

Now, while it seems I have the correct calibration values to calculate the pitch, heading, and roll, the tilt-compensation appears to be effectively not working. The only code I have changed in any of the steps has been to replaced the calibrated bias values. In the pitch, roll, and heading script they appear as follows:

// Accelerometer calibration
Xa_off = compass.a.x/16.0 + 17.224598; //X-axis combined bias (Non calibrated data - bias)
Ya_off = compass.a.y/16.0 + 12.282323; //Y-axis combined bias (Default: substracting bias)
Za_off = compass.a.z/16.0 - 3.162899; //Z-axis combined bias
Xa_cal =  0.949813*Xa_off + 0.009282*Ya_off + 0.004454*Za_off; //X-axis correction for combined scale factors (Default: positive factors)
Ya_cal =  0.009282*Xa_off + 0.983771*Ya_off - 0.001821*Za_off; //Y-axis correction for combined scale factors
Za_cal =  0.004454*Xa_off - 0.001821*Ya_off + 0.980313*Za_off; //Z-axis correction for combined scale factors

// Magnetometer calibration
Xm_off = compass.m.x*(100000.0/1100.0) + 683.481495; //X-axis combined bias (Non calibrated data - bias)
Ym_off = compass.m.y*(100000.0/1100.0) + 8449.523695; //Y-axis combined bias (Default: substracting bias)
Zm_off = compass.m.z*(100000.0/980.0 ) + 6823.709607; //Z-axis combined bias
Xm_cal =  0.882879*Xm_off + 0.024205*Ym_off + 0.016546*Zm_off; //X-axis correction for combined scale factors (Default: positive factors)
Ym_cal =  0.024205*Xm_off + 0.863208*Ym_off + 0.007950*Zm_off; //Y-axis correction for combined scale factors
Zm_cal =  0.016546*Xm_off + 0.007950*Ym_off + 0.829441*Zm_off; //Z-axis correction for combined scale factors

I'm very curious as to whether I may have made a glaring mistake somewhere that has escaped notice. If anyone might have any thoughts on where I could have gone wrong, I would certainly appreciate it! :slight_smile:

Cheers,
A