cancel
Showing results for 
Search instead for 
Did you mean: 

Integrating and Using NanoEdge AI in STM32CubeIDE for N Classification

IsmailYangui
Associate III

Hello everyone,

I'm working on an N classification project using the NanoEdge AI library on an STM32 microcontroller. My databases contain data on diabetes (diabetic or non-diabetic) with 8 different parameters.

Objective: I want to integrate the deployed files (.a and .h) of NanoEdge AI into STM32CubeIDE and configure communication with a serial monitor (Serial Monitor). The idea is to capture inputs through the serial monitor, process them in STM32CubeIDE with the NanoEdge AI Studio classification model, and then display the classification results (diabetes or non-diabetes) on the serial monitor.

Need Help For:

Integration of NanoEdge AI deployed files into STM32CubeIDE.
Configuring serial communication to receive user input.
Processing input data and calling the NanoEdge AI classification model.
Displaying classification results (diabetes or non-diabetes) on the serial monitor.
Any assistance, advice, code samples or documentation would be greatly appreciated!

Thanks a lot for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
Julian E.
ST Employee

Hello @IsmailYangui,

Thank you for reaching out!

 

Create a STM32CubeIDE Project:

First, you need basic knowledge about STM32 embedded development to create a STM32CubeIDE project for your hardware. If you have never done that you can start here:

Getting started with STM32: STM32 step-by-step - stm32mcu

 

I can also suggest looking at the available data logger and the sensors in NanoEdge AI Studio, find something that suits you need, get the corresponding board and use the data logger code to create a project.

In the linked github, you will find the source code to read data of the selected sensor and send the data via serial: https://github.com/stm32-hotspot/stm32ai-nanoedge-datalogger/

Example: 

STEVAL-STWINKT1B with ISM330DHCX accelerometer (source code on the github)

JulianE_0-1721051328785.png

 

Once you have a CubeIDE project and you are able to send data coming from your sensor via serial, using a NanoEdge AI Library is pretty easy. The process is very similar for the 4 kinds of project in NanoEdge. Here I will explain the N class classification as it is your question:

 

NanoEdge AI Library Integration:

In the last step of NanoEdge, you will compile your library and get a .zip file that contains:

  • (Arduino file if compiling for an Arduino target)
  • A Docs folder with link to the documentation
  • A Emulator folder to run the model on your pc with command lines
  • knowledge.h:  the weight of the model
  • libneai.a: the compiled library
  • metadata.json
  • NanoEdgeAI.h: the function header and variable to use in your main.c

The files that you need are in bold.

 

Open your STM32CubeIDE project and import knowledge.h, libneai.a and NanoEdgeAI.h. Generally, you want to had knowledge.h and NanoEdge.h in the Inc folder and the libneai.a in the src folder. Like this:

JulianE_1-1721053678485.png

Make sure that the path to the libneai is set:

  • right click on your project > properties > Settings > MCU GCC Linker and add the path if not set

Screenshot from 2024-07-15 15-37-04.png

Once you have that, you can add the code in your main.c to use the NanoEdge Library:

Add the includes:

 

 

 

#include "NanoEdgeAI.h"
#include <knowledge.h>

 

 

 

 

Add the variable in comments at the end of the NanoEdgeAI.h file:

 

 

 

uint16_t id_class = 0; // Point to id class (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
        "class1",
        "class2",
        ....
};

 

 

 

 

Initialize the model:

 

 

 

neai_state = neai_classification_init(knowledge);
printf("Initialize NEAI library. NEAI init return: %d.\n",  neai_state);();

 

 

 

 

  • NEAI_OK: the library is working as expected
  • NEAI_INIT_FCT_NOT_CALLED: the learn or detect function has been called without running the init function before. Initialize your library.
  • NEAI_BOARD_ERROR: the board detected is not authorized. For instance, it may happen if you are trying to use a library (for instance obtained from the free version of NanoEdge AI Studio) with a non-supported board.
  • NEAI_KNOWLEDGE_BUFFER_ERROR: the knowledge loaded is not compatible with this library. Make sure that the knowledge being used is the one obtained with this exact library.
  • NEAI_NOT_ENOUGH_CALL_TO_LEARNING: this is a fail-safe to prevent users from running an insufficient (only one or a few) number of iterations of the learning function. Run more learning iterations.
  • NEAI_UNKNOWN_ERROR: there is an unknown error with the library.

It should return NEAI_OK.

 

Then to use the model and do the classification and print the detected class, here is what you need to do:

 

 

 

neai_state = neai_classification(input_user_buffer, output_class_buffer, &id_class);
// you can print id2class[id_class] to get the predicted class
// you can also look at the output_class_buffer to get the probability of your signal to be of each class

 

 

 

 

You can find the AI Libraries documentation here:

 

I hope it is clear, do not hesitate to ask any other question you may have!

 

Have a good day,

Julian

 


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

View solution in original post

6 REPLIES 6
Julian E.
ST Employee

Hello @IsmailYangui,

Thank you for reaching out!

 

Create a STM32CubeIDE Project:

First, you need basic knowledge about STM32 embedded development to create a STM32CubeIDE project for your hardware. If you have never done that you can start here:

Getting started with STM32: STM32 step-by-step - stm32mcu

 

I can also suggest looking at the available data logger and the sensors in NanoEdge AI Studio, find something that suits you need, get the corresponding board and use the data logger code to create a project.

In the linked github, you will find the source code to read data of the selected sensor and send the data via serial: https://github.com/stm32-hotspot/stm32ai-nanoedge-datalogger/

Example: 

STEVAL-STWINKT1B with ISM330DHCX accelerometer (source code on the github)

JulianE_0-1721051328785.png

 

Once you have a CubeIDE project and you are able to send data coming from your sensor via serial, using a NanoEdge AI Library is pretty easy. The process is very similar for the 4 kinds of project in NanoEdge. Here I will explain the N class classification as it is your question:

 

NanoEdge AI Library Integration:

In the last step of NanoEdge, you will compile your library and get a .zip file that contains:

  • (Arduino file if compiling for an Arduino target)
  • A Docs folder with link to the documentation
  • A Emulator folder to run the model on your pc with command lines
  • knowledge.h:  the weight of the model
  • libneai.a: the compiled library
  • metadata.json
  • NanoEdgeAI.h: the function header and variable to use in your main.c

The files that you need are in bold.

 

Open your STM32CubeIDE project and import knowledge.h, libneai.a and NanoEdgeAI.h. Generally, you want to had knowledge.h and NanoEdge.h in the Inc folder and the libneai.a in the src folder. Like this:

JulianE_1-1721053678485.png

Make sure that the path to the libneai is set:

  • right click on your project > properties > Settings > MCU GCC Linker and add the path if not set

Screenshot from 2024-07-15 15-37-04.png

Once you have that, you can add the code in your main.c to use the NanoEdge Library:

Add the includes:

 

 

 

#include "NanoEdgeAI.h"
#include <knowledge.h>

 

 

 

 

Add the variable in comments at the end of the NanoEdgeAI.h file:

 

 

 

uint16_t id_class = 0; // Point to id class (see argument of neai_classification fct)
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
const char *id2class[CLASS_NUMBER + 1] = { // Buffer for mapping class id to class name
	"unknown",
        "class1",
        "class2",
        ....
};

 

 

 

 

Initialize the model:

 

 

 

neai_state = neai_classification_init(knowledge);
printf("Initialize NEAI library. NEAI init return: %d.\n",  neai_state);();

 

 

 

 

  • NEAI_OK: the library is working as expected
  • NEAI_INIT_FCT_NOT_CALLED: the learn or detect function has been called without running the init function before. Initialize your library.
  • NEAI_BOARD_ERROR: the board detected is not authorized. For instance, it may happen if you are trying to use a library (for instance obtained from the free version of NanoEdge AI Studio) with a non-supported board.
  • NEAI_KNOWLEDGE_BUFFER_ERROR: the knowledge loaded is not compatible with this library. Make sure that the knowledge being used is the one obtained with this exact library.
  • NEAI_NOT_ENOUGH_CALL_TO_LEARNING: this is a fail-safe to prevent users from running an insufficient (only one or a few) number of iterations of the learning function. Run more learning iterations.
  • NEAI_UNKNOWN_ERROR: there is an unknown error with the library.

It should return NEAI_OK.

 

Then to use the model and do the classification and print the detected class, here is what you need to do:

 

 

 

neai_state = neai_classification(input_user_buffer, output_class_buffer, &id_class);
// you can print id2class[id_class] to get the predicted class
// you can also look at the output_class_buffer to get the probability of your signal to be of each class

 

 

 

 

You can find the AI Libraries documentation here:

 

I hope it is clear, do not hesitate to ask any other question you may have!

 

Have a good day,

Julian

 


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

hello . thank you for your help.

is this code correct?

IsmailYangui_0-1721636984262.png

 

Hello, 

 

I believe that your rx_buffer contains the value that you want to use for the classification:

What does your rx_buffer look like?

  • if it looks like this: "0,1, 2, 3, 4, 5, 6, 7", then the code for your sscanf should look like this:
sscanf("0,1,2,3,4,5,6,7", "%f,%f,%f,%f,%f,%f,%f,%f", &input_data[0],&input_data[1],&input_data[2],&input_data[3],&input_data[4],&input_data[5],&input_data[6],&input_data[7]);
  • If it looks like this: "0 1 2 3 4 5 6 7", then your sscanf should look like this:4
sscanf("0 1 2 3 4 5 6 7", "%f %f %f %f %f %f %f %f", &input_data[0],&input_data[1],&input_data[2],&input_data[3],&input_data[4],&input_data[5],&input_data[6],&input_data[7]);

 

You can put the rx_buffer directly in the sscanf.

 

Then, you also have an issue with the classification. The result of the classification is not diabetes_status (it only returns a neai_state, look at my previous message). The result of the classification is id_class, so you need to have something like this:

if (&id_class == 0){
    sprintf(result_msg, "Diabetes\r\n");
} else{
    sprintf(result_msg, "Non-diabetes\r\n");
}

 

Best regards,

Julian


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.

Hello, thank you very much for your support and help

i change my code like this :

this image for declaartion 

IsmailYangui_0-1721726053473.png

this for uart :

IsmailYangui_2-1721726201593.png

and this is the main code in boucle while(1) :

IsmailYangui_3-1721726265031.png

and this exemple for my data :

I have 8 axes and each axis has 32 values ​​in the line:

IsmailYangui_4-1721726540260.png

my biggest probleme now is how i put my data in the input_user_buffer and how i scan this input anf finnaly how in tera term i see is diabetes or non_diabetes

 

 

hello . i use this code 

 

IsmailYangui_0-1721741438916.png

 

 

IsmailYangui_1-1721741475463.png

 

 

IsmailYangui_2-1721741512369.png

but always the same display whatever the data

IsmailYangui_3-1721741629491.png

 

Hello,

 

I did make a mistake in my last message. The condition "&id_class == 0" is false. The id_class 0 correspond to unknown. the condition should test if the model is returning either 1 or 2 for the class Diabetes or Not diabetes:

if (&id_class == 1){
    sprintf(result_msg, "Diabetes\r\n");
} else{
    sprintf(result_msg, "Non-diabetes\r\n");
}

It should solve your issue.

 

If you have any other question related to NanoEdge, I will gladly help you but if your question is about UART, reading data, printing data or any related topics, I will ask you to first look online, there are plenty of content to explain these kinds of things. You also have chatGPT that is very good at handling basic topics like these and if you still struggle, then please create a new post on the right related ST community.

 

Have a good day!

Julian

 


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.