cancel
Showing results for 
Search instead for 
Did you mean: 

North heading using LSM303AGRTR

GBISW.1
Associate II

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

8 REPLIES 8
Eleon BORLINI
ST Employee

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

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

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

Hello Eleon,

I am doing calibration by rotating the device in 3D and collecting raw data(mag_raw)

Then raw data is converted to Gauss by using formula mag_Gauss(x,y,z) = mag_raw(x,y,z)*1.5/1000.

max_x = max of all values in mag_Gauss_x

max_y= max of all values in mag_Gauss_y

max_z = max of all values in mag_Gauss_z

min_x = min of all values in mag_Gauss_x

min_y = min of all values in mag_Gauss_y

min_z = min of all values in mag_Gauss_z

Then compensated value calculated by using below equations

mag_x = mag_Gauss_x-(max_x + min_x)/2

mag_y = mag_Gauss_y-(max_y + min_y)/2

mag_z = mag_Gauss_z-(max_z + min_z)/2

But I thing data is not properly calibrated since elliptic nature is there.

Results and raw data attached

0693W000006G11VQAS.jpg 

Regards,

GBiswal

Raw data captured

Eleon BORLINI
ST Employee

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

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

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