STM_ATEM/User/data_packet.c
zhoujie 5b245f89c1 feat(gps): 集成GPS模块支持数据采集系统
- 新增GPS驱动模块,支持NMEA GPGGA/GNGGA语句解析
- 修改USART3配置,波特率从2000000调整为115200用于GPS数据接收
- 新增带GPS信息的校正数据包结构`CorrectedDataPacketWithGPS_t`
- 在ADC数据处理流程中集成GPS数据获取和打包
- 更新数据包处理函数,支持GPS时间戳和经纬度信息
- 新增GPS驱动使用指南和集成说明文档
- 修改主循环,添加GPS数据处理调用
- 更新中断处理,添加GPS UART接收回调支持

📝 docs(gps): 添加GPS驱动和集成说明文档

- 新增`GPS_Driver_Guide.md`详细说明GPS驱动API和使用方法
- 新增`GPS_Integration_Guide.md`说明GPS数据集成到采集系统的实现细节
- 文档包含硬件连接、数据格式、使用示例和故障排除等内容

♻️ refactor(data): 重构数据包结构以支持GPS信息

- 修改`DataPacket_t`和`CorrectedDataPacket_t`结构,添加GPS时间戳和经纬度字段
- 新增`CorrectedDataPacketWithGPS_t`结构用于带完整GPS信息的数据包
- 更新数据打包函数,支持GPS参数传递
- 简化数据包验证逻辑,移除校验和检查以提高处理速度

🔧 chore(config): 更新硬件配置文件

- 更新STM32CubeMX项目文件,修改USART3波特率配置
- 在中断处理文件中添加GPS驱动头文件包含
2026-02-07 19:34:48 +08:00

122 lines
3.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "data_packet.h"
#include "stddef.h"
#include "stm32f4xx_hal.h"
// CRC16-MODBUS算法实现
uint16_t Calculate_CRC16(const uint8_t *data, uint16_t len) {
uint16_t crc = 0xFFFF;
for (uint16_t i = 0; i < len; i++) {
crc ^= data[i];
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xA001;
} else {
crc = crc >> 1;
}
}
}
return crc;
}
void PackData(DataPacket_t *packet, int32_t adc1, int32_t adc2, int32_t adc3,
uint32_t gps_time, float latitude, float longitude)
{
if (packet == NULL) return;
// 设置包头
packet->start_byte = PACKET_START_BYTE;
// 设置时间戳
packet->timestamp = HAL_GetTick();
// 设置ADC数据
packet->adc_data1 = adc1;
packet->adc_data2 = adc2;
packet->adc_data3 = adc3;
// 设置GPS数据
packet->gps_time = gps_time;
packet->gps_latitude = latitude;
packet->gps_longitude = longitude;
}
uint8_t ValidatePacket(const DataPacket_t *packet)
{
if (packet == NULL) return 0;
// 检查包头
if (packet->start_byte != PACKET_START_BYTE) return 0;
// 精简版数据包无校验和,仅检查包头
return 1; // 包头正确,认为有效
}
uint8_t ValidateCorrectedPacket(const CorrectedDataPacket_t *packet)
{
if (packet == NULL) return 0;
// 检查包头
if (packet->start_byte != PACKET_START_BYTE) return 0;
// 精简版数据包无校验和,仅检查包头
return 1; // 包头正确,认为有效
}
void PackCorrectedData(CorrectedDataPacket_t *packet, float x, float y, float z,
uint32_t gps_time, float latitude, float longitude)
{
if (packet == NULL) return;
// 设置包头
packet->start_byte = PACKET_START_BYTE;
// 设置时间戳
packet->timestamp = HAL_GetTick();
// 设置校正后数据
packet->corrected_x = x;
packet->corrected_y = y;
packet->corrected_z = z;
// 设置GPS数据
packet->gps_time = gps_time;
packet->gps_latitude = latitude;
packet->gps_longitude = longitude;
}
void PackCorrectedDataWithGPS(CorrectedDataPacketWithGPS_t *packet, float x, float y, float z,
uint32_t gps_time, float latitude, float longitude)
{
if (packet == NULL) return;
// 设置包头
packet->start_byte = PACKET_START_BYTE;
// 设置时间戳
packet->timestamp = HAL_GetTick();
// 设置校正后数据
packet->corrected_x = x;
packet->corrected_y = y;
packet->corrected_z = z;
// 设置GPS数据
packet->gps_time = gps_time;
packet->gps_latitude = latitude;
packet->gps_longitude = longitude;
}
uint8_t ValidateCorrectedPacketWithGPS(const CorrectedDataPacketWithGPS_t *packet)
{
if (packet == NULL) return 0;
// 检查包头
if (packet->start_byte != PACKET_START_BYTE) return 0;
// 精简版数据包无校验和,仅检查包头
// 可以添加简单的数据合理性检查
// 例如检查GPS坐标是否在有效范围内等
return 1; // 包头正确,认为有效
}