rm_balance/User/task/ai.c
2026-03-16 10:39:28 +08:00

87 lines
3.9 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 - 下层板
*/
/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "module/aimbot.h"
#include "module/config.h"
#include "module/gimbal.h"
#include "module/shoot.h"
#include "device/referee_proto_types.h"
#include <string.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 lower_board_fb; /* 打包好发给上层板的数据 */
static Aimbot_AIResult_t ai_cmd_from_can; /* 上层板下发并传到底板的指令 */
static AI_cmd_t for_cmdnuc; /* 转换后发往 cmd.nuc 的指令 */
static Referee_ForAI_t ai_ref; /* 裁判系统转发给 AI 的数据 */
static Shoot_AI_t raw_shoot; /* 发射机构反馈 */
/* 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 */
/* 1. 读取裁判系统数据(获知机器人系统状态/弹速等) */
osMessageQueueGet(task_runtime.msgq.ai.ref, &ai_ref, NULL, 0);
/* 2. 读取发射机构反馈数据(非阻塞),以获取弹速、弹数等信息
todo: 检查是否有单独发弹速消息队列
*/
osMessageQueueGet(task_runtime.msgq.ai.rawdata_FromShoot, &raw_shoot, NULL, 0);
/* 3. 打包当前底层板的数据弹速、弹数、模式通过CAN发送给上层板
由于现在只能拿到一部分,预留测试值,实车时接入射击模块返回的信息
*/
// TODO: 使用实际读取到的 bullet_speed 和 bullet_count
float current_bullet_speed = raw_shoot.bullet_speed;
uint16_t current_bullet_count = raw_shoot.num_shooted;
uint8_t current_mode = 1;
Aimbot_PackFeedback(&lower_board_fb, current_bullet_speed, current_bullet_count, current_mode);
Aimbot_SendFeedbackOnCAN(&aimbot_can, &lower_board_fb);
/* 4. 解析上层板发来的 AI 指令(非阻塞提取) */
if (Aimbot_ParseCmdFromCAN(&aimbot_can, &ai_cmd_from_can) == DEVICE_OK) {
/* 将 Aimbot_AIResult_t 转换为 AI_cmd_t 并推送到 cmd.nuc 队列 */
for_cmdnuc.mode = ai_cmd_from_can.mode;
for_cmdnuc.gimbal.setpoint.yaw = ai_cmd_from_can.gimbal_t.setpoint.yaw;
for_cmdnuc.gimbal.setpoint.pit = ai_cmd_from_can.gimbal_t.setpoint.pit;
for_cmdnuc.gimbal.vel.yaw = ai_cmd_from_can.gimbal_t.vel.yaw;
for_cmdnuc.gimbal.vel.pit = ai_cmd_from_can.gimbal_t.vel.pit;
for_cmdnuc.gimbal.accl.yaw = ai_cmd_from_can.gimbal_t.accl.yaw;
for_cmdnuc.gimbal.accl.pit = ai_cmd_from_can.gimbal_t.accl.pit;
osMessageQueueReset(task_runtime.msgq.cmd.nuc);
osMessageQueuePut(task_runtime.msgq.cmd.nuc, &for_cmdnuc, 0, 0);
}
/* USER CODE END */
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}