How to create a multi-state vibrations classifier using NanoEdge AI studio

This article is a tutorial on how to use NanoEdge™ AI Studio to create a multi-state vibrations classifier running on the STWIN SensorTile Wireless Industrial Node development kit (STEVAL-STWINKT1B).

This tutorial shows you how to program the STWIN for datalogging; how to use NanoEdge™ AI Studio to collect data, label data, find the best algorithm, run the Emulator, generate the library files for deployment; how to use STM32CubeIDE to modify codes, build the project, program the STWIN; and finally run the classifier on STWIN. The main process of this tutorial is shown in the flowchart below:

Flowchart of this article


1. Prerequisites

1.1. Hardware

  • An STEVAL-STWINKT1B development kit board,
  • An STLINK-V3MINI, which is included in the dev kit,
  • Two Micro-USB cables (one to connect the STWIN to PC, and another one for the STLINK-V3MINI),
  • A Windows® powered laptop/PC (Windows® 7, 8, or 10),
  • A multi-speed USB powered fan (can be ordered from this link), which has three speed mode: low, medium and high.

Hardware prerequisites for STWIN fan classifier

1.2. Software

1.2.1. Source Code

  • A firmware package stwin-fan-classifier-wiki-package.zip is ready to download from st.com, please download the zip file here. The package includes:
  • Project source code in subfolder STWIN_Fan_Classifier
  • Two Pre-compiled binary files in subfolder Binary:
  • STWIN_Fan_Classifier-datalog.bin for datalogging
  • STWIN_Fan_Classifier-detection.bin for detection
  • Library files generated by NanoEdge™ AI Studio in subfolder libneai_stwin-fan-classifier.zip

1.2.2. NanoEdge™ AI Studio

  • NanoEdge™ AI Studio is a new Machine Learning (ML) technology to bring true innovation easily to the end-users. In just a few steps, developers can create an optimal ML library for their project, based on a minimal amount of data.
  • Download NanoEdge™ AI Studio as a free trial version for the STMicroelectronics boards.
  • After registration, you will receive the license key in your registered email, which will be used to active NanoEdge™ AI Studio when you launch it for the first time.
Info white.png Information
  • If you have any issues during installation or activation process, please follow this Troubleshooting Guide to configure proxy or other settings.
  • If you need to run NanoEdge™ AI Studio on a virtual machine, please submit a request on our Online Support platform.
  • To ensure your request is quickly routed to the correct support team, please select Product / Application for Category, and NanoEdgeAIStudio for Product Category/Part Number.

1.2.3. STM32CubeIDE

  • STM32CubeIDE is an all-in-one multi-OS development tool, which is part of the STM32Cube software ecosystem. It is an advanced C/C++ development platform with peripheral configuration, code generation, code compilation, and debug features for STM32 microcontrollers and microprocessors.
  • Download STM32CubeIDE from st.com

1.2.4. Tera Term

  • Tera Term is an open-source and freely available software terminal emulator, which is used to display the classification results.
  • Users can download and install the latest version available from Tera Term.


2. Program STWIN for datalogging

The first step is to connect the STWIN to your PC and program it for datalogging mode with the provided binary file.

2.1. Board connection

Cable connection on STWIN

There are three connections to make on the STWIN, as shown in the picture:

  1. Connect the STDC14 cable between the STWIN and STLINK-V3MINI.
  2. Plug the USB cable micro-B side into the STWIN.
  3. Plug the USB cable micro-B side into the STLINK-V3MINI.
  4. Plug the STWIN USB cable into PC.
  5. Plug the STLINK-V3MINI USB cable into PC.

2.2. Program STWIN using binary file

The easiest way to program the STWIN is using the drag-and-drop. Once you have the pre-built binary file in your local disk, the STWIN can be very easily programmed with the binary by simply performing a drag-and-drop action:

Drag and drop to program STWIN for fan classifier datalogging
  1. In Windows File Explorer, find the binary file ..\stwin-fan-classifier-wiki-package\Binary\STWIN_Fan_Classifier-datalog.bin
  2. Left click on the file and hold the left button on your mouse
  3. Find the STLINK_V3M disk in the Navigation pane on the left hand side, drag the binary to that disk and release the left button
  4. When the transfer is done, the STWIN is programmed successfully
Info white.png Information
If the drag-and-drop method doesn't work, there are two other alternative methods to program the STWIN:

3. Create an n-Class Classification Project in NanoEdge™ AI Studio

In this step, we will create an n-Class classification project in NanoEdge™ AI Studio, and configure the project settings.

Open NanoEdge™ AI Studio on PC, you should see the welcome interface below and then follow the steps:

NanoEdge AI Studio welcome page
  1. Click on n-Class Classification to choose it as project type.
  2. Click on CREATE NEW PROJECT to create the project.

You should see the Project settings page as below:

Project configuration for STWIN fan classifier

  1. Give the project a name, e.g. STWIN-Fan-Classifier
  2. Add a brief description for the project.
  3. Choose STEVAL-STWINKT1B for Target.
  4. Choose Accelerometer 3 axes for Sensor type.
  5. Leave the Max RAM as default.
  6. Choose No Flash limit
  7. Finally, click on SAVE & NEXT to go to the next step.

4. Datalogging in NanoEdge™ AI Studio

In this step, we will alter the speed of the fan by pressing the button on the back, as well as applying clog to the fan to collect data for seven (7) classes: stop, low speed, medium speed, high speed, low speed clogged, medium speed clogged and high speed clogged.

4.1. Hardware Setup before datalogging

Before starting datalogging, it is recommended to remove the STLINK-V3MINI from STWIN and power cycle the board.

  1. Remove the STDC14 cable from STWIN to disconnect STLINK-V3MINI, and unplug STLINK-V3MINI from PC.
  2. Reconnect the USB cable on STWIN to reset the board. The orange LED should blink when running in datalogging mode.
  3. Stick the STWIN on the top of the fan with double-sided tape (or simply use normal tape to make a ring as double-sided tape)
  4. Plug the USB cable of the fan into PC or USB charger. Leave the fan in stop mode.
  5. The final hardware setup is shown below
STWIN attached at the top of USB fan

4.2. Start Datalogging

Next, we will use the programmed STWIN and NanoEdge™ AI Studio to collect the data for each class via USB streaming. Go back to NanoEdge™ AI Studio, once the project was created, you should see the window as shown below:

    main window for data logging in NanoEdge AI Studio
  1. Click on ADD SIGNAL to open the "Import signal" window.

  2. Select signal source from USB
  3. In the pop-up window, choose FROM SERIAL (USB) as Type of signal source.

  4. Configuration of datalogging via USB for STWIN fan classifier

  5. In the next window, select your Serial / COM port. Refresh if needed.
  6. Info white.png Information
    How to find correct COM port with Windows Device Manage

    COM port number varies on different PCs. In this example, it is COM13. You can go to Windows Device Manager to look for USB Serial Device under Ports (COM & LPT)

  7. Select 115200 for Baudrate
  8. Check the box to define Max lines of 100 signals. By doing this, the data logger will automatically stop when 100 lines of signal are collected.
  9. Give a class name, e.g. "stop", which will be used to shown as classification result later.
  10. Make sure the fan is initially turned off. Then click on the START/STOP button to start data logging. The real-time data and plotting will be shown.
  11. Datalogging for one class is finished for STWIN fan classifier

  12. Once 100 lines of signal are collected, the data logger will stop. click on CONTINUE to the next step.

  13. Preview collected data and finish import for one class

  14. You can preview the collected data. If no problem, click on IMPORT to finish datalogging for this class.
  15. Repeat step 1-9 to add all classes
    • Press the button on the back the fan to switch its speed between low, medium and high
    • Use a sticky note or a notebook (stick the sticky note or put the notebook closely at the front side of the fan) to clog the fan for clogged classes.
    • You should finally have 7 classes in total, including stop, low speed, medium speed, high speed, low speed clogged, medium speed clogged and high speed clogged. The final result is shown below and you are ready for next step.
All data for 7 classes are collected


5. Benchmarking in NanoEdge™ AI Studio

In this step, we will use NanoEdge™ AI Studio to quickly and automatically find the optimized algorithm for our use case.

After collecting the data for all classes, click on "Benchmark" in NanoEdge™ AI Studio. You should see the welcome interface as shown below:

    NanoEdge AI Studio benchmark
  1. Click on RUN NEW BENCHMARK
  2. A "Select your Class" window will pop up. Select All classes and click on START.
  3. Select all classes for benchmarking
  4. Then the Studio will start the benchmarking, you should see the window below:

  5. NanoEdge AI Studio benchmarking in progress for STWIN fan classifier
    1. This area indicates the benchmark status:
      • How many benchmarks have run
      • Number of libraries tested
      • Elapsed time
    2. This area shows the progress of the running benchmark, and its time stamp
    3. This area shows the performance indicators. The higher balanced accuracy and confidence level are better, while the lower RAM and Flash sizes are better.
    4. This small log window will display notable information and events such as benchmark status, search speed per thread, new libraries found, etc.
    5. This graph shows the evolution of the performance indicators over time, as thousands of candidate libraries are tested.
    6. This area contains control buttons:
      • You can pause the benchmark and resume later
      • You can stop the benchmark when the desired performance is reached
    Info white.png Information
    • Benchmarking can run for hours or even days, so you don't need to wait for it to completely finish. For a demo purpose, a 90% score is good enough, which will normally be achieved within 5 minutes.
    • Regarding the performance indicators, you can refer to this NanoEdge™ AI Studio wiki for more details.

  6. Once you are satisfied with the performance during the library selection process, click on the "STOP" button to end the benchmarking. The optimized algorithm will be selected for deployment later. You should see the details of this benchmark as below:
  7. NEAI benchmark result for STWIN fan classifier

6. Emulator in NanoEdge™ AI Studio

In this step, we will use the Emulator in NanoEdge™ AI Studio to verify the selected library before we generate and deploy the library files on STWIN.

After finish the benchmarking, click on "Emulator" in NanoEdge™ AI Studio to open the Emulator interface.

Initialize NEAI emulator for STWIN fan classifier
  • Click on the INITIALIZE EMULATOR button to open the Emulator, and click on FROM SERIAL(USB) tab. Then the process is similar to datalogging step as shown below:


NEAI emulator results for STWIN fan classifier
  • Select Serial data as we did in datalogging step.
  • Select the same COM port and Baudrate.
  • Get the fan ready in any mode, and click on the "START/STOP" button. The real-time classification result should be shown in the Live Repartition as blue bar.
  • Swtich between differnt speeds, and add clogs. Check the classification results.
  • Click on the "START/STOP" button to stop the emulation.
  • If the classification results are not accurate, you should go back to datalogging step to start over the datalogging and benchmarking process.
  • Otherwise you can go to the next step to generate the library files for deployment.
Info white.png Information
For more information about the Emulator, please refer to this NanoEdge™ AI Studio wiki

7. Validation in NanoEdge™ AI Studio

The validation screen is used to summarize any selected benchmark or to compare the metrics of two benchmarks. After finish testing in the Emulator, click on "Validation" in NanoEdge™ AI Studio to open the validation interface:

Validation step in NEAI
  1. You can switch between the summary page and the comparison page.
  2. You can choose one completed benchmarks to display its information
  3. You can export the validation report to pdf file for future reference
Info white.png Information
For more information about Validation, please refer to this NanoEdge™ AI Studio wiki

8. Deployment in NanoEdge™ AI Studio

In this step, we will generate the library files using NanoEdge™ AI Studio.

8.1. Generate library files in NanoEdge™ AI Studio

When you finish the Emulator, click on "Deployment" in NanoEdge™ AI Studio to open the Deploy interface for library generation.

NanoEdge AI Studio deploy step
  1. If you have multiple benchmarks, choose the one you want to compile. Otherwise, leave it default.
  2. Check the box for Float abi to enable the hardware FPU operations.
  3. Click on the COMPILE LIBRARY button, and there will be a pop-up window as shown below:


NanoEdge AI Studio compile version
  • Choose the DEMO VERSION by clicking on it, then there will be another pop-up window letting you to choose a local directory to save the libraries:
Save the compiled NEAI library to local disk
  • Locate to a local directory you prefer, rename the file as you want, then click on Save.
  • Go to the directory you chose, make sure the file libneai_*.zip is successfully saved there, extract the file, and you are ready for the next step.

8.2. Library files explanation

The NanoEdge AI static library for classification is the code that contains an AI model (for example, as a bundle of signal treatment, machine learning model, optimally tuned hyperparameters, etc.) designed to identify a sensor pattern in a class. All classes are defined by the user in NanoEdge™ AI Studio and are used during the training process of the AI model.

After the zip file is extracted, you should find these folders and files included:

NanoEdge AI Studio generated library files

There are three files we will need for next step: libneai.a, knowledge.h and NanoEdgeAI.h.

  • libneai.a is the C/C++ static library for the classifier.
  • knowledge.h is the header file that contains the knowledge buffer declaration.
  • NanoEdgeAI.h is the header file that contains function prototypes and sample declarations. An example of the code is shown below:
NanoEdgeAI.h content
Info white.png Information


9. Program STWIN for Detection

In this step, we will add the library files into the project, import the project into STM32CubeIDE, modify the source code, then program the STWIN for detection.

  1. Replace the old library files by the new ones
    • Go to the library folder, copy the file libneai.a
    • Go to the project folder ..\STWIN_Fan_Classifier\Core\Src, paste the file here, and click on Replace the file in the destination in the pop-up window.
    • Go back to the library folder, copy the files knowledge.h and NanoEdgeAI.h.
    • Go back to the project folder ..\STWIN_Fan_Classifier\Core\Inc, paste the files here, and click on Replace the files in the destination in the pop-up window.
  2. Import project into STM32CubeIDE workspace
    • Open STM32CubeIDE on your PC, create or choose a workspace and click Launch to launch your workspace.
    • In the menu bar, click File -> Import...
    • In the pop-up window, select Existing Projects into Workspace, and click Next, as shown below:
    Pop-up window when importing project in STM32CubeIDE

    • In the next interface, click Browse..., find the project folder where you saved and then click Select Folder, then click Finish and wait for the project to be imported, as shown in the picture below:
    Import existing project into STM32CubeIDE

  3. Build Project and Program the STWIN
    • In STM32CubeIDE, unfold the project in Project Explorer, find ..\Core\Src\main.c and ..\Core\Inc\NanoEdgeAI.h. Double click on the two files separately to open them in the Editor, as shown below:
    STM32CubeIDE project explorer for STWIN_Fan_Classifier

    • In main.c, go to line 48 to find #define NEAI_MODE. Change the value to "1" if it is not, as shown below:
    NanoEdge project main.c line 48

    • Then go to NanoEdgeAI.h, find the buffer declarations in the commented part underneath, select the codes of "buffer for mapping class id to class name" const char *id2class[CLASS_NUMBER + 1]. Right click and choose "Copy" or simply press Ctrl+C on the keyboard to copy the codes:

    Copy buffer declaration from NanoEdgeAI.h for STWIN fan classifier


    • Go back to main.c, paste the copied codes below float output_class_buffer[CLASS_NUMBER]; to replace the existing declarations. As shown below:

    Paste copied code to main.c for STWIN fan classifier


    • Press CTRL + S to save the changes.
    • Unplug STWIN from PC, reconnect the STLINK_V3MINI to STWIN with the STDC14 cable; connect STWIN to PC first, then connect STLINK-V3MINI to PC
    • Go back to STM32CubeIDE, click on the "Run" button in the Toolbar to build and program the project:
    STM32CubeIDE Run button

    Info white.png Information
    • When run the project for the first time, there will be an "Edit launch configuration properties" window pops up. Keep all settings default and click on OK to proceed.
    • If there's a pop-up window asking you a newer firmware version of ST-LINK is available, click Yes to open the Upgrader. In the Upgrader, click on Open in update mode and then click on Upgrade. Wait for upgrading to be done, then close the upgrader and click RUN again in STM32CubeIDE.


    • You can see the log information in the Console window. When the project is successfully built and programmed on the STWIN, you should see the following information:
    Download verified successfully
    
    Shutting down...
    Exit.
    
    • The green LED should blink when running in NEAI mode. Now you are ready for the final step - to run the classifier on STWIN.

10. Run the Classifier on STWIN

In this step, we will run the fan classifier on STWIN and use Tera Term to display the classification results.

  • Once the STWIN is successfully programmed for detection, you can open the Tera Term on PC.
  • In Tera Term's menu bar, click on Setup -> Terminal.... In the New-line setting, change Receive to AUTO, and Transmit to CR:
Change the New-line setting in Tera Term


  • Go back to Tera Term's menu bar, click on Setup -> Serial port.... The serial port setup window will pop up as shown below:
Tera Term Serial port setup for STWIN fan classifier
  • Select the same COM port as we did in datalogging step for datalogging.
  • Select the Baudrate value "115200" for Speed.
  • Leave other settings as default, and click on New open.
  • Get the fan ready and make sure STWIN is firmly attached at the top of the fan. The classification results will show in the interface, along with the probability of each class, as shown below:
Classification results in Tera Term for STWIN fan classifier

11. Resources