/*
    Init Task
    任务初始化，创建各个线程任务和消息队列
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"

/* USER INCLUDE BEGIN */
#include "component/ahrs.h"
#include "module/xm_quadctrl.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* Private function --------------------------------------------------------- */
/* Exported functions -------------------------------------------------k------ */

/**
 * \brief 初始化
 *
 * \param argument 未使用
 */
void Task_Init(void *argument) {
  (void)argument; /* 未使用argument，消除警告 */
    /* USER CODE INIT BEGIN */

    /* USER CODE INIT END */
  osKernelLock(); /* 锁定内核，防止任务切换 */
  
  /* 创建任务线程 */
  task_runtime.thread.atti_esti = osThreadNew(Task_atti_esti, NULL, &attr_atti_esti);
  task_runtime.thread.blink = osThreadNew(Task_blink, NULL, &attr_blink);
  task_runtime.thread.height_cal = osThreadNew(Task_height_cal, NULL, &attr_height_cal);
  task_runtime.thread.quad_ctrl = osThreadNew(Task_quad_ctrl, NULL, &attr_quad_ctrl);
  task_runtime.thread.cmd = osThreadNew(Task_cmd, NULL, &attr_cmd);

  // 创建消息队列
  /* USER MESSAGE BEGIN */
	task_runtime.msgq.heightestimation.accl= osMessageQueueNew(5, sizeof(AHRS_Accl_t), NULL);
	task_runtime.msgq.heightestimation.quaternion= osMessageQueueNew(5, sizeof(AHRS_Quaternion_t), NULL);
	task_runtime.msgq.quad.eulr= osMessageQueueNew(3, sizeof(AHRS_Eulr_t), NULL);
  task_runtime.msgq.quad.accl= osMessageQueueNew(3, sizeof(AHRS_Accl_t), NULL);
  task_runtime.msgq.quad.gyro= osMessageQueueNew(3, sizeof(AHRS_Gyro_t), NULL);
	task_runtime.msgq.quad.alt= osMessageQueueNew(3, sizeof(float), NULL);
	task_runtime.msgq.quad.vel= osMessageQueueNew(3, sizeof(float), NULL);
	task_runtime.msgq.quad.cmd= osMessageQueueNew(3, sizeof(Quad_CMD_t), NULL);
  /* USER MESSAGE END */

  osKernelUnlock(); // 解锁内核
  osThreadTerminate(osThreadGetId()); // 任务完成后结束自身
}