From dc68657c16868daf5b7016831fbabe13b2f1067b Mon Sep 17 00:00:00 2001 From: kidneygood <2979564623@qq.com> Date: Thu, 26 Jan 2023 19:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E3=80=81=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=BD=A6?= =?UTF-8?q?=E9=97=B4=E9=80=9A=E4=BF=A1=E7=9A=84=E5=8F=91=E9=80=81=E5=92=8C?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=EF=BC=8C=E4=BB=85=E6=B5=8B=E8=AF=95=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/referee/referee.c | 14 ++++- modules/referee/referee_UI.c | 22 +++++-- modules/referee/referee_communication.c | 84 ++++++++++++------------- modules/referee/referee_communication.h | 75 +--------------------- modules/referee/referee_def.h | 79 ++++++++++++++++++++--- modules/referee/rm_referee.c | 13 ++-- modules/referee/rm_referee.h | 12 ++-- 7 files changed, 154 insertions(+), 145 deletions(-) diff --git a/application/referee/referee.c b/application/referee/referee.c index 7915f2a..c21122d 100644 --- a/application/referee/referee.c +++ b/application/referee/referee.c @@ -12,15 +12,21 @@ #include "robot_def.h" #include "rm_referee.h" #include "referee_UI.h" +#include "referee_communication.h" static Referee_Interactive_info_t Interactive_data; // 非裁判系统数据 -static referee_info_t *referee_data; // 裁判系统相关数据 +static referee_info_t *referee_data; // 裁判系统相关数据 +static robot_interactive_data_t *SendData={1,2,3,4,5}; + static void determine_ID(referee_info_t *_referee_info); static void My_UI_init(referee_info_t *_referee_info); static void My_UI_Refresh(referee_info_t *_referee_info, Referee_Interactive_info_t *_Interactive_data); -static void robot_mode_change(Referee_Interactive_info_t *_Interactive_data); // 测试用函数,实现模式自动变化 static void Mode_Change_Check(Referee_Interactive_info_t *_Interactive_data); // 模式切换检测 +//syhtod 正式上车后需删除 +static void robot_mode_change(Referee_Interactive_info_t *_Interactive_data); // 测试用函数,实现模式自动变化 + + void Referee_Interactive_init() { referee_data = RefereeInit(&huart6); // 裁判系统初始化 @@ -28,6 +34,8 @@ void Referee_Interactive_init() ; determine_ID(referee_data); My_UI_init(referee_data); + referee_data->referee_id.Receiver_Robot_ID = RobotID_BEngineer; // 机器人车间通信时接收者的ID暂时发给蓝色2 + Communicate_SendData(&referee_data->referee_id,SendData); } void Referee_Interactive_task() @@ -345,5 +353,5 @@ static void determine_ID(referee_info_t *_referee_info) _referee_info->referee_id.Robot_Color = _referee_info->GameRobotState.robot_id > 7 ? Robot_Blue : Robot_Red; _referee_info->referee_id.Robot_ID = _referee_info->GameRobotState.robot_id; _referee_info->referee_id.Cilent_ID = 0x0100 + _referee_info->referee_id.Robot_ID; // 计算客户端ID - _referee_info->referee_id.Receiver_Robot_ID = 0x00; // 机器人车间通信时接收者的ID暂时为0 + _referee_info->referee_id.Receiver_Robot_ID = 0; } diff --git a/modules/referee/referee_UI.c b/modules/referee/referee_UI.c index 074e323..3e20cc1 100644 --- a/modules/referee/referee_UI.c +++ b/modules/referee/referee_UI.c @@ -1,10 +1,20 @@ +/** + * @file referee_UI.C + * @author kidneygood (you@domain.com) + * @brief + * @version 0.1 + * @date 2023-1-18 + * + * @copyright Copyright (c) 2022 + * + */ #include "referee_UI.h" #include "string.h" #include "crc.h" #include "stdio.h" #include "rm_referee.h" -static uint8_t UI_Seq; //包序号 + //包序号 /********************************************删除操作************************************* **参数:_id 对应的id结构体 Del_Operate 对应头文件删除操作 @@ -13,7 +23,7 @@ static uint8_t UI_Seq; //包序号 void UI_Delete(referee_id_t *_id,uint8_t Del_Operate,uint8_t Del_Layer) { UI_delete_t UI_delete_data; - uint8_t temp_datalength = UI_Data_LEN_Head + UI_Operate_LEN_Del; //计算交互数据长度 + uint8_t temp_datalength = Interactive_Data_LEN_Head + UI_Operate_LEN_Del; //计算交互数据长度 UI_delete_data.FrameHeader.SOF = REFEREE_SOF; UI_delete_data.FrameHeader.DataLength = temp_datalength; @@ -349,7 +359,7 @@ void UI_ReFresh(referee_id_t *_id,int cnt,...) va_start(ap,cnt);//初始化 va_list 变量为一个参数列表 UI_GraphReFresh_data.FrameHeader.SOF = REFEREE_SOF; - UI_GraphReFresh_data.FrameHeader.DataLength = UI_Data_LEN_Head+cnt*UI_Operate_LEN_PerDraw; + UI_GraphReFresh_data.FrameHeader.DataLength = Interactive_Data_LEN_Head+cnt*UI_Operate_LEN_PerDraw; UI_GraphReFresh_data.FrameHeader.Seq = UI_Seq; UI_GraphReFresh_data.FrameHeader.CRC8 = Get_CRC8_Check_Sum((uint8_t *)&UI_GraphReFresh_data,LEN_CRC8,0xFF); @@ -375,8 +385,8 @@ void UI_ReFresh(referee_id_t *_id,int cnt,...) UI_GraphReFresh_data.datahead.sender_ID = _id->Robot_ID; //先发送帧头、命令码、交互数据帧头三部分,并计算CRC16校验值 - UI_GraphReFresh_data.frametail=Get_CRC16_Check_Sum((uint8_t *)&UI_GraphReFresh_data,LEN_HEADER+LEN_CMDID+UI_Data_LEN_Head,0xFFFF); - RefereeSend((uint8_t *)&UI_GraphReFresh_data,LEN_HEADER+LEN_CMDID+UI_Data_LEN_Head); + UI_GraphReFresh_data.frametail=Get_CRC16_Check_Sum((uint8_t *)&UI_GraphReFresh_data,LEN_HEADER+LEN_CMDID+Interactive_Data_LEN_Head,0xFFFF); + RefereeSend((uint8_t *)&UI_GraphReFresh_data,LEN_HEADER+LEN_CMDID+Interactive_Data_LEN_Head); for(i=0;iRobot_ID; + SendData.datahead.receiver_ID = _id->Receiver_Robot_ID; + SendData.Data = *_data; + + SendData.frametail = Get_CRC16_Check_Sum((uint8_t *)&SendData,LEN_HEADER+LEN_CMDID+temp_datalength,0xFFFF); + + RefereeSend((uint8_t *)&SendData,LEN_HEADER+LEN_CMDID+temp_datalength+LEN_TAIL); //发送 + UI_Seq++; // 包序号+1 +} diff --git a/modules/referee/referee_communication.h b/modules/referee/referee_communication.h index 4acc879..cba2324 100644 --- a/modules/referee/referee_communication.h +++ b/modules/referee/referee_communication.h @@ -1,79 +1,10 @@ -/** - * @file referee_communication.h - * @author kidneygood (you@domain.com) - * @version 0.1 - * @date 2022-12-02 - * - * @copyright Copyright (c) HNU YueLu EC 2022 all rights reserved - * - */ - #ifndef REFEREE_COMMUNICATION_H #define REFEREE_COMMUNICATION_H #include "stdint.h" #include "referee_def.h" -#pragma pack(1) +#include "rm_referee.h" +void Communicate_SendData(referee_id_t *_id,robot_interactive_data_t *_data); - - -/* - 客户端 客户端自定义数据:cmd_id:0x0301。内容 ID:0xD180 - 发送频率:上限 10Hz - 1. 客户端 客户端自定义数据:cmd_id:0x0301。内容 ID:0xD180。发送频率:上限 10Hz - 字节偏移量 大小 说明 备注 - 0 2 数据的内容 ID 0xD180 - 2 2 送者的 ID 需要校验发送者机器人的 ID 正确性 - 4 2 客户端的 ID 只能为发送者机器人对应的客户端 - 6 4 自定义浮点数据 1 - 10 4 自定义浮点数据 2 - 14 4 自定义浮点数据 3 - 18 1 自定义 8 位数据 4 - -*/ -typedef struct -{ - float data1; - float data2; - float data3; - uint8_t masks; -} client_custom_data_t; - -/* - 学生机器人间通信 cmd_id 0x0301,内容 ID:0x0200~0x02FF - 交互数据 机器人间通信:0x0301。 - 发送频率:上限 10Hz -*/ -typedef struct -{ - uint8_t data[10]; // 数据段,n需要小于113 -} robot_interactive_data_t; - -// 发送给客户端的信息 -//交互数据包括一个统一的数据段头结构。数据段包含了内容 ID,发送者以及接收者的 ID 和内容数据段, -// 整个交互数据的包总共长最大为 128 个字节,减去 frame_header,cmd_id 和 frame_tail 共 9 个字节以及 -// 数据段头结构的 6 个字节,故而发送的内容数据段最大为 113。 -// 帧头 命令码 数据段头结构 数据段 帧尾 -typedef struct -{ - xFrameHeader txFrameHeader; // 帧头 - uint16_t CmdID; // 命令码 - ext_student_interactive_header_data_t dataFrameHeader; // 数据段头结构 - client_custom_data_t clientData; // 数据段 - uint16_t FrameTail; // 帧尾 -} ext_SendClientData_t; - -// 机器人交互信息 -typedef struct -{ - xFrameHeader txFrameHeader; // 帧头 - uint16_t CmdID; // 命令码 - ext_student_interactive_header_data_t dataFrameHeader; // 数据段头结构 - robot_interactive_data_t interactData; // 数据段 - uint16_t FrameTail; // 帧尾 -} ext_CommunatianData_t; - -#pragma pack() - -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/modules/referee/referee_def.h b/modules/referee/referee_def.h index ac82dfb..11d44ee 100644 --- a/modules/referee/referee_def.h +++ b/modules/referee/referee_def.h @@ -19,8 +19,9 @@ #define REFEREE_SOF 0xA5 // 起始字节,协议固定为0xA5 #define Robot_Red 0 #define Robot_Blue 1 +#define Communicate_Data_LEN 5 //自定义交互数据长度,该长度决定了我方发送和他方接收,自定义交互数据协议更改时只需要更改此宏定义即可 -#pragma pack(1) +#pragma pack(1) /****************************通信协议格式****************************/ /****************************通信协议格式****************************/ @@ -86,7 +87,7 @@ typedef enum ID_student_interactive = 0x0301, // 机器人间交互数据 } CmdID_e; -/* 命令码数据段长,根据官方协议来定义长度 */ +/* 命令码数据段长,根据官方协议来定义长度,还有自定义数据长度 */ typedef enum { LEN_game_state = 3, // 0x0001 @@ -101,6 +102,8 @@ typedef enum LEN_aerial_robot_energy = 1, // 0x0205 LEN_robot_hurt = 1, // 0x0206 LEN_shoot_data = 7, // 0x0207 + LEN_receive_data = 6+Communicate_Data_LEN, //0x0301 + } JudgeDataLength_e; /****************************接收数据的详细说明****************************/ @@ -226,17 +229,40 @@ typedef struct float bullet_speed; } ext_shoot_data_t; -/****************************机器人间交互数据****************************/ -/****************************机器人间交互数据****************************/ -/* 发送的内容数据段最大为 113 检测是否超出大小限制?实际上图形段不会超,数据段最多30个,也不会超下*/ +/****************************机器人交互数据****************************/ +/****************************机器人交互数据****************************/ +/* 发送的内容数据段最大为 113 检测是否超出大小限制?实际上图形段不会超,数据段最多30个,也不会超*/ /* 交互数据头结构 */ typedef struct { - uint16_t data_cmd_id; + uint16_t data_cmd_id; //由于存在多个内容 ID,但整个cmd_id 上行频率最大为 10Hz,请合理安排带宽。syhtodo 注意交互部分的上行频率 uint16_t sender_ID; uint16_t receiver_ID; } ext_student_interactive_header_data_t; +/* 机器人id */ +typedef enum +{ + // 红方机器人ID + RobotID_RHero = 1, + RobotID_REngineer = 2, + RobotID_RStandard1 = 3, + RobotID_RStandard2 = 4, + RobotID_RStandard3 = 5, + RobotID_RAerial = 6, + RobotID_RSentry = 7, + RobotID_RRadar = 9, + // 蓝方机器人ID + RobotID_BHero = 101, + RobotID_BEngineer = 102, + RobotID_BStandard1 = 103, + RobotID_BStandard2 = 104, + RobotID_BStandard3 = 105, + RobotID_BAerial = 106, + RobotID_BSentry = 107, + RobotID_BRadar = 109, +} Robot_ID_e; + /* 交互数据ID */ typedef enum { @@ -246,15 +272,52 @@ typedef enum UI_Data_ID_Draw5 = 0x103, UI_Data_ID_Draw7 = 0x104, UI_Data_ID_DrawChar = 0x110, + + /* 自定义交互数据部分 */ + Communicate_Data_ID = 0x0200, + } Interactive_Data_ID_e; /* 交互数据长度 */ typedef enum { - UI_Data_LEN_Head = 6, + Interactive_Data_LEN_Head = 6, UI_Operate_LEN_Del = 2, UI_Operate_LEN_PerDraw = 15, UI_Operate_LEN_DrawChar = 15 + 30, -} UI_Data_Length_e; + + /* 自定义交互数据部分 */ + // Communicate_Data_LEN = 5, + +} Interactive_Data_Length_e; + +/****************************自定义交互数据****************************/ +/****************************自定义交互数据****************************/ +/* + 学生机器人间通信 cmd_id 0x0301,内容 ID:0x0200~0x02FF + 自定义交互数据 机器人间通信:0x0301。 + 发送频率:上限 10Hz +*/ +//自定义交互数据协议,可更改,更改后需要修改最上方宏定义数据长度的值 +typedef struct +{ + uint8_t data[Communicate_Data_LEN]; // 数据段,n需要小于113 +} robot_interactive_data_t; + +// 机器人交互信息_发送 +typedef struct +{ + xFrameHeader FrameHeader; + uint16_t CmdID; + ext_student_interactive_header_data_t datahead; + robot_interactive_data_t Data; // 数据段 + uint16_t frametail; +} Communicate_SendData_t; +// 机器人交互信息_接收 +typedef struct +{ + ext_student_interactive_header_data_t datahead; + robot_interactive_data_t Data; // 数据段 +} Communicate_ReceiveData_t; /****************************UI交互数据****************************/ diff --git a/modules/referee/rm_referee.c b/modules/referee/rm_referee.c index b73b32c..3a3cbb1 100644 --- a/modules/referee/rm_referee.c +++ b/modules/referee/rm_referee.c @@ -16,12 +16,14 @@ #define RE_RX_BUFFER_SIZE 200 -USARTInstance *referee_usart_instance; //暂时改为非静态变量 +static USARTInstance *referee_usart_instance; // 暂时改为非静态变量 static referee_info_t referee_info; static void JudgeReadData(uint8_t *ReadFromUsart); static void RefereeRxCallback(); +uint8_t UI_Seq = 0; // 包序号,供整个referee文件使用 + /* 裁判系统通信初始化 */ referee_info_t *RefereeInit(UART_HandleTypeDef *referee_usart_handle) { @@ -37,9 +39,9 @@ referee_info_t *RefereeInit(UART_HandleTypeDef *referee_usart_handle) * @brief 发送函数 * @param send 待发送数据 */ -void RefereeSend(uint8_t *send,uint16_t tx_len) +void RefereeSend(uint8_t *send, uint16_t tx_len) { - USARTSend(referee_usart_instance,send,tx_len); + USARTSend(referee_usart_instance, send, tx_len); /* syhtodo DMA请求过快会导致数据发送丢失,考虑数据尽可能打成一个整包以及队列发送,并且发送函数添加缓冲区 */ HAL_Delay(5); } @@ -58,7 +60,7 @@ static void RefereeRxCallback() */ static void JudgeReadData(uint8_t *ReadFromUsart) { - uint16_t judge_length; // 统计一帧数据长度 + uint16_t judge_length; // 统计一帧数据长度 if (ReadFromUsart == NULL) // 空数据包,则不作任何处理 return; @@ -118,6 +120,9 @@ static void JudgeReadData(uint8_t *ReadFromUsart) case ID_shoot_data: // 0x0207 memcpy(&referee_info.ShootData, (ReadFromUsart + DATA_Offset), LEN_shoot_data); break; + case ID_student_interactive: // 0x0301 syhtodo接收代码未测试 + memcpy(&referee_info.ReceiveData, (ReadFromUsart + DATA_Offset), LEN_receive_data); + break; } } } diff --git a/modules/referee/rm_referee.h b/modules/referee/rm_referee.h index 0c22a1f..16fb381 100644 --- a/modules/referee/rm_referee.h +++ b/modules/referee/rm_referee.h @@ -4,23 +4,22 @@ #include "usart.h" #include "referee_def.h" #include "bsp_usart.h" -#include "robot_def.h" +extern uint8_t UI_Seq; #pragma pack(1) - typedef struct { uint8_t Robot_Color; //机器人颜色 uint16_t Robot_ID; //本机器人ID uint16_t Cilent_ID; //本机器人对应的客户端ID - uint16_t Receiver_Robot_ID; //机器人车间通信时接收者的ID + uint16_t Receiver_Robot_ID; //机器人车间通信时接收者的ID,必须和本机器人同颜色 } referee_id_t; // 此结构体包含裁判系统接收数据以及UI绘制与机器人车间通信的相关信息 typedef struct { referee_id_t referee_id; - + xFrameHeader FrameHeader; // 接收到的帧头信息 uint16_t CmdID; ext_game_state_t GameState; // 0x0001 @@ -36,7 +35,8 @@ typedef struct ext_robot_hurt_t RobotHurt; // 0x0206 ext_shoot_data_t ShootData; // 0x0207 - // syhtodo 机器人间通信如何获取数据 + //自定义交互数据的接收 + Communicate_ReceiveData_t ReceiveData; } referee_info_t; @@ -57,6 +57,4 @@ referee_info_t *RefereeInit(UART_HandleTypeDef *referee_usart_handle); */ void RefereeSend(uint8_t *send, uint16_t tx_len); -extern USARTInstance *referee_usart_instance; - #endif // !REFEREE_H