112 lines
3.8 KiB
C
112 lines
3.8 KiB
C
//
|
|
// Created by SJQ on 2024/4/28.
|
|
//
|
|
|
|
#include "referee_VT.h"
|
|
|
|
/**
|
|
* @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"
|
|
#include "string.h"
|
|
#include "crc_ref.h"
|
|
#include "bsp_usart.h"
|
|
#include "task.h"
|
|
#include "daemon.h"
|
|
#include "bsp_log.h"
|
|
#include "cmsis_os.h"
|
|
|
|
#define RE_RX_BUFFER_SIZE 255u // 裁判系统接收缓冲区大小
|
|
|
|
static USARTInstance *VT_usart_instance; // 裁判系统串口实例
|
|
static DaemonInstance *VT_daemon; // 裁判系统守护进程
|
|
static VT_info_t VT_info; // 裁判系统数据
|
|
uint8_t debug_data[39];
|
|
/**
|
|
* @brief 读取图传链路数据,中断中读取保证速度
|
|
* @param buff: 读取到的裁判系统原始数据
|
|
* @retval 是否对正误判断做处理
|
|
* @attention 在此判断帧头和CRC校验,无误再写入数据,不重复判断帧头
|
|
*/
|
|
static void VTJudgeReadData(uint8_t *buff)
|
|
{
|
|
uint16_t judge_length; // 统计一帧数据长度
|
|
if (buff == NULL) // 空数据包,则不作任何处理
|
|
return;
|
|
|
|
// 写入帧头数据(5-byte),用于判断是否开始存储裁判数据
|
|
memcpy(&VT_info.FrameHeader, buff, LEN_HEADER);
|
|
memcpy(&debug_data,buff,39);
|
|
// 判断帧头数据(0)是否为0xA5
|
|
if (buff[SOF] == REFEREE_SOF)
|
|
{
|
|
// 帧头CRC8校验
|
|
if (Verify_CRC8_Check_Sum(buff, LEN_HEADER) == TRUE)
|
|
{
|
|
// 统计一帧数据长度(byte),用于CR16校验
|
|
judge_length = buff[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL;
|
|
// 帧尾CRC16校验
|
|
if (Verify_CRC16_Check_Sum(buff, judge_length) == TRUE)
|
|
{
|
|
// 2个8位拼成16位int
|
|
VT_info.CmdID = (buff[6] << 8 | buff[5]);
|
|
// 解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度)
|
|
// 第8个字节开始才是数据 data=7
|
|
switch (VT_info.CmdID)
|
|
{
|
|
case ID_remote_control: // 0x0304
|
|
memcpy(&VT_info.RemoteControl, (buff + DATA_Offset), LEN_remote_control);
|
|
break;
|
|
case ID_custom_robot: // 0x0302
|
|
memcpy(&VT_info.CustomControl, (buff + DATA_Offset), LEN_custom_control);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// 首地址加帧长度,指向CRC16下一字节,用来判断是否为0xA5,从而判断一个数据包是否有多帧数据
|
|
if (*(buff + sizeof(xFrameHeader) + LEN_CMDID + VT_info.FrameHeader.DataLength + LEN_TAIL) == 0xA5)
|
|
{ // 如果一个数据包出现了多帧数据,则再次调用解析函数,直到所有数据包解析完毕
|
|
VTJudgeReadData(buff + sizeof(xFrameHeader) + LEN_CMDID + VT_info.FrameHeader.DataLength + LEN_TAIL);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*裁判系统串口接收回调函数,解析数据 */
|
|
static void VTRxCallback()
|
|
{
|
|
DaemonReload(VT_daemon);
|
|
VTJudgeReadData(VT_usart_instance->recv_buff);
|
|
}
|
|
// 裁判系统丢失回调函数,重新初始化裁判系统串口
|
|
static void VTLostCallback(void *arg)
|
|
{
|
|
USARTServiceInit(VT_usart_instance);
|
|
LOGWARNING("[rm_vt] lost vt data");
|
|
}
|
|
|
|
/* 裁判系统通信初始化 */
|
|
VT_info_t *VTInit(UART_HandleTypeDef *VT_usart_handle)
|
|
{
|
|
USART_Init_Config_s conf;
|
|
conf.module_callback = VTRxCallback;
|
|
conf.usart_handle = VT_usart_handle;
|
|
conf.recv_buff_size = RE_RX_BUFFER_SIZE; // mx 255(u8)
|
|
VT_usart_instance = USARTRegister(&conf);
|
|
|
|
Daemon_Init_Config_s daemon_conf = {
|
|
.callback = VTLostCallback,
|
|
.owner_id = VT_usart_instance,
|
|
.reload_count = 30, // 0.3s没有收到数据,则认为丢失,重启串口接收
|
|
};
|
|
VT_daemon = DaemonRegister(&daemon_conf);
|
|
|
|
return &VT_info;
|
|
} |