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

Revision as of 10:43, 1 July 2020 by Registered User
Under construction.png Delivery for this distribution is being prepared

In this guide, you will learn how to get started using the Function Pack for Predictive Maintenance FP-AI-NanoEdg1 and use it as your first project to detect anomalies in your setup using accelerometer data.

This Function Pack supports two STM32 boards, STWIN and STM32L562E. Following the steps provided in this page a users will be able to create the data logs for their setups, and generate EdgeAi libraries using Cartesiam NanoEdge AI Studio. Once these libraries are generated the user can use these libraries by plugging them into the provided FP and can learn the nominal behavior of his setup by issuing commands through a command line interface. After training phase the monitoring can be started and any anomalies from the normal behavior are reported on the CLI as well as on the LCD (in case of L5).

1. What you will learn

  • How to log the motion sensor data.
  • How to parse and read the motion sensor data.
  • How to generate the suitable libraries for your sample data set using Cartesiam NanoEdge AI Studio.
  • Learn and detect the anomalies and controlling the hyper parameters of learning and detection.

2. Requirements

2.1. Hardware

STWIN or STM32L562E

The STWIN (STEVAL-STWINKT1) and STM32L562E are two STM32 boards with environment sensors whose an accelerometer.

STLINK-V3MINI

The STLINK-V3MINI is a stand-alone debugging and programming mini probe for STM32 microcontrollers.

Micro SD card between 4GB and 16GB

2.2. Software

Install STM32 CubeIDE

Download STM32 CubeIDE from ST website. This software allows the development and the compilation of firmwares.

Optional: Install STM32 ST-LINK Utility (STSW-LINK004) or STM32CubeProgrammer

STM32 ST-LINK Utility and STM32CubeProgrammer allow the programming of targets and can be useful for some cases (example : download bineries on target or erase the flash of the target).

Install Tera Term

You can easily download Tera Term form the internet.

Install Cartesiam NanoEdgeAIStudio

You can get a pro licence with your company. For that contact support@cartesiam.com.

Otherwise, you can download NanoEdge AI Studio for free by filling out the following form: https://share.hsforms.com/1vVgNZ5JfQAWeJs10Yw6oHQ2a3c9

At this moment of the tutorial, if you want more information about this software see the NanoEdge AI Studio Documentation.

Install Jupyter notebook

Allows to execute python code.

Download or clone the FP-AI-NanoEdg1 Function Pack

Warning white.png Warning
Add the link from ST website for the download of FP-AI-NanoEdg1 once it's ready

Download the latest version of FP-AI-NanoEdg1 Function Pack from ST website or clone the project into your STM32CubeIDE workspace directory.

Command to clone FP-AI-NanoEdg1:

cd <userName>/STM32CubeIDE/workspace_<version>

git clone ssh://gitolite@codex.cro.st.com/ai-poc/STM32_PREDMNT1_TMP.git

3. Step 1 : Programming the STWIN with FP-AI-NanoEdg1

Warning white.png Warning
Same method for the L5?

In order to flash the STWIN with the FP-AI-NanoEdg1 you need to use the STLINK-V3MINI.

The connection steps are the following:

• Connect one side of the programming cable to STLINK-V3MINI and the other side to STWIN

File:Cabling STWIN step1.png
Cabling STWIN - Step 1

• Connect STWIN to a micro USB linked to your PC

File:Cabling STWIN step2.png
Cabling STWIN - Step 2

• Connect STLINK-V3MINI to a micro USB linked to your PC

File:Cabling STWIN step3.png
Cabling STWIN - Step 3

Once the cabling is done you have several options in order to flash the FP-AI-NanoEdg1 firmware inside the STWIN.

3.1. Option 1: Drag and drop the binary (if the binary is already generated)

Drag and drop the binary (.bin) located in:

FP_AI_NANOEDG1\Projects\<boardName>\Applications\NanoEdgeConsole\Binary

File:Drap and drop method.png
Drap and drop method

3.2. Option 2: Flash the binary with STM32 ST-LINK Utility (if the binary is already generated)

• Open STM32 ST-LINK Utility

• Click on Connect to the target

• Click on Program verify

• Browse the binary (.bin) located in:

FP_AI_NANOEDG1\Projects\<boardName>\Applications\NanoEdgeConsole\Binary

• Load the firmware with the Start button

3.3. Option 3: Compile the FP-AI-NanoEdg1 project with STM32CubeIDE

  • Open STM32 CubeIDE
  • Import a STM32CubeIDE project
    • Click on File -->Import
    • Select General --> Existing Projects into Workspace
    • Browse the STM32CubeIDE project according to the board you use :

STWIN : STM32_PREDMNT1_TMP\FP_AI_NANOEDG1\Projects\STM32L4R9ZI-STWIN STM32L562E : STM32_PREDMNT1_TMP\FP_AI_NANOEDG1\Projects\STM32L562E-DK

  • Click on Finish

If you have imported the two STM32CubeIDE projects you will get the following organization:

File:STM32CubeIDE organisation.png
STM32CubeIDE organisation
  • Click on Finish
  • Build and program the project on STWIN.
Info white.png Information
Make sure the STWIN is connected through STLINK and is powered.

4. Step 2 : How to configure the sensor and log the motion data

Once the STWIN is programmed, you can remove the STLINK-MINI and let only the STWIN connected to the PC. The data are logging inside a micro SD card.

4.1. Connection between the sensor and the Tera Term application

Once the micro SD card is installed at the correct localization, you can connect the STWIN through serial port to the Tera Term Application. For the configuration of the application follow these steps:

  • Open Tera Term
  • Open a new connection: File --> New connection
  • Select “Serial” and chose the port number relative to the STWIN (STMicroelectronics Virtual COM Port)
  • Click on OK
Info white.png Information
By default, the baud is 9600 but you can change it if it’s necessary (Configuration --> Serial port --> Speed).

You will see the following terminal with a $ which indicates you can write a command:

Info white.png Information
If you don’t have directly displayed the $, try to typ ‘enter’ inside the terminal or reset the board.

4.2. Sensor configurations with the CLI commands

The CLI proposes several commands. Some commands are explained just below.

  • Help

Allows to see all the possible commands.

$ help

  • Sensor info

Allows to get a list of all supported sensors by your boad and their ID.

$ sensor_info

For example, with the STWIN, you will get the following information about supported sensors and sub sensors:

Here, the command shows us that the STWIN has two sensors: IIS3DWB (ID = 0) and ISM330DHCX (ID = 1). For the predictive maintenance application, we collect data from an accelerometer. In that case, we can only use the accelerometer (ID = 0) from ISM330DHCX sensor (ID = 1).

  • Sensor get

Allows to get the value of a parameter for a sensor with sensor id. For example, with the STWIN, we can get the value of all the parameters of the accelerometer from ISM330DHCX sensor (ID = 1.0) with the following command:

$ sensor_get 1.0 all

Here we can see that the sub sensor is activated, has an ODR at 1667 Hz and a full scale at 16 mg.

  • Sensor set

Allows to set the required values of a parameter for the datalogging with sensor id. Example: with the STWIN, we can change the full scale value of the accelerometer from ISM330DHCX sensor (ID = 1.0) with the following command:

$ sensor_set 1.0 fullScale 4.0

  • Start datalog

Allows to start to logging of the data.

How to parse and read the motion sensor data. $ start datalog

Info white.png Information
Make sure an SD card is present inside the STWIN.
  • Stop

Allows to stop the logging of the data.

$ stop

Info white.png Information
A folder will be generated after each ‘’’start’’’ and ‘’’stop’’’ commands. Each folder must correspond to one specific condition: normal or abnormal. Normal and abnormal data logs are to be separated in separate folders. Various data logs are required to be created to expose the data for all the possible normal and abnormal conditions.

5. Step 3 : How to parse and read the motion sensor data

5.1. Take the data form the SD card

Once you have finish with the data collection, remove the micro SD card from the STWIN and see its capacity on your PC. If you made four recording, you will get something like the following example:

File:CapaciyMicroSD.png
Example of capacity on micro SD

Each data folder has the following organization:

STWIN_001/
├── DeviceConfig.json 
└── ISM330DHCX.dat
  • Json file

The json file corresponds to all configurations about the STWIN (sensor and sub sensor activated, ODR or full-scale values, …).

  • Dat file

The dat file corresponds to the X, Y, Z values of the accelerometer of the ISM330DHCX sensor. However, we can’t read directly the data so we need to parse the data in order to understand the meaning in a human way.

5.2. Parse

To parse the motion sensor data you need to use the python script called Parser_for_HS_Logged_Data.ipynb that you can execute with Jupyter notebook. The python script is available inside the function pack at the following path:

STM32_PREDMNT1_TMP\FP_AI_NANOEDG1\Utilities\AI_ressources\DataLog.

The python script has two parts:

  1. The data parser
  2. Prepare CSVs for NanoEdgeAI

The second part will be used after, for now we focus on only on the first part which contains the two following blocs:

File:DataParserBlocks.png
Data parser blocks (on Jupyter)

To parse your data follow these steps:

  • Put your data folder inside the Sample- Datalogs folder

The organization will be the following:

Data_log/
├── Sample-DataLogs
│   ├──STWIN_001
│        ├── DeviceConfig.json
│        ├── ISM330DHCX.dat
│   ├──STWIN_002
│        ├── DeviceConfig.json
│        ├── ISM330DHCX.dat
│   ├── ...
 
  • Chose one of the two blocs on Jupyter (part 1.1 or part 1.2)
  • Change the python script about the name of your data folders
  • Execute the code

The code generates an ISM330DHCX_ACC.csv file

Data_log/
├── Sample-DataLogs
│   ├──STWIN_001
│        ├── DeviceConfig.json
│        ├── ISM330DHCX.dat
│        ├── ISM330DHCX_ACC.csv
│   ├──STWIN_002
│        ├── DeviceConfig.json
│        ├── ISM330DHCX.dat
│        ├── ISM330DHCX_ACC.csv
│   ├── ...
 

6. Step 4: How to generate normal and abnormal databases

Now, we can gather all your normal data inside a same csv file and it’s the same for your abnormal data. To do so we will use the second part of the python script Parser_for_HS_Logged_Data.ipynb: Prepare CSVs for NanoEdgeAI which contains the two following blocs:

File:PrepareCSVsBlocks.png
Prepare CSVs blocks (on Jupyter)

To generate your two databases, follow these steps:

  • Put your csv data files inside the good folders (normal or abnormal)

The organization will be the following (for example) :

Data_log/
├── Sample-DataLogs
│   ├── Normal folder
│        ├──STWIN_001
│             ├── DeviceConfig.json
│             ├── ISM330DHCX.dat
│             ├── ISM330DHCX_ACC.csv
│         ├──STWIN_002
│   ├── Abnormal folder
│        ├──STWIN_003
│        ├──STWIN_004
 
  • Choose one of the two blocs on Jupyter (part 2.1 or part 2.2)
  • Change the python script about the name of your data folders
  • Execute the code

The code will generate two csv abnormalSegments.css and normalSegments.csv inside the Data log folder.

Data_log
├── abnormalSegments.csv
├── normalSegments.csv
 

7. Step 5: How to generate the suitable libraries for your sample data set using Cartesiam NanoEdge AI Studio

For this step you can refer to the part IV of the Cartesiam’s documentation or the Cartesiam’s tutorial YouTube.

Nevertheless, we will give you some precisions about some steps :

  • Create new project

In target select the microcontroller type according to the board your using. That is to say :

STWIN : ARM Cortex M4
L5 : ARM Cortex M33
  • Importing signal files

Select the csv files by the python script python script Parser_for_HS_Logged_Data.ipynb (see step 4):

normalSegments.csv as regular signals
abnormalSegments.csv as abnormal signals
  • Compiling and downloading the NanoEdge AI Library

In compilation flags don’t selection -mfloat-abi

File:Cartesiam1.png
Compilation flags option on NanoEdge

8. Step 7 : How to implement Cartesiam libraries in FP-AI-NanoEdg1

9. Step 8 : Learn and detect the anomalies and controlling the hyper parameters of learning and detection