使用stm32HAL库读取IMU数据
·
声明:部分图片截图来源于维特JY901B数据手册官网
JY901B是基于MEMS技术的高性能三维运动姿态测量系统。它包含三轴陀螺仪、三轴加速度计,三轴电子罗盘等运动传感器。

本文介绍利用stm32HAL库读取本imu的数据(目前只读出了单个数据,对于实时数据作者检查通过各种渠道也没有发现问题,求大神解答!!!!)
在stm332CubeMX中先进行基础配置,在新建工程选好单片机型号后点击SYS,在Debug中选择Seral Wire,如下图所示:
然后我们可以在接口连接中查找JY901B的数据手册,对于stm32的连接与JY901B连线图可知我们需要配置UART接口:


设置如上图所示:注意USART1和USART2配置相同,因为我们这里需要一个USART读取imu信息、一个串口发送信息到我们的电脑上,所以选择两个USART配置,这里波特率选择9600Bits/s是对应JY901B默认波特率为9600Bits/s
在CubeMX完成配置之后便可开始编写程序:
主程序如下:
uint8_t rxbuf[11];
void JY901B_ReadFrame(void)
{
// 读取一帧(阻塞方式)
HAL_UART_Receive(&huart2, rxbuf, 11, HAL_MAX_DELAY);
if (rxbuf[0] != 0x55) return; // 帧头检查
uint8_t sum = 0;
for(int i=0; i<10; i++) sum += rxbuf[i];
if (sum != rxbuf[10]) return; // 校验失败
switch(rxbuf[1])
{
case 0x51: { // 加速度
int16_t ax = (rxbuf[3]<<8)|rxbuf[2];
int16_t ay = (rxbuf[5]<<8)|rxbuf[4];
int16_t az = (rxbuf[7]<<8)|rxbuf[6];
float AccX = ax/32768.0f*16;
float AccY = ay/32768.0f*16;
float AccZ = az/32768.0f*16;
char buf[80];
sprintf(buf,"AX=%.2f AY=%.2f AZ=%.2f\r\n",AccX,AccY,AccZ);
HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100);
} break;
case 0x52: { // 角速度
int16_t gx = (rxbuf[3]<<8)|rxbuf[2];
int16_t gy = (rxbuf[5]<<8)|rxbuf[4];
int16_t gz = (rxbuf[7]<<8)|rxbuf[6];
float GyroX = gx/32768.0f*2000;
float GyroY = gy/32768.0f*2000;
float GyroZ = gz/32768.0f*2000;
char buf[80];
sprintf(buf,"GX=%.2f GY=%.2f GZ=%.2f\r\n",GyroX,GyroY,GyroZ);
HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100);
} break;
case 0x53: { // 角度
int16_t roll = (rxbuf[3]<<8)|rxbuf[2];
int16_t pitch= (rxbuf[5]<<8)|rxbuf[4];
int16_t yaw = (rxbuf[7]<<8)|rxbuf[6];
float Roll = roll/32768.0f*180;
float Pitch = pitch/32768.0f*180;
float Yaw = yaw/32768.0f*180;
char buf[80];
sprintf(buf,"Roll=%.2f Pitch=%.2f Yaw=%.2f\r\n",Roll,Pitch,Yaw);
HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100);
} break;
}
}
最后再到while循环中写入读取和接收数据:
while (1)
{
JY901B_ReadFrame();//读取和接收数据
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
编译运行后可以看到效果:

这里需要注意细节是串口选择的波特率要和之前设置的波特率保持一致也就是9600。
其次在对于三个参量的计算公式均能在数据手册中查找到,例如对于加速度而言:

因为在编程过程中对于回调函数中只定义了读取一帧,所以在每次读数时只会读取加速度、角度或者角速度中的一个。
而我们想读取三个值不需要改变配置,只需要更改程序的写法:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 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<math.h>
#include<stdio.h>
#include<string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t rxbyte; // 每次接收一个字节
uint8_t rxbuf[11]; // 一帧缓存
uint8_t rxIndex = 0; // 缓存计数器
// 数据变量
float AccX=0, AccY=0, AccZ=0;
float GyroX=0, GyroY=0, GyroZ=0;
float Roll=0, Pitch=0, Yaw=0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void JY901B_ProcessFrame(uint8_t *buf)
{
if (buf[0] != 0x55) return; // 帧头
uint8_t sum=0;
for(int i=0;i<10;i++) sum += buf[i];
if (sum != buf[10]) return; // 校验失败
switch(buf[1])
{
case 0x51: { // 加速度
int16_t ax = (buf[3]<<8)|buf[2];
int16_t ay = (buf[5]<<8)|buf[4];
int16_t az = (buf[7]<<8)|buf[6];
AccX = ax/32768.0f*16;
AccY = ay/32768.0f*16;
AccZ = az/32768.0f*16;
} break;
case 0x52: { // 角速度
int16_t gx = (buf[3]<<8)|buf[2];
int16_t gy = (buf[5]<<8)|buf[4];
int16_t gz = (buf[7]<<8)|buf[6];
GyroX = gx/32768.0f*2000;
GyroY = gy/32768.0f*2000;
GyroZ = gz/32768.0f*2000;
} break;
case 0x53: { // 角度
int16_t roll = (buf[3]<<8)|buf[2];
int16_t pitch = (buf[5]<<8)|buf[4];
int16_t yaw = (buf[7]<<8)|buf[6];
Roll = roll/32768.0f*180;
Pitch = pitch/32768.0f*180;
Yaw = yaw/32768.0f*180;
} break;
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART2) // JY901B 走 USART2
{
rxbuf[rxIndex++] = rxbyte;
if (rxIndex >= 11) {
JY901B_ProcessFrame(rxbuf);
rxIndex = 0; // 重置准备接收下一字符
}
// 继续接收下一个字节
HAL_UART_Receive_IT(&huart2, &rxbyte, 1);
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* 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_USART1_UART_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart2, &rxbyte, 1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// 实时输出三个数据
char buf[120];
sprintf(buf,
"AX=%.2f AY=%.2f AZ=%.2f | "
"GX=%.2f GY=%.2f GZ=%.2f | "
"Roll=%.2f Pitch=%.2f Yaw=%.2f\r\n",
AccX, AccY, AccZ, GyroX, GyroY, GyroZ, Roll, Pitch, Yaw);
HAL_UART_Transmit(&huart1,(uint8_t*)buf,strlen(buf),100);
HAL_Delay(200); // 每200ms打印一次
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* 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_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @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 */
但通过上述代码打印出的数据遇到的疑问是它只能打印各物理量及其分量为0,貌似并未读取到imu的值,实验情况如下:

更多推荐



所有评论(0)