Getting started with MotionTL tilt measurement library in X-CUBE-MEMS1
expansion for STM32Cube
Introduction
The MotionTL middleware library is part of the X-CUBE-MEMS1 software and runs on STM32. 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.
This library is intended to work with ST MEMS only.
The algorithm is provided in static library format and is designed to be used on STM32 microcontrollers based on the ARM
Cortex®-M0+, ARM® Cortex®-M3, ARM® Cortex®-M4 or ARM® Cortex®-M7 architecture.
It is built on top of STM32Cube software technology to ease portability across different STM32 microcontrollers.
The software comes with a sample implementation running on an X-NUCLEO-IKS01A2, X-NUCLEO-IKS01A3 or X-NUCLEO-
IKS02A1 expansion board on a NUCLEO-F401RE, NUCLEO-L476RG, NUCLEO-L152RE or NUCLEO-L073RZ development
board.
®
UM2277 - Rev 8 - April 2021
For further information contact your local STMicroelectronics sales office.
www.st.com
1Acronyms and abbreviations
Table 1. List of acronyms
AcronymDescription
APIApplication programming interface
BSPBoard support package
GUIGraphical user interface
HALHardware abstraction layer
IDEIntegrated development environment
UM2277
Acronyms and abbreviations
UM2277 - Rev 8
page 2/20
UM2277
MotionTL middleware library in X-CUBE-MEMS1 software expansion for STM32Cube
2MotionTL middleware library in X-CUBE-MEMS1 software expansion
for STM32Cube
2.1MotionTL overview
The MotionTL library expands the functionality of the X-CUBE-MEMS1 software.
The library acquires data from the accelerometer and provides real-time tilt information with multi-mode support
for 3-axis accelerometer. This library is suitable for static inclinometer where system acceleration is negligible,
such as in industrial applications, leveling, satellite antennas, solar panels, and automotive.
The library is also able to perform accelerometer 6-position calibration.
The library is designed for ST MEMS only. Functionality and performance when using other MEMS sensors are
not analyzed and can be significantly different from what described in the document.
A sample implementation is available for X-NUCLEO-IKS01A2, X-NUCLEO-IKS01A3 and X-NUCLEO-IKS02A1
expansion boards, mounted on a NUCLEO-F401RE, NUCLEO-L476RG, NUCLEO-L152RE or NUCLEO-L073RZ
development board.
2.2MotionTL library
Technical information fully describing the functions and parameters of the MotionTL APIs can be found in the
MotionTL_Package.chm compiled HTML file located in the Documentation folder.
2.2.1MotionTL library description
The MotionTL pedometer library manages the data acquired from the accelerometer; it features:
•calculation of pitch, roll and gravity inclination angles (Pitch-Roll-Gravity-Inclination mode)
•calculation of theta, psi and phi tilt angles (Theta-Psi-Phi mode)
–performs MotionTL library initialization and setup of the internal mechanism
–the CRC module in STM32 microcontroller (in RCC peripheral clock enable register) has to be enabled
before using the library
•mcu_type is the type of MCU:
–MFX_CM0P_MCU_STM32 is a standard STM32 MCU
–MFX_CM0P_MCU_BLUE_NRG1 is BlueNRG-1
–MFX_CM0P_MCU_BLUE_NRG2 is BlueNRG-2
–MFX_CM0P_MCU_BLUE_NRG_LP is BlueNRG-LP
•*acc_orientation is the reference system of the accelerometer raw data
UM2277 - Rev 8
page 3/20
Note:This function must be called before using the tilt library.
•void MotionTL_SetKnobs(MTL_knobs_t *knobs)
–sets the knobs
–the parameters for the structure type MTL_knobs_t are:
◦fullscale is the full scale of accelerometer (in g). It is recommended to set full scale >1g for
the sensor. A lower full scale can be selected if the tilt variation is limited and higher resolution is
required for the application.
◦k is the filtering coefficient. The range of k is [0.1 to ODR]. The lower value of k increases the
filtering and removes the noise. For systems with high vibration, it is recommended to reduce the
value of k.
◦orn[3] is the accelerometer 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)
(see Figure 1). The orn is defined to bring the sensor into the X-NUCLEO-IKS01A3 frame.
◦mode is the operational mode where:
•MODE_PITCH_ROLL_GRAVITY_INCLINATION enables angle representation in Euler
angles (Roll, Pitch and Phi) form
•MODE_THETA_PSI_PHI enables angle computation of theta, psi and phi angle which
measure the angle individually on each axis
Note:The API can be called after MotionTL_Initialize() but before MotionTL_Update()
UM2277
MotionTL library
•void MotionTL_GetKnobs(MTL_knobs_t *knobs)
–gets the knobs setting
–for the parameters for the structure type MTL_knobs_t refer to the MotionTL_SetKnobs() function
–this function is used to set the accelerometer data orientation
–configuration is usually performed immediately after the MotionTL_Initialize () function call
–*acc_orientation parameter is a pointer to a string of three characters indicating the direction of
each of the positive orientations of the reference frame used for 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 (x-North,
y-West, z-Up), so the string is: “nwu”.
UM2277 - Rev 8
page 5/20
Figure 1. Example of sensor orientations
UM2277
MotionTL library
2.2.3Orientation
The MotionTL library works with ENU orientation system, which means device X axis going to East, Y axis going
to North, Z axis going Up.
Any sensor orientation is internally transformed into device ENU orientation system. For this reason the sensor
orientation must be defined using MotionTL_SetOrientation_Acc() function. All the outputs (angels) are
then calculated relative to the ENU orientation system.
Figure 2. Device ENU orientation system
UM2277 - Rev 8
The MotionTL library has different types of outputs angles as detailed in the following tables.
page 6/20
MotionTL library
Table 2. Pitch, Roll and Gravity Inclination output angles
ValuePitchRollGravity Inclination
Formulaarctan2 (-Y/Z)arcsin (X)arccos (Z)
Range-180°, +180°-90°, +90°0, 180°
Description
Sign
Angle between Y axis and horizontal
plane.
Top edge of the device going towards
ground generates positive pitch.
Angle between X axis and horizontal
plane.
Left edge of the device going towards
ground generates positive roll.
/* Tilt API initialization function */
/* Note: Use MCU type according to target HW – in this example we use standard STM32 MCU */
MotionTL_Initialize(MTL_MCU_STM32, acc_orientation);
/* OPTIONAL */
/* Get library version */
lib_version_len = MotionTL_GetLibVersion(lib_version);
The MotionTL middleware can be easily manipulated to build user applications; a sample application is provided
in the Application folder.
It is designed to run on a NUCLEO-F401RE, NUCLEO-L476RG, NUCLEO-L152RE or NUCLEO-L073RZ
development board connected to an X-NUCLEO-IKS01A2, X-NUCLEO-IKS01A3 or X-NUCLEO-IKS02A1
expansion board.
The tilt sensing algorithm only uses data from the accelerometer. It detects and provides real-time information
about the tilt angles of the user device. The library is also able to perform accelerometer 6-position calibration if
required. The data can be displayed through a GUI.
A USB cable connection is required to monitor real-time data. This allows the user to display in real-time
calculated tilt angles, accelerometer data, time stamp and eventually other sensor data using the Unicleo-GUI.
After pressing the Tilt Calibrate button in the Unicleo-GUI, the user is asked to hold the device still in each of the
six positions while the calibration data is collected. Then the calibration parameters (offset and gain for all 3 axes)
are calculated and sent to the Unicleo-GUI.
Keil µVision 5.27
STM32CubeIDE
1.2.0
IAR EWARM
8.32.3
UM2277
Sample application
Keil µVision 5.27
2.4Unicleo-GUI application
The sample application uses the Windows Unicleo-GUI utility, which can be downloaded from www.st.com.
Step 1.Ensure that the necessary drivers are installed and the STM32 Nucleo board with appropriate
expansion board is connected to the PC.
Step 2.Launch the Unicleo-GUI application to open the main application window.
If an STM32 Nucleo board with supported firmware is connected to the PC, it is automatically detected
and the appropriate COM port is opened.
Figure 5. Unicleo main window
UM2277 - Rev 8
page 10/20
Unicleo-GUI application
Step 3.Start and stop data streaming by using the appropriate buttons on the vertical tool bar.
The data coming from the connected sensor can be viewed in the User Messages tab.
Figure 6. User Messages tab
UM2277
UM2277 - Rev 8
page 11/20
Unicleo-GUI application
Step 4.Click on the Motion Tilt icon in the vertical tool bar to open the dedicated application window.
Figure 7. Motion Tilt Sensing window 1
UM2277
You can switch between two angle modes. In the first mode the Pitch, Roll and Gravity Inclination
angles are displayed, in the second mode the Theta, Psi and Phi angles are displayed. The meaning of
the angles in the second mode is displayed right below each indicator:
Figure 8. Motion Tilt Sensing window 2
UM2277 - Rev 8
page 12/20
UM2277
Unicleo-GUI application
Step 5.Click on the Tilt Calibration icon in the vertical toolbar to open the dedicated application window
Figure 9. Motion Tilt calibration window 1
This window first shows currently used calibration values calculated and stored during the previous
calibration or default values if the calibration has never been performed.
You can start a new calibration by clicking the Start button:
a.put the device in the first calibration position as displayed in the picture that shows up (see
Figure 8. Motion Tilt Sensing window 2)
b.click Next and hold the device still until the picture changes to another calibration position
c.repeat these steps for all 6 calibration positions.
The information about the estimated time necessary for taking all calibration data in the current position
is also displayed. Once the last position is calibrated, the new calibration parameters are calculated
and displayed in the window (see Figure 7. Motion Tilt Sensing window 1).
UM2277 - Rev 8
page 13/20
Figure 10. Motion Tilt calibration window 2
UM2277
Unicleo-GUI application
Step 6.Click on the Datalog icon in the vertical tool bar to open the datalog configuration window:
you can select which sensor and activity data to save in files. You can start or stop saving by clicking
on the corresponding button.
You can also load the previously stored data.
Figure 11. Datalog window
UM2277 - Rev 8
page 14/20
3References
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 software
expansion for STM32Cube
2.UM1724: STM32 Nucleo-64 boards (MB1136)
3.UM2128: Getting started with Unicleo-GUI for motion MEMS and environmental sensor software expansion
for STM32Cube
UM2277
References
UM2277 - Rev 8
page 15/20
Revision history
DateVersion Changes
22-Sep-20171Initial release.
25-Jan-20182
21-Mar-20183Updated Introduction and Section 2.1 MotionTL overview.
23-Oct-20184
20-Nov-20185
21-Feb-20196
25-Mar-20207
07-Apr-20218
UM2277
Table 6. Document revision history
Added references to NUCLEO-L152RE development board and Section 2.2.5 Algorithm
performance.
Removed references to X-NUCLEO-IKS01A1 throughout document.
Updated Figure 1. Example of sensor orientations and Section 2.2.5 Demo code.
Added Section 2.2.3 Orientation.
Added Table 5. Cortex -M0+: elapsed time (µs) algorithm.
Added references to ARM
Updated Table 4. Cortex -M4 and Cortex-M3: elapsed time (µs) algorithm and Table 5. Cortex -M0+:
elapsed time (µs) algorithm.
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST
products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders. ST
products are sold pursuant to ST’s terms and conditions of sale in place at the time of order acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products 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 products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other product or service
names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.