电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 成果帖|让STM32听懂电机振动:基于NanoEdgeAI的启停状态识别

共4条 1/1 1 跳转至页

成果帖|让STM32听懂电机振动:基于NanoEdgeAI的启停状态识别

菜鸟
2026-11-03 12:45:13   被打赏 40 分(兑奖)     打赏
成果帖|让 STM32 听懂电机振动:基于 NanoEdge AI 的启停状态识别

参加 Let's do 2026 年第 3 期“基于边缘 AI 的电机状态检测” 时,我使用 NUCLEO-C562RE、LIS3DH 三轴加速度计和 ULN2003 + CX28BYJ-48 减速步进电机,完成了“触摸启停—振动采集—数据整理—模型训练—板端识别”的完整流程。触摸模块负责切换电机状态,STM32 在运行时读取振动并输出 AI 判断,板载 LED 和串口提供直观的运行反馈。

一、系统框图

成果帖_系统框图.png

触摸模块的数字输出进入 STM32 外部中断;STM32 通过四路 GPIO 控制 ULN2003,驱动步进电机。固定在电机附近的 LIS3DH 将三轴加速度通过 I²C 送回主控。采集阶段的数据经 ST-LINK 虚拟串口送到 PC 整理和训练;部署后,NanoEdge AI 分类库直接在 STM32 上推理,不依赖电脑完成判断。

二、电路原理与接线

成果帖_接线原理图.png

功能NUCLEO-C562RE外设连接
加速度计 I²C1PB6、PB7、3V3、GNDLIS3DH SCL、SDA、VCC、GND
触摸启停PA10、3V3、GNDAT42QT1010 OUT、VDD、GND
电机四路驱动PC1、PB4、PB10、PA8ULN2003 IN1、IN2、IN3、IN4
电机供电5V、GNDULN2003 VCC、GND;电机接驱动板插座
灯光与串口PA5、PA2/PA3板载 LED;USART2 经 ST-LINK VCP 到 PC

驱动板、电机、传感器和触摸模块与开发板共地。接线完成后,先验证 LED 与串口,再逐个验证电机和传感器,可更快定位问题。

三、主要参数

项目设置
主控与工具链STM32C562RET6,Cortex-M33,GNU Arm GCC + CMake
串口USART2,经 ST-LINK VCP;115200 baud,8N1
加速度计LIS3DH,I²C1,±2 g,约 100 Hz,输出单位 mg
电机控制ULN2003 四路半步序列,半步间隔 10 ms
触摸输入PA10 外部中断,30 ms 电平确认
AI 输入三轴交错排列,每轴连续 64 点;每个窗口约 0.64 s
AI 输出停止 STOP、运行 RUN 两类

四、实现步骤

1. 跑通基础外设。使用 STM32Cube 生成的工程框架和 GCC 构建工程,配置 PA5 板载 LED 闪烁、USART2 串口输出、I²C1 读取 LIS3DH。电机部分按八相半步表依次驱动四个输入,触摸模块通过 PA10 外部中断控制启停。串口发送留在主循环,避免在中断里阻塞。

2. 采集并标记状态。LIS3DH 的三轴样本按 ACC_MG,状态,x,y,z 输出,其中状态 0 表示命令停止、1 表示命令运行。使用 scripts/capture_motor.ps1 保存串口原始记录,并在停止与运行两种状态下各采集足够长的稳定片段。

3. 整理 NanoEdge AI 数据。使用 scripts/prepare_neai.py 按状态切分日志,跳过每次切换后的前约 1 秒,再把每 64 组连续样本写成一行 CSV。每行共 192 个数,排列为 x0,y0,z0,x1,y1,z1,...,没有表头。训练集包含停止 110 个窗口、运行 115 个窗口;另留停止 22 个和运行 20 个窗口独立检查。

4. 训练并部署。在 NanoEdge AI Studio 中新建 Classification 项目,选择时间序列三轴加速度计,分别导入两类 CSV,运行 Benchmark 并导出面向 NUCLEO-C562RE 的 GCC 分类库。工程链接 libneai.a,初始化分类器后,每收满 64 组三轴样本就转换为浮点 mg 数组并调用推理函数。模型输入只有加速度,不读取电机控制状态。采集阶段可以开启原始 ACC_MG 串口输出;板端演示时关闭高频原始数据输出,保留电机状态和 AI 结果。

五、如何开启运行与效果演示

  1. 将 LIS3DH 固定在电机附近,检查 3.3 V、5 V 和公共地接线;通过 ST-LINK USB 给开发板上电。

  2. 打开 ST-LINK 虚拟串口(本机为 COM14),设置 115200、8N1。上电后可看到 MOTOR,STOP,板载 LED 闪烁。

  3. 触摸模块一次,电机启动,串口出现 MOTOR,RUN;再触摸一次,电机停止,出现 MOTOR,STOP。

  4. 保持两种状态片刻,观察周期输出的 AI,RUN 和 AI,STOP,即可展示模型在开发板上的启停识别。

导出库附带的 PC 模拟器在未参与训练的 42 个窗口上得到:停止 22/22、运行 20/20 分类正确。板端已完成模型链接、烧录和启停状态识别。这个项目的关键是让数据格式、实际采样顺序和板端推理输入保持一致:三轴按同一顺序交错排列,窗口固定为每轴 64 点。这样,采集到的振动数据才能直接用于训练和部署。

PixPin_2026-11-03_12-45-03.png



院士
2026-11-03 15:34:00     打赏
2楼

谢谢分享,学习了。


管理员
2026-11-10 00:29:22     打赏
3楼

成果表达清晰,NanoEdgeAI启停识别链路完整


菜鸟
2026-11-15 19:49:03     打赏
4楼
= ,
  ,
  ,
  touch_state_t;

BOARD_LED_PORT                    HAL_GPIOA
BOARD_LED_PIN                     HAL_GPIO_PIN_5
BOARD_UART_BAUD_RATE              BOARD_UART_TIMEOUT_MS             BOARD_LED_INTERVAL_MS             BOARD_ACCEL_INTERVAL_MS           BOARD_ACCEL_RETRY_MS              BOARD_I2C_TIMEOUT_MS              BOARD_I2C_TIMING_100KHZ           BOARD_MOTOR_HALF_STEP_MS          BOARD_TOUCH_CONFIRM_MS            BOARD_TOUCH_DIAG_INTERVAL_MS       BOARD_TOUCH_DIAGNOSTIC             BOARD_ACCEL_STREAM                 BOARD_AI_CONFIRM_WINDOWS           BOARD_TOUCH_PORT                  HAL_GPIOA
BOARD_TOUCH_PIN                   HAL_GPIO_PIN_10

BOARD_MOTOR_IN1_PORT              HAL_GPIOC
BOARD_MOTOR_IN1_PIN               HAL_GPIO_PIN_1
BOARD_MOTOR_IN2_PORT              HAL_GPIOB
BOARD_MOTOR_IN2_PIN               HAL_GPIO_PIN_4
BOARD_MOTOR_IN3_PORT              HAL_GPIOB
BOARD_MOTOR_IN3_PIN               HAL_GPIO_PIN_10
BOARD_MOTOR_IN4_PORT              HAL_GPIOA
BOARD_MOTOR_IN4_PIN               HAL_GPIO_PIN_8

LIS3DH_WHO_AM_I_REG               LIS3DH_WHO_AM_I_VALUE             LIS3DH_CTRL_REG1                  LIS3DH_CTRL_REG4                  LIS3DH_OUT_X_L                    LIS3DH_CTRL_REG1_100HZ_XYZ        LIS3DH_CTRL_REG4_BDU_HR_2G        NEAI_INPUT_SIGNAL_LENGTH != || NEAI_INPUT_AXIS_NUMBER != || NEAI_NUMBER_OF_CLASSES != hal_uart_handle_t uart2;
BOARD_TOUCH_DIAGNOSTIC == hal_i2c_handle_t i2c1;
hal_exti_handle_t touch_exti;
BOARD_TOUCH_DIAGNOSTIC == uint32_t lis3dh_address;
ai_signalNEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER;
uint16_t ai_sample_count;
uint8_t ai_ready;
int8_t ai_confirmed_class = -;
int8_t ai_candidate_class = -;
uint8_t ai_candidate_count;
uint8_t motor_phase_index;
uint32_t next_motor_step_ms;
uint8_t is_motor_running;
uint8_t motor_status_pending;
uint8_t touch_ready;
touch_state_t touch_state;
uint32_t touch_confirm_due_ms;
uint32_t touch_edge_count;
uint32_t touch_accept_count;
uint32_t touch_reject_count;

hal_status_t ;
hal_status_t ;
hal_status_t *message, uint32_t size_bytes;
BOARD_TOUCH_DIAGNOSTIC == hal_status_t ;
hal_status_t ;
hal_status_t int16_t acceleration_mg;
int16_t acceleration_mg;
hal_status_t ;
hal_status_t ;
hal_exti_handle_t *hexti, hal_exti_trigger_t trigger;
uint8_t coil_mask;
uint32_t now_ms;

hal_status_t hal_gpio_config_t config = .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ;

  ;
  BOARD_LED_PORT, BOARD_LED_PIN, &config;
hal_status_t hal_gpio_config_t pins = .= ,
    .= ,
    .= ,
    .= ,
    .= HAL_GPIO_AF7_USART2,
    .= ;
  hal_uart_config_t config = .= BOARD_UART_BAUD_RATE,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ;

  ;
  , HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3, &pins!= ;
  != ;
  ;

  &uart2, != ;
  &uart2, &config;
hal_status_t *message, uint32_t size_bytes&uart2, message, size_bytes, BOARD_UART_TIMEOUT_MS;
BOARD_TOUCH_DIAGNOSTIC == hal_status_t hal_gpio_config_t pins = .= ,
    .= ,
    .= ,
    .= ,
    .= HAL_GPIO_AF4_I2C1,
    .= ;
  hal_i2c_config_t config = .= BOARD_I2C_TIMING_100KHZ,
    .= ,
    .= ;

  ;
  , HAL_GPIO_PIN_6 | HAL_GPIO_PIN_7, &pins!= ;
  != ;
  != ;
  ;

  &i2c1, != ;
  &i2c1, &config;
hal_status_t uint32_t addresses= << , << ;
  uint8_t id;
  uint8_t value;

  uint32_t index = ; index < ; ++index&i2c1, addressesindex, LIS3DH_WHO_AM_I_REG,
                                , &id, , BOARD_I2C_TIMEOUT_MS== &&
        id == LIS3DH_WHO_AM_I_VALUElis3dh_address = addressesindex;
      value = LIS3DH_CTRL_REG4_BDU_HR_2G;
      &i2c1, lis3dh_address, LIS3DH_CTRL_REG4,
                                  , &value, , BOARD_I2C_TIMEOUT_MS!= ;
      value = LIS3DH_CTRL_REG1_100HZ_XYZ;
      &i2c1, lis3dh_address, LIS3DH_CTRL_REG1,
                                     , &value, , BOARD_I2C_TIMEOUT_MS;
    ;
hal_status_t int16_t acceleration_mguint8_t raw;
  hal_status_t status = &i2c1, lis3dh_address,
                                                     LIS3DH_OUT_X_L | ,
                                                     , raw,
                                                     raw, BOARD_I2C_TIMEOUT_MS;
  status != status;
  uint32_t axis = ; axis < ; ++axisint16_t value = int16_tuint16_traw* axis|
                                    uint16_traw* axis + << ;
    acceleration_mgaxis= value / ;
  ;
int16_t acceleration_mgai_ready == ;
  uint32_t offset = uint32_tai_sample_count * NEAI_INPUT_AXIS_NUMBER;
  uint32_t axis = ; axis < NEAI_INPUT_AXIS_NUMBER; ++axisai_signaloffset + axis= acceleration_mgaxis;
  ++ai_sample_count;
  ai_sample_count < NEAI_INPUT_SIGNAL_LENGTH;
  ai_sample_count = ;
  probabilitiesNEAI_NUMBER_OF_CLASSES;
  class_id = -;
  neai_state status = ai_signal, probabilities, &class_id;
  status != ai_candidate_count = ;
    , - ;
    ;
  class_id != && class_id != ai_candidate_count = ;
    , - ;
    ;
  class_id != ai_confirmed_classclass_id != ai_candidate_classai_candidate_class = int8_tclass_id;
      ai_candidate_count = ;
    ++ai_candidate_count >= BOARD_AI_CONFIRM_WINDOWSai_confirmed_class = int8_tclass_id;
      ai_candidate_count = ;
    ai_candidate_count = ;
  ai_candidate_count == *label = ai_confirmed_class == ? : ;
    label, ai_confirmed_class == ? - :
                                                        - ;
  hal_status_t hal_gpio_config_t config = .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ;

  ;
  ;
  ;

  BOARD_MOTOR_IN1_PORT, BOARD_MOTOR_IN1_PIN, &config!= ||
      BOARD_MOTOR_IN2_PORT, BOARD_MOTOR_IN2_PIN, &config!= ||
      BOARD_MOTOR_IN3_PORT, BOARD_MOTOR_IN3_PIN, &config!= ||
      BOARD_MOTOR_IN4_PORT, BOARD_MOTOR_IN4_PIN, &config!= ;
  ;
hal_status_t hal_gpio_config_t config = .= ,
    .= ,
    .= ,
    .= ,
    .= ,
    .= ;

  ;
  BOARD_TOUCH_PORT, BOARD_TOUCH_PIN, &config!= ;
  hal_exti_config_t exti_config = .= ,
    .= ;
  &touch_exti, != ||
      &touch_exti, &exti_config!= ||
      &touch_exti, != ;
  &touch_exti, ;
  touch_state = BOARD_TOUCH_PORT, BOARD_TOUCH_PIN== ?
                : ;
  NVIC_SetPriority, USE_HAL_TICK_INT_PRIORITY;
  NVIC_EnableIRQ;
  &touch_exti, ;
hal_exti_handle_t *hexti, hal_exti_trigger_t triggerhexti;
  touch_edge_count++;
  trigger == trigger = BOARD_TOUCH_PORT, BOARD_TOUCH_PIN== ?
              : ;
  trigger == touch_state == touch_confirm_due_ms = + BOARD_TOUCH_CONFIRM_MS;
      touch_state = ;
    touch_state == touch_state = ;
    trigger == && touch_state == touch_confirm_due_ms = + BOARD_TOUCH_CONFIRM_MS;
    touch_state = ;
  &touch_exti;
uint8_t coil_maskBOARD_MOTOR_IN1_PORT, BOARD_MOTOR_IN1_PIN,
                    coil_mask & ? : ;
  BOARD_MOTOR_IN2_PORT, BOARD_MOTOR_IN2_PIN,
                    coil_mask & ? : ;
  BOARD_MOTOR_IN3_PORT, BOARD_MOTOR_IN3_PIN,
                    coil_mask & ? : ;
  BOARD_MOTOR_IN4_PORT, BOARD_MOTOR_IN4_PIN,
                    coil_mask & ? : ;
uint32_t now_msuint8_t = , , , , , , , ;

  is_motor_running == || int32_tnow_ms - next_motor_step_ms< ;
  motor_phase_index;
  motor_phase_index = motor_phase_index + & ;
  next_motor_step_ms = now_ms + BOARD_MOTOR_HALF_STEP_MS;
uint32_t now_ms = ;

  touch_ready == ;
  touch_state == ||
       touch_state == &&
      int32_tnow_ms - touch_confirm_due_ms>= uint8_t touch_is_high =
      BOARD_TOUCH_PORT, BOARD_TOUCH_PIN== ? : ;

    touch_state == touch_state = touch_is_high == ? : ;
    touch_is_high != touch_state = ;
      touch_accept_count++;
      is_motor_running = !is_motor_running;
      is_motor_running != motor_phase_index = ;
        next_motor_step_ms = now_ms;
      ;
      motor_status_pending = ;
    touch_state = ;
      touch_reject_count++;
    now_ms;
uint32_t next_led_ms;
BOARD_TOUCH_DIAGNOSTIC == uint32_t next_accel_ms;
  uint32_t next_probe_ms;
BOARD_TOUCH_DIAGNOSTIC == uint8_t is_led_on = ;
BOARD_TOUCH_DIAGNOSTIC == uint8_t has_sensor = ;
  int16_t acceleration_mg;
BOARD_TOUCH_DIAGNOSTIC == || BOARD_ACCEL_STREAM == != -;
  != || != ||
      != ||
      != -;
  BOARD_TOUCH_DIAGNOSTIC == != -;
  ai_ready = == ? : ;
, - ;
BOARD_TOUCH_DIAGNOSTIC == ai_ready == , - ;
  BOARD_TOUCH_DIAGNOSTIC == next_led_ms = ;
BOARD_TOUCH_DIAGNOSTIC == next_accel_ms = ;
  next_probe_ms = ;
BOARD_TOUCH_DIAGNOSTIC == , - ;
  touch_ready = ;

  uint32_t now_ms = ;

    uint8_t has_motor_status;
    uint8_t motor_state_snapshot;
    ;
    has_motor_status = motor_status_pending;
    motor_status_pending = ;
    motor_state_snapshot = is_motor_running;
    ;

    has_motor_status != motor_state_snapshot != ? : ,
                          motor_state_snapshot != ? - :
                                                       - ;
    BOARD_TOUCH_DIAGNOSTIC == int32_tnow_ms - next_led_ms>= is_led_on = !is_led_on;
      BOARD_LED_PORT, BOARD_LED_PIN,
                        is_led_on ? : ;
      next_led_ms += BOARD_LED_INTERVAL_MS;
    BOARD_TOUCH_DIAGNOSTIC == has_sensor == && int32_tnow_ms - next_probe_ms>= == has_sensor = ;
        next_accel_ms = + BOARD_ACCEL_INTERVAL_MS;
        , - ;
      , - ;
      next_probe_ms = + BOARD_ACCEL_RETRY_MS;
    has_sensor != && int32_tnow_ms - next_accel_ms>= acceleration_mg== BOARD_ACCEL_STREAM == acceleration_mg;
      has_sensor = ;
        ai_sample_count = ;
        ai_confirmed_class = -;
        ai_candidate_class = -;
        ai_candidate_count = ;
        , - ;
      next_accel_ms += BOARD_ACCEL_INTERVAL_MS;
      int32_t- next_accel_ms>= next_accel_ms = + BOARD_ACCEL_INTERVAL_MS;



共4条 1/1 1 跳转至页

回复

匿名不能发帖!请先 [ 登陆 注册 ]