2019-09-09 07:24 AM
Hello everyone. I have been struggling with creating the tilt compensation compass.
I have prepared the code which is based on demo firmware but I don't receive reliable output. I mean, when I rotate the development board, the heading value doesn't look linear.
Link to my repository for better reading: https://github.com/matwiszynmarek/lsm303dlhc
Where did I do a mistake?
2019-09-09 07:33 AM
Currently won't be able to test/comment on the algorithm of LSM303DLHC_Azimuth function (maybe later), but a technical note: you are using float variables with math.h functions which do calculations on doubles (sqrt, acos, etc..), which is useless and wasteful.
If you want double precision, use double variables, otherwise use the float functions (sqrtf, acosf, etc..) which will accelerate your code (especially if your MCU has an FPU).
2019-09-09 07:45 AM
More to the point:
Don't forget about magnetic declination, find out the current value in your region.
There are several open source UAV autopilot firmware out there, like Ardupilot or PX4, see their code, maybe they can help you.
Here's Ardupilot's LSM303D module, and the general Compass module, refer to its calculate_heading function.