How to perform condition monitoring on STM32 using FP-AI-NANOEDG1

Revision as of 15:01, 12 January 2021 by Registered User (→‎Configure the sensor for data logging)
Under construction.png Delivery for this distribution is being prepared

The aim of this article is to provide a step-by-step guide for the readers to set up a vibration-based condition monitoring solution using STM32 sensor board. The anomaly detection AI libraries used in this tutorial will be generated using Cartesiam NanoEdge AI studio and the software used to program the sensor board can be downloaded from the ST website.

In this article you will learn:

  • setting up a motor control project,
  • programming the STWIN with FP-AI-NANOEDG1,
  • setting-up the sensor node and performing the data logging,
  • generating the AI libraries for condition monitoring, and
  • condition monitoring of the motor setup using vibration data.

1. Requirements

1.1. Hardware

The hardware required to reproduce this tutorial includes:

  • STEVAL-STWINKT1B
    The STWIN SensorTile wireless industrial node (STEVAL-STWINKT1B) is a development kit and reference design that simplifies prototyping and testing of advanced industrial IoT applications such as condition monitoring and predictive maintenance. For details visit the link.
stwinkt1b.jpg
  • STM32 P-NUCLEO-IHM03 Motor Control Nucleo Pack
    The P-NUCLEO-IHM03 STM32 motor-control pack is a kit composed of the X-NUCLEO-IHM16M1 board, the NUCLEO-G431RB board, a brushless Gimbal motor (GBM2804H-100T), and the DC power supply. For details visit the link.
ihm03Set.jpg
  • Misceleneous
stlink-v3mini.jpg
  • MicroSDTM card for the data logging operation,
microSDcard.jpg
  • 3 x Micro USB cables to connect the motor control board, STLINK-V3Mini and STWIN
microUSBCable.png
  • Plexiglass: to fix the motor, the X-NUCLEO-IHM16M1, and the NUCLEO-G431RB boards.
  • 20 cm x 15 cm
  • 9 cm x 15 cm
  • Clamps: to fix the whole set up to the table.
  • Screws: to fix the boards and the motor to the plexiglass base,
screws and nuts.png
  • Magnets and Metallic tape: to attach the STWIN on the motor setup.

1.2. Software

  • STM32 Motor Control Software Development Kit
    STM32 MC SDK (motor control software development kit) firmware (X-CUBE-MCSDK and X-CUBE-MCSDK-FUL) includes the Permanent-magnet synchronous motor (PMSM) firmware library (FOC control) and the STM32 Motor Control Workbench (to configure the FOC firmware library parameters), with its graphical user interface (GUI). For details visit link.
  • FP-AI-NANOEDG1
    An Artificial Intelligence (AI) condition monitoring function pack for STM32Cube provides a complete firmware to program an STM32L4+ sensor node for condition monitoring and predictive maintenance applications on the STEVALSTWINKT1B SensorTile wireless industrial node. The function pack can be downloaded from
  • One of the following IDEs
Info white.png Information
In this document all the steps presented are carried out with STM32CubeIDE but the same can also be done with the other two IDEs.
  • STM32 ST-LINK Utility
    A software interface for programming STM32 microcontrollers. This software is only required to install the prepared binaries provided in the function pack folder at the start. Note that this software will be used by not just users to install the already provided binaries but by STM32CubeIDE also to program the boards with the generated binaries.
    NOTE: This software can be downloaded from STSW-LINK004.
  • Tera Term
    Tera Term is an open-source and freely available software terminal emulator, which is used to host the CLI of the FP-AI-NANOEDG1 through a serial connection. Users can download and install the latest version available from Tera Term.
  • NanoEdge AI Studio
    The function pack is to be powered by Cartesiam. For this, the user needs to generate the libraries from the NanoEdge AI Studio and then embed these libraries in the FP-AI-NANOEDG1. The Studio can be downloaded from this https://cartesiam.ai/st-developpement-board-2/. Cartesiam provides a free evaluation version for the STMicroelectronics boards.

2. Motor set up

2.1. Hardware set up

The setup process includes:

  • A two-level plexiglass base, by fixing two plexiglass pieces with the help of the spacer screws.
  • Fix the motor using double-sided tape from the left side of the smaller board.
  • Fix the G431RB and the IHM16M1 board using the provided screws in the kit with the board.
  • Put the metallic strip in the center of the small board in order to attach the STWIN to the setup.
MotorWorkBench.png
Info white.png Information
For this demonstration, we have arranged the hardware as shown in the figure below. However, the readers can arrange the setup the way it suits them.

2.2. Project Defination

For creating the motor, the project has to be created using the STM32 Motor Control Software Development Kit or STM32-MCSDK for short. In this section, we show the steps to create the project and to program the motor control board with the FW in order to start, stop, or accelerate and decelerate the motor.

Step 1

The first step is to create a new project from the ST Motor Control Workbench. Here we are using version 5.4.4. To start the project click on the New Project icon, this will open a dialog as shown in the figure below.
motor control sdk define motor control project.png

Step 2 In the second step define the parameters for your motor control project. For example, for our project we are defining following parameters which fits our Nucleo-IHM03 Board kit.

  • Application type: Custom
  • System: Single Motor
  • Select Boards
    • Control: NUCELO-G431RB
    • Power: X-NUCLEO_IHM16M1-3Sh
  • Motor: GimBal GBM2804H-100T

2.3. Project Creation

Once the project is defined the reader will be presented with a screen will all the components of the project and the schematic diagram. Here the reader can start the creation of the project and code for the project definition provided in the first step. Press the download button in the toolbar and choose a location to save the project definitions as shown in the figure below. At pressing the save button the definitions of the project will be saved in the chosen location and a new dialog for C-code settings will be shown.

creatingProjectCodeForGimBalProject.png

2.3.1. C-Code Generation for Motor Control Project

To generate the C-Code for the motor control project the reader is presented with a dialog to define the settings. The process is very simple and straight forward. In the settings shown in the figure below we selected:

  • STM32CubeMX: 6.0.1
  • Target ToolChain: ST STM32CubeIDE
  • Firmware Package Version: STM32 FW 1.3.0.

and then press the generate button, which will start generating the C-Code in the same directory where the definition of the projects are generated.

motorControlSDKCodeGeneration.png

Once the C-Code generation process is finished the buttons for the UPDATE' and RUN STM32CubeMX are turned Blue from Gray and the text are changed for three buttons to, OPEN FOLDER, RUN STM32CubeMX, and CLOSE respectively. Clicking on the OPEN FOLDER button will open the directory containing the C-Code just generated for the project. The structure and contents for this folder are shown in the figure above.

2.3.2. Programming the G431RB to Control the Motor

importing gimbal project in IDE.png
building and flashing motor control project.png

2.4. Controlling the motor

Once the control board is programmed with the project, the motor is ready to be run. Clicking on the meter icon will open the motor monitor as shown in the figure below.

connecting to the motor.png

Once the right connection parameters are chosen the connection to the motor can be established by pressing the plug button. The steps to connection are:

  • Choice of port: Port 30 (in our case)
  • Baud Rate: 115200
  • Press the connect button.

Once connected the connect icon changes to a connection and tick icon as shown in the figure below. Now the motor can be started by pressing the start button and stopped by pressing the stop button as shown in the right pan of the figure below. By default, the motor is set to be started at 522 RPM, but the speed of the motor can be changed either by rotating the knob or by entering it manually in the text box below the knob. While the motor is running the current/measured speed is also shown in the two places in the monitor as shown in the figure below, i.e. under the speed meter in the main panel and in the left panel under the monitor.

running the motor.png

3. Sensor Node Setup

3.1. Get the function pack

The first step is to get the function pack. The FP-AI-NANOEDG1 can be downloaded from ST website. Once the pack is downloaded, the user can unpack/unzip it and copy the contents to a folder on the PC. The steps of the process along with the contents of the folder are shown in the following image.

installing cli app img1.png

3.2. Flashing the application on the sensor board STEVAL-STWINKT1B

Once the package has been downloaded and unpacked, the next step is to program the sensor node with the binary of the function pack. For the convenience of the users, the function pack is equipped with a pre-built binary file of the project. The sensor board can be very easily programmed with the provided binary by simply performing a drag-and-drop action as shown in the figure below.

installing cli app img2.png

3.3. Setting up the Console

Once the sensor board is programmed with the binary of the project (as shown in section 2), the next step is to set up the serial connection of the board with the PC through the Tera Term. To do so, start Tera Term, and create a new connection by either selecting from the toolbar, select the proper port to establish the serial communication with the board. In the figure below, this is COM10 - USB Serial Device (COM 10).

tera term connection port choice.png

Set the following parameters:

tera term connection new line.png

Once the connection is made the user will see a message like below. If this message does not appear, the user can try to reset the board, to make this message appear.

STWIN FP-AI-NANOEDG1 welcome message stubbed.png
Info white.png Information
NOTE: The message This is a stubbed version, please install Nanoe Edge AI library! shows that the FW is currently using a stub for an actual library and the user needs to generate a valid library using NanoEdgeTM AI Studio and replace the stubbed library with that. Once this is done this warning message will disappear.

Typing help will show the list of all available commands.

4. Condition Monitoring using an AI library generated with NanoEdge AI Studio

The process of condition monitoring using FP-AI-NANOEDG1 is described in the following diagram.

flow diagram.png

Readers have already seen how to program the sensor-board with FP-AI-NANOEDG1, let us see step by step in this section how to perform the rest of the steps.

4.1. Generating a Condition Monitoring Library

In this section, we perform the steps to generate a condition monitoring library with the help of NanoEdge AI Studio to monitor the motor running in a balanced condition. Assumptions:

  • In normal conditions, the motor should run at one of the three speeds [1000, 1250, 1500] RPM.
  • Motor running in unbalanced condition (unbalance introduced with a weight, i.e. a screw in the disc) or motor stopped or impacts or taps on the motor platform base are abnormalities and should be detected when running the condition monitoring phase.

4.1.1. Contextual Datalogging

NanoEdge AI studio requires the user to provide some contextual data in order to provide the best libraries which will fit the setup of the user to be monitored. This data is not used for the learning of the libraries but just the initialization and choosing of the library from a big pool of available solutions. This section describes the process of the sample data acquisition for normal and abnormal conditions.

4.1.1.1. Configure the sensor for data logging

Through the CLI interface, a user can configure the supported sensors on the sensor-board and can log the data with these sensors. In the ISM330DHCX sensor, there are multiple sub-sensors to log acceleration and gyrometer data. These sub-sensors can be individually configured. The configurable options for these sensors and sub sensors include:

  • Activate/deactivate the sensor or a sub-sensor,
  • Set an output data rate (ODR) from the list of available options,
  • Set full-scale range from the list of available options.

The list of all the supported sensors and sub-sensors can be displayed on the CLI console by entering the command:

$ sensor_info

This command will print the ids for the supported sensors with sub-sensors. The detailed information of a given supported sensor or sub-sensor can be shown by typing command:

$ sensor_get <sensor_id.sub-sensor_id> all

Similarly, the values for any of the available configurable parameters can be set through command

$ sensor_set <sensor_id.sub-sensor_id> <param> <val>
sensor configuration.png

In this article we will use the IIS3DWB sensor which is a high bandwidth accelerometer capable of logging the data at ODR (output data rate) of 26.6 kHz, but readers can perform the same procedure with the other sensor ISM330DHCX too, at there desired ODR (one of the supported ODRs).

Info white.png Information
MLC (Machine Learning Core) is not supported.

The first step is to configure the sensor. In order to do so, the user has to reset the sensor-board and set up the sensor IIS3DWB execute the following set of commands.

$ reset
-----------------------------------------------------------------
! This is a stubbed version, please install NanoEdge AI library!
-----------------------------------------------------------------
Console command server.
Type 'help' to view a list of registered commands.

$ sensor_set 0.0 enable 1
sensor 0.0: enable

$ sensor_set 0.0 fullScale 4
sensor fullScale: 4.000000

$ sensor_get 0.0 all
enable = true
ODR = 26667.000000 Hz, measured ODR = 0.000000 Hz
Availabe ODRs:
 26667.000000 Hz
fullScale = 4.000000 g
Available fullScales:
 2.000000 g
 4.000000 g
 8.000000 g
 16.000000 g
$
4.1.1.2. Normal Data Logging

The NanoEdge AI studio requires the use the provide the contextual data for both normal and abnormal conditions. In this section, we will record the normal data for the motor at three different speeds 1000, 1250, and 1500 RPM. NanoEdge AI studio requires only the contextual data, we do not need much data. CLI application for the FP-AI-NANOEDG1 also provides the option of the timed data log. In this example, we will acquire data for just 4 seconds for each condition. In order to do the data log, the reader needs to set the datalog timer the user can set the timer variable and confirm it is set as below:

$ datalog_set timer 4000
data log: timer set to 4000 ms

$ datalog_get timer
data log: timer = 4000 ms
$

The data log can be performed as below:

  1. Run the motor and set the Sensor_board on the motor base as shown in the figure of motor setup in section 2.
  2. Change the speed of the motor to the required speed.
  3. start datalog
  4. Wait for the datalog to stop and then repeat from steps 2 to 4 until all the three speeds are recorded.
4.1.1.3. Normal Data Logging

To record the abnormal data we will present the data for the motor running at three different speeds in the unbalance condition. To introduce the unbalance we have added a screw on the disc as shown in the figure below.

unbalanced wheel.png

Log the data for the three speeds for unbalanced conditions following the same steps described in the section above.

4.1.2. Data Preparation for NanoEdge AI Studio

The data logged through the datalogger is in the binary format and is not user readable in its current condition. To convert this data to a useful form, FP-AI-NANOEDG1 provides Python™ utility scripts on the path /FP-AI-NANOEDG1_V2.0.0/Utilities/AI-resources/DataLog/ as shown in the figure below.

contents of python utilities.png

The following calls to the python functions will convert the datalogs in STM32_DL_001, and STM32_DL_002 data directories to human-readable .csv files.

>> python STM_DataParser.py ./Sample-DataLogs/STM32_DL_001
>> python STM_DataParser.py ./Sample-DataLogs/STM32_DL_002 

Once the .csv files are generated for every data log, then the user needs to prepare the NanoEdge TM AI Studio compliant files. This can be achieved by issuing following python call.

>> python PrepareNEAIData.py ./Sample-DataLogs/STM32_DL_001/ ./Sample-DataLogs/STM32_DL_002/ -seqLength 1024 -sensorName IIS3DWB_ACC
Info white.png Information
NOTE: STM32_DL_001 will be used as normal data and STM32_DL_002 will be used as abnormal data.

The detailed examples for different settings of these calls and documentation are provided in the form of a Python™ Jupyter™ Notebook Parser_for_HS_Logged_Data.ipynb , in the same folder, as shown in the figure above.

4.1.3. Library generation with NanoEdge AI Studio

4.1.4. Linking the library with FP-AI-NANOEDG1

Once the libraries are generated and downloaded from Cartesiam NanoEdge AI Studio, the next step is to incorporate these libraries to FP-AI-NANOEDG1. The FP-AI-NANOEDG1, comes with the library stubs in the place of the actual libraries generated by NanoEdge AI Studio. This is done to make it easy for users to link the generated libraries and have a place holder for the libraries, which are generated by following the steps provided in the previous section. In order to link the actual libraries, the user needs to copy the generated libraries and replace the existing stub/dummy libraries and header files NanoEdgeAI.h, and libneai.a files present in the folders Inc, and lib, respectively. The relative paths of these folders are /FP_AI_NANOEDG1_V2.0.0/Middlewares/Third_Party/Cartesiam_NanoEdge_AI_Library/ as shown in the figure below.

linking libraries.png

Once these files are copied, the project must be reconstructed and programmed on the sensor board to link the libraries. For this, the user needs to open the .project file from the FP-AI-NANOEDG1 folder, located at the path /FP_AI_NANOEDG1_V2.0.0/Projects/STM32L4R9ZI-STWIN/Applications/NanoEdgeConcole/STM32CubeIDE/ as shown in the step 2 in figure below.

programming FW after linking libraries.png

To install the new firmware after linking the library, connect the sensor board, and rebuild the project using the play button. See the console for the outputs and wait for the Build and Download success message as shown in step 4 in the figure below.

programming FW after linking libraries 2.png

Once the sensor-board is programmed successfully, the following welcome message will appear in the CLI (Tera Term terminal). If the message does not appear you can try to reset the board by pressing the RESET button.

STWIN FP-AI-NANOEDG1 welcome message library.png
Info white.png Information
NOTE: As one can see the welcome message is now included with a message saying Powered by NanoEdge AI library! . This denotes that now the library stub has been replaced with an actual library generated using NanoEdgeTM AI Studio.

4.2. Condition monitoring

4.2.1. Learning the normal behavior

Once the STWIN is programmed with the FW with a valid library, the condition monitoring libraries are ready to be tested on the sensor board. The learning phase can be started by simply issuing a command start neai_learn in the CLI console. Starting this command will show the process of learning and will show a message on the console every time learning is performed on a new signal, as shown in the below snippet.

 
$ start neai_learn
NanoEdgeAI: starting

$ {"signal": 1, "status": success}
{"signal": 2, "status": success}
{"signal": 3, "status": success}
{"signal": 4, "status": success}
{"signal": 5, "status": success}
{"signal": 6, "status": success}
{"signal": 7, "status": success}
{"signal": 8, "status": success}
{"signal": 9, "status": success}

The process can be stopped either by issuing the

$ stop

command or by simply pressing the ESC key on the keyboard.

The learning can also be started by performing a long-press of the user button. Once the learning is happening, pressing the user button again will stop the learning process.

Info white.png Information
NOTE: For achieving the expected performances, the user should perform the learning using the same sensor configurations which were used during the data acquisition for the contextual data.

4.2.2. Condition Monitoring

Once the normal conditions are learned, the user can start the condition monitoring process by starting the neai_detect mode. This mode can be started by issuing the command

$ start neai_detect
NanoEdgeAI: starting

or by short pressing the user button on the sensor board. If the similarity of the current signal will be less than the threshold, the similarity will be printed on the CLI console showing the signal is an anomaly as shown in the snippet below. Nothing will be printed in the CLI console if the similarity is higher than 90% to the normal signals. Other than this the orange LED will blink fast (alternating 200 msec on-off sequence) to show the anomaly is detected.

4.2.3. Fine Tuning of Library

For the convenience of the users, the CLI application also provides some handy options to fine-tune the inference and learning process of the condition monitoring easily. Users can see all the variables they can play with by issuing the following command:

$ neai_get all
NanoEdgeAI: signals = 0
NanoEdgeAI: sensitivity = 1.000000
NanoEdgeAI: threshold = 95
NanoEdgeAI: timer = 0

Each of the these parameters can be configured using neai_set <param> <val> command. The details are provided in the command table above. This section provides small details on neai_set signals <nr of signals> . Using this command, a user can start the learning or condition monitoring mode for a given number of samples. For example, to learn on 100 signals, the user can issue this command, before issuing the learn command. In the following snippet, an example is presented where the learning is performed for 10 signals.

$ neai_set signals 10
NanoEdge AI: signals set to 10

$ start neai_learn
NanoEdgeAI: starting

$ {"signal": 1, "status": success}
{"signal": 2, "status": success}
{"signal": 3, "status": success}
{"signal": 4, "status": success}
{"signal": 5, "status": success}
{"signal": 6, "status": success}
{"signal": 7, "status": success}
{"signal": 8, "status": success}
{"signal": 9, "status": success}
{"signal": 10, "status": success}
NanoEdge AI: stopped

The threshold parameter is used to report the anomalies. For any signal which has similarities below the threshold value is reported as anomaly. The default value used in the CLI application is 90. Users can change this value by using neai_set threshold <val> command.

The sensitivity parameter is used as an emphasis parameter. The default value is set to 1. Increasing this sensitivity will mean that the matching of the signals is to be performed more strictly, reducing it will also reduce the matching calculation process, i.e. resulting in higher matching values.

Info white.png Information
Note: For good performance, the user is required to expose all the normal conditions to the sensor-board during the learning, and library generation process, i.e. in case of motor monitoring the required speeds and ramps that are required to be monitored should be exposed.

For details, users are invited to read the detailed documentation of NanoEdge AI studio.

5. Resources