本帖最后由 不爱胡萝卜的仓鼠 于 2024-11-11 21:37 编辑
/***********************************************************************************************************/
本文强烈建议和上一篇文章以及datalogger的那篇一起交叉看,因为今天又学到了一点新东西。并且还发现了之前datalogger的一个重大BUG
/***********************************************************************************************************/
在上一篇中最后一步移植.a文件到工程中没能顺利完成,我个人感觉还是工程的问题,可惜我能力有限,没法解决。
正经能力我可能有限,但是我歪办法多。我的思路是创建一个空白工程,用Keil,然后只要boot工程,不要跳转APP工程,直接把Boot工程当做主工程来用,我看了主芯片Flash和Ram其实还是可以的,有64K Flash,620K RAM,简单跑跑这个项目应该问题不大。只要我能想尽一切办法让这个boot工程的代码能顺利编译,那就万事大吉啦
经过不懈努力,我终于搞定了我想要的东西,现在这个工程就是实现了之前dataLogger功能的,可以用keil编译的,相比之前的我还增加了ready信号的get,这样应该会更好一点,每次都可以保证获取到新的值。还发现之前datalogger代码的一个BUG,他打印出来的结果是错误的,偏移写的不正确。
之前的工程红框位置的偏移写错了
工程如下:(后面我把datalogger的功能整合进最终的工程了,所以这个大家有兴趣的话就打开看看,可以忽略不看,并且后续有一些代码的小修改,他和最终工程中的也不一定一致,主要是数据格式从int改成了float)
一.再次训练模型
因为上一个模型有问题,我就重新训练了一个模型,这个模型比之前的那个好,现在这个可以到100%,上一篇就92.5(不过上一篇用了错误的datalogger代码,原始样本数据就是错的,那个模型就是报废的。也没啥参考意义)。具体操作步骤我就不说了,详见上一篇
并且在验证环节,我尝试了正确位置放正确数据,还尝试交换放数据,结果都是100%正确识别。这个模型肯定嘎嘎好用
/***********************************************************************************************************/
这里再补充一点,最后一步生成库文件时,Compilation Flags默认会勾选第1个和第3个。我的编译器可能不匹配,如果按默认的来,我这儿能编译,能运行,但是检测出来的结果一直是同一个,很奇怪,编译器这玩意也是触摸到了我的知识盲区,搞不明白。反正全部去掉勾选,就可以了
/***********************************************************************************************************/
二.在线验证模型
并且本次我还发现一个很有用的东西,就是在线模拟,通过串口直接把datalogger实时采集的数据传给电脑,电脑直接运行模型,推算结果。这样就不用把模型下载到开发板上再验证结果了,并且这样在线验证后,我们就可以知道模型是不是真的好用,如果回头在开发板上运行时有问题,那至少可以排除模型的问题
开发板上运行datalogger的代码,然后界面上选择串口和波特率,再点开始即可
这个玩意儿图文不够生动,我就录了一段视频给大家
IMG_7316
三.移植模型到开发版上运行
最终得到的模型压缩包如下:
接下来就是把本次得到的新模型弄到工程中去运行一下,在如图路径创建一个文件夹,用于存放.a和.h
然后把压缩包里的.a、.h拷贝到这个路径下
接下来我们打开keil,然后添加nanoedgeai的文件夹和文件
接下来是添加头文件路径
然后要对.a文件做一下修改,右键点击.a文件
接下来就是对main.c进行修改了,参照nanoEdgeAi给的示例代码,和上一篇的修改差不多,只是我这边用了printf(这玩意儿终于好用了)
以下是整个main.c,我这边是整合了datalogger工程和AI模型的,通过“MODE”这个宏定义控制
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @File : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "iks4a1_motion_sensors.h"
#include "iks4a1_motion_sensors_ex.h"
#include "lsm6dso16is_reg.h"
#include "NanoEdgeAI.h"
#include "knowledge.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct displayFloatToInt_s {
int8_t sign; /* 0 means positive, 1 means negative*/
uint32_t out_int;
uint32_t out_dec;
} displayFloatToInt_t;
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MAX_BUF_SIZE 256
#define ACC_SAMPLE_MAX 64
#define AXIS 3
#define MODE (1) /* 0:datalogger模式, 1: 运行nanoedgeai模型模式 */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart4;
/* USER CODE BEGIN PV */
//static uint8_t verbose = 0; /* Verbose output to UART terminal ON/OFF. */
static IKS4A1_MOTION_SENSOR_Capabilities_t MotionCapabilities[IKS4A1_MOTION_INSTANCES_NBR];
static char dataOut[MAX_BUF_SIZE];
// static int acc_sample_buffer[AXIS * ACC_SAMPLE_MAX] = {0};
float input_user_buffer[DATA_INPUT_USER * AXIS_NUMBER]; // Buffer of input values
float output_class_buffer[CLASS_NUMBER]; // Buffer of class probabilities
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
static void MX_GPIO_Init(void);
static void MX_UART4_Init(void);
static void MX_FLASH_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
static void floatToInt(float in, displayFloatToInt_t *out_value, int32_t dec_prec)
{
if(in >= 0.0f)
{
out_value->sign = 0;
}else
{
out_value->sign = 1;
in = -in;
}
in = in + (0.5f / pow(10, dec_prec));
out_value->out_int = (int32_t)in;
in = in - (float)(out_value->out_int);
out_value->out_dec = (int32_t)trunc(in * pow(10, dec_prec));
}
static void Accelero_Sensor_Handler(uint32_t Instance)
{
// float odr;
// int32_t fullScale;
IKS4A1_MOTION_SENSOR_Axes_t acceleration;
// displayFloatToInt_t out_value;
// uint8_t whoami;
uint16_t i = 0;
lsm6dso16is_status_reg_t Status;
for (i = 0; i < ACC_SAMPLE_MAX;)
{
while(1)
{
if (BSP_ERROR_NONE == IKS4A1_MOTION_SENSOR_Get_DRDY_Status(Instance, MOTION_ACCELERO, (uint8_t *)&Status))
{
if (Status.xlda == 1)
{
break;
}
}
}
if (0 == IKS4A1_MOTION_SENSOR_GetAxes(Instance, MOTION_ACCELERO, &acceleration))
{
input_user_buffer[AXIS * i] = (float)acceleration.x;
input_user_buffer[(AXIS * i) + 1] = (float)acceleration.y;
input_user_buffer[(AXIS * i) + 2] = (float)acceleration.z;
i++;
}
}
for(i = 0; i < ACC_SAMPLE_MAX - 1; i++)
{
sprintf(dataOut, "%.2f,%.2f,%.2f,", input_user_buffer[(AXIS * i)], input_user_buffer[(AXIS * i)+1], input_user_buffer[(AXIS * i)+2]);
printf("%s", dataOut);
}
i = ACC_SAMPLE_MAX - 1;
sprintf(dataOut, "%.2f,%.2f,%.2f", input_user_buffer[(AXIS * i)], input_user_buffer[(AXIS * i)+1], input_user_buffer[(AXIS * i)+2]);
printf("%s\r\n", dataOut);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
#if MODE == 1
uint16_t id_class = 0;
#endif
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* MCU Configuration--------------------------------------------------------*/
/* 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_UART4_Init();
MX_FLASH_Init();
/* USER CODE BEGIN 2 */
printf("test");
/* ACC传感器初始化 */
displayFloatToInt_t out_value_odr;
int i;
IKS4A1_MOTION_SENSOR_Init(IKS4A1_LSM6DSO16IS_0, MOTION_ACCELERO | MOTION_GYRO);
printf("IKS4A1_MOTION_SENSOR_Init ok\r\n");
for(i = 0; i < IKS4A1_MOTION_INSTANCES_NBR; i++)
{
IKS4A1_MOTION_SENSOR_GetCapabilities(i, &MotionCapabilities[i]);
snprintf(dataOut, MAX_BUF_SIZE,
"\r\nMotion Sensor Instance %d capabilities: \r\n ACCELEROMETER: %d\r\n GYROSCOPE: %d\r\n MAGNETOMETER: %d\r\n LOW POWER: %d\r\n",
i, MotionCapabilities[i].Acc, MotionCapabilities[i].Gyro, MotionCapabilities[i].Magneto, MotionCapabilities[i].LowPower);
printf("%s", dataOut);
floatToInt(MotionCapabilities[i].AccMaxOdr, &out_value_odr, 3);
snprintf(dataOut, MAX_BUF_SIZE, " MAX ACC ODR: %d.%03d Hz, MAX ACC FS: %d\r\n", (int)out_value_odr.out_int,
(int)out_value_odr.out_dec, (int)MotionCapabilities[i].AccMaxFS);
printf("%s", dataOut);
floatToInt(MotionCapabilities[i].GyroMaxOdr, &out_value_odr, 3);
snprintf(dataOut, MAX_BUF_SIZE, " MAX GYRO ODR: %d.%03d Hz, MAX GYRO FS: %d\r\n", (int)out_value_odr.out_int,
(int)out_value_odr.out_dec, (int)MotionCapabilities[i].GyroMaxFS);
printf("%s", dataOut);
floatToInt(MotionCapabilities[i].MagMaxOdr, &out_value_odr, 3);
snprintf(dataOut, MAX_BUF_SIZE, " MAX MAG ODR: %d.%03d Hz, MAX MAG FS: %d\r\n", (int)out_value_odr.out_int,
(int)out_value_odr.out_dec, (int)MotionCapabilities[i].MagMaxFS);
printf("%s", dataOut);
}
#if MODE == 1
enum neai_state error_code = neai_classification_init(knowledge);
if (error_code != NEAI_OK)
{
while(1)
{
/* This happens if the knowledge does not correspond to the library or if the library works into a not supported board. */
printf("neai_classification_init fail");
HAL_Delay(1000);
}
}
printf("neai_classification_init ok");
#endif
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// HAL_GPIO_WritePin(GPIOO, GPIO_PIN_1, GPIO_PIN_RESET);
// HAL_Delay(1000);
// printf("led on");
// HAL_GPIO_WritePin(GPIOO, GPIO_PIN_1, GPIO_PIN_SET);
// HAL_Delay(1000);
// printf("led off");
Accelero_Sensor_Handler(0);
#if MODE == 1
neai_classification(input_user_buffer, output_class_buffer, &id_class);
printf("stop:%.2f, line:%.2f, circle:%.2f\r\n", output_class_buffer[0], output_class_buffer[1], output_class_buffer[2]);
printf("result: %d\r\n", id_class);
#endif
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_PCLK4|RCC_CLOCKTYPE_PCLK5;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
RCC_ClkInitStruct.APB5CLKDivider = RCC_APB5_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief FLASH Initialization Function
* @param None
* @retval None
*/
static void MX_FLASH_Init(void)
{
/* USER CODE BEGIN FLASH_Init 0 */
/* USER CODE END FLASH_Init 0 */
FLASH_OBProgramInitTypeDef pOBInit = {0};
/* USER CODE BEGIN FLASH_Init 1 */
/* USER CODE END FLASH_Init 1 */
HAL_FLASHEx_OBGetConfig(&pOBInit);
if ((pOBInit.USERConfig2 & OB_I2C_NI3C_I2C) != OB_I2C_NI3C_I2C)
{
if (HAL_FLASH_Unlock() != HAL_OK)
{
Error_Handler();
}
if (HAL_FLASH_OB_Unlock() != HAL_OK)
{
Error_Handler();
}
pOBInit.OptionType = OPTIONBYTE_USER;
pOBInit.USERType = OB_USER_I2C_NI3C;
pOBInit.USERConfig2 = OB_I2C_NI3C_I2C;
if (HAL_FLASHEx_OBProgram(&pOBInit) != HAL_OK)
{
Error_Handler();
}
if (HAL_FLASH_OB_Lock() != HAL_OK)
{
Error_Handler();
}
if (HAL_FLASH_Lock() != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN FLASH_Init 2 */
/* USER CODE END FLASH_Init 2 */
}
/**
* @brief UART4 Initialization Function
* @param None
* @retval None
*/
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 921600;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOO_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOO, GPIO_PIN_1, GPIO_PIN_RESET);
/*Configure GPIO pin : PO1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOO, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* MPU Configuration */
static void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x0;
MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
MPU_InitStruct.SubRegionDisable = 0x87;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
编译后有如图报错
TEST_Boot\TEST_Boot.axf: Error: L6242E: Cannot link object NanoEdgeAI.o as its attributes are incompatible with the image attributes.
... wchart-16 clashes with wchart-32.
Not enough information to list image symbols.
Not enough information to list load addresses in the image map.
需要做如下修改:
--wchar32
还要切换回AC5,否则这个改了以后还会有更多的报错
这样就万事大吉了,虽然有点警告,但问题不大
接下来就是激动人心的时刻了,运行代码,看看能否正确识别
IMG_7317
本次最终的工程:
四.心得
本次STM32H7R/S-DK开发板的测评终于圆满完成。本次真的是收获满满,我在今年上半年就了解到ST在Ai方向的各种方案及工具,但是当时nanoedgeai还是要收费的,并且我的本职工作不是做AI方向的开发的,对于Ai相关的知识以及如何训练模型、使用模型完全不懂,是属于小白的档位,所以一直就处于观望阶段,最近nanoedgeai免费了,正好遇上H7S7开发板的试用,就趁着这个契机逼自己一把,学习一下相关的知识和技能。
不得不说,ST在文档、demo、视频教程相关的技术支持、工具的设计等真的没的说,在业界绝对算的上数一数二。我通过看官方提供的学习资料,一个门外汉,通过几十小时的学习、实践。可以快速掌握模型训练、在单片上运行模型的开发技能,可以看得出来ST的工程师绝对是付出了巨大的努力,并且训练出来的模型实际效果也是非常棒,100条数据,训练一次,就得到了一个非常完美的模型,体积很小,响应很快,准确度很高。这样的结果真的是令我非常震撼,在得到结果前我一直认为会需要有多轮的训练、调参数、调整样本等
·最后再次感谢ST和EEWORLD给我这次试用的机会,谢谢