From 15e6a6e1dc9deb94b62639c3602912673f0da689 Mon Sep 17 00:00:00 2001 From: Qiea <1310371422@qq.com> Date: Tue, 12 Nov 2024 10:59:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9Ase?= =?UTF-8?q?ndblock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 楠岃瘉閫氳繃 --- PORTING/CNN/cnn.c | 11 ++++++-- PORTING/CNN/cnn.h | 1 + PORTING/CNN/tools.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ PORTING/CNN/tools.h | 6 +++++ USER/FATFS.uvprojx | 5 ++++ USER/main.c | 13 ++++++--- 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 PORTING/CNN/tools.c create mode 100644 PORTING/CNN/tools.h diff --git a/PORTING/CNN/cnn.c b/PORTING/CNN/cnn.c index 0fbabe7..36b433e 100644 --- a/PORTING/CNN/cnn.c +++ b/PORTING/CNN/cnn.c @@ -278,7 +278,7 @@ void cnn_run(){ calculate_statistics(data,&value[0]); if (check_threshold(data,&value[0])){ float* _data = generateMatrix(data,&value[0]); - + send_blocks(_data); DEBUG_PRINTF("开始第一层\r\n"); //第一层:填充102 * 102 @@ -286,12 +286,14 @@ void cnn_run(){ float* conv_rlst_1 = convolution(conv1_weight,conv1_bias,expand_matrix_1, 102); float* pool_rslt_1 = pooling(conv1_weight, conv_rlst_1, 100); + { myfree(SRAMEX,_data); _data = NULL; myfree(SRAMEX,expand_matrix_1); expand_matrix_1 = NULL; myfree(SRAMEX,conv_rlst_1); conv_rlst_1 = NULL; + } DEBUG_PRINTF("开始第二层\r\n"); @@ -300,13 +302,14 @@ void cnn_run(){ float* conv_rlst_2 = convolution(conv2_weight,conv2_bias,expand_matrix_2, 52); float* pool_rslt_2 = pooling(conv2_weight, conv_rlst_2, 50); + { myfree(SRAMEX,pool_rslt_1); pool_rslt_1 = NULL; myfree(SRAMEX,expand_matrix_2); expand_matrix_2 = NULL; myfree(SRAMEX,conv_rlst_2); conv_rlst_2 = NULL; - + } DEBUG_PRINTF("开始第三层\r\n"); //第三层:填充 64 * 27 * 27 @@ -314,12 +317,14 @@ void cnn_run(){ float* conv_rlst_3 = convolution(conv3_weight,conv3_bias,expand_matrix_3, 27); float* pool_rslt_3 = pooling(conv3_weight, conv_rlst_3, 25); + { myfree(SRAMEX,pool_rslt_2); pool_rslt_2 = NULL; myfree(SRAMEX,expand_matrix_3); expand_matrix_3 = NULL; myfree(SRAMEX,conv_rlst_3); conv_rlst_3 = NULL; + } DEBUG_PRINTF("开始第四层\r\n"); float* affine1_rslt = hidden(pool_rslt_3); @@ -327,12 +332,14 @@ void cnn_run(){ DEBUG_PRINTF("开始第五层\r\n"); float* affine2_rslt = output(affine1_rslt); + { myfree(SRAMEX,pool_rslt_3); pool_rslt_3 = NULL; myfree(SRAMEX,affine1_rslt); affine1_rslt = NULL; myfree(SRAMEX,affine2_rslt); affine2_rslt = NULL; + } } else{ printf("未放电!最大值:%f 平均值:%f 标准差:%f\r\n",value[0],value[1],value[2]); diff --git a/PORTING/CNN/cnn.h b/PORTING/CNN/cnn.h index 255e0c9..1adacf7 100644 --- a/PORTING/CNN/cnn.h +++ b/PORTING/CNN/cnn.h @@ -4,6 +4,7 @@ #include "malloc.h" #include #include +#include "tools.h" void cnn_run(void); void _cnn_run(void); diff --git a/PORTING/CNN/tools.c b/PORTING/CNN/tools.c new file mode 100644 index 0000000..17d14ce --- /dev/null +++ b/PORTING/CNN/tools.c @@ -0,0 +1,64 @@ +#include "tools.h" + + + +void send_blocks(float* arr){ + char uuid_str[9]; + u32 time_stamp = HAL_GetTick(); + u32 random_part = rand(); + snprintf(uuid_str, 9, // 16 位 UUID(8 个字符 + 空终止符) + "%04lX%04lX", + (unsigned long)(time_stamp & 0xFFFF), + (unsigned long)(random_part & 0xFFFF)); + + // 按块处理 每个块的最大字符数(最多存储1000个浮点数的字符串) + char* block = (char*) mymalloc(SRAMEX,sizeof(char)*100*100); + memset(block, 0 ,sizeof(char)*100*100); + int block_index = 0; + int _total_len = 0; + int total_len = 0; + + for (int i = 0; i < 100; ++i) { + for (int j = 0; j < 100; ++j) { + int n = snprintf(block + block_index, 100 * 100 - block_index, "%.6f|", arr[i * 100 + j]); + block_index += n; + if (block_index >= 9000) { + block_index = 0; // 重置块 + _total_len++; + } + } + } + if (!block_index)_total_len--; + + block_index = 0; + for (int i = 0; i < 100; ++i) { + for (int j = 0; j < 100; ++j) { + int n = snprintf(block + block_index, 100*100 - block_index, "%.6f|", arr[i*100+j]); + block_index += n; + if (block_index >= 9000) { + char* _block = (char*) mymalloc(SRAMEX,sizeof(char)*100*100); + memset(_block, 0 ,sizeof(char)*100*100); + block_index = 0; // 重置块 + sprintf(_block, "{\"uuid\":\"%s\",\"Bid\":\"%d\",\"Eid\":\"%d\",\"Block\":\"%s\"}\n", uuid_str, total_len++, _total_len, block); + printf("%s", _block); + myfree(SRAMEX,_block); + _block = NULL; + } + } + } + + // 输出最后一个块 + if (block_index > 0) { + char* _block = (char*) mymalloc(SRAMEX,sizeof(char)*100*100); + memset(_block, 0 ,sizeof(char)*100*100); + block_index = 0; + sprintf(_block, "{\"uuid\":\"%s\",\"Bid\":\"%d\",\"Eid\":\"%d\",\"Block\":\"%s\"}\n", uuid_str, total_len, _total_len, block); + printf("%s", _block); + myfree(SRAMEX,_block); + _block = NULL; + } + + myfree(SRAMEX,block); + block = NULL; +} + diff --git a/PORTING/CNN/tools.h b/PORTING/CNN/tools.h new file mode 100644 index 0000000..c072e51 --- /dev/null +++ b/PORTING/CNN/tools.h @@ -0,0 +1,6 @@ +#include "cnn_model.h" +#include "debug.h" + + + +void send_blocks(float* arr); diff --git a/USER/FATFS.uvprojx b/USER/FATFS.uvprojx index 318b986..6f20acb 100644 --- a/USER/FATFS.uvprojx +++ b/USER/FATFS.uvprojx @@ -677,6 +677,11 @@ 1 ..\PORTING\CNN\cnn.c + + tools.c + 1 + ..\PORTING\CNN\tools.c + diff --git a/USER/main.c b/USER/main.c index b26faea..ca75b9a 100644 --- a/USER/main.c +++ b/USER/main.c @@ -18,6 +18,7 @@ #include "cnn_model.h" #include "debug.h" #include "cnn.h" +#include "tools.h" @@ -130,10 +131,14 @@ int main(void) LED0=!LED0; } - model_write("all"); - printf("鍒濆鍖栧畬鎴愶紒\r\n"); - run_dataset(); - SDRAM_USED(); +// model_write("all"); +// printf("鍒濆鍖栧畬鎴愶紒\r\n"); +// run_dataset(); +// SDRAM_USED(); + + model_switchdata("C1autosave00095_right_new_2"); + cnn_run(); + while(1){ if(isrun)cnn_run(); }