/*
    cmd_task Task

*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "module/cmd.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
CMD_RC_t rc_data;                 /*遥控器数据解析*/
Assemble_CMD_t send_cmd_assemble; /*取杆命令*/
Chassis_CMD_t send_cmd_chassis;   /*底盘命令*/
/* USER STRUCT END */

/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_cmd_task(void *argument)
{
  (void)argument; /* 未使用argument，消除警告 */

  /* 计算任务运行到指定频率需要等待的tick数 */
  const uint32_t delay_tick = osKernelGetTickFreq() / CMD_TASK_FREQ;

  osDelay(CMD_TASK_INIT_DELAY); /* 延时一段时间再开启任务 */

  uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
  /* USER CODE INIT BEGIN */
  CMD_Init(&send_cmd_chassis, &send_cmd_assemble);
  /* USER CODE INIT END */

  while (1)
  {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
                        /* USER CODE BEGIN */
    osMessageQueueGet(task_runtime.msgq.cmd.rc, &rc_data, NULL, 0);

    CMD_ParseRc(&rc_data, &send_cmd_chassis, &send_cmd_assemble);
    CMD_CtrlSet(&send_cmd_chassis, &rc_data, &send_cmd_assemble);

    osMessageQueueReset(task_runtime.msgq.assmble.assmble_cmd);
    osMessageQueuePut(task_runtime.msgq.assmble.assmble_cmd, &send_cmd_assemble, 0, 0);
    osMessageQueueReset(task_runtime.msgq.chassis.cmd);
    osMessageQueuePut(task_runtime.msgq.chassis.cmd, &send_cmd_chassis, 0, 0);
    /* USER CODE END */
    osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
}