瑞萨单片机AI教程【五】模型文件移植

发布时间:2026/8/7 10:52:37
瑞萨单片机AI教程【五】模型文件移植 开发板官网www.edevkit.com模型文件移植移植模型到工程上一步生成的代码文件夹复制到工程“src/”文件夹下。文件里有“librai_edsp_f32_arm.a”库文件工程需要添加库配置路径才能使用。添加头文件路径点击工程右键选择“属性”-“C/C构建”-“设置”-“GNU ARM Cross C Compiler”-“include”。如下图。打开添加一条路径“${workspace_loc:/${ProjName}/src/mp}”选择当前工程目录“src/mp”。2.添加库文件路径接着选择“GNU ARM Cross C Linker”-“Libraries”如下图。在右下“Library search path (-L)”添加路径“${workspace_loc:/${ProjName}/src/mp}”。在右上“Libraries (-l)”添加库名称“rai_edsp_f32_arm”不要前缀“lib”和后缀“.a”。配置完成点击“应用并关闭”提示配置修改了点击“重建索引”完成。编译工程没有错误。模型内存需求设置打开“READMI.txt”,说明了当前模型的资源占用情况“Stack Usage”和“Heap Usage”特别注意需要的内存大小。 对应打开工程的“configuration.xml”FSP配置 - “BSP”-“RA Common”配置“Main Stack size”和“Heap Size”比上面需要的大即可。模型函数使用打开“example.c”文件有一个“int example_main(void)”函数这是示例函数展示如何使用模型。①使用随机数填入输入数据输入数据在训练模型时配置的是三个通道每个通道512个数据所以这里需要准备“static float inputData[mp_SAMPLES_PER_CHANNEL * mp_NUM_CHANNELS]”3*512大小数组。按ch0[0]~[n]ch1[0]~[n]ch2[0]~[n]顺序填入。②“mp_AICLASS AIC mp_predict(inputData)”填入输入数据开始模型训练返回值“AIC”就是三个动作的状态值。③“RealityAI_get_class_scores()”获取准确度分数。④打印结果。接下来使用真实数据修改输入真实数据。把随机数部分删除替换从函数输入三组数据。int example_main(float *ax, float *ay, float *az); #define APPLY_SOFTMAX 1 static float conf_scores[mp_NUM_CLASSES]; /* confidence scores */ static float inputData[mp_SAMPLES_PER_CHANNEL * mp_NUM_CHANNELS]; int retState 0xff; int example_main(float *ax, float *ay, float *az) { /* * Fetch next data input window from your device. * Sensor data is simulated in this loop and formatted as a single dimensional array in WC * format (width W, channels C). Multi-channel windows are constructed for single-event prediction. * * Expected format of sensor data stream: * ch0[0],ch0[1],...,ch0[N-1], ch1[0],ch1[1],...,ch1[N-1], ... where N is SAMPLES_PER_CHANNEL (Window Length) * REPLACE THIS BLOCK WITH INPUT FROM YOUR DEVICE */ for (size_t sample 0; sample mp_SAMPLES_PER_CHANNEL; sample) { for (size_t channel 0; channel mp_NUM_CHANNELS; channel) { if (channel 0) inputData[channel * mp_SAMPLES_PER_CHANNEL sample] ax[sample]; if (channel 1) inputData[channel * mp_SAMPLES_PER_CHANNEL sample] ay[sample]; if (channel 2) inputData[channel * mp_SAMPLES_PER_CHANNEL sample] az[sample]; } } /* Model prediction */ mp_AICLASS AIC mp_predict (inputData); /* Convert class score to confidence score (CS) */ RealityAI_get_class_scores (conf_scores, mp_NUM_CLASSES, APPLY_SOFTMAX, get_mp_model ()); /* Decode classification event using class names */ switch (AIC) { case (mp_no_results): //printf (Predicted Class: no_results\n); retState 0; break; case (mp_flat): //printf (Predicted Class: flat [%.2f]\n, conf_scores[0]); retState 1; break; case (mp_left): //printf (Predicted Class: left [%.2f]\n, conf_scores[1]); retState 2; break; case (mp_right): //printf (Predicted Class: right [%.2f]\n, conf_scores[2]); retState 3; break; }; return 0; }然后在读数据时调用。extern int example_main(float *ax, float *ay, float *az); #define bufSize 512 uint32_t counts 0; float accel[3], gyro[3], temp; float ax[bufSize], ay[bufSize], az[bufSize]; void read_mp6050_write_shipper(void) { MPU6050_ReadAll(accel, gyro, temp); //printf(Accel(g): %.2f, %.2f, %.2f | Gyro(dps): %.2f, %.2f, %.2f | Temp: %.2f C\r\n,accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2], temp); ax[counts] accel[0]; ay[counts] accel[1]; az[counts] accel[2]; counts; if(counts bufSize1) { counts0; //rai_opts_collector_write_shipper(ax, ay, az, bufSize); example_main(ax, ay, az); } }以上就可以测试数据。先平放-左倾斜-右倾斜-平放执行动作如图打印信息准确度值较好。AI Live Monitor视图以上移植完模型代码后查看测试结果可以和上面一样用串口打印查看Studio还有一个工具“AI live Monitor”也可以查看测试结果数据。在使用前需要添加一下代码需要把测试数据通过rai_data_shipper传输器发送给“AI Live Monitor”。打开视图界面点击①“Device Code Generation”展示了代码示例。意思就是在shipper传输数据时把模型数据赋值给shipper参数。在“第二章”“rai_opts.c”中修改。#include mp_model.h //数据采集传输函数采集到ax, ay, az数据后通过采集器写入通道在通过传输器发出。 void rai_opts_collector_write_shipper(float *datax, float *datay, float *dataz, uint32_t len) { RM_RAI_DATA_COLLECTOR_ChannelWrite (g_rai_data_collector0_ctrl, 0, datax, len); RM_RAI_DATA_COLLECTOR_ChannelWrite (g_rai_data_collector0_ctrl, 1, datay, len); RM_RAI_DATA_COLLECTOR_ChannelWrite (g_rai_data_collector0_ctrl, 2, dataz, len); struct rai_model_struct* p_model (struct rai_model_struct*)get_mp_model(); //定义数据传输的参数 rai_data_shipper_write_params_t arg; arg.diagnostic_data_len RealityAI_get_monitor_data_size(p_model); arg.events g_events; arg.p_diagnostic_data (uint8_t*)RealityAI_get_monitor_data(p_model); arg.p_sensor_data g_callback_args; g_dc_callback false; //将准备好的数据通过指定的方式发送出去 RM_RAI_DATA_SHIPPER_Write (g_rai_data_shipper0_ctrl, arg); }以上代码中标蓝色代码之前没有值现在获取模型的数据通过shipper发送出去。编译下载打开“AI Live Monitor”视图点击“Data connection”打开串口配置界面和前面数据录制一样的串口。连接数据后可以看到结果视图。