cancel
Showing results for 
Search instead for 
Did you mean: 

MotionFX Magnetometer Calibration Save to NVM

Artur Vieira
Associate III

Hi

I am trying to include the MotionFX library to my project, and having a little issue.

The functions "MotionFX_SaveMagCalInNVM" and "MotionFX_LoadMagCalFromNVM" are never called. I copied the functions declarations and empty implementation from the example "X-CUBE-MEMS1\8.3.0\Projects\STM32L476RG-Nucleo\Applications\IKS01A3\DataLogFusion".

Shouldn't they be called when using "MotionFX_MagCal_init(ALGO_PERIOD, 0);" and "MotionFX_MagCal_init(ALGO_PERIOD, 1);"?

For the moment what I am doing is that I added the "Motion_MC" to the project, and using this to calibrate the magnetometer (with this library, the NVM functions are called). Is there any downside to using the "Motion_MC" to calibrate the magnetometer instead of the one included in "Motion_FX"?

Thank You

4 REPLIES 4
Eleon BORLINI
ST Employee

Hi @Artur Vieira​ ,

the way you are using the MotionMC library is correct, and should be effective, provided that you use it just before the MotionFX algorithm.

My guess is that the:

MotionFX_SaveMagCalInNVM

and the:

MotionFX_LoadMagCalFromNVM

functions are doing their functions at lower level and are called from higher-level functions (the MotionFX_MagCal_ ones).

Namely, your question is also part of an older community post from our expert Miroslav, that I suggest you to read for a broader comprehension of the MotionFX library, and whose answer is reported here below for clarity:

>> MotionFX_LoadMagCalFromNVM and MotionFX_SaveMagCalInNVM seems to be never called. They are needed at linking time but they seems to be ignored. I've put a simple "save to flash" function (that works correctly outside) but no data is stored in the flash memory. How should I trigger the load/save? If I save parameters, how should I behave using the MagCal routines? What is the advantage?

These functions are callbacks, Load is called when you enable MagCal and Save is called when you disable MagCal.

If my reply answered your question, please click on Select as Best at the bottom of this post. This will help other users with the same issue to find the answer faster. 

-Eleon

Artur Vieira
Associate III

Hi @Eleon BORLINI​ 

Regarding that old community post, i had already seen it, but it doesn't clarify my doubt.

The answer in that post states:

These functions are callbacks, Load is called when you enable MagCal and Save is called when you disable MagCal.

But I don't see that happen. I have the functions declared has:

/**
 * @brief  Load calibration parameter from memory
 * @param  dataSize length ot the data
 * @param  data pointer to the data
 * @retval (1) fail, (0) success
 */
char MotionFX_LoadMagCalFromNVM(unsigned short int dataSize, unsigned int *data)
{
  DEBUG_Printf(true, true, "MOTION_FX: Get Saved Calibration function called, Size: %lu\r", dataSize);
 
  return (char) 1;
}
 
/**
 * @brief  Save calibration parameter to memory
 * @param  dataSize length ot the data
 * @param  data pointer to the data
 * @retval (1) fail, (0) success
 */
char MotionFX_SaveMagCalInNVM(unsigned short int dataSize, unsigned int *data)
{
  DEBUG_Printf(true, true, "MOTION_FX: Save Calibration function called, Size: %lu\r", dataSize);
 
  return (char) 1;
}

And the functions are removed (not included in the final elf file) by the compiler since they seem to not be used.

At this moment the code is always calling at startup:

MotionFX_MagCal_init(ALGO_PERIOD, 1);

and:

MotionFX_MagCal_init(ALGO_PERIOD, 0);

When the mag calibration status is "MFX_MAGCALGOOD".

Are the function signatures wrong? Speculating this since the compiler remove the functions, they appear in the MAP file in the "Discarded input sections"

Shouldn't they being called at that moment? Or is there another way to enable / disable Mag calibration in MotionFX to trigger these callbacks.

For the moment using the "MotionMC" seems to do the trick, I am able to save and load the calibration values from memory. Just interested in using the calibration function in "MotionFX" (considering the end result is the same) more in a perspective of program memory / RAM saving (it seems to exist a difference of around 10KB Flash and 2.4KB of RAM extra use).

Thank You

Hi @Artur Vieira​ ,

those two functions should only rise (or not) the "calibration flag", that is managed at the higher level of the MagCal functions.

But did you try whether your code works if you just use the MotionFX (and not the MotionMC)?

-Eleon

Artur Vieira
Associate III

Hi @Eleon BORLINI​ 

The code works, the outputs values from MotionFX are as expected, either only using the MotionFX or using the MotionMC for calibration. The difference is that with MotionMC since I am getting the callback to save/get calibration data from NVM, upon system restart the mag is always calibrated. Using the MotionFX since the NVM callbacks are never happening, upon system restart the mag calibration is always "UNKNOWN" and is necessary to perform "the 8 in air" in order to properly calibrate the the mag, until the mag is calibrated is noticeable that the behaviour of output "heading" is not "decent".

Thank You