From e3137d867f8263d7cf55320429ec0c63f1a33f21 Mon Sep 17 00:00:00 2001 From: NeoZeng Date: Sat, 19 Nov 2022 16:28:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86bsp=5Fusart=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=96=B9=E5=BC=8F,=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=87=BA=E7=8E=B0=E5=8F=98=E5=8C=85=E9=95=BF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=AF=BB=E5=8F=96=E4=B8=80=E5=9C=BA=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98.=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=B3=A8=E9=87=8A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- HAL_N_Middlewares/Src/main.c | 2 +- README.md | 2 + bsp/bsp_usart.c | 3 +- bsp/bsp_usart.h | 1 + modules/motor/dji_motor.c | 3 +- modules/referee/referee.c | 57 ++++++++----------- modules/referee/referee.h | 107 +++++++++++++++++------------------ 8 files changed, 85 insertions(+), 93 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 11f2092..e7f09d4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,8 @@ "memory.h": "c", "stdint-gcc.h": "c", "led_task.h": "c", - "stdinf.h": "c" + "stdinf.h": "c", + "usart.h": "c" }, "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools", } \ No newline at end of file diff --git a/HAL_N_Middlewares/Src/main.c b/HAL_N_Middlewares/Src/main.c index b8c8b3f..f98c2b0 100644 --- a/HAL_N_Middlewares/Src/main.c +++ b/HAL_N_Middlewares/Src/main.c @@ -127,7 +127,7 @@ int main(void) .controller_param_init_config = {.current_PID = {.Improve = 0, .Kp = 1, .Ki = 0, .Kd = 0, .DeadBand = 0, .MaxOut = 4000}, .speed_PID = {.Improve = 0, .Kp = 1, .Ki = 0, .Kd = 0, .DeadBand = 0, .MaxOut = 4000}}}; dji_motor_instance *djimotor = DJIMotorInit(config); - referee_init(&huart6); + RefereeInit(&huart6); /* USER CODE END 2 */ /* Call init function for freertos objects (in freertos.c) */ diff --git a/README.md b/README.md index 17992c8..4ef5a58 100644 --- a/README.md +++ b/README.md @@ -259,3 +259,5 @@ HAL库初始化 --> BSP初始化 --> Application初始化 --> app调用其拥有 **注意,应用初始化不得放入其对应任务中,即使是在死循环前,否则可能导致一些需要定时器的任务初始化异常**。 APP会调用其所有的模块的初始化函数(注册函数),这是因为本框架的设计思想是任何模块在被注册(构造/初始化)之前,都是不存在的,当且仅当定义了一个模块结构体(也称实例)的时候,才有一个实体的概念。 + +> 参考了哈工深南宫小樱战队的框架设计,在此鸣谢。 \ No newline at end of file diff --git a/bsp/bsp_usart.c b/bsp/bsp_usart.c index 3470139..20a0ba6 100644 --- a/bsp/bsp_usart.c +++ b/bsp/bsp_usart.c @@ -67,8 +67,7 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) if (huart == instance[i]->usart_handle) { instance[i]->module_callback(); - memset(instance[i]->recv_buff,0,instance[i]->recv_buff_size); - + memset(instance[i]->recv_buff,0,instance[i]->recv_buff_size); // 接收结束后清空buffer,对于变长数据是必要的 HAL_UARTEx_ReceiveToIdle_DMA(instance[i]->usart_handle, instance[i]->recv_buff, instance[i]->recv_buff_size); __HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT); break; diff --git a/bsp/bsp_usart.h b/bsp/bsp_usart.h index 9e8ff29..9d4e6a6 100644 --- a/bsp/bsp_usart.h +++ b/bsp/bsp_usart.h @@ -13,6 +13,7 @@ typedef void (*usart_module_callback)(); /* usart_instance struct,each app would have one instance */ typedef struct { + // 更新:弃用malloc方案,使用了固定大小的数组方便debug时查看 uint8_t recv_buff[USART_RXBUFF_LIMIT]; // 预先定义的最大buff大小,如果太小请修改USART_RXBUFF_LIMIT uint8_t recv_buff_size; // 模块接收一包数据的大小 UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle diff --git a/modules/motor/dji_motor.c b/modules/motor/dji_motor.c index a17ca4b..2077470 100644 --- a/modules/motor/dji_motor.c +++ b/modules/motor/dji_motor.c @@ -209,6 +209,7 @@ void DJIMotorControl() motor_controller = &motor->motor_controller; motor_measure = &motor->motor_measure; + // pid_ref会顺次通过被启用的环充当数据的载体 if (motor_setting->close_loop_type & ANGLE_LOOP) // 计算位置环 { if (motor_setting->angle_feedback_source == OTHER_FEED) @@ -233,7 +234,7 @@ void DJIMotorControl() motor_controller->pid_ref = PID_Calculate(&motor_controller->current_PID, motor_measure->given_current, motor_controller->pid_ref); } - set = (int16_t)motor_controller->pid_ref; + set = (int16_t)motor_controller->pid_ref; // 获取最终输出 if (motor_setting->reverse_flag == MOTOR_DIRECTION_REVERSE) // 设置反转 set *= -1; diff --git a/modules/referee/referee.c b/modules/referee/referee.c index 6e75f30..96f65a9 100644 --- a/modules/referee/referee.c +++ b/modules/referee/referee.c @@ -4,8 +4,7 @@ #include "bsp_usart.h" #include "dma.h" - -//参考深圳大学 Infantry_X-master +// 参考深圳大学 Infantry_X-master #define RE_RX_BUFFER_SIZE 200 // static usart_instance referee_usart_instance; @@ -13,17 +12,15 @@ usart_instance referee_usart_instance; /**************裁判系统数据******************/ referee_info_t referee_info; -uint8_t Judge_Self_ID; //当前机器人的ID -uint16_t Judge_SelfClient_ID; //发送者机器人对应的客户端ID - - +uint8_t Judge_Self_ID; // 当前机器人的ID +uint16_t Judge_SelfClient_ID; // 发送者机器人对应的客户端ID static void ReceiveCallback() { - Judge_Read_Data(referee_usart_instance.recv_buff); + JudgeReadData(referee_usart_instance.recv_buff); } -void referee_init(UART_HandleTypeDef *referee_usart_handle) +void RefereeInit(UART_HandleTypeDef *referee_usart_handle) { referee_usart_instance.module_callback = ReceiveCallback; referee_usart_instance.usart_handle = referee_usart_handle; @@ -31,41 +28,38 @@ void referee_init(UART_HandleTypeDef *referee_usart_handle) USARTRegister(&referee_usart_instance); } - /** * @brief 读取裁判数据,中断中读取保证速度 * @param 缓存数据 * @retval 是否对正误判断做处理 * @attention 在此判断帧头和CRC校验,无误再写入数据,不重复判断帧头 */ -void Judge_Read_Data(uint8_t *ReadFromUsart) +void JudgeReadData(uint8_t *ReadFromUsart) { - uint16_t judge_length; //统计一帧数据长度 -// referee_info.CmdID = 0; //数据命令码解析 - //空数据包,则不作任何处理 + uint16_t judge_length; // 统计一帧数据长度 + // referee_info.CmdID = 0; //数据命令码解析 + // 空数据包,则不作任何处理 if (ReadFromUsart == NULL) - { - return -1; - } - - //写入帧头数据(5-byte),用于判断是否开始存储裁判数据 + return; + + // 写入帧头数据(5-byte),用于判断是否开始存储裁判数据 memcpy(&referee_info.FrameHeader, ReadFromUsart, LEN_HEADER); - //判断帧头数据(0)是否为0xA5 - if (ReadFromUsart[SOF] == JUDGE_FRAME_HEADER) + // 判断帧头数据(0)是否为0xA5 + if (ReadFromUsart[SOF] == JUDGE_FRAME_HEADER) { - //帧头CRC8校验 + // 帧头CRC8校验 if (Verify_CRC8_Check_Sum(ReadFromUsart, LEN_HEADER) == TRUE) { - //统计一帧数据长度(byte),用于CR16校验 + // 统计一帧数据长度(byte),用于CR16校验 judge_length = ReadFromUsart[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL; - //帧尾CRC16校验 + // 帧尾CRC16校验 if (Verify_CRC16_Check_Sum(ReadFromUsart, judge_length) == TRUE) { // 2个8位拼成16位int referee_info.CmdID = (ReadFromUsart[6] << 8 | ReadFromUsart[5]); - //解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度) - //第8个字节开始才是数据 data=7 + // 解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度) + // 第8个字节开始才是数据 data=7 switch (referee_info.CmdID) { case ID_game_state: // 0x0001 @@ -89,10 +83,10 @@ void Judge_Read_Data(uint8_t *ReadFromUsart) break; case ID_game_robot_state: // 0x0201 - memcpy(&referee_info.GameRobotStat,(ReadFromUsart + DATA),LEN_game_robot_state); + memcpy(&referee_info.GameRobotStat, (ReadFromUsart + DATA), LEN_game_robot_state); break; case ID_power_heat_data: // 0x0202 - memcpy(&referee_info.PowerHeatData, (ReadFromUsart + DATA), LEN_power_heat_data); + memcpy(&referee_info.PowerHeatData, (ReadFromUsart + DATA), LEN_power_heat_data); break; case ID_game_robot_pos: // 0x0203 @@ -118,12 +112,11 @@ void Judge_Read_Data(uint8_t *ReadFromUsart) } } } - //首地址加帧长度,指向CRC16下一字节,用来判断是否为0xA5,用来判断一个数据包是否有多帧数据 + // 首地址加帧长度,指向CRC16下一字节,用来判断是否为0xA5,用来判断一个数据包是否有多帧数据 if (*(ReadFromUsart + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL) == 0xA5) { - //如果一个数据包出现了多帧数据,则再次读取 - Judge_Read_Data(ReadFromUsart + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL); - + // 如果一个数据包出现了多帧数据,则再次读取 + JudgeReadData(ReadFromUsart + sizeof(xFrameHeader) + LEN_CMDID + referee_info.FrameHeader.DataLength + LEN_TAIL); } } } @@ -134,7 +127,7 @@ void Judge_Read_Data(uint8_t *ReadFromUsart) * @retval 实时功率值 * @attention */ -float JUDGE_fGetChassisPower(void) +float JudgeGetChassisPower(void) { return (referee_info.PowerHeatData.chassis_power); } diff --git a/modules/referee/referee.h b/modules/referee/referee.h index c93d30f..ec05993 100644 --- a/modules/referee/referee.h +++ b/modules/referee/referee.h @@ -1,19 +1,19 @@ /** * @file referee.h * @author kidneygood (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2022-11-18 - * + * * @copyright Copyright (c) 2022 - * + * */ #ifndef REFEREE_H #define REFEREE_H -#include"bsp_usart.h" -#include"usart.h" +#include "bsp_usart.h" +#include "usart.h" #define FALSE 0 #define TRUE 1 @@ -21,12 +21,13 @@ #define JUDGE_DATA_ERROR 0 #define JUDGE_DATA_CORRECT 1 -#define LEN_HEADER 5 //帧头长 -#define LEN_CMDID 2 //命令码长度 -#define LEN_TAIL 2 //帧尾CRC16 +#define LEN_HEADER 5 // 帧头长 +#define LEN_CMDID 2 // 命令码长度 +#define LEN_TAIL 2 // 帧尾CRC16 -//起始字节,协议固定为0xA5 +// 起始字节,协议固定为0xA5 #define JUDGE_FRAME_HEADER (0xA5) + #pragma pack(1) /***************裁判系统数据接收********************/ @@ -41,33 +42,33 @@ typedef enum // 5字节帧头,偏移位置 typedef enum { - SOF = 0, //起始位 - DATA_LENGTH = 1, //帧内数据长度,根据这个来获取数据长度 - SEQ = 3, //包序号 + SOF = 0, // 起始位 + DATA_LENGTH = 1, // 帧内数据长度,根据这个来获取数据长度 + SEQ = 3, // 包序号 CRC8 = 4 // CRC8 } FrameHeaderOffset; -//命令码ID,用来判断接收的是什么数据 +// 命令码ID,用来判断接收的是什么数据 typedef enum { - ID_game_state = 0x0001, //比赛状态数据 - ID_game_result = 0x0002, //比赛结果数据 - ID_game_robot_survivors = 0x0003, //比赛机器人血量数据 - ID_event_data = 0x0101, //场地事件数据 - ID_supply_projectile_action = 0x0102, //场地补给站动作标识数据 - ID_supply_projectile_booking = 0x0103, //场地补给站预约子弹数据 - ID_game_robot_state = 0x0201, //机器人状态数据 - ID_power_heat_data = 0x0202, //实时功率热量数据 - ID_game_robot_pos = 0x0203, //机器人位置数据 - ID_buff_musk = 0x0204, //机器人增益数据 - ID_aerial_robot_energy = 0x0205, //空中机器人能量状态数据 - ID_robot_hurt = 0x0206, //伤害状态数据 - ID_shoot_data = 0x0207, //实时射击数据 + ID_game_state = 0x0001, // 比赛状态数据 + ID_game_result = 0x0002, // 比赛结果数据 + ID_game_robot_survivors = 0x0003, // 比赛机器人血量数据 + ID_event_data = 0x0101, // 场地事件数据 + ID_supply_projectile_action = 0x0102, // 场地补给站动作标识数据 + ID_supply_projectile_booking = 0x0103, // 场地补给站预约子弹数据 + ID_game_robot_state = 0x0201, // 机器人状态数据 + ID_power_heat_data = 0x0202, // 实时功率热量数据 + ID_game_robot_pos = 0x0203, // 机器人位置数据 + ID_buff_musk = 0x0204, // 机器人增益数据 + ID_aerial_robot_energy = 0x0205, // 空中机器人能量状态数据 + ID_robot_hurt = 0x0206, // 伤害状态数据 + ID_shoot_data = 0x0207, // 实时射击数据 } CmdID; -//命令码数据段长,根据官方协议来定义长度 +// 命令码数据段长,根据官方协议来定义长度 typedef enum { LEN_game_state = 3, // 0x0001 @@ -86,7 +87,7 @@ typedef enum } JudgeDataLength; /* 自定义帧头 */ -typedef struct +typedef struct { uint8_t SOF; uint16_t DataLength; @@ -172,7 +173,7 @@ typedef struct { uint16_t chassis_volt; uint16_t chassis_current; - float chassis_power; //瞬时功率 + float chassis_power; // 瞬时功率 uint16_t chassis_power_buffer; // 60焦耳缓冲能量 uint16_t shooter_heat0; // 17mm uint16_t shooter_heat1; @@ -293,34 +294,34 @@ typedef struct */ typedef struct { - uint8_t data[10]; //数据段,n需要小于113 + uint8_t data[10]; // 数据段,n需要小于113 } robot_interactive_data_t; -//发送给客户端的信息 -//帧头 命令码 数据段头结构 数据段 帧尾 +// 发送给客户端的信息 +// 帧头 命令码 数据段头结构 数据段 帧尾 typedef struct { - xFrameHeader txFrameHeader; //帧头 - uint16_t CmdID; //命令码 - ext_student_interactive_header_data_t dataFrameHeader; //数据段头结构 - client_custom_data_t clientData; //数据段 - uint16_t FrameTail; //帧尾 + 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; //帧尾 + xFrameHeader txFrameHeader; // 帧头 + uint16_t CmdID; // 命令码 + ext_student_interactive_header_data_t dataFrameHeader; // 数据段头结构 + robot_interactive_data_t interactData; // 数据段 + uint16_t FrameTail; // 帧尾 } ext_CommunatianData_t; -//裁判系统接收数据整合进一个结构体 +// 裁判系统接收数据整合进一个结构体 typedef struct { - xFrameHeader FrameHeader; //接收到的帧头信息 + xFrameHeader FrameHeader; // 接收到的帧头信息 uint16_t CmdID; ext_game_state_t GameState; // 0x0001 ext_game_result_t GameResult; // 0x0002 @@ -335,20 +336,14 @@ typedef struct ext_robot_hurt_t RobotHurt; // 0x0206 ext_shoot_data_t ShootData; // 0x0207 - ext_SendClientData_t ShowData; //客户端信息 - ext_CommunatianData_t CommuData; //队友通信信息 + ext_SendClientData_t ShowData; // 客户端信息 + ext_CommunatianData_t CommuData; // 队友通信信息 } referee_info_t; extern referee_info_t referee_info; -void referee_init(UART_HandleTypeDef *referee_usart_handle); -void Judge_Read_Data(uint8_t *ReadFromUsart); -float JUDGE_fGetChassisPower(void); +void RefereeInit(UART_HandleTypeDef *referee_usart_handle); +void JudgeReadData(uint8_t *ReadFromUsart); +float JudgeGetChassisPower(void); #endif // !REFEREE_H - - - - - -