电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 【let'sdo边缘AI电机状态检测】AI识别电机运行状态成果

共1条 1/1 1 跳转至页

【let'sdo边缘AI电机状态检测】AI识别电机运行状态成果

工程师
2026-11-16 23:26:09     打赏

        在进行了电机驱动以及三轴传感器驱动之后,就要开始进行AI模型的训练了,这里实际上是分为2个阶段,第一个阶段是进行模型的训练,设计到数据的采集,模型的训练,获取到边缘模型。第二个阶段就是部署。

        这里要介绍一下NanoEdge AI Studio的便捷程度了,不过NanoEdge AI Studio的使用实际上都需要进行授权验证的,会导致启动会有点慢。打开后我们直接可以进行相关训练工程的创建:

371e258a-7b48-496d-94d7-b843d5abc566.png

        选择适合的类型,这里选择的是Class,目前就支持上面这几种类型,分类模型数据比较经典的。

        用什么ST的开发板就选择什么:

b752f5e9-effd-4f55-8bab-eec9edab6108.png

        使用的是什么数据就选择什么数据源:

5761da7b-5ac8-4001-952e-44b46e29f13a.png

        支持还是挺多的,然后就是分类类别的选择,这里我们就只有停止和运行,所以只有2类:

ebe9c22c-5d9a-4e00-9ad9-2d8ddd3bc8e2.png

        然后就可以开始训练的,可以看到是根据二八原则进行的:

4576d71b-f7cc-4644-8aa2-0f777f80fe9c.png

        经过几个小时的训练就出现了比较合适的模型可以使用:

edce86d5-b2b4-4071-93ab-90d6da82835e.png

        会出现很多通过不同路径产生的模型,可以选择其中一个进行应用部署。

        接下来就来到了部署,实际上我们的数据量比较少,不过是模型文件还是用到的存储都非常有限,对于H533来说基本上没有什么负担,部署也很简单,实际上就是一个加密的库,怎么说呢,把和训练用的同源的数据给到边缘边缘模型就行,然后就会输出结果,根据置信度进行选择,部署很简单:

/* Includes --------------------------------------------------------------------*/
#include "NanoEdgeAI.h"
/* Private define --------------------------------------------------------------*/
/* Private variables defined by user -------------------------------------------*/
float input_signal[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER]; // Buffer of input values
float probabilities[NEAI_NUMBER_OF_CLASSES]; // Buffer of class probabilities
/* Private function prototypes defined by user ---------------------------------*/
/*
 * @brief Collect data process
 *
 * This function is defined by user, depends on applications and sensors
 *
 * @param input_signal: [in, out] buffer of sample values
 * @retval None
 * @note   If NEAI_INPUT_AXIS_NUMBER = 3 (cf NanoEdgeAI.h), the buffer must be
 *         ordered as follow:
 *         [x0 y0 z0 x1 y1 z1 ... xn yn zn], where xi, yi and zi
 *         are the values for x, y and z axes, n is equal to
 *         NEAI_INPUT_SIGNAL_LENGTH (cf NanoEdgeAI.h)
 */
void fill_buffer(float *input_signal)
{
    /* USER BEGIN */
    /* USER END */
}
/* -----------------------------------------------------------------------------*/
int main(void)
{
    /* Initialization ------------------------------------------------------------*/
    enum neai_state error_code = neai_classification_init();
    if (error_code != NEAI_OK) {
        /* Check the returned error code (cf NanoEdgeAI.h). */
    }

    /* Classification ------------------------------------------------------------*/
    int id_class = 0;
    while (1) {
        fill_buffer(input_signal);
        neai_classification(input_signal, probabilities, &id_class);
        /* USER BEGIN */
        /*
        * e.g.: Trigger functions depending on id_class
        * (print output class probabilities using probabilities[id_class],
        * print the name of the associated class using the getter function defined 
        * in NanoEdgeAI.h, neai_get_class_name(id_class),
        * blink LED, ring alarm, etc.).
        */
        /* USER END */
    }
}

        我们在这个的基础上加了一些串口打印的内容:

while (1)
  {
    /* Act on the commands that arrived on the serial port. */
    console_command_t command = console_command_poll();

    if (command == CONSOLE_CMD_START)
    {
      set_motor_running(true);
    }
    else if (command == CONSOLE_CMD_STOP)
    {
      set_motor_running(false);
    }

    /* Report a motor change requested from the button. */
    if (motor_request_pending)
    {
      motor_request_pending = false;

      console_report_line("[motor] %s\r\n",
                          (actuator_drive_state() == ACTUATOR_TURNING) ? "turning"
                                                                     : "stopped");
    }

    /* Keep the sensor FIFO drained. */
    (void)motion_sense_poll();

    /* Classify once per second and light the LED on the verdict. Every
     * verdict is printed, so the console shows the current decision of the
     * model as it comes. */
    if (edge_inference_update(&monitored_state, &monitored_score))
    {
      /* The green LED shows the verdict: lit while the motor turns, dark
       * while it is stopped. */
      if (monitored_state == EDGE_STATE_RUNNING)
      {
        BSP_LED_On(LED_GREEN);
      }
      else
      {
        BSP_LED_Off(LED_GREEN);
      }

      console_report_line("[  ai ] %s (%u%%)\r\n",
                          edge_inference_state_name(monitored_state),
                          (unsigned)monitored_score);
    }

    /* Advance the stepper phase table. */
    actuator_drive_poll();

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

    /* Short sleep so the main loop does not spin at full speed. The FIFO
     * holds 32 samples and fills up in 80 ms at 400 Hz, so a 1 ms sleep is
     * more than fast enough to keep it drained. */
    HAL_Delay(1U);
  }
  /* USER CODE END 3 */
}
        实际的效果可以看视频讲解:【AI识别电机运行状态成果】 https://www.bilibili.com/video/BV1GfsN6vErS/?share_source=copy_web&vd_source=2176e73645ba9710d1c29e12a1f03ada








关键词: 成果     边缘    

共1条 1/1 1 跳转至页

回复

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