在进行了电机驱动以及三轴传感器驱动之后,就要开始进行AI模型的训练了,这里实际上是分为2个阶段,第一个阶段是进行模型的训练,设计到数据的采集,模型的训练,获取到边缘模型。第二个阶段就是部署。
这里要介绍一下NanoEdge AI Studio的便捷程度了,不过NanoEdge AI Studio的使用实际上都需要进行授权验证的,会导致启动会有点慢。打开后我们直接可以进行相关训练工程的创建:

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

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

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

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

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

接下来就来到了部署,实际上我们的数据量比较少,不过是模型文件还是用到的存储都非常有限,对于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
我要赚赏金
