From 1f7274e7049a9e83a0d44aad6d02d9b16a6c7b34 Mon Sep 17 00:00:00 2001 From: jia-xie <103150184+jia-xie@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:55:38 -0400 Subject: [PATCH] referee add daemon protection --- src/bsp/Inc/bsp_uart.h | 2 +- src/bsp/Src/bsp_uart.c | 2 +- src/devices/inc/referee_system.h | 279 +++++++++++++++---------------- src/devices/src/referee_system.c | 46 +++-- 4 files changed, 167 insertions(+), 162 deletions(-) diff --git a/src/bsp/Inc/bsp_uart.h b/src/bsp/Inc/bsp_uart.h index 13d2c601..751a76cc 100644 --- a/src/bsp/Inc/bsp_uart.h +++ b/src/bsp/Inc/bsp_uart.h @@ -16,6 +16,6 @@ typedef struct _UART_Instance } UART_Instance_t; void UART_Service_Init(UART_Instance_t *uart_insatce); -UART_Instance_t *UART_Register(UART_HandleTypeDef *huart, uint8_t *rx_buffer, uint8_t rx_buffer_size, void (*callback)(UART_Instance_t *uart_instance)); +UART_Instance_t *UART_Register(UART_HandleTypeDef *huart, uint8_t *rx_buffer, uint16_t rx_buffer_size, void (*callback)(UART_Instance_t *uart_instance)); HAL_StatusTypeDef UART_Transmit(UART_Instance_t *uart_instance, uint8_t *tx_buffer, uint16_t tx_buffer_size, uint8_t send_type); #endif // BSP_UART_H \ No newline at end of file diff --git a/src/bsp/Src/bsp_uart.c b/src/bsp/Src/bsp_uart.c index 34b5d84f..95ae8104 100644 --- a/src/bsp/Src/bsp_uart.c +++ b/src/bsp/Src/bsp_uart.c @@ -41,7 +41,7 @@ void UART_Service_Init(UART_Instance_t *uart_insatce) * @param rx_buffer_size size of the buffer * @param callback callback function when UART receive is complete */ -UART_Instance_t *UART_Register(UART_HandleTypeDef *huart, uint8_t *rx_buffer, uint8_t rx_buffer_size, void (*callback)(UART_Instance_t *uart_instance)) +UART_Instance_t *UART_Register(UART_HandleTypeDef *huart, uint8_t *rx_buffer, uint16_t rx_buffer_size, void (*callback)(UART_Instance_t *uart_instance)) { UART_Instance_t *uart_instance = (UART_Instance_t *)malloc(sizeof(UART_Instance_t)); uart_instance->uart_handle = huart; diff --git a/src/devices/inc/referee_system.h b/src/devices/inc/referee_system.h index 1f441974..6c11c4fe 100644 --- a/src/devices/inc/referee_system.h +++ b/src/devices/inc/referee_system.h @@ -2,31 +2,22 @@ * @file Referee_System.h * @author Leo Liu * @brief header file for referee system - * @version 1.0 - * @date 2022-07-19 - * - * @copyright Copyright (c) 2022 - * + * @version 1.1 + * @date 2024-04-04 + * + * @copyright Copyright (c) 2024 + * */ #ifndef __REFEREE_SYSTEM_H #define __REFEREE_SYSTEM_H #include "bsp_crc.h" -#include "dma.h" -#include "usart.h" +#include "bsp_uart.h" #include #include -#define Referee_System_Func_GroundInit \ -{ \ - &Referee_UART_Receive_Interrupt, \ - &Referee_System_Handler, \ - &Referee_Get_Data, \ - &Referee_Set_Robot_State, \ -} - -//Standard Confrontation +// Standard Confrontation #define V1_STANDARD_POWER_MAX 120 #define V1_STANDARD_HP_MAX 200 #define V1_STANDARD_BULLET_SPEED_MAX 18 @@ -39,7 +30,7 @@ #define V1_SBR_HEAT_MAX 280 #define V1_SBR_COOLING_RATE 50 -//3V3 Confrontation +// 3V3 Confrontation #define V3_POWER_FOCUSED_LV1_HP_MAX 150 #define V3_POWER_FOCUSED_LV1_POWER_MAX 60 #define V3_POWER_FOCUSED_LV2_HP_MAX 200 @@ -91,164 +82,162 @@ #define V3_BULLET_SPEED_FOCUSED_V3_COOLING_RATE 35 #define V3_BULLET_SPEED_FOCUSED_V3_BULLET_SPEED_MAX 30 +// Referee System Communication Definition +#define REFEREE_BUFFER_LEN 389u // Buffer length to receive all data +#define REFEREE_FRAME_HEADER_START 0xA5 // Frame header +#define REFEREE_FRAME_HEADER_LEN 5 // Frame header length +#define REFEREE_ID_LEN 2 // ID length +#define REFEREE_FRAME_TAIL_LEN 2 // Frame tail length -//Referee System Communication Definition -#define REFEREE_BUFFER_LEN 389u //Buffer length to receive all data -#define REFEREE_FRAME_HEADER_START 0xA5 //Frame header -#define REFEREE_FRAME_HEADER_LEN 5 //Frame header length -#define REFEREE_ID_LEN 2 //ID length -#define REFEREE_FRAME_TAIL_LEN 2 //Frame tail length - -#define REFEREE_OFFSET_SOF 0 //SOF offset -#define REFEREE_OFFSET_DATA_LENGTH 1 //Data length offset -#define REFEREE_OFFSET_SEQ 3 //SEQ offset -#define REFEREE_OFFSET_CRC8 4 //CRC offset - -#define REFEREE_GAME_STATUS 0x0001 -#define REFEREE_GAME_RESULT 0x0002 -#define REFEREE_ROBOT_HP 0x0003 -#define REFEREE_DART_LAUNCH 0x0004 -#define REFEREE_AI_CHALLENGEBUFF 0x0005 -#define REFEREE_EVENT_DATA 0x0101 -#define REFEREE_SUPPLY_STATION 0x0102 -//#define REFEREE_REQUEST_RECHARGE 0x0103 -#define REFEREE_REFEREE_WARNING 0x0104 -#define REFEREE_DART_COUNTDOWN 0x0105 -#define REFEREE_ROBOT_STATE 0x0201 -#define REFEREE_POWER_HEAT 0x0202 -#define REFEREE_ROBOT_POSITION 0x0203 -#define REFEREE_ROBOT_BUFF 0x0204 -#define REFEREE_AERIAL_ENERGY 0x0205 -#define REFEREE_INJURY_STATE 0x0206 -#define REFEREE_SHOOTER_STATE 0x0207 -#define REFEREE_REMAINING_AMMO 0x0208 -#define REFEREE_ROBOT_RFID 0x0209 -#define REFEREE_DART_CLIENT 0x020A -#define REFEREE_ROBOT_COMMUNICATE 0x0301 -#define REFEREE_USER_DEFINED 0x0302 +#define REFEREE_OFFSET_SOF 0 // SOF offset +#define REFEREE_OFFSET_DATA_LENGTH 1 // Data length offset +#define REFEREE_OFFSET_SEQ 3 // SEQ offset +#define REFEREE_OFFSET_CRC8 4 // CRC offset +#define REFEREE_GAME_STATUS 0x0001 +#define REFEREE_GAME_RESULT 0x0002 +#define REFEREE_ROBOT_HP 0x0003 +#define REFEREE_DART_LAUNCH 0x0004 +#define REFEREE_AI_CHALLENGEBUFF 0x0005 +#define REFEREE_EVENT_DATA 0x0101 +#define REFEREE_SUPPLY_STATION 0x0102 +// #define REFEREE_REQUEST_RECHARGE 0x0103 +#define REFEREE_REFEREE_WARNING 0x0104 +#define REFEREE_DART_COUNTDOWN 0x0105 +#define REFEREE_ROBOT_STATE 0x0201 +#define REFEREE_POWER_HEAT 0x0202 +#define REFEREE_ROBOT_POSITION 0x0203 +#define REFEREE_ROBOT_BUFF 0x0204 +#define REFEREE_AERIAL_ENERGY 0x0205 +#define REFEREE_INJURY_STATE 0x0206 +#define REFEREE_SHOOTER_STATE 0x0207 +#define REFEREE_REMAINING_AMMO 0x0208 +#define REFEREE_ROBOT_RFID 0x0209 +#define REFEREE_DART_CLIENT 0x020A +#define REFEREE_ROBOT_COMMUNICATE 0x0301 +#define REFEREE_USER_DEFINED 0x0302 /*Calculation: REFEREE_FRAME_HEADER_LEN(5-byte) + REFEREE_ID_LEN(2-byte) + DATA(n-byte) + REFEREE_FRAME_TAIL_LEN(2-byte,CRC16)*/ -#define REFEREE_GAME_STATUS_LEN 20 -#define REFEREE_GAME_RESULT_LEN 10 -#define REFEREE_ROBOT_HP_LEN 41 -#define REFEREE_DART_LAUNCH_LEN 12 -#define REFEREE_AI_CHALLENGEBUFF_LEN 20 -#define REFEREE_EVENT_DATA_LEN 13 -#define REFEREE_SUPPLY_STATION_LEN 13 -//#define REFEREE_REQUEST_RECHARGE_LEN 11 -#define REFEREE_REFEREE_WARNING_LEN 11 -#define REFEREE_DART_COUNTDOWN_LEN 10 -#define REFEREE_ROBOT_STATE_LEN 36 -#define REFEREE_POWER_HEAT_LEN 25 -#define REFEREE_ROBOT_POSITION_LEN 25 -#define REFEREE_ROBOT_BUFF_LEN 10 -#define REFEREE_AERIAL_ENERGY_LEN 10 -#define REFEREE_INJURY_STATE_LEN 10 -#define REFEREE_SHOOTER_STATE_LEN 16 -#define REFEREE_REMAINING_AMMO_LEN 15 -#define REFEREE_ROBOT_RFID_LEN 13 -#define REFEREE_DART_CLIENT_LEN 21 -#define REFEREE_ROBOT_COMMUNICATE_LEN 35 -#define REFEREE_USER_DEFINED_LEN 26 +#define REFEREE_GAME_STATUS_LEN 20 +#define REFEREE_GAME_RESULT_LEN 10 +#define REFEREE_ROBOT_HP_LEN 41 +#define REFEREE_DART_LAUNCH_LEN 12 +#define REFEREE_AI_CHALLENGEBUFF_LEN 20 +#define REFEREE_EVENT_DATA_LEN 13 +#define REFEREE_SUPPLY_STATION_LEN 13 +// #define REFEREE_REQUEST_RECHARGE_LEN 11 +#define REFEREE_REFEREE_WARNING_LEN 11 +#define REFEREE_DART_COUNTDOWN_LEN 10 +#define REFEREE_ROBOT_STATE_LEN 36 +#define REFEREE_POWER_HEAT_LEN 25 +#define REFEREE_ROBOT_POSITION_LEN 25 +#define REFEREE_ROBOT_BUFF_LEN 10 +#define REFEREE_AERIAL_ENERGY_LEN 10 +#define REFEREE_INJURY_STATE_LEN 10 +#define REFEREE_SHOOTER_STATE_LEN 16 +#define REFEREE_REMAINING_AMMO_LEN 15 +#define REFEREE_ROBOT_RFID_LEN 13 +#define REFEREE_DART_CLIENT_LEN 21 +#define REFEREE_ROBOT_COMMUNICATE_LEN 35 +#define REFEREE_USER_DEFINED_LEN 26 typedef struct { - uint8_t Game_Type; //1 for 7v7, 4 for 3v3, 5 for 1v1 - uint8_t ID; //3,4,5 Red Standard - 103,104,105 Blue Standard + uint8_t Game_Type; // 1 for 7v7, 4 for 3v3, 5 for 1v1 + uint8_t ID; // 3,4,5 Red Standard - 103,104,105 Blue Standard uint8_t Level; - + uint16_t Cooling_Rate; uint16_t Heat_Max; uint16_t Bullet_Speed_Max; uint16_t Chassis_Power_Max; - + float Chassis_Power; float Power_Buffer; uint16_t Shooter_Heat; uint8_t Shooting_Frequency; float Shooting_Speed; -}Referee_Robot_State_t; +} Referee_Robot_State_t; typedef struct { UART_HandleTypeDef *huart; uint16_t DMA_Counter; uint8_t Buffer[REFEREE_BUFFER_LEN]; - - struct __attribute__ ((__packed__)) + + struct __attribute__((__packed__)) { uint8_t Type; uint8_t Progress; uint16_t Remaining_Time; uint64_t Sync_Time_Stamp; - }Game_Status; - - struct __attribute__ ((__packed__)) + } Game_Status; + + struct __attribute__((__packed__)) { uint8_t Winner; - }Game_Result; - - struct __attribute__ ((__packed__)) + } Game_Result; + + struct __attribute__((__packed__)) { - uint16_t Red_1_HP; - uint16_t Red_2_HP; - uint16_t Red_3_HP; - uint16_t Red_4_HP; - uint16_t Red_5_HP; - uint16_t Red_6_HP; - uint16_t Red_7_HP; - uint16_t Red_Outpost_HP; - uint16_t Red_Base_HP; - - uint16_t Blue_1_HP; - uint16_t Blue_2_HP; - uint16_t Blue_3_HP; - uint16_t Blue_4_HP; - uint16_t Blue_5_HP; - uint16_t Blue_6_HP; - uint16_t Blue_7_HP; - uint16_t Blue_Outpost_HP; - uint16_t Blue_Base_HP; - }Alive_Robot; - - struct __attribute__ ((__packed__)) + uint16_t Red_1_HP; + uint16_t Red_2_HP; + uint16_t Red_3_HP; + uint16_t Red_4_HP; + uint16_t Red_5_HP; + uint16_t Red_6_HP; + uint16_t Red_7_HP; + uint16_t Red_Outpost_HP; + uint16_t Red_Base_HP; + + uint16_t Blue_1_HP; + uint16_t Blue_2_HP; + uint16_t Blue_3_HP; + uint16_t Blue_4_HP; + uint16_t Blue_5_HP; + uint16_t Blue_6_HP; + uint16_t Blue_7_HP; + uint16_t Blue_Outpost_HP; + uint16_t Blue_Base_HP; + } Alive_Robot; + + struct __attribute__((__packed__)) { uint32_t Type; - }Event; - - struct __attribute__ ((__packed__)) + } Event; + + struct __attribute__((__packed__)) { uint8_t Card; uint8_t Foul_Robot_ID; - }Warning; - - struct __attribute__ ((__packed__)) + } Warning; + + struct __attribute__((__packed__)) { uint8_t ID; uint8_t Level; uint16_t Remaining_HP; uint16_t HP_Max; - + uint16_t Shooter_1_Cooling_Rate_17mm; uint16_t Shooter_1_Heat_Max_17mm; uint16_t Shooter_1_Speed_Limit_17mm; - + uint16_t Shooter_2_Cooling_Rate_17mm; uint16_t Shooter_2_Heat_Max_17mm; uint16_t Shooter_2_Speed_Limit_17mm; - + uint16_t Shooter_Cooling_Rate_42mm; uint16_t Shooter_Heat_Max_42mm; uint16_t Shooter_Speed_Limit_42mm; - - uint16_t Chassis_Power_Max; - uint8_t Gimbal_Power_Output:1; - uint8_t Chassis_Power_Output:1; - uint8_t Shooter_Power_Output:1; - }Robot_State; - - struct __attribute__ ((__packed__)) + + uint16_t Chassis_Power_Max; + uint8_t Gimbal_Power_Output : 1; + uint8_t Chassis_Power_Output : 1; + uint8_t Shooter_Power_Output : 1; + } Robot_State; + + struct __attribute__((__packed__)) { uint16_t Chassis_Voltage; uint16_t Chassis_Current; @@ -257,47 +246,47 @@ typedef struct uint16_t Shooter_1_Heat; uint16_t Shooter_2_Heat; uint16_t Mobile_Shooter_Heat; - }Power_n_Heat; - - struct __attribute__ ((__packed__)) + } Power_n_Heat; + + struct __attribute__((__packed__)) { float x; float y; float z; float Yaw; - }Location; - - struct __attribute__ ((__packed__)) + } Location; + + struct __attribute__((__packed__)) { uint8_t Buff; - }Robot_Buff; - - struct __attribute__ ((__packed__)) + } Robot_Buff; + + struct __attribute__((__packed__)) { uint8_t Armor_ID; uint8_t Type; - }Damage; - - struct __attribute__ ((__packed__)) + } Damage; + + struct __attribute__((__packed__)) { uint8_t Ammo_Type; uint8_t Shooter_Type; uint8_t Frequency; float Speed; - }Shooter; - - struct __attribute__ ((__packed__)) + } Shooter; + + struct __attribute__((__packed__)) { uint16_t Type_17mm; uint16_t Type_42mm; uint16_t Type_Gold; - }Remaining_Ammo; - - struct __attribute__ ((__packed__)) + } Remaining_Ammo; + + struct __attribute__((__packed__)) { uint32_t State; - }RFID; - + } RFID; + uint16_t Info_Update_Frame; uint8_t Offline_Flag; } Referee_System_t; diff --git a/src/devices/src/referee_system.c b/src/devices/src/referee_system.c index 6c0ab00c..f403ca31 100644 --- a/src/devices/src/referee_system.c +++ b/src/devices/src/referee_system.c @@ -1,21 +1,24 @@ /** * @file Referee_System.c - * @author Leo Liu + * @author Leo Liu, Jia Xie * @brief reading data from the referee system - * @version 1.0 - * @date 2022-07-19 - * - * @copyright Copyright (c) 2022 + * @version 1.1 + * @date 2024-04-04 * + * @copyright Copyright (c) 2024 */ #include "referee_system.h" #include "usart.h" +#include "bsp_uart.h" +#include "bsp_daemon.h" + +#define REFEREE_SYSTEM_TIMEOUT_MS (3000) Referee_System_t Referee_System; Referee_Robot_State_t Referee_Robot_State; -void Referee_Get_Data(void); -void Referee_Set_Robot_State(void); +UART_Instance_t *g_referee_system_uart_ptr; +Daemon_Instance_t *g_referee_system_daemon_ptr; void Referee_Set_Robot_State(void) { @@ -71,14 +74,8 @@ void Referee_Set_Robot_State(void) // } } -void Referee_System_Init(UART_HandleTypeDef *huart) -{ - Referee_System.huart = huart; - HAL_UART_Receive_DMA(huart, Referee_System.Buffer, REFEREE_BUFFER_LEN); -} - // Get referee system data based on ID -void Referee_Get_Data(void) +void Referee_System_Decode(void) { for (int n = 0; n < REFEREE_BUFFER_LEN;) { @@ -211,4 +208,23 @@ void Referee_Get_Data(void) else n++; } -} \ No newline at end of file +} + +void Referee_System_Rx_Callback(UART_Instance_t *uart_instance) +{ + Referee_System_Decode(); + Referee_Set_Robot_State(); +} + +void Referee_System_Timeout_Callback() +{ + UART_Service_Init(g_referee_system_uart_ptr); +} + +void Referee_System_Init(UART_HandleTypeDef *huart) +{ + g_referee_system_uart_ptr = UART_Register(huart, Referee_System.Buffer, REFEREE_BUFFER_LEN, Referee_System_Rx_Callback); + uint16_t reload_value = REFEREE_SYSTEM_TIMEOUT_MS / DAEMON_PERIOD; + uint16_t initial_counter = reload_value; + g_referee_system_daemon_ptr = Daemon_Register(reload_value, initial_counter, Referee_System_Timeout_Callback); +}