sentry_chassis_hzz/modules/referee/rm_referee.c

132 lines
4.6 KiB
C
Raw Normal View History

/**
* @file rm_referee.C
* @author kidneygood (you@domain.com)
* @brief
* @version 0.1
* @date 2022-11-18
*
* @copyright Copyright (c) 2022
*
*/
#include "rm_referee.h"
2022-11-18 22:06:06 +08:00
#include "string.h"
#include "crc.h"
#include "bsp_usart.h"
#define RE_RX_BUFFER_SIZE 200
2023-01-09 16:17:03 +08:00
USARTInstance *referee_usart_instance; //暂时改为非静态变量
2022-11-18 22:06:06 +08:00
static referee_info_t referee_info;
2023-01-09 16:17:03 +08:00
static void JudgeReadData(uint8_t *ReadFromUsart);
static void RefereeRxCallback();
/* 裁判系统通信初始化 */
referee_info_t *RefereeInit(UART_HandleTypeDef *referee_usart_handle)
{
USART_Init_Config_s conf;
conf.module_callback = RefereeRxCallback;
conf.usart_handle = referee_usart_handle;
conf.recv_buff_size = RE_RX_BUFFER_SIZE;
referee_usart_instance = USARTRegister(&conf);
return &referee_info;
}
/**
* @brief
* @param send
*/
void RefereeSend(uint8_t *send,uint16_t tx_len)
{
USARTSend(referee_usart_instance,send,tx_len);
2023-01-26 15:07:00 +08:00
/* syhtodo DMA请求过快会导致数据发送丢失考虑数据尽可能打成一个整包以及队列发送并且发送函数添加缓冲区 */
2023-01-12 15:24:56 +08:00
HAL_Delay(5);
2023-01-09 16:17:03 +08:00
}
/*裁判系统串口接收回调函数,解析数据 */
static void RefereeRxCallback()
{
JudgeReadData(referee_usart_instance->recv_buff);
}
2022-11-18 22:06:06 +08:00
/**
* @brief ,
2023-01-01 17:32:22 +08:00
* @param ReadFromUsart:
2022-11-18 22:06:06 +08:00
* @retval
* @attention CRC校验,
*/
static void JudgeReadData(uint8_t *ReadFromUsart)
2022-11-18 22:06:06 +08:00
{
uint16_t judge_length; // 统计一帧数据长度
2023-01-06 22:54:01 +08:00
if (ReadFromUsart == NULL) // 空数据包,则不作任何处理
return;
// 写入帧头数据(5-byte),用于判断是否开始存储裁判数据
2022-11-18 22:06:06 +08:00
memcpy(&referee_info.FrameHeader, ReadFromUsart, LEN_HEADER);
// 判断帧头数据(0)是否为0xA5
if (ReadFromUsart[SOF] == REFEREE_SOF)
2022-11-18 22:06:06 +08:00
{
// 帧头CRC8校验
2022-11-18 22:06:06 +08:00
if (Verify_CRC8_Check_Sum(ReadFromUsart, LEN_HEADER) == TRUE)
{
// 统计一帧数据长度(byte),用于CR16校验
2022-11-18 22:06:06 +08:00
judge_length = ReadFromUsart[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL;
// 帧尾CRC16校验
2022-11-18 22:06:06 +08:00
if (Verify_CRC16_Check_Sum(ReadFromUsart, judge_length) == TRUE)
{
// 2个8位拼成16位int
referee_info.CmdID = (ReadFromUsart[6] << 8 | ReadFromUsart[5]);
// 解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度)
// 第8个字节开始才是数据 data=7
2022-11-18 22:06:06 +08:00
switch (referee_info.CmdID)
{
case ID_game_state: // 0x0001
memcpy(&referee_info.GameState, (ReadFromUsart + DATA_Offset), LEN_game_state);
2022-11-18 22:06:06 +08:00
break;
case ID_game_result: // 0x0002
memcpy(&referee_info.GameResult, (ReadFromUsart + DATA_Offset), LEN_game_result);
2022-11-18 22:06:06 +08:00
break;
case ID_game_robot_survivors: // 0x0003
memcpy(&referee_info.GameRobotHP, (ReadFromUsart + DATA_Offset), LEN_game_robot_HP);
2022-11-18 22:06:06 +08:00
break;
case ID_event_data: // 0x0101
memcpy(&referee_info.EventData, (ReadFromUsart + DATA_Offset), LEN_event_data);
2022-11-18 22:06:06 +08:00
break;
case ID_supply_projectile_action: // 0x0102
memcpy(&referee_info.SupplyProjectileAction, (ReadFromUsart + DATA_Offset), LEN_supply_projectile_action);
2022-11-18 22:06:06 +08:00
break;
case ID_game_robot_state: // 0x0201
memcpy(&referee_info.GameRobotState, (ReadFromUsart + DATA_Offset), LEN_game_robot_state);
2022-11-18 22:06:06 +08:00
break;
case ID_power_heat_data: // 0x0202
memcpy(&referee_info.PowerHeatData, (ReadFromUsart + DATA_Offset), LEN_power_heat_data);
2022-11-18 22:06:06 +08:00
break;
case ID_game_robot_pos: // 0x0203
memcpy(&referee_info.GameRobotPos, (ReadFromUsart + DATA_Offset), LEN_game_robot_pos);
2022-11-18 22:06:06 +08:00
break;
case ID_buff_musk: // 0x0204
memcpy(&referee_info.BuffMusk, (ReadFromUsart + DATA_Offset), LEN_buff_musk);
2022-11-18 22:06:06 +08:00
break;
case ID_aerial_robot_energy: // 0x0205
memcpy(&referee_info.AerialRobotEnergy, (ReadFromUsart + DATA_Offset), LEN_aerial_robot_energy);
2022-11-18 22:06:06 +08:00
break;
case ID_robot_hurt: // 0x0206
memcpy(&referee_info.RobotHurt, (ReadFromUsart + DATA_Offset), LEN_robot_hurt);
2022-11-18 22:06:06 +08:00
break;
case ID_shoot_data: // 0x0207
memcpy(&referee_info.ShootData, (ReadFromUsart + DATA_Offset), LEN_shoot_data);
2022-11-18 22:06:06 +08:00
break;
}
}
}
// 首地址加帧长度,指向CRC16下一字节,用来判断是否为0xA5,从而判断一个数据包是否有多帧数据
2022-11-18 22:06:06 +08:00
if (*(ReadFromUsart + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL) == 0xA5)
{
// 如果一个数据包出现了多帧数据,则再次调用解析函数,直到所有数据包解析完毕
JudgeReadData(ReadFromUsart + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL);
2022-11-18 22:06:06 +08:00
}
}
}