Skip to main content
Visitor II
September 9, 2019
Question

LSM303DLHC heading calculation

  • September 9, 2019
  • 2 replies
  • 815 views

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?

    This topic has been closed for replies.

    2 replies

    Visitor II
    September 9, 2019

    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).

    Visitor II
    September 9, 2019

    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.