2024-07-15 04:34 AM - last edited on 2024-07-23 02:39 AM by Andrew Neil
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.
Solved! Go to Solution.
2024-07-15 07:51 AM - edited 2024-07-16 12:06 AM
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)
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:
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:
Make sure that the path to the libneai is set:
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 expectedNEAI_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
2024-07-15 07:51 AM - edited 2024-07-16 12:06 AM
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)
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:
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:
Make sure that the path to the libneai is set:
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 expectedNEAI_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
2024-07-22 01:29 AM
hello . thank you for your help.
is this code correct?
2024-07-22 03:09 AM
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?
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]);
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
2024-07-23 02:25 AM
Hello, thank you very much for your support and help
i change my code like this :
this image for declaartion
this for uart :
and this is the main code in boucle while(1) :
and this exemple for my data :
I have 8 axes and each axis has 32 values in the line:
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
2024-07-23 06:34 AM
hello . i use this code
but always the same display whatever the data
2024-07-23 07:58 AM
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