LSM303D tilt compensation problem

Next assault. sixD, have you tried Kris’s IMU as l said in earlier post’s. I know it might be $50 but it will prove one way or the other. He backs his and is the person who shows all codes and fixes everyone else is.
He says ±2, which they best l could get was 7.
Buy one and prove one way or the other.
You shouldn’t need to be going into the .h (apart from looking and learning, not needing to change the code, maybe preferances) unless of course you can understand it all and if this was the case, you wouldn’t need to be asking these questions.


My code is here.

It obviously won’t work for you, because it’s meant for a telescope, and as you said, they don’t move like boats.

My point is, instead of the code you’re using (which has so many trigonometric functions) you could try Pololu’s driver.

It has a “heading” method which according to the documentation:

float LSM303::heading(vector from)

“Returns the angular difference in the horizontal plane between the ‘from’ vector and north, in degrees”

Maybe you could try it.

Hi Jim, by the look of things you aren’t willing to show the code you get the great results of zero tilt error with???
l am very keen to have a look and you could help a lot of people with it, so please post and help the IMU community.
Regards Kevin

I’ve been using RTIMULib, which has a decent calibration routine built in, and have no need to revisit code for the LSM303D. I imagine that it wouldn’t be difficult to modify RTIMULib to use that sensor, though.

My efforts were described here: State of the art AHRS for $25 and I highly recommend this software.

Of course, the accuracy of any IMU depends on doing the very best you can to calibrate both the magnetometer and the accelerometer, and in the latest posts, I don’t see evidence that you folks have really accomplished that. I would like to see before and after calibration plots, showing nice circles (not ellipses) or spheres perfectly centered on the origin.

Thanks Jim, by the sounds of it you are using Rtimulib2 with soft and hard iron correction.
Using Magneto l have centred circles on the Mag and Accel and as l said it only improved tilt error correction by 1-2 degrees to about ±10 of error (at worst).
Also my guru Jack, devised an even more accurate means of axis measurement (data thru a whole sphere) that we put thru Magneto with no better results. His V3 went from ±7(max/min) to about ±5(magneto), he is in the Northern Hemisphere.
Regards Kevin

Although this is an old thread, I think I found a possible solution to the problems that many of you were having.

The application note from for the LSM303, has some example matrix description for rotations by roll and pitch, which I think was used to generate most of the examples and libraries. There seems to be an error in that application note, which has flowed through to the examples. If you look at the line from many of the examples provided for the LSM303 you should find something like this:

fXm_comp = fXmcos(pitch)+fZmsin(pitch);

I think it should be:
fXm_comp = fXmcos(pitch)-fZmsin(pitch);

When I made this change, and used good calibration data, my compass seems to be very accurate, and I don’t see the heading errors with pitch changes that were discussed extensively above.

Hope this helps someone else.


Hi Chris, good detective work.
I haven’t tried it as yet but, in my case there is nothing wrong with the pitch tilt error, it is the roll which is the issue, but it all goes into the heading formula.
Will trial shortly and let you know the results.
What is your worst case error from tilting pitch and roll?

Hi cholm
Where can I locate the coded line equivalent to the “cos(pitch)-fZmsin(pitch);” within the LSM303 library?
Regards KWings