/*
    Task1 Task
    任务一用来进行c板自带的陀螺仪bmi088的数据采集和姿态解算，同时对陀螺仪进行温控
  控制IMU加热到指定温度防止温漂，收集IMU数据给AHRS算法。
  收集BMI088的数据，解算后得到四元数，转换为欧拉角之后放到消息队列中，
  等待其他任务取用。
*/

/* 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"

/* USER INCLUDE END */

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

AHRS_t ahrs;
AHRS_Eulr_t imu_eulr;

KPID_t imu_temp;

#else

static BMI088_t    bmi088;
static IST8310_t   ist8310;

static AHRS_t 			gimbal_ahrs;
static AHRS_Eulr_t imu_eulr;

pid_type_def imu_temp;

uint8_t exit_flag =0;

#endif

 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
};
//  int a=0;
/* USER STRUCT END */

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

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

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

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

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

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



  /* USER CODE INIT END */
  
  while (1) {
    tick += delay_tick; /* 计算下一个唤醒时刻 */
    /* USER CODE BEGIN */
    /*等待陀螺仪更新数据*/
	  // a +=1;
		BMI088_WaitNew();

		BMI088_AcclStartDmaRecv();
		BMI088_AcclWaitDmaCplt();
		BMI088_GyroStartDmaRecv();
		BMI088_GyroWaitDmaCplt();

   

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

     /* 根据设备接收到的数据进行姿态解析 */
    AHRS_Update(&ahrs, &bmi088.accl, &bmi088.gyro, &ist8310.magn);

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

    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);

    osKernelUnlock();	
		/* 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); /* 运行结束，等待下一次唤醒 */
  }
  
}