修改了bsp_usart的初始化方式,防止出现变包长数据读取一场的问题.增加了注释.

This commit is contained in:
NeoZeng 2022-11-19 16:28:39 +08:00
parent 269b08f701
commit e3137d867f
8 changed files with 85 additions and 93 deletions

View File

@ -3,7 +3,8 @@
"memory.h": "c", "memory.h": "c",
"stdint-gcc.h": "c", "stdint-gcc.h": "c",
"led_task.h": "c", "led_task.h": "c",
"stdinf.h": "c" "stdinf.h": "c",
"usart.h": "c"
}, },
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools", "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools",
} }

View File

@ -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}}}; .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); dji_motor_instance *djimotor = DJIMotorInit(config);
referee_init(&huart6); RefereeInit(&huart6);
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Call init function for freertos objects (in freertos.c) */ /* Call init function for freertos objects (in freertos.c) */

View File

@ -259,3 +259,5 @@ HAL库初始化 --> BSP初始化 --> Application初始化 --> app调用其拥有
**注意,应用初始化不得放入其对应任务中,即使是在死循环前,否则可能导致一些需要定时器的任务初始化异常**。 **注意,应用初始化不得放入其对应任务中,即使是在死循环前,否则可能导致一些需要定时器的任务初始化异常**。
APP会调用其所有的模块的初始化函数注册函数这是因为本框架的设计思想是任何模块在被注册构造/初始化)之前,都是不存在的,当且仅当定义了一个模块结构体(也称实例)的时候,才有一个实体的概念。 APP会调用其所有的模块的初始化函数注册函数这是因为本框架的设计思想是任何模块在被注册构造/初始化)之前,都是不存在的,当且仅当定义了一个模块结构体(也称实例)的时候,才有一个实体的概念。
> 参考了哈工深南宫小樱战队的框架设计,在此鸣谢。

View File

@ -67,8 +67,7 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
if (huart == instance[i]->usart_handle) if (huart == instance[i]->usart_handle)
{ {
instance[i]->module_callback(); 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_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); __HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT);
break; break;

View File

@ -13,6 +13,7 @@ typedef void (*usart_module_callback)();
/* usart_instance struct,each app would have one instance */ /* usart_instance struct,each app would have one instance */
typedef struct typedef struct
{ {
// 更新:弃用malloc方案,使用了固定大小的数组方便debug时查看
uint8_t recv_buff[USART_RXBUFF_LIMIT]; // 预先定义的最大buff大小,如果太小请修改USART_RXBUFF_LIMIT uint8_t recv_buff[USART_RXBUFF_LIMIT]; // 预先定义的最大buff大小,如果太小请修改USART_RXBUFF_LIMIT
uint8_t recv_buff_size; // 模块接收一包数据的大小 uint8_t recv_buff_size; // 模块接收一包数据的大小
UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle UART_HandleTypeDef *usart_handle; // 实例对应的usart_handle

View File

@ -209,6 +209,7 @@ void DJIMotorControl()
motor_controller = &motor->motor_controller; motor_controller = &motor->motor_controller;
motor_measure = &motor->motor_measure; motor_measure = &motor->motor_measure;
// pid_ref会顺次通过被启用的环充当数据的载体
if (motor_setting->close_loop_type & ANGLE_LOOP) // 计算位置环 if (motor_setting->close_loop_type & ANGLE_LOOP) // 计算位置环
{ {
if (motor_setting->angle_feedback_source == OTHER_FEED) 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); 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) // 设置反转 if (motor_setting->reverse_flag == MOTOR_DIRECTION_REVERSE) // 设置反转
set *= -1; set *= -1;

View File

@ -4,8 +4,7 @@
#include "bsp_usart.h" #include "bsp_usart.h"
#include "dma.h" #include "dma.h"
// 参考深圳大学 Infantry_X-master
//参考深圳大学 Infantry_X-master
#define RE_RX_BUFFER_SIZE 200 #define RE_RX_BUFFER_SIZE 200
// static usart_instance referee_usart_instance; // static usart_instance referee_usart_instance;
@ -13,17 +12,15 @@ usart_instance referee_usart_instance;
/**************裁判系统数据******************/ /**************裁判系统数据******************/
referee_info_t referee_info; referee_info_t referee_info;
uint8_t Judge_Self_ID; //当前机器人的ID uint8_t Judge_Self_ID; // 当前机器人的ID
uint16_t Judge_SelfClient_ID; //发送者机器人对应的客户端ID uint16_t Judge_SelfClient_ID; // 发送者机器人对应的客户端ID
static void ReceiveCallback() 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.module_callback = ReceiveCallback;
referee_usart_instance.usart_handle = referee_usart_handle; referee_usart_instance.usart_handle = referee_usart_handle;
@ -31,41 +28,38 @@ void referee_init(UART_HandleTypeDef *referee_usart_handle)
USARTRegister(&referee_usart_instance); USARTRegister(&referee_usart_instance);
} }
/** /**
* @brief , * @brief ,
* @param * @param
* @retval * @retval
* @attention CRC校验, * @attention CRC校验,
*/ */
void Judge_Read_Data(uint8_t *ReadFromUsart) void JudgeReadData(uint8_t *ReadFromUsart)
{ {
uint16_t judge_length; //统计一帧数据长度 uint16_t judge_length; // 统计一帧数据长度
// referee_info.CmdID = 0; //数据命令码解析 // referee_info.CmdID = 0; //数据命令码解析
//空数据包,则不作任何处理 // 空数据包,则不作任何处理
if (ReadFromUsart == NULL) if (ReadFromUsart == NULL)
{ return;
return -1;
} // 写入帧头数据(5-byte),用于判断是否开始存储裁判数据
//写入帧头数据(5-byte),用于判断是否开始存储裁判数据
memcpy(&referee_info.FrameHeader, ReadFromUsart, LEN_HEADER); memcpy(&referee_info.FrameHeader, ReadFromUsart, LEN_HEADER);
//判断帧头数据(0)是否为0xA5 // 判断帧头数据(0)是否为0xA5
if (ReadFromUsart[SOF] == JUDGE_FRAME_HEADER) if (ReadFromUsart[SOF] == JUDGE_FRAME_HEADER)
{ {
//帧头CRC8校验 // 帧头CRC8校验
if (Verify_CRC8_Check_Sum(ReadFromUsart, LEN_HEADER) == TRUE) if (Verify_CRC8_Check_Sum(ReadFromUsart, LEN_HEADER) == TRUE)
{ {
//统计一帧数据长度(byte),用于CR16校验 // 统计一帧数据长度(byte),用于CR16校验
judge_length = ReadFromUsart[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL; judge_length = ReadFromUsart[DATA_LENGTH] + LEN_HEADER + LEN_CMDID + LEN_TAIL;
//帧尾CRC16校验 // 帧尾CRC16校验
if (Verify_CRC16_Check_Sum(ReadFromUsart, judge_length) == TRUE) if (Verify_CRC16_Check_Sum(ReadFromUsart, judge_length) == TRUE)
{ {
// 2个8位拼成16位int // 2个8位拼成16位int
referee_info.CmdID = (ReadFromUsart[6] << 8 | ReadFromUsart[5]); referee_info.CmdID = (ReadFromUsart[6] << 8 | ReadFromUsart[5]);
//解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度) // 解析数据命令码,将数据拷贝到相应结构体中(注意拷贝数据的长度)
//第8个字节开始才是数据 data=7 // 第8个字节开始才是数据 data=7
switch (referee_info.CmdID) switch (referee_info.CmdID)
{ {
case ID_game_state: // 0x0001 case ID_game_state: // 0x0001
@ -89,10 +83,10 @@ void Judge_Read_Data(uint8_t *ReadFromUsart)
break; break;
case ID_game_robot_state: // 0x0201 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; break;
case ID_power_heat_data: // 0x0202 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; break;
case ID_game_robot_pos: // 0x0203 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) 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 * @retval
* @attention * @attention
*/ */
float JUDGE_fGetChassisPower(void) float JudgeGetChassisPower(void)
{ {
return (referee_info.PowerHeatData.chassis_power); return (referee_info.PowerHeatData.chassis_power);
} }

View File

@ -1,19 +1,19 @@
/** /**
* @file referee.h * @file referee.h
* @author kidneygood (you@domain.com) * @author kidneygood (you@domain.com)
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2022-11-18 * @date 2022-11-18
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#ifndef REFEREE_H #ifndef REFEREE_H
#define REFEREE_H #define REFEREE_H
#include"bsp_usart.h" #include "bsp_usart.h"
#include"usart.h" #include "usart.h"
#define FALSE 0 #define FALSE 0
#define TRUE 1 #define TRUE 1
@ -21,12 +21,13 @@
#define JUDGE_DATA_ERROR 0 #define JUDGE_DATA_ERROR 0
#define JUDGE_DATA_CORRECT 1 #define JUDGE_DATA_CORRECT 1
#define LEN_HEADER 5 //帧头长 #define LEN_HEADER 5 // 帧头长
#define LEN_CMDID 2 //命令码长度 #define LEN_CMDID 2 // 命令码长度
#define LEN_TAIL 2 //帧尾CRC16 #define LEN_TAIL 2 // 帧尾CRC16
//起始字节,协议固定为0xA5 // 起始字节,协议固定为0xA5
#define JUDGE_FRAME_HEADER (0xA5) #define JUDGE_FRAME_HEADER (0xA5)
#pragma pack(1) #pragma pack(1)
/***************裁判系统数据接收********************/ /***************裁判系统数据接收********************/
@ -41,33 +42,33 @@ typedef enum
// 5字节帧头,偏移位置 // 5字节帧头,偏移位置
typedef enum typedef enum
{ {
SOF = 0, //起始位 SOF = 0, // 起始位
DATA_LENGTH = 1, //帧内数据长度,根据这个来获取数据长度 DATA_LENGTH = 1, // 帧内数据长度,根据这个来获取数据长度
SEQ = 3, //包序号 SEQ = 3, // 包序号
CRC8 = 4 // CRC8 CRC8 = 4 // CRC8
} FrameHeaderOffset; } FrameHeaderOffset;
//命令码ID,用来判断接收的是什么数据 // 命令码ID,用来判断接收的是什么数据
typedef enum typedef enum
{ {
ID_game_state = 0x0001, //比赛状态数据 ID_game_state = 0x0001, // 比赛状态数据
ID_game_result = 0x0002, //比赛结果数据 ID_game_result = 0x0002, // 比赛结果数据
ID_game_robot_survivors = 0x0003, //比赛机器人血量数据 ID_game_robot_survivors = 0x0003, // 比赛机器人血量数据
ID_event_data = 0x0101, //场地事件数据 ID_event_data = 0x0101, // 场地事件数据
ID_supply_projectile_action = 0x0102, //场地补给站动作标识数据 ID_supply_projectile_action = 0x0102, // 场地补给站动作标识数据
ID_supply_projectile_booking = 0x0103, //场地补给站预约子弹数据 ID_supply_projectile_booking = 0x0103, // 场地补给站预约子弹数据
ID_game_robot_state = 0x0201, //机器人状态数据 ID_game_robot_state = 0x0201, // 机器人状态数据
ID_power_heat_data = 0x0202, //实时功率热量数据 ID_power_heat_data = 0x0202, // 实时功率热量数据
ID_game_robot_pos = 0x0203, //机器人位置数据 ID_game_robot_pos = 0x0203, // 机器人位置数据
ID_buff_musk = 0x0204, //机器人增益数据 ID_buff_musk = 0x0204, // 机器人增益数据
ID_aerial_robot_energy = 0x0205, //空中机器人能量状态数据 ID_aerial_robot_energy = 0x0205, // 空中机器人能量状态数据
ID_robot_hurt = 0x0206, //伤害状态数据 ID_robot_hurt = 0x0206, // 伤害状态数据
ID_shoot_data = 0x0207, //实时射击数据 ID_shoot_data = 0x0207, // 实时射击数据
} CmdID; } CmdID;
//命令码数据段长,根据官方协议来定义长度 // 命令码数据段长,根据官方协议来定义长度
typedef enum typedef enum
{ {
LEN_game_state = 3, // 0x0001 LEN_game_state = 3, // 0x0001
@ -86,7 +87,7 @@ typedef enum
} JudgeDataLength; } JudgeDataLength;
/* 自定义帧头 */ /* 自定义帧头 */
typedef struct typedef struct
{ {
uint8_t SOF; uint8_t SOF;
uint16_t DataLength; uint16_t DataLength;
@ -172,7 +173,7 @@ typedef struct
{ {
uint16_t chassis_volt; uint16_t chassis_volt;
uint16_t chassis_current; uint16_t chassis_current;
float chassis_power; //瞬时功率 float chassis_power; // 瞬时功率
uint16_t chassis_power_buffer; // 60焦耳缓冲能量 uint16_t chassis_power_buffer; // 60焦耳缓冲能量
uint16_t shooter_heat0; // 17mm uint16_t shooter_heat0; // 17mm
uint16_t shooter_heat1; uint16_t shooter_heat1;
@ -293,34 +294,34 @@ typedef struct
*/ */
typedef struct typedef struct
{ {
uint8_t data[10]; //数据段,n需要小于113 uint8_t data[10]; // 数据段,n需要小于113
} robot_interactive_data_t; } robot_interactive_data_t;
//发送给客户端的信息 // 发送给客户端的信息
//帧头 命令码 数据段头结构 数据段 帧尾 // 帧头 命令码 数据段头结构 数据段 帧尾
typedef struct typedef struct
{ {
xFrameHeader txFrameHeader; //帧头 xFrameHeader txFrameHeader; // 帧头
uint16_t CmdID; //命令码 uint16_t CmdID; // 命令码
ext_student_interactive_header_data_t dataFrameHeader; //数据段头结构 ext_student_interactive_header_data_t dataFrameHeader; // 数据段头结构
client_custom_data_t clientData; //数据段 client_custom_data_t clientData; // 数据段
uint16_t FrameTail; //帧尾 uint16_t FrameTail; // 帧尾
} ext_SendClientData_t; } ext_SendClientData_t;
//机器人交互信息 // 机器人交互信息
typedef struct typedef struct
{ {
xFrameHeader txFrameHeader; //帧头 xFrameHeader txFrameHeader; // 帧头
uint16_t CmdID; //命令码 uint16_t CmdID; // 命令码
ext_student_interactive_header_data_t dataFrameHeader; //数据段头结构 ext_student_interactive_header_data_t dataFrameHeader; // 数据段头结构
robot_interactive_data_t interactData; //数据段 robot_interactive_data_t interactData; // 数据段
uint16_t FrameTail; //帧尾 uint16_t FrameTail; // 帧尾
} ext_CommunatianData_t; } ext_CommunatianData_t;
//裁判系统接收数据整合进一个结构体 // 裁判系统接收数据整合进一个结构体
typedef struct typedef struct
{ {
xFrameHeader FrameHeader; //接收到的帧头信息 xFrameHeader FrameHeader; // 接收到的帧头信息
uint16_t CmdID; uint16_t CmdID;
ext_game_state_t GameState; // 0x0001 ext_game_state_t GameState; // 0x0001
ext_game_result_t GameResult; // 0x0002 ext_game_result_t GameResult; // 0x0002
@ -335,20 +336,14 @@ typedef struct
ext_robot_hurt_t RobotHurt; // 0x0206 ext_robot_hurt_t RobotHurt; // 0x0206
ext_shoot_data_t ShootData; // 0x0207 ext_shoot_data_t ShootData; // 0x0207
ext_SendClientData_t ShowData; //客户端信息 ext_SendClientData_t ShowData; // 客户端信息
ext_CommunatianData_t CommuData; //队友通信信息 ext_CommunatianData_t CommuData; // 队友通信信息
} referee_info_t; } referee_info_t;
extern referee_info_t referee_info; extern referee_info_t referee_info;
void referee_init(UART_HandleTypeDef *referee_usart_handle); void RefereeInit(UART_HandleTypeDef *referee_usart_handle);
void Judge_Read_Data(uint8_t *ReadFromUsart); void JudgeReadData(uint8_t *ReadFromUsart);
float JUDGE_fGetChassisPower(void); float JudgeGetChassisPower(void);
#endif // !REFEREE_H #endif // !REFEREE_H