Skip to main content
Visitor II
November 26, 2020
Question

North heading using LSM303AGRTR

  • November 26, 2020
  • 2 replies
  • 2818 views

Hello,

I am using LSM303AGRTR in a custom board.

After collecting X(68,69 reg),Y(6A,6B reg)and z(6C,6D reg) values, I want to know how to calculate angle with respect to north ?(z axis is perpendicular to earth surface)

Also if the IC is given 90deg tilt means z axis is parallel to earth surface , how to calculate heading north?

Regards,

GBiswal

    This topic has been closed for replies.

    2 replies

    ST Employee
    November 27, 2020

    Hi @GBISW.1​ ,

    The simplest way to implement the calculation heading with the magnetometer output is the atan2() function.

    Here below a possible pseudo-code function to implement it:

    // Calculate the angle of the vector y,x
     float heading = (atan2(magnetic.value.y, magnetic.value.x) * 180) / Pi;

    I however suggest you to have a look to more complex and accurate functions, for example the MotionEC library of X-Cube-MEMS1, that provides real-time information about the device orientation and movement status based on data from a device. 

    -Eleon

    GBISW.1Author
    Visitor II
    December 1, 2020

    Hello Eleon,

    Thanks for quick response.

    I am using the formula suggested by you. after rotating the board about 90 degree, I am getting rougly 30degree change in output.

    Steps I am following

    --------------------------------

    1) Startup initialisation by writing value 0x00 in REGA(0x60) and 0x01in REGC(0x62)

    2) Reading reg values(0x68 and 0x69) for x value, (6A,6B) for y value and (6C,6D) for z value using I2C protocol.

    I want to do without using library.

    Kindly suggest

    Regards,

    GBiswal

    ST Employee
    December 2, 2020

    Hi GBiswal @GBISW.1​ ,

    If you use the magnetometer for the tilt measure, you should always calibrate the sensor first, as for example explained in the LSM303AGR datasheet (p. 23) this application note for an ST library that performs such (hard and soft iron) calibration, since it is sometime a complex procedure, especially for the soft-iron calibration.

    I would however use the accelerometer output for calculating the tilt of the device, and I suggest to check this other similar community topic for this purpose.

    -Eleon

    ST Employee
    December 21, 2020

    Hi GBiswal @GBISW.1​ ,

    the elliptic shape of the magnetic data plots are denoting the presence of soft iron distortion, so the simple "offset" compensation is not so effective as desired.

    For this kind of compensation, you should first check if you have any source of unwanted magnetic fields around your application platform, and then use more complex algorithms, such as for example the MotionMC magnetometer calibration library form the X-CUBE-MEMS1 firmware function pack.

    "The library acquires data from the magnetometer and calculates the hard iron (HI) and soft iron (SI) coefficients or, in the case of the library for Cortex-M3 and M4, only the scale factor (SF - diagonal elements of soft iron matrix) coefficients together with the calibration quality value. The calibration coefficients are then used to compensate raw data from the magnetometer and reduce hard iron and soft iron effects."

    -Eleon

    GBISW.1Author
    Visitor II
    December 29, 2020

    Thanks Eleon,

    Since I am new to use MEMS library , I dont know how to add library in a project.

    Is there any reference material which I can follow?

    Regards

    GBiswal

    ST Employee
    January 7, 2021

    Hi GBiswal,

    please check this post that should match your request. With STM32CubeIDE, in particular:

    under Project > Properties > C/C++ Build > Settings > Tool Settings > MCU GCC Linker > Libraries add the .a library name and the path to that file. This shall match the linker. Note that you don't enter the "lib" prefix and the extension .a, e.g. when the filename is libmylib.a, enter mylib

    Besides, under MCU GCC Compiler > Include paths add the path to the header files to make the compiler happy.

    You may keep copies of the .a and .h files within the project folder tree or "out-of-tree" in a separate folder as you prefer.

    Note that .a files may exist for different MCU/CPU architectures and ABIs like floating point. Make sure that you get the correct libray linked in.

    Please let me know if you can make it work.

    -Eleon