/*
    atti_esti Task
    
*/

/* Includes ----------------------------------------------------------------- */
#include "task/user_task.h"
/* USER INCLUDE BEGIN */
#include "device/bmi088.h"
#include "component/ahrs.h"
#include "component/pid.h"
#include  "bsp/pwm.h"
#include "bsp/gpio.h"
#include "bsp/time.h"
#include "module/gimbal.h"
/* USER INCLUDE END */

/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
/* USER STRUCT BEGIN */
BMI088_t bmi088;
IST8310_t ist8310;

AHRS_t ahrs;
AHRS_Eulr_t imu_eulr;

KPID_t imu_temp;
Gimbal_IMU_t gimbal_imu_send;
 KPID_Params_t imu_temp_pid_param = {
    .k = 1.0f,
    .p = 1.5f,
    .i =0.0f,
    .d =0.0f,
    .i_limit = 1.0f,
    .out_limit =1.0f,
    .d_cutoff_freq =0.0f,
    .range =0.0f
};

BMI088_Cali_t cali_bmi088= {
  .gyro_offset = {0.00285338191f,-0.00402083341f,-0.00201198575f},
};

/* 校准相关变量 */
typedef enum {
  CALIB_IDLE,     // 空闲状态
  CALIB_RUNNING,  // 正在校准
  CALIB_DONE      // 校准完成
} CalibState_t;

static CalibState_t calib_state = CALIB_IDLE;
static uint16_t calib_count = 0;
static struct {
  float x_sum;
  float y_sum; 
  float z_sum;
} gyro_sum= {0.0f,0.0f,0.0f};

static void start_gyro_calibration(void) {
  if (calib_state == CALIB_IDLE) {
    BSP_TIME_Delay(1000);
    calib_state = CALIB_RUNNING;
    calib_count = 0;
    gyro_sum.x_sum = 0.0f;
    gyro_sum.y_sum = 0.0f;
    gyro_sum.z_sum = 0.0f;
  }
}
#define CALIB_SAMPLES 5000  // 校准采样数量

int jiaozhun=0;
/* USER STRUCT END */

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

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

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

  uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
  /* USER CODE INIT BEGIN */
 /*陀螺仪初始化*/
    BMI088_Init(&bmi088,&cali_bmi088);

  /* AHRS初始化*/
    AHRS_Init(&ahrs,&ist8310.magn,BMI088_GetUpdateFreq(&bmi088));
    
    /* 初始化IMU温度控制PID，防止温漂 */
    PID_Init(&imu_temp,KPID_MODE_NO_D,ATTI_ESTI_FREQ,&imu_temp_pid_param);

      /* IMU温度控制PWM输出 */
  BSP_PWM_Start(BSP_PWM_IMU_HEAT_PWM);

    /* 注册按钮回调函数并启用中断 */
  BSP_GPIO_RegisterCallback(BSP_GPIO_KEY, start_gyro_calibration);
  BSP_GPIO_EnableIRQ(BSP_GPIO_KEY);
  /* USER CODE INIT END */
  
  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
    /* USER CODE BEGIN */
BMI088_WaitNew();

		BMI088_AcclStartDmaRecv();
		BMI088_AcclWaitDmaCplt();
		BMI088_GyroStartDmaRecv();
		BMI088_GyroWaitDmaCplt();
        /* 锁住RTOS内核防止数据解析过程中断，造成错误 */
		osKernelLock();
	  
		BMI088_ParseAccl(&bmi088);
		BMI088_ParseGyro(&bmi088);

   /* 陀螺仪校准处理 */
    // if (calib_state == CALIB_RUNNING) 
    if(jiaozhun==1){
      /* 累加陀螺仪数据用于计算零偏 */
      gyro_sum.x_sum += bmi088.gyro.x;
      gyro_sum.y_sum += bmi088.gyro.y;
      gyro_sum.z_sum += bmi088.gyro.z;
      calib_count++;

      /* 达到采样数量后计算平均值并更新零偏 */
      if (calib_count >= CALIB_SAMPLES) {
        /* 计算平均值作为零偏 */
        cali_bmi088.gyro_offset.x = gyro_sum.x_sum / CALIB_SAMPLES;
        cali_bmi088.gyro_offset.y = gyro_sum.y_sum / CALIB_SAMPLES;
        cali_bmi088.gyro_offset.z = gyro_sum.z_sum / CALIB_SAMPLES;
        
        /* 校准完成，重置为空闲状态以便下次校准 */
       calib_state = CALIB_IDLE;
        jiaozhun=0;
        /* 重新初始化BMI088以应用新的校准数据 */
       BMI088_Init(&bmi088, &cali_bmi088);
       AHRS_Init(&ahrs, &ist8310.magn, BMI088_GetUpdateFreq(&bmi088));
      }
    }
        /* 只有在非校准状态下才进行正常的姿态解析 */
    if (calib_state != CALIB_RUNNING) {
      /* 根据设备接收到的数据进行姿态解析 */
      AHRS_Update(&ahrs, &bmi088.accl, &bmi088.gyro, &ist8310.magn);

      /* 根据解析出来的四元数计算欧拉角 */
     AHRS_GetEulr(&imu_eulr, & ahrs);
    }

     osKernelUnlock();	

    gimbal_imu_send.eulr = imu_eulr;
    gimbal_imu_send.gyro = bmi088.gyro;

    osMessageQueueReset(task_runtime.msgq.gimbal.imu);
    osMessageQueuePut(task_runtime.msgq.gimbal.imu, &gimbal_imu_send, 0, 0);

    osMessageQueueReset(task_runtime.msgq.imu.gyro);
    osMessageQueuePut(task_runtime.msgq.imu.gyro, &bmi088.gyro, 0, 0);
		
		osMessageQueueReset(task_runtime.msgq.imu.eulr);
		osMessageQueuePut(task_runtime.msgq.imu.eulr, &imu_eulr, 0, 0);

   
		/* PID控制IMU温度，PWM输出 */
		BSP_PWM_SetComp(BSP_PWM_IMU_HEAT_PWM,
			PID_Calc(&imu_temp, 40.0f, bmi088.temp, 0.0f, 0.0f));
      
    /* USER CODE END */
    osDelayUntil(tick); /* 运行结束，等待下一次唤醒 */
  }
  
}