在前两篇帖子中,我已经实现了电机控制与调速,以及加速度传感器获取三轴加速度信号,本文将基于NanoEdgeAI Studio,实现电机运行状态的分类,包括电机停止、慢速运行、中速运行、快速运行。
首先打开NanoEdgeAI Studio,在Project Settings界面选择开发板为NUCLEO-H533RE,时序信号为三轴加速度计

在上一篇帖子中,我将电机控制放入滴答定时器的中断回调函数中,实现了STM32H5在运行电机控制程序的同时还能兼顾加速度信号的获取
在Signals界面中,依次创建New Class以及Add Signal,用串口将三轴信息输入到NanoEdgeAI Studio,这里我将数据分为四分类,分别为stop、run_slow、run_medium、run_fast

在Benchmark界面对数据进行训练,NanoEdgeAI Studio会自行尝试多个模型进行训练,并综合得到准确率更高、占用MCU空间更小的模型

在Validation界面可以看到预设的多个模型以及每个模型的最终表现

选取评分最高的模型并在Deployment界面中将其导出

可以看到导出文件中的内容如下

其中最重要的分别为NanoEdgeAI.h和libneai.a,NanoEdgeAI.h为程序提供了相关接口,而libneai.a则是训练出来的相关参数数据,用于后续模型调用
在主函数中,使用加速度传感器获取三轴加速度值,接着调用ai接口实现分类结果的输出
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_ICACHE_Init();
MX_TIM6_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Initialize leds */
BSP_LED_Init(LED_GREEN);
/* Initialize USER push-button. */
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
/* Initialize COM1 port (115200, 8 data bits, 1 stop bit, no parity). */
BspCOMInit.BaudRate = 115200;
BspCOMInit.WordLength = COM_WORDLENGTH_8B;
BspCOMInit.StopBits = COM_STOPBITS_1;
BspCOMInit.Parity = COM_PARITY_NONE;
BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)
{
Error_Handler();
}
/* Infinite loop */
/* USER CODE BEGIN WHILE */
lis3dev.hi2c = &hi2c1;
lis3_status_t status = LIS3_Init(&lis3dev);
if (status != LIS3_OK)
{
char message[48];
int length = snprintf(message, sizeof(message), "# LIS3 init error: %d\r\n", (int)status);
if (length > 0 && (size_t)length < sizeof(message))
{
COM_Write(message, (uint16_t)length);
}
Error_Handler();
}
{
uint32_t primask = __get_PRIMASK();
__disable_irq();
StepMotor_Init();
motor_initialized = 1U;
__set_PRIMASK(primask);
}
{
enum neai_state ai_status = neai_classification_init();
if (ai_status != NEAI_OK)
{
char message[48];
int length = snprintf(message, sizeof(message), "# AI init error: %d\r\n", (int)ai_status);
if (length > 0 && (size_t)length < sizeof(message))
{
COM_Write(message, (uint16_t)length);
}
Error_Handler();
}
static const char header[] = "# AI classification: name (class=0..3)\r\n";
COM_Write(header, (uint16_t)(sizeof(header) - 1U));
}
uint32_t last_report = HAL_GetTick();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
uint32_t now = HAL_GetTick();
if ((uint32_t)(now - last_report) >= AI_REPORT_INTERVAL_MS)
{
int32_t x, y, z;
char message[64];
int length;
last_report = now;
status = LIS3_ReadAccelMG(&lis3dev, &x, &y, &z);
if (status == LIS3_OK)
{
int id_class = -1;
/* Preserve the existing mg units and axis order (x, y, z). */
input_signal[0] = (float)x;
input_signal[1] = (float)y;
input_signal[2] = (float)z;
enum neai_state ai_status = neai_classification(input_signal, probabilities, &id_class);
if (ai_status != NEAI_OK)
{
length = snprintf(message, sizeof(message), "# AI classification error: %d\r\n",
(int)ai_status);
}
else
{
const char *class_name = NULL;
if (id_class >= 0 && id_class < NEAI_NUMBER_OF_CLASSES)
{
class_name = neai_get_class_name(id_class);
}
if (class_name == NULL)
{
static const char error[] = "# AI invalid classification result\r\n";
COM_Write(error, (uint16_t)(sizeof(error) - 1U));
Error_Handler();
}
length = snprintf(message, sizeof(message), "AI: %s (class=%d)\r\n",
class_name, id_class);
}
}
else
{
length = snprintf(message, sizeof(message), "# LIS3 read error: %d\r\n", (int)status);
}
if (length <= 0 || (size_t)length >= sizeof(message))
{
Error_Handler();
}
COM_Write(message, (uint16_t)length);
}
}
/* USER CODE END 3 */
}实现效果如下,可以看到在电机停止时的判断准确率会比较高,而电机运行之后三种运行状态的运行状态的分类可能就不一定很准确了

完整工程代码如下
我要赚赏金
