#pragma once

#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <cmsis_os2.h>
#include "FreeRTOS.h"
#include "task.h"

/* USER INCLUDE BEGIN */
#include "module/config.h"
/* USER INCLUDE END */
/* Exported constants ------------------------------------------------------- */
/* 任务运行频率 */
#define ATTI_ESTI_FREQ (500.0)
#define RC_FREQ (500.0)
#define CHASSIS_FREQ (500.0)
#define CMD_FREQ (500.0)
#define GIMBAL_FREQ (500.0)
#define SHOOT_FREQ (500.0)
#define AI_FREQ (250.0)
#define TASK8_FREQ (500.0)

/* 任务初始化延时ms */
#define TASK_INIT_DELAY (100u)
#define ATTI_ESTI_INIT_DELAY (0)
#define RC_INIT_DELAY (0)
#define CHASSIS_INIT_DELAY (0)
#define CMD_INIT_DELAY (0)
#define GIMBAL_INIT_DELAY (0)
#define SHOOT_INIT_DELAY (0)
#define AI_INIT_DELAY (0)
#define TASK8_INIT_DELAY (0)

/* Exported defines --------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */

/* 任务运行时结构体 */
typedef struct {
    /* 各任务，也可以叫做线程 */
    struct {
        osThreadId_t atti_esti;
        osThreadId_t rc;
        osThreadId_t chassis;
        osThreadId_t cmd;
        osThreadId_t gimbal;
        osThreadId_t shoot;
        osThreadId_t ai;
        osThreadId_t Task8;
    } thread;

    /* USER MESSAGE BEGIN */
        struct
        {
            osMessageQueueId_t user_msg; /* 用户自定义任务消息队列 */

            struct
            {
                struct
                {
                    osMessageQueueId_t rc; // dr16->cmd
                    // osMessageQueueId_t nuc;   // nuc->cmd
                    // osMessageQueueId_t ops_9; // navi->cmd
                } raw;
                // osMessageQueueId_t chassis; // cmd->chassis
                // osMessageQueueId_t calc;    // cmd->calc
                // osMessageQueueId_t nuc;     // cmd->nuc
            } cmd;

            struct 
            {
                osMessageQueueId_t cmd; 
            }chassis;

            struct
            {
                osMessageQueueId_t accl; /* IMU读取 */
                osMessageQueueId_t gyro; /* IMU读取 */
                osMessageQueueId_t eulr; /* 姿态解算得到的欧拉角 */
                osMessageQueueId_t quat; /* 姿态解算得到的四元数 */
            } imu;

            struct
            {
                osMessageQueueId_t imu;
                osMessageQueueId_t cmd;
                osMessageQueueId_t yaw6020; /* 新增的 yaw_6020 消息队列 主要是给底盘传6020位置的反馈*/
                struct{
               osMessageQueueId_t quat;                  
                osMessageQueueId_t feedback;
                osMessageQueueId_t g_cmd;
                osMessageQueueId_t s_cmd;
                }ai;
                 /* 新增的 ai 消息队列 主要是给自瞄*/
            } gimbal;

            struct 
            {
                osMessageQueueId_t cmd; 
            }shoot;

        } msgq;
        /* USER MESSAGE END */

    /* 机器人状态 */
    struct {
        float battery; /* 电池电量百分比 */
        float vbat; /* 电池电压 */
        float cpu_temp; /* CPU温度 */
    } status;

    /* USER CONFIG BEGIN */
        Config_t config;
        /* USER CONFIG END */

    /* 各任务的stack使用 */
    struct {
        UBaseType_t atti_esti;
        UBaseType_t rc;
        UBaseType_t chassis;
        UBaseType_t cmd;
        UBaseType_t gimbal;
        UBaseType_t shoot;
        UBaseType_t ai;
        UBaseType_t Task8;
    } stack_water_mark;

    /* 各任务运行频率 */
    struct {
        float atti_esti;
        float rc;
        float chassis;
        float cmd;
        float gimbal;
        float shoot;
        float ai;
        float Task8;
    } freq;

    /* 任务最近运行时间 */
    struct {
        float atti_esti;
        float rc;
        float chassis;
        float cmd;
        float gimbal;
        float shoot;
        float ai;
        float Task8;
    } last_up_time;

} Task_Runtime_t;

/* 任务运行时结构体 */
extern Task_Runtime_t task_runtime;

/* 初始化任务句柄 */
extern const osThreadAttr_t attr_init;
extern const osThreadAttr_t attr_atti_esti;
extern const osThreadAttr_t attr_rc;
extern const osThreadAttr_t attr_chassis;
extern const osThreadAttr_t attr_cmd;
extern const osThreadAttr_t attr_gimbal;
extern const osThreadAttr_t attr_shoot;
extern const osThreadAttr_t attr_ai;
extern const osThreadAttr_t attr_Task8;

/* 任务函数声明 */
void Task_Init(void *argument);
void Task_atti_esti(void *argument);
void Task_rc(void *argument);
void Task_chassis(void *argument);
void Task_cmd(void *argument);
void Task_gimbal(void *argument);
void Task_shoot(void *argument);
void Task_ai(void *argument);
void Task_Task8(void *argument);

#ifdef __cplusplus
}
#endif