cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303AH - Mag field on Z ax doubt

FGome
Associate II

Hi, I have implemented a tilt and heading calculation using the simplified heading calculation that only works when the device is horizontal, and was until now ignoring the magnetic field value on the Z ax. Now I'm trying to implement a tilt compensated heading to be able to calculate heading when the device is not horizontal, and I was starting by trying to obtain the Z ax max and min magnetic field value to compensate its offset as I did before on the X and Y axes, and noticed that the minimum is with the board lying horizontally on the table, components facing up, and the maximum is with the board lying horizontally on the table with the components facing down, I never get close to these max/min values rotating the board in any other direction. I was rotating the board vertically and the Z value varies but its max/min value when it is in a vertical position is less than half of the value when it is lying horizontally on the table. Does it makes sense? For the Z ax It seems that the stronger magnetic field is below the table, but that doesn't agree with the measurements I get on the other axes, the X and Y axes need some offset compensation but their value varies with their relative orientation with the North so their maximum values point to North and not 'down'. Any help / clarification will be appreciated, I think I might be missing something here.

For the tilt compensated heading, is there any reference code that could be used? I have already implemented but having a reference code to compare will be great, the code is running on a STM32L4xx.

Thank you in advance!

Fernando

1 ACCEPTED SOLUTION

Accepted Solutions
FGome
Associate II

Hi Eleon,

Thank you very much for your answer! For tilt I'm using only the accelerometer data, and it is working well, for the heading I was using a simplified algorithm only with X and Y mag fields, it worked well when the unit was in the horizontal plane (as expected) but it doesn't work when there was a significative tilt, and this is why I needed to add the Z magnetic component. I'll take a look to the MotionC you pointed me to, thank you. In the meantime for the heading I have implemented an algorithm similar to the one used in the Pololu library for the LSM303D - https://github.com/pololu/lsm303-arduino, and it seems to work well, even before calibrating the Z ax, I only had max and min values for the X and Y axes, and even in this case the results are good for the application, going from 0º tilt to 80º tilt the heading doesn't change a lot, I've observed a change of about 5º in the heading but it might be due to being difficult in my setup to change the tilt without affecting the real heading.

Best regards

Fernando

View solution in original post

4 REPLIES 4
Eleon BORLINI
ST Employee

Hi @FGome​ ,

I would not use the magnetic sensor's z axis for measuring the tilt angle in your application (which is, I believe, more indicated for North to South heading angle), but the accelerometer part of LSM303AH device.

In particular, you can refer to some application notes (AN4509) and user manuals (UM2277) for a deepening into the argument. The basic formulas are the following ones (usting the atan2 function):

0693W000006E2aXQAS.png 

A library that allows you to calculate the tilt given the accelerometer MotionTL tilt measurement library of the X-CUBE-MEMS1 firmware function pack: it provides real-time information about the tilt angles of the user device, i.e. cell phone. The library is also able to perform accelerometer 6-position calibration. 

A more complete reference library for the with accelerometer and magnetic sensor is the MotionEC real-time E-Compass library, from the same function pack, that provides real-time information about the device orientation and movement status based on data from a device, and in particular the device orientation (quaternions, Euler angles), device rotation (virtual gyroscope functionality), gravity vector and linear acceleration. 

-Eleon

FGome
Associate II

Hi Eleon,

Thank you very much for your answer! For tilt I'm using only the accelerometer data, and it is working well, for the heading I was using a simplified algorithm only with X and Y mag fields, it worked well when the unit was in the horizontal plane (as expected) but it doesn't work when there was a significative tilt, and this is why I needed to add the Z magnetic component. I'll take a look to the MotionC you pointed me to, thank you. In the meantime for the heading I have implemented an algorithm similar to the one used in the Pololu library for the LSM303D - https://github.com/pololu/lsm303-arduino, and it seems to work well, even before calibrating the Z ax, I only had max and min values for the X and Y axes, and even in this case the results are good for the application, going from 0º tilt to 80º tilt the heading doesn't change a lot, I've observed a change of about 5º in the heading but it might be due to being difficult in my setup to change the tilt without affecting the real heading.

Best regards

Fernando

Eleon BORLINI
ST Employee

Hi @FGome​ ,

Thank you for sharing this tip!

I would however calibrate the magnetic sensor before using it, or at least after some predefined time, to guarantee a proper accuracy especially along z. You can for example use the suggestion in the datasheet p.23 or another library called MotionMC magnetometer calibration library, from the same function pack of the previous one.

-Eleon

FGome
Associate II

Hi Eleon,

Thanks again for your help. Sure, I'll take a look to the MotionMC and also do a calibration on all axes, I only tested without the Z ax calibrated because I didn't had data for it at that time. I suppose I was also a bit lucky also because the Z ax is much more symmetric than X and Y axes, I think that was the reason to have reasonable results without Z ax calibration, that would be impossible with the huge offsets I have on X an Y axes, where there are lots of metallic components around the LSM303 that distort the magnetic field a lot on these axes.

Best regards

Fernando