This post was last edited by Hamster who doesn't like carrots on 2024-11-6 21:09
When we use nano edge ai software to train models, we need to provide sample data for training. In nano edge ai, there are two ways to upload sample data: CSV file prepared in advance and real-time acquisition through serial port. These two solutions can be selected according to your actual situation.
CSV: Suitable for scenarios where sample data is already available or it is inconvenient to collect data in real time. Then you need to fill in the data according to the specified file format and then upload it.
Serial port: The advantage of real-time acquisition through the serial port is that it can be collected on site, but it requires the microcontroller to be able to transmit the formatted data in real time through the serial port.
For the serial port method, we can choose to use the ready-made datalogger provided by ST, or we can choose to make our own
1. DataLogger provided by ST
There is a dataLogeer button in the upper left corner of the nano edge ai interface
Click to see what development boards ST provides for you
The number is still quite considerable, with nearly 20 species
We can select any development board and choose the sensor and configuration parameters to collect data.
When generated you will get a compressed package
This bin can be used directly after burning. Because I don't have the corresponding development board, I can't give you a trial
If you want to get the source code, ST also provides it. You can get the source code in the following place
2. Make your own dataLogger
As mentioned above, I don’t have a corresponding development board, and this time we are evaluating the H7R/S development board, so I will use the H7R/S development board to make a dataLogger.
Before writing code, we need to understand what function dataLogger implements, or what format of data it needs to send. The software data description interface is as follows
In the interface, ST has actually simply described the data content he needs, which is very vivid. We need to collect many rounds of data, corresponding to the lines in the interface. Then in each round of data collection, data needs to be sampled at a certain frequency, and each round of sampling can collect up to 256 times.
For example, I am collecting accelerometer data here, and all three-axis data are collected, collecting M rounds, sampling N+1 times each round, then the final result is
line1 (first round of sampling): ACC_X_0 ACC_Y_0 ACC_Z_0 ACC_X_1 ACC_Y_1 ACC_Z_1 ...... ACC_X_N ACC_Y_N ACC_Z_N
. . . . . .
lineM (Mth round of sampling): . . . . . .
If you have other data, assuming that there is only one data per sampling, assuming that it is the acceleration of one axis, then each round of data is as follows
lineX: ACC_X_0 ACC_X_1... ACC_X_N
This is what is finally presented in the software. So what is the format of the data uploaded on our development board? What protocol is there? And how to distinguish which characters are this data and which characters are that data? How to distinguish each round of data? This part is not shown in detail on this page.
The following are the results of my exploration. There is no formal protocol for data transmission, only some simple data formats, and printing is sent in the form of text: [data][separator][data][separator]. . . . . [data][/r/n][data][separator]…
The number of data for each sampling is set when the nano dege ai project is configured, and the software knows it. Then each data is distinguished by a separator. There are many choices for separators, such as commas, spaces, etc. The details are explained later. Each round of data is distinguished by line feeds and carriage returns (/r/n are required, otherwise the recognition will be abnormal)
Now that I have sorted out my thoughts, I will start to modify the code. My code follows the project of transplanting the X-NUCLEO-IKS4A1 MEMS sensor from the last [STM32H7R/S] evaluation.
First we need to define some variables
#define ACC_SAMPLE_MAX 64
#define AXIS 3
static int acc_sample_buffer[AXIS * ACC_SAMPLE_MAX] = {0};
We will store the sampled data in this buffer and then print it out uniformly
Next, modify while1. Since we only use one sensor on the development board, the previous for loop can be discarded and directly changed to the following
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
#if 1
Accelero_Sensor_Handler(0);
HAL_Delay(100);
#endif
}
/* USER CODE END 3 */
Directly call the ACC sensor processing function, with the input parameter 0, because there is only one sensor, and the first sensor's serial number in the array is 0. The delay function is to leave enough time for printing (I don't know why, after I switched back to this branch, the printf that was originally called suddenly didn't work, which was very strange. I could only use the HAL_UART_Transmit function, but it needs a delay, otherwise the data cannot be printed out)
Then we need to modify the Accelero_Sensor_Handler function. The original demo will print out a lot of messy data, but now it is not necessary. We need to complete the data collection in the for loop, and then print the data in the format mentioned before. The modified function is as follows
static void Accelero_Sensor_Handler(uint32_t Instance)
{
IKS4A1_MOTION_SENSOR_Axes_t acceleration;
uint16_t i = 0;
for (i = 0; i < ACC_SAMPLE_MAX;)
{
if (0 == IKS4A1_MOTION_SENSOR_GetAxes(Instance, MOTION_ACCELERO, &acceleration))
{
acc_sample_buffer[AXIS * i] = (int)acceleration.x;
acc_sample_buffer[(AXIS * i) + 1] = (int)acceleration.y;
acc_sample_buffer[(AXIS * i) + 2] = (int)acceleration.z;
i++;
}
}
unsigned char data[50] = {0};
uint8_t len = 0;
for(i = 0; i < ACC_SAMPLE_MAX - 1; i++)
{
sprintf(data, "%d,%d,%d,", acc_sample_buffer[i], acc_sample_buffer[i+1], acc_sample_buffer[i+2]);
len = strlen(data);
HAL_UART_Transmit(&huart4, data, len, 0xFFFF);
}
sprintf(data, "%d,%d,%d", acc_sample_buffer[i], acc_sample_buffer[i+1], acc_sample_buffer[i+2]);
len = strlen(data);
HAL_UART_Transmit(&huart4, data, len, 0xFFFF);
unsigned char data2[] = {"\r\n"};
len = strlen(data2);
HAL_UART_Transmit(&huart4, data2, len, 0xFFFF);
}
Then, in order to increase the serial port printing rate, I also changed the baud rate from the default 115200 to 921600 (I changed the code directly, and no longer modified it in cubemx. First, the code of this branch will not regenerate the project in the future, and regenerating it requires changing ld, which is too troublesome)
This is basically it. The code is a bit messy and it took too long to debug. Please forgive me, but the basic functions are OK.
After running, the SSCOM log is as follows:
3. Test function
Next, we will test the function in nano edge ai. The steps and instructions for creating a project in nano edge ai will be described in subsequent articles. Today, we will jump directly to the data collection step.
First you need to select upload data and use the serial port to upload data
When we select the serial port mode, the following interface will pop up
You need to select the serial port and baud rate of the development board
Then we can configure the maximum number of rounds of sampling data. If you do not select it, it will continue to collect data until you press stop. Otherwise, it will automatically stop when the maximum value is reached.
Finally, click start to start sampling. We can see the number of sampling rounds and the data obtained from each sampling in real time.
After the sampling is completed, click continue to parse the data, and then you can get the results of the data analysis, as shown in the following interface
On the left side of this interface, we can choose which delimiter to use. The first one is comma, which is also what I use. There is no need to change it. On the right side is the result of the analysis.
line_index is the number of sampling rounds.
nb_columns is the number of data, for example, the 3-axis data written in my code * sampling 64 times = 192 data
If the data is normal, it will be white. If the analysis is obviously abnormal, it will be red (if only one or two data are wrong, it may be a serial transmission error. If there are many, I suggest you take a good look at the original data to see if it meets the requirements. I used /n between each round at the beginning, and it was abnormal. The first 5 data were all marked red, and the rest were white, but the white data was obviously wrong).
We can select delete to delete abnormal data. We also need to look at the white data, because there may be some sampled data that is obviously abnormal, which requires manual filtering.
After checking the data, you can click import to load the data.
We will discuss how to further use this data in subsequent articles. Today, the production and use of this datalogger is successfully completed.