move_xrobot/User/task/ai.c
2025-01-14 22:57:05 +08:00

77 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
AI上位机通信任务
*/
/* Includes ----------------------------------------------------------------- */
#include "device\ai.h"
#include "bsp\usb.h"
#include "task\user_task.h"
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
#ifdef DEBUG
AI_t ai;
CMD_Host_t cmd_host;
AHRS_Quaternion_t quat;
Referee_ForAI_t referee_ai;
#else
static AI_t ai;
static CMD_Host_t cmd_host;
static AHRS_Quaternion_t quat;
static Referee_ForAI_t referee_ai;
#endif
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
/**
* \brief AI通信
*
* \param argument 未使用
*/
void Task_Ai(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_AI;
/* 初始化AI通信 */
AI_Init(&ai);
uint32_t tick = osKernelGetTickCount();
uint32_t last_online_tick = tick;
while (1) {
#ifdef DEBUG
task_runtime.stack_water_mark.ai = osThreadGetStackSpace(osThreadGetId());
#endif
/* Task body */
tick += delay_tick;
AI_StartReceiving(&ai);
if (AI_WaitDmaCplt()) {
AI_ParseHost(&ai, &cmd_host);
last_online_tick = tick;
} else {
if (tick - last_online_tick > 300) AI_HandleOffline(&ai, &cmd_host);
}
osMessageQueueReset(task_runtime.msgq.cmd.raw.host);
osMessageQueuePut(task_runtime.msgq.cmd.raw.host, &(cmd_host), 0, 0);
osMessageQueueGet(task_runtime.msgq.ai.quat, &(quat), NULL, 0);
osMessageQueueGet(task_runtime.msgq.cmd.ai, &(ai.status), NULL, 0);
bool ref_update = (osMessageQueueGet(task_runtime.msgq.referee.ai,
&(referee_ai), NULL, 0) == osOK);
AI_PackMCU(&ai, &quat);
if (ref_update) AI_PackRef(&ai, &(referee_ai));
AI_StartSend(&(ai), ref_update);
osDelayUntil(tick);
}
}