121 lines
4.2 KiB
C
121 lines
4.2 KiB
C
/*
|
||
cmd Task
|
||
|
||
*/
|
||
|
||
/* Includes ----------------------------------------------------------------- */
|
||
#include "task/user_task.h"
|
||
/* USER INCLUDE BEGIN */
|
||
#include "device/dr16.h"
|
||
#include "device/AT9S_Pro.h"
|
||
#include "module/config.h"
|
||
#include "module/chassis.h"
|
||
#include "module/gimbal.h"
|
||
#include "module/shoot.h"
|
||
#include "module/track.h"
|
||
#include "module/cmd/cmd.h"
|
||
#include "bsp/fdcan.h"
|
||
//#define DEAD_AREA 0.05f
|
||
/* USER INCLUDE END */
|
||
|
||
/* Private typedef ---------------------------------------------------------- */
|
||
/* Private define ----------------------------------------------------------- */
|
||
#define AI_CMD_CAN_ID (0x101u)
|
||
#define AI_CMD_CAN_DLC (5u) /* 1字节mode + 2字节yaw + 2字节pit */
|
||
#define AI_CMD_ANGLE_SCALE (10000.0f) /* 0.0001 rad per LSB */
|
||
/* Private macro ------------------------------------------------------------ */
|
||
/* Private variables -------------------------------------------------------- */
|
||
/* USER STRUCT BEGIN */
|
||
#if CMD_RCTypeTable_Index == 0
|
||
DR16_t cmd_dr16;
|
||
#elif CMD_RCTypeTable_Index == 1
|
||
AT9S_t cmd_at9s;
|
||
#endif
|
||
AI_cmd_t cmd_ai;
|
||
|
||
Shoot_CMD_t *cmd_for_shoot;
|
||
Chassis_CMD_t *cmd_for_chassis;
|
||
Gimbal_CMD_t *cmd_for_gimbal;
|
||
Track_CMD_t *cmd_for_track;
|
||
static CMD_t cmd;
|
||
/* USER STRUCT END */
|
||
|
||
/* Private function --------------------------------------------------------- */
|
||
static void AI_ParseCmdFromCan(const BSP_FDCAN_Message_t *msg, AI_cmd_t *cmd);
|
||
/* Exported functions ------------------------------------------------------- */
|
||
void Task_cmd(void *argument) {
|
||
(void)argument; /* 未使用argument,消除警告 */
|
||
|
||
|
||
/* 计算任务运行到指定频率需要等待的tick数 */
|
||
const uint32_t delay_tick = osKernelGetTickFreq() / CMD_FREQ;
|
||
|
||
osDelay(CMD_INIT_DELAY); /* 延时一段时间再开启任务 */
|
||
|
||
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
|
||
/* USER CODE INIT BEGIN */
|
||
CMD_Init(&cmd, &Config_GetRobotParam()->cmd_param);
|
||
|
||
/* 注册CAN接收ID */
|
||
BSP_FDCAN_RegisterId(BSP_FDCAN_2, AI_CMD_CAN_ID, BSP_FDCAN_DEFAULT_QUEUE_SIZE);
|
||
/* USER CODE INIT END */
|
||
|
||
while (1) {
|
||
tick += delay_tick; /* 计算下一个唤醒时刻 */
|
||
/* USER CODE BEGIN */
|
||
#if CMD_RCTypeTable_Index == 0
|
||
osMessageQueueGet(task_runtime.msgq.cmd.rc, &cmd_dr16, NULL, 0);
|
||
#elif CMD_RCTypeTable_Index == 1
|
||
osMessageQueueGet(task_runtime.msgq.cmd.rc, &cmd_at9s, NULL, 0);
|
||
#endif
|
||
|
||
/* 从CAN2接收AI命令 */
|
||
BSP_FDCAN_Message_t can_msg;
|
||
if (BSP_FDCAN_GetMessage(BSP_FDCAN_2, AI_CMD_CAN_ID, &can_msg, BSP_FDCAN_TIMEOUT_IMMEDIATE) == 0) {
|
||
AI_ParseCmdFromCan(&can_msg, &cmd_ai);
|
||
}
|
||
|
||
CMD_Update(&cmd);
|
||
|
||
/* 获取命令发送到各模块 */
|
||
cmd_for_chassis = CMD_GetChassisCmd(&cmd);
|
||
cmd_for_gimbal = CMD_GetGimbalCmd(&cmd);
|
||
cmd_for_shoot = CMD_GetShootCmd(&cmd);
|
||
cmd_for_track = CMD_GetTrackCmd(&cmd);
|
||
osMessageQueueReset(task_runtime.msgq.gimbal.cmd);
|
||
osMessageQueuePut(task_runtime.msgq.gimbal.cmd, cmd_for_gimbal, 0, 0);
|
||
osMessageQueueReset(task_runtime.msgq.shoot.cmd);
|
||
osMessageQueuePut(task_runtime.msgq.shoot.cmd, cmd_for_shoot, 0, 0);
|
||
osMessageQueueReset(task_runtime.msgq.chassis.cmd);
|
||
osMessageQueuePut(task_runtime.msgq.chassis.cmd, cmd_for_chassis, 0, 0);
|
||
osMessageQueueReset(task_runtime.msgq.track.cmd);
|
||
osMessageQueuePut(task_runtime.msgq.track.cmd, cmd_for_track, 0, 0);
|
||
/* USER CODE END */
|
||
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
|
||
}
|
||
|
||
}
|
||
|
||
/* 从CAN消息解析AI命令 (mode + yaw + pit) */
|
||
static void AI_ParseCmdFromCan(const BSP_FDCAN_Message_t *msg, AI_cmd_t *cmd) {
|
||
if (msg == NULL || cmd == NULL || msg->dlc < AI_CMD_CAN_DLC) {
|
||
return;
|
||
}
|
||
|
||
/* 解析mode (1字节) */
|
||
cmd->mode = msg->data[0];
|
||
|
||
/* 解析yaw (2字节,高字节在前) */
|
||
int16_t yaw_raw = (int16_t)((msg->data[1] << 8) | msg->data[2]);
|
||
cmd->gimbal.setpoint.yaw = (float)yaw_raw / AI_CMD_ANGLE_SCALE;
|
||
|
||
/* 解析pit (2字节,高字节在前) */
|
||
int16_t pit_raw = (int16_t)((msg->data[3] << 8) | msg->data[4]);
|
||
cmd->gimbal.setpoint.pit = (float)pit_raw / AI_CMD_ANGLE_SCALE;
|
||
|
||
/* 其他字段根据需要可以初始化为0 */
|
||
cmd->gimbal.vel.yaw = 0.0f;
|
||
cmd->gimbal.vel.pit = 0.0f;
|
||
cmd->gimbal.accl.yaw = 0.0f;
|
||
cmd->gimbal.accl.pit = 0.0f;
|
||
} |