ST X-CUBE-MEMS1 User Manual Download Page 4

2.2.3

MotionFX library operation

The MotionFX library integrates 6- and 9-axis sensor fusion algorithms in one library; they can even run
simultaneously to provide both rotation vector (9X) and game rotation vector (6X).

Cortex-M3, Cortex-M4 and Cortex-M7

The library implements the following critical internal functions associated with sensor fusion computation:
1.

the 

MotionFX_propagate

 is a prediction function used to estimate the orientation in 3D space; gyroscope

data is given more weight in this phase.

2.

the 

MotionFX_update

 is the corrective function which adjusts the predicted value when necessary;

accelerometer and magnetometer data are given more weight in this phase.

The 

MotionFX_update

 function can be called whenever the 

MotionFX_propagate

 is invoked, or less often in

systems that have less computation power.
The 

MotionFX_update

 function takes approximately three times more MCU computation time than the

MotionFX_propagate

 function.

Cortex-M0+

The sensor fusion computation is concentrated in only one function 

MotionFX_CM0P_update

.

2.2.4

MotionFX library parameters

Cortex-M3, Cortex-M4 and Cortex-M7

The library is "parameterized" using an 

MFX_knobs_t

 structure.

The parameters for the structure are:

ATime

MTime

FrTime

 represent the weighting stability of sensors for prediction (trust factor), from 0 to 1.

Default values are recommended.

ATime

: lowering the value will increase the accelerometer weight and sensitivity towards external

acceleration. If the application experiences low acceleration (<1g) most of time, it is recommended to
increase the value.

MTime

FrTime

: for 9X solution, the lower value will increase the weight of magnetometer reading. If

the application experiences low magnetic interference (<50 μT) most of time, it is recommended to
increase the value.

LMode

 represents the gyroscope bias learning mode; the library automatically tracks and calibrates gyro

zero-rate bias drift.
This possible parameter values are:

LMode = 0 – learning off; use this mode if the gyro is already calibrated

LMode = 1 – static learning; learning is performed only when the system is not moving

LMode = 2 – dynamic learning; learning is also performed when the system is moving

gbias_acc/gyro/mag_th_sc_6X, gbias_acc/gyro/mag_th_sc_9X

 represent the thresholds below

which the gbias algorithm automatically starts. These values should be established through testing (they are
different for different part numbers). The values in the example project are usually correct. The value can be
determined by enabling 

start_automatic_gbias_calculation

, placing the device stationary for 10

seconds and reading the value of knobs.

modx

 represents the decimation of 

MotionFX_update

 call frequency

output_type

 represents the sensor fusion library output orientation: 0 = NED, 1 = ENU

start_automatic_gbias_calculation

: this flag allows computing 

gbias_acc/gyr/

mag_sens_th_sc_6X/9x

 for an application, by placing the device stationary for 10 seconds and

setting this flag to 1 by calling 

MotionFX_setKnobs

 API. After 10 seconds or more, call

MotionFX_getKnobs

 API to get the value of these thresholds.

acc/gyro/mag_orientation

 is the acc/gyro/mag data orientation string of three characters

indicating the direction of each positive orientation of the reference frame used for the accelerometer
data output, in the sequence x, y, z. Valid values are: n (north) or s (south), w (west) or e (east), u (up)
or d (down).
As shown in the figure below, the 

X-NUCLEO-IKS01A2

 accelerometer sensor has an NWU orientation

(x - East, y - North, z - Up), so the string is: “nwu”.

UM2220

MotionFX library

UM2220

 - 

Rev 7

page 4/24

Summary of Contents for X-CUBE-MEMS1

Page 1: ...M Cortex M0 ARM Cortex M3 ARM Cortex M4 or ARM Cortex M7 architectures It is built on top of STM32Cube software technology to ease portability across different STM32 microcontrollers The software come...

Page 2: ...ronyms Acronym Description API Application programming interface BSP Board support package GUI Graphical user interface HAL Hardware abstraction layer IDE Integrated development environment UM2220 Acr...

Page 3: ...fusion accelerometer gyroscope magnetometer real time 6 axis motion sensor data fusion accelerometer gyroscope computation of rotation quaternions gravity and linear acceleration data gyroscope bias...

Page 4: ...ation experiences low magnetic interference 50 T most of time it is recommended to increase the value LMode represents the gyroscope bias learning mode the library automatically tracks and calibrates...

Page 5: ...The MotionFX_propagate and the MotionFX_update MotionFX_CM0P_update functions provide the sensor fusion output in the MFX_output_t MFX_CM0P_output_t structure rotation_6 9X represents the system orie...

Page 6: ...eter hard iron calibration The magnetometer is affected by the hard iron and soft iron phenomena described below Hard iron distortion Hard iron distortion is normally generated by ferromagnetic materi...

Page 7: ...eturns mag_data_out cal_quality MFX_MAGCALGOOD or MFX_CM0P_MAGCALGOOD the calibration was successfully performed 4 apply calibration results MAG_Calibrated AXIS_X MAG_Value AXIS_X MAG_Offset AXIS_X MA...

Page 8: ...MFX_engine_state_t MotionFX_getStatus_9X void gets the 9 axes library status returns 1 if enabled 0 if disabled void MotionFX_enable_6X MFX_engine_state_t enable enables or disables the 6 axes functio...

Page 9: ...re type MFX_MagCal_output_t are hi_bias is the hard iron offset array T 50 cal_quality is the calibration quality factor MFX_MAGCALUNKNOWN 0 accuracy of the calibration parameters is unknown MFX_MAGCA...

Page 10: ...Status_euler void gets the status of euler angles calculation returns 1 if enabled 0 if disabled MFX_CM0P_engine_state_t MotionFX_CM0P__getStatus_gbias void gets the status of gyroscope calibration re...

Page 11: ...alibration algorithm data_in parameter is a pointer to input data structure the parameters for the structure type MFX_MagCal_CM0P_input_t are mag is uncalibrated magnetometer data T 50 void MotionFX_C...

Page 12: ...Outputs Data Read Interrupt Initialize MagCal_Init getKnobs setKnobs Propagate MagCal_run MagCal_getParams 2 2 9 Demo code The following demonstration code reads data from the accelerometer gyroscope...

Page 13: ...onFX_enable_9X MFX_ENGINE_ENABLE Using Sensor Fusion algorithm Timer_OR_DataRate_Interrupt_Handler MFX_input_t data_in MFX_output_t data_out float dT float q Quaternion pointer to either to Game Rotat...

Page 14: ...ed time s algorithm Cortex M0 STM32L073RZ at 32 MHz Cortex M7 STM32F767ZI at 96 MHz STM32CubeIDE 1 2 0 IAR EWARM 8 32 3 Keil Vision 5 27 STM32CubeIDE 1 2 0 IAR EWARM 8 32 3 Keil Vision 5 27 Min Avg Ma...

Page 15: ...LEO L476RG NUCLEO L152RE To calibrate the magnetometer perform the figure 8 calibration movement Note You can calibrate the magnetometer only when the sensor fusion is activated When user button B1 is...

Page 16: ...it is automatically detected and the appropriate COM port is opened Figure 5 Unicleo main window Step 3 Start and stop data streaming by using the appropriate buttons on the vertical tool bar The data...

Page 17: ...d application window To switch between 9 axes and 6 axes sensor fusion click on the appropriate button To align the tea pot position point the Nucleo board towards the screen and press Reset model but...

Page 18: ...tep 5 Click on the Scatter Plot icon to check the magnetometer calibration quality Figure 8 Magnetometer scatter plot properly calibrated magnetometer UM2220 Sample application UM2220 Rev 7 page 18 24...

Page 19: ...ding button Figure 9 Datalog window 2 4 References All of the following resources are freely available on www st com 1 UM1859 Getting started with the X CUBE MEMS1 motion MEMS and environmental sensor...

Page 20: ...and ARM Cortex M0 01 Oct 2018 5 Removed references to X NUCLEO IKS01A1 throughout document Updated Section 2 2 4 MotionFX library parameters Table 2 Cortex M4 and Cortex M3 elapsed time s algorithm Fi...

Page 21: ...d 9 axis sensor fusion modes 3 2 2 3 MotionFX library operation 4 2 2 4 MotionFX library parameters 4 2 2 5 MotionFX library output data rate 6 2 2 6 Sensor calibration in the MotionFX library 6 2 2 7...

Page 22: ...List of acronyms 2 Table 2 Cortex M4 and Cortex M3 elapsed time s algorithm 14 Table 3 Cortex M0 and Cortex M7 elapsed time s algorithm 14 Table 4 Document revision history 20 UM2220 List of tables U...

Page 23: ...Figure 3 MotionFX API logic sequence 12 Figure 4 STM32 Nucleo LEDs button jumper 15 Figure 5 Unicleo main window 16 Figure 6 User Messages tab 16 Figure 7 Fusion window 17 Figure 8 Magnetometer scatte...

Page 24: ...ts and ST assumes no liability for application assistance or the design of Purchasers products No license express or implied to any intellectual property right is granted by ST herein Resale of ST pro...

Reviews: