diff --git a/User/device/ai.c b/User/device/ai.c index 70b634c..9bc3f88 100644 --- a/User/device/ai.c +++ b/User/device/ai.c @@ -5,10 +5,12 @@ AI /* Includes ----------------------------------------------------------------- */ #include "ai.h" +#include #include #include "bsp/time.h" #include "bsp/uart.h" +#include "component/ahrs.h" #include "component/crc16.h" #include "component/crc8.h" #include "component/user_math.h" @@ -64,14 +66,16 @@ int8_t AI_Restart(AI_t *ai) { int8_t AI_StartReceiving(AI_t *ai) { UNUSED(ai); - if (HAL_UART_Receive_DMA(BSP_UART_GetHandle(BSP_UART_AI), rxbuf, - AI_LEN_RX_BUFF) == HAL_OK) + // if (HAL_UART_Receive_DMA(BSP_UART_GetHandle(BSP_UART_AI), rxbuf, + // AI_LEN_RX_BUFF) == HAL_OK) + if (BSP_UART_Receive(BSP_UART_AI, rxbuf, + AI_LEN_RX_BUFF, true) == HAL_OK) return DEVICE_OK; return DEVICE_ERR; } -bool AI_WaitDmaCplt(uint32_t timeout) { - return (osThreadFlagsWait(SIGNAL_AI_RAW_REDY, osFlagsWaitAll, timeout) == +bool AI_WaitDmaCplt(void) { + return (osThreadFlagsWait(SIGNAL_AI_RAW_REDY, osFlagsWaitAll,0) == SIGNAL_AI_RAW_REDY); } @@ -89,18 +93,47 @@ error: return DEVICE_ERR; } -// int8_t AI_PackMCU(AI_t *ai, const AI_Protucol_UpDataMCU_t *data){ -// if (ai == NULL || data == NULL) return DEVICE_ERR_NULL; -// ai->to_host.mcu.id = AI_ID_MCU; -// ai->to_host.mcu.package = *data; -// ai->to_host.mcu.crc16 = -// CRC16_Calc((const uint8_t *)&(ai->to_host.mcu), sizeof(AI_UpPackageMCU_t) - 2); -// return DEVICE_OK; -// } +int8_t AI_PackMCU(AI_t *ai, const AHRS_Quaternion_t *data){ + if (ai == NULL || data == NULL) return DEVICE_ERR_NULL; + ai->to_host.mcu.id = AI_ID_MCU; + ai->to_host.mcu.package.quat=*data; + ai->to_host.mcu.package.notice = ai->status; + ai->to_host.mcu.crc16 = CRC16_Calc((const uint8_t *)&(ai->to_host.mcu), sizeof(AI_UpPackageMCU_t) - 2, CRC16_INIT); + return DEVICE_OK; +} -int8_t AI_PackRef(AI_t *ai, const AI_UpPackageReferee_t *data); +int8_t AI_PackRef(AI_t *ai, const AI_UpPackageReferee_t *data) { + if (ai == NULL || data == NULL) return DEVICE_ERR_NULL; + ai->to_host.ref = *data; + return DEVICE_OK; +} -int8_t AI_HandleOffline(AI_t *ai); +int8_t AI_HandleOffline(AI_t *ai) { + if (ai == NULL) return DEVICE_ERR_NULL; + if (BSP_TIME_Get() - ai->header.last_online_time > + 100000) { + ai->header.online = false; + } + return DEVICE_OK; +} -int8_t AI_StartSend(AI_t *ai, bool ref_online); +int8_t AI_StartSend(AI_t *ai, bool ref_online){ + if (ai == NULL) return DEVICE_ERR_NULL; + + if (ref_online) { + // 发送裁判系统数据和MCU数据 + if (BSP_UART_Transmit(BSP_UART_AI, (uint8_t *)&(ai->to_host), + sizeof(ai->to_host.ref) + sizeof(ai->to_host.mcu), true) == HAL_OK) + return DEVICE_OK; + else + return DEVICE_ERR; + } else { + // 只发送MCU数据 + if (BSP_UART_Transmit(BSP_UART_AI, (uint8_t *)&(ai->to_host.mcu), + sizeof(ai->to_host.mcu), true) == HAL_OK) + return DEVICE_OK; + else + return DEVICE_ERR; + } +} diff --git a/User/device/ai.h b/User/device/ai.h index 101d780..2ce35ca 100644 --- a/User/device/ai.h +++ b/User/device/ai.h @@ -25,10 +25,39 @@ extern "C" { #define AI_ID_AI (0xA1) /* Exported types ----------------------------------------------------------- */ +typedef enum { + AI_ARMOR_HERO = 0, /*英雄机器人*/ + AI_ARMOR_INFANTRY, /*步兵机器人*/ + AI_ARMOR_SENTRY, /*哨兵机器人*/ + AI_ARMOR_ENGINEER, /*工程机器人*/ + AI_ARMOR_OUTPOST, /*前哨占*/ + AI_ARMOR_BASE, /*基地*/ + AI_ARMOR_NORMAL, /*由AI自动选择*/ +} AI_ArmorsType_t; + +typedef enum { + AI_STATUS_OFF = 0, /* 关闭 */ + AI_STATUS_AUTOAIM, /* 自瞄 */ + AI_STATUS_AUTOPICK, /* 自动取矿 */ + AI_STATUS_AUTOPUT, /* 自动兑矿 */ + AI_STATUS_AUTOHITBUFF, /* 自动打符 */ + AI_STATUS_AUTONAV, +} AI_Status_t; + +typedef enum { + AI_NOTICE_NONE = 0, + AI_NOTICE_SEARCH, + AI_NOTICE_FIRE, +}AI_Notice_t; + /* 电控 -> 视觉 MCU数据结构体*/ typedef struct __packed { AHRS_Quaternion_t quat; /* 四元数 */ - uint8_t notice; /* 控制命令 */ + // struct { + // AI_ArmorsType_t armor_type; + // AI_Status_t status; + // }notice; /* 控制命令 */ + uint8_t notice; } AI_Protucol_UpDataMCU_t; /* 电控 -> 视觉 裁判系统数据结构体*/ @@ -37,7 +66,7 @@ typedef struct __packed { uint16_t time; /* 比赛开始时间 */ } AI_Protocol_UpDataReferee_t; -/* 电控 -> 视觉 数据包结构体*/ +/* 视觉 -> 电控 数据包结构体*/ typedef struct __packed { AHRS_Eulr_t eulr; /* 欧拉角 */ MoveVector_t move_vec; /* 运动向量 */ @@ -68,6 +97,7 @@ typedef struct __packed { typedef struct __packed { DEVICE_Header_t header; /* 设备通用头部 */ AI_DownPackage_t from_host; + AI_Status_t status; struct { AI_UpPackageReferee_t ref; AI_UpPackageMCU_t mcu; @@ -82,11 +112,11 @@ int8_t AI_Restart(AI_t *ai); int8_t AI_StartReceiving(AI_t *ai); -bool AI_WaitDmaCplt(uint32_t timeout); +bool AI_WaitDmaCplt(void); int8_t AI_ParseHost(AI_t *ai); -int8_t AI_PackMCU(AI_t *ai, const AI_Protucol_UpDataMCU_t *data); +int8_t AI_PackMCU(AI_t *ai, const AHRS_Quaternion_t *quat); int8_t AI_PackRef(AI_t *ai, const AI_UpPackageReferee_t *data); @@ -94,7 +124,6 @@ int8_t AI_HandleOffline(AI_t *ai); int8_t AI_StartSend(AI_t *ai, bool ref_online); - #ifdef __cplusplus } #endif diff --git a/User/task/ai.c b/User/task/ai.c index ae90c59..c72f9c8 100644 --- a/User/task/ai.c +++ b/User/task/ai.c @@ -4,9 +4,12 @@ */ /* Includes ----------------------------------------------------------------- */ +#include "cmsis_os2.h" #include "task/user_task.h" /* USER INCLUDE BEGIN */ - +#include "device/ai.h" +#include "component/ahrs.h" +#include /* USER INCLUDE END */ /* Private typedef ---------------------------------------------------------- */ @@ -14,7 +17,8 @@ /* Private macro ------------------------------------------------------------ */ /* Private variables -------------------------------------------------------- */ /* USER STRUCT BEGIN */ - +AI_t ai; +AHRS_Quaternion_t quat; /* USER STRUCT END */ /* Private function --------------------------------------------------------- */ @@ -30,12 +34,23 @@ void Task_ai(void *argument) { uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ /* USER CODE INIT BEGIN */ - + AI_Init(&ai); /* USER CODE INIT END */ while (1) { tick += delay_tick; /* 计算下一个唤醒时刻 */ /* USER CODE BEGIN */ + AI_StartReceiving(&ai); + if (AI_WaitDmaCplt()) { + AI_ParseHost(&ai); + } else { + AI_HandleOffline(&ai); + } + if (osMessageQueueGet(task_runtime.msgq.ai.quat, &quat, NULL, 0) == osOK) { + AI_PackMCU(&ai, &quat); + } + + AI_StartSend(&ai, false); /* USER CODE END */ osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ diff --git a/User/task/atti_esti.c b/User/task/atti_esti.c index 2a0e8a9..15ddb5c 100644 --- a/User/task/atti_esti.c +++ b/User/task/atti_esti.c @@ -4,6 +4,7 @@ */ /* Includes ----------------------------------------------------------------- */ +#include "cmsis_os2.h" #include "task/user_task.h" /* USER INCLUDE BEGIN */ #include "bsp/mm.h" @@ -154,6 +155,8 @@ void Task_atti_esti(void *argument) { osMessageQueueReset(task_runtime.msgq.gimbal.imu); osMessageQueuePut(task_runtime.msgq.gimbal.imu, &gimbal_to_send, 0, 0); + osMessageQueuePut(task_runtime.msgq.ai.quat, &gimbal_ahrs.quat, 0, 0); + BSP_PWM_SetComp(BSP_PWM_IMU_HEAT_PWM, PID_Calc(&imu_temp_ctrl_pid, 40.0f, bmi088.temp, 0.0f, 0.0f)); /* USER CODE END */ diff --git a/User/task/init.c b/User/task/init.c index ad6ec55..ae40091 100644 --- a/User/task/init.c +++ b/User/task/init.c @@ -52,6 +52,8 @@ void Task_Init(void *argument) { task_runtime.msgq.gimbal.imu= osMessageQueueNew(2u, sizeof(Gimbal_IMU_t), NULL); task_runtime.msgq.gimbal.cmd= osMessageQueueNew(2u, sizeof(Gimbal_CMD_t), NULL); task_runtime.msgq.shoot.shoot_cmd = osMessageQueueNew(2u, sizeof(Shoot_CMD_t), NULL); + task_runtime.msgq.ai.quat = osMessageQueueNew(2u, sizeof(AHRS_Quaternion_t), NULL); + task_runtime.msgq.ai.move_vec = osMessageQueueNew(2u, sizeof(MoveVector_t), NULL); /* USER MESSAGE END */ osKernelUnlock(); // 解锁内核 diff --git a/User/task/user_task.h b/User/task/user_task.h index b055b3e..53eeb03 100644 --- a/User/task/user_task.h +++ b/User/task/user_task.h @@ -75,6 +75,12 @@ typedef struct { osMessageQueueId_t ref; osMessageQueueId_t ai; }cmd; + struct { + osMessageQueueId_t quat; + osMessageQueueId_t move_vec; + osMessageQueueId_t eulr; + osMessageQueueId_t fire; + }ai; } msgq; /* USER MESSAGE END */