/*
    gimbal Task
    
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "module/gimbal.h"
#include "module/config.h"
#include "device/ai.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
Gimbal_CMD_t cmd_gimbal; //遥控器
Gimbal_IMU_t gimbal_imu;
Gimbal_t gimbal;
AI_cmd_t ai_gimbal_cmd; 

Gimbal_CMD_t final_gimbal_cmd; //最终命令

/* USER STRUCT END */

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

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

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

  uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
  /* USER CODE INIT BEGIN */
  Gimbal_Init(&gimbal, &Config_GetRobotParam()->gimbal, GIMBAL_FREQ);
  /* USER CODE INIT END */
  
  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
    /* USER CODE BEGIN */
    osMessageQueueGet(task_runtime.msgq.gimbal.imu, &gimbal_imu,NULL, 0);
    Gimbal_UpdateIMU(&gimbal, &gimbal_imu);

osMessageQueueGet(task_runtime.msgq.gimbal.ai.g_cmd,&ai_gimbal_cmd,NULL, 0);
		/* ai指令 */
		// if(osMessageQueueGet(task_runtime.msgq.gimbal.ai.g_cmd, &ai_gimbal_cmd, NULL, 0)==osOK){
		// 	if(ai_gimbal_cmd.mode==0){
    //   final_gimbal_cmd.set_pitch =gimbal_imu.eulr.rol;
    //   final_gimbal_cmd.set_yaw=gimbal_imu.eulr.yaw;			
		// 	}
		// 	if(ai_gimbal_cmd.mode==2){
    //   final_gimbal_cmd.set_pitch=ai_gimbal_cmd.gimbal_t.setpoint.pit;
    //   final_gimbal_cmd.set_yaw=ai_gimbal_cmd.gimbal_t.setpoint.yaw;
		// 	}   
    // }       



osMessageQueueGet(task_runtime.msgq.gimbal.cmd, &cmd_gimbal, NULL, 0);

if(ai_gimbal_cmd.mode==0){
      cmd_gimbal.set_pitch =gimbal_imu.eulr.pit;
      cmd_gimbal.set_yaw=gimbal_imu.eulr.yaw;			
    }
    if(ai_gimbal_cmd.mode==1)
    {
      cmd_gimbal.set_pitch=ai_gimbal_cmd.gimbal_t.setpoint.pit;
      cmd_gimbal.set_yaw=ai_gimbal_cmd.gimbal_t.setpoint.yaw;
    }
    if(ai_gimbal_cmd.mode==2){
      cmd_gimbal.set_pitch=ai_gimbal_cmd.gimbal_t.setpoint.pit;
      cmd_gimbal.set_yaw=ai_gimbal_cmd.gimbal_t.setpoint.yaw;
    }
Gimbal_UpdateFeedback(&gimbal);

osMessageQueueReset(task_runtime.msgq.gimbal.yaw6020);
osMessageQueuePut(task_runtime.msgq.gimbal.yaw6020, &gimbal.feedback.motor.yaw_4310_motor_feedback, 0, 0);

osMessageQueueReset(task_runtime.msgq.gimbal.ai.feedback);
osMessageQueuePut(task_runtime.msgq.gimbal.ai.feedback, &gimbal.feedback, 0, 0);


Gimbal_Control(&gimbal, &cmd_gimbal);
Gimbal_Output(&gimbal);



    /* USER CODE END */
    osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
  
}