自定义交互数据、机器人车间通信的发送和接收,仅测试发送

This commit is contained in:
kidneygood 2023-01-26 19:52:07 +08:00
parent 67940a4e38
commit dc68657c16
7 changed files with 154 additions and 145 deletions

View File

@ -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;
}

View File

@ -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;i<cnt;i++) //发送交互数据的数据帧并计算CRC16校验值
{
@ -397,7 +407,7 @@ void Char_ReFresh(referee_id_t *_id,String_Data_t string_Data)
{
UI_CharReFresh_t UI_CharReFresh_data;
uint8_t temp_datalength = UI_Data_LEN_Head + UI_Operate_LEN_DrawChar; //计算交互数据长度
uint8_t temp_datalength = Interactive_Data_LEN_Head + UI_Operate_LEN_DrawChar; //计算交互数据长度
UI_CharReFresh_data.FrameHeader.SOF = REFEREE_SOF;
UI_CharReFresh_data.FrameHeader.DataLength = temp_datalength;

View File

@ -1,51 +1,45 @@
/**
* @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
*
*/
#include "referee_communication.h"
#include "crc.h"
#include "stdio.h"
#include "rm_referee.h"
/**
* @brief
* @param referee_id_t *_id sender为本机器人receiver为接收方机器人Receiver_Robot_ID再调用该函数
* robot_interactive_data_t *data
* @retval none
* @attention
*/
void Communicate_SendData(referee_id_t *_id,robot_interactive_data_t *_data)
{
Communicate_SendData_t SendData;
uint8_t temp_datalength = Interactive_Data_LEN_Head + Communicate_Data_LEN; // 计算交互数据长度 6+n,n为交互数据长度
SendData.FrameHeader.SOF = REFEREE_SOF;
SendData.FrameHeader.DataLength = temp_datalength;
SendData.FrameHeader.Seq = UI_Seq;
SendData.FrameHeader.CRC8 = Get_CRC8_Check_Sum((uint8_t *)&SendData, LEN_CRC8, 0xFF);
// #define send_max_len 200
// unsigned char CliendTxBuffer[send_max_len];
// void JUDGE_Show_Data(void)
// {
// static uint8_t datalength,i;
// uint8_t judge_led = 0xff;//初始化led为全绿
// static uint8_t auto_led_time = 0;
// static uint8_t buff_led_time = 0;
// determine_ID();//判断发送者ID和其对应的客户端ID
// ShowData.txFrameHeader.SOF = 0xA5;
// ShowData.txFrameHeader.DataLength = sizeof(ext_student_interactive_header_data_t) + sizeof(client_custom_data_t);
// ShowData.txFrameHeader.Seq = 0;
// memcpy(CliendTxBuffer, &ShowData.txFrameHeader, sizeof(xFrameHeader));//写入帧头数据
// Append_CRC8_Check_Sum(CliendTxBuffer, sizeof(xFrameHeader));//写入帧头CRC8校验码
// ShowData.CmdID = 0x0301;
// ShowData.dataFrameHeader.data_cmd_id = 0xD180;//发给客户端的cmd,官方固定
// //ID已经是自动读取的了
// ShowData.dataFrameHeader.send_ID = Judge_Self_ID;//发送者的ID
// ShowData.dataFrameHeader.receiver_ID = Judge_SelfClient_ID;//客户端的ID只能为发送者机器人对应的客户端
// /*- 自定义内容 -*/
// ShowData.clientData.data1 = (float)Capvoltage_Percent();//电容剩余电量
// ShowData.clientData.data2 = (float)Base_Angle_Measure();//吊射角度测
// ShowData.clientData.data3 = GIMBAL_PITCH_Judge_Angle();//云台抬头角度
// ShowData.clientData.masks = judge_led;//0~5位0红灯,1绿灯
// //打包写入数据段
// memcpy(
// CliendTxBuffer + 5,
// (uint8_t*)&ShowData.CmdID,
// (sizeof(ShowData.CmdID)+ sizeof(ShowData.dataFrameHeader)+ sizeof(ShowData.clientData))
// );
// Append_CRC16_Check_Sum(CliendTxBuffer,sizeof(ShowData));//写入数据段CRC16校验码
SendData.CmdID = ID_student_interactive;
// datalength = sizeof(ShowData);
// for(i = 0;i < datalength;i++)
// {
// USART_SendData(UART5,(uint16_t)CliendTxBuffer[i]);
// while(USART_GetFlagStatus(UART5,USART_FLAG_TC)==RESET);
// }
// }
SendData.datahead.data_cmd_id = Communicate_Data_ID;
SendData.datahead.sender_ID = _id->Robot_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
}

View File

@ -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
#endif

View File

@ -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交互数据****************************/

View File

@ -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;
}
}
}

View File

@ -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