/* 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" #include "dma.h" #include "i2c.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "my_mpu6050.h" #include "inv_mpu.h" #include "dmpmap.h" #include "inv_mpu_dmp_motion_driver.h" #include "stdio.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, HAL_MAX_DELAY); return ch; } /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ float Pitch; float Roll; float Yaw; int temp; char mes[10]; /* 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_DMA_Init(); MX_I2C2_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ while(w_mpu_init() != mpu_ok) { HAL_Delay(100); } dmp_init(); //dmp³õʼ»¯ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { if (read_dmp(&mpu_pose_msg) == 0) { printf("Pitch: %.2f\t Roll: %.2f\t Yaw: %.2f\r\n", mpu_pose_msg.pitch, mpu_pose_msg.roll, mpu_pose_msg.yaw); } HAL_Delay(20); /* 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}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** 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.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 72; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; 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(); } } /* 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 */ 以上为我的main.c代码,请根据以上代码帮我修改,实现ANOTech上位机V2.6协议要求
时间: 2025-05-28 20:49:39 浏览: 31
### 修改 `main.c` 代码以适配 ANO Tech 上位机 V2.6 协议
为了使现有 `main.c` 文件能够兼容 ANO Tech 上位机 V2.6 的通信协议,需了解其具体需求。根据描述,V2.6 版本采用 **大端模式** 进行数据传输[^3],而 STM32 默认可能使用小端模式或其他特定配置。因此,在修改代码时需要注意以下几个方面:
#### 1. 数据打包与解析
ANO Tech 上位机 V2.6 需要接收来自 STM32 的数据包,这些数据通常包括陀螺仪原始数据、姿态角以及 PID 参数等。以下是实现数据打包的一个通用方法。
```c
#include <stdint.h>
#include <string.h>
// 定义一个结构体用于存储需要发送的数据
typedef struct {
uint16_t gyro_x; // 陀螺仪X轴数据
uint16_t gyro_y; // 陀螺仪Y轴数据
uint16_t gyro_z; // 陀螺仪Z轴数据
float pitch_angle; // 姿态角 - 俯仰角
float roll_angle; // 姿态角 - 横滚角
} DataPacket;
void pack_data(uint8_t *buffer, const DataPacket *data) {
// 将数据转换为大端格式并写入缓冲区
buffer[0] = (uint8_t)(data->gyro_x >> 8); // 大端高位字节
buffer[1] = (uint8_t)data->gyro_x; // 大端低位字节
buffer[2] = (uint8_t)(data->gyro_y >> 8);
buffer[3] = (uint8_t)data->gyro_y;
buffer[4] = (uint8_t)(data->gyro_z >> 8);
buffer[5] = (uint8_t)data->gyro_z;
union { // 浮点数转整型处理
float f;
uint32_t i;
} u_pitch, u_roll;
u_pitch.f = data->pitch_angle;
u_roll.f = data->roll_angle;
for(int i=0;i<4;i++) {
buffer[6+i] = ((uint8_t*)&u_pitch.i)[3-i]; // 大端浮点数高位优先
buffer[10+i] = ((uint8_t*)&u_roll.i)[3-i];
}
}
```
以上代码实现了将结构体中的数据按照大端顺序打包到指定的缓冲区中。
#### 2. 发送机制调整
在 STM32 中,通过串口或 USB 接口向 PC 端发送数据。假设当前项目已经初始化了 UART 或其他外设,则可以在主循环中定期调用以下函数来完成数据发送。
```c
#include "usart.h"
#define DATA_BUFFER_SIZE 14 // 缓冲区大小应匹配实际数据长度
int main(void) {
uint8_t send_buffer[DATA_BUFFER_SIZE];
DataPacket packet = { .gyro_x = 1234, .gyro_y = 5678, .gyro_z = 9101,
.pitch_angle = 15.5f, .roll_angle = -10.2f };
HAL_Init(); // 初始化HAL库
SystemClock_Config(); // 配置系统时钟
MX_USART2_UART_Init(); // 初始化UART接口
while(1){
pack_data(send_buffer, &packet); // 打包数据
HAL_UART_Transmit(&huart2, send_buffer, sizeof(send_buffer), HAL_MAX_DELAY); // 发送数据
HAL_Delay(100); // 控制发送频率
}
return 0;
}
```
这段代码展示了如何利用 HAL 库的功能将打包好的数据通过 USART2 接口发送给上位机[^2]。
#### 3. 注意事项
- **校验和计算**:如果协议中有规定校验方式(如 CRC),应在发送前加入相应逻辑。
- **波特率一致性**:确保 MCU 和上位机之间的波特率一致,否则可能导致乱码现象发生。
- **同步信号**:部分复杂协议可能会要求特殊的起始标志或者结束符作为帧边界指示器。
---
###
阅读全文
相关推荐











