god-yuan-hero/User/task/ai.c
2026-03-13 01:58:46 +08:00

90 lines
4.1 KiB
C
Raw 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 Task
通路:上位机 <--串口--> 上层板 <--CAN--> 下层板(本文件)
下层板职责:
1. 将云台/IMU 反馈数据通过 CAN 发送给上层板
2. 从 CAN 解析上层板转发的 AI 控制指令
3. 将解析结果写入 cmd.nuc 队列供 cmd task 消费
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "module/aimbot.h"
#include "module/config.h"
#include "module/vision_bridge.h"
#include "component/ahrs/ahrs.h"
#include "module/gimbal.h"
#include "device/referee_proto_types.h"
/* USER INCLUDE END */
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
static Aimbot_Param_t aimbot_can; /* CAN 通信参数(从 config 拷贝) */
static Aimbot_FeedbackData_t aimbot_fb; /* 打包好的反馈数据 */
static Aimbot_AIResult_t aimbot_cmd; /* 从 CAN 解析到的 AI 指令 */
static Gimbal_Feedback_t ai_gimbal_fb; /* 来自云台任务的反馈 */
static AHRS_Quaternion_t ai_imu_quat; /* 来自姿态估计任务的四元数 */
static Referee_ForAI_t ai_ref; /* 裁判系统转发给 AI 的数据 */
static AI_cmd_t ai_cmd_for_nuc; /* 转换后发往 cmd.nuc 的指令 */
/* USER STRUCT END */
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_ai(void *argument) {
(void)argument; /* 未使用argument消除警告 */
/* 计算任务运行到指定频率需要等待的tick数 */
const uint32_t delay_tick = osKernelGetTickFreq() / AI_FREQ;
osDelay(AI_INIT_DELAY); /* 延时一段时间再开启任务 */
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
/* USER CODE INIT BEGIN */
aimbot_can = Config_GetRobotParam()->aimbot_param;
Aimbot_Init(&aimbot_can);
/* USER CODE INIT END */
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
/* USER CODE BEGIN */
/* 读取裁判系统状态(非阻塞) */
osMessageQueueGet(task_runtime.msgq.ai.ref, &ai_ref, NULL, 0);
/* 读取云台反馈数据(非阻塞) */
osMessageQueueGet(task_runtime.msgq.ai.rawdata_FromGimbal, &ai_gimbal_fb, NULL, 0);
/* 读取 IMU 四元数(非阻塞) */
osMessageQueueGet(task_runtime.msgq.ai.rawdata_FromIMU, &ai_imu_quat, NULL, 0);
/* 打包反馈并通过 CAN 发给上层板6 帧)
* bullet_speed / bullet_count 暂无独立队列,置零;
* 如有裁判系统弹速信息可在此处填入。 */
Aimbot_PackFeedback(&aimbot_fb, &ai_gimbal_fb, &ai_imu_quat,
0.0f, 0u,
(uint8_t)(ai_ref.ref_status == REF_STATUS_RUNNING ? 1u : 0u));
Aimbot_SendFeedbackOnCAN(&aimbot_can, &aimbot_fb);
/* 解析上层板发来的 AI 指令(非阻塞) */
if (Aimbot_ParseCmdFromCAN(&aimbot_can, &aimbot_cmd) == DEVICE_OK) {
/* 将 Aimbot_AIResult_t 转换为 AI_cmd_t 并推送到 cmd.nuc 队列 */
ai_cmd_for_nuc.mode = aimbot_cmd.mode;
ai_cmd_for_nuc.gimbal.setpoint.yaw = aimbot_cmd.gimbal_t.setpoint.yaw;
ai_cmd_for_nuc.gimbal.setpoint.pit = aimbot_cmd.gimbal_t.setpoint.pit;
ai_cmd_for_nuc.gimbal.vel.yaw = aimbot_cmd.gimbal_t.vel.yaw;
ai_cmd_for_nuc.gimbal.vel.pit = aimbot_cmd.gimbal_t.vel.pit;
ai_cmd_for_nuc.gimbal.accl.yaw = aimbot_cmd.gimbal_t.accl.yaw;
ai_cmd_for_nuc.gimbal.accl.pit = aimbot_cmd.gimbal_t.accl.pit;
osMessageQueueReset(task_runtime.msgq.cmd.nuc);
osMessageQueuePut(task_runtime.msgq.cmd.nuc, &ai_cmd_for_nuc, 0, 0);
}
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}