/*
  姿态解算任务。

  控制IMU加热到指定温度防止温漂,收集IMU数据给AHRS算法。

  收集BMI088的数据,解算后得到四元数,转换为欧拉角之后放到消息队列中,
  等待其他任务取用。
*/
/* Includes ----------------------------------------------------------------- */
#include <string.h>
#include "task\user_task.h"
#include "bsp\pwm.h"
#include "ahrs.h"
#include "Algorithm\pid.h"
#include "device\bmi088.h"
#include "cmsis_os2.h"                  // ::CMSIS:RTOS2
#include "atti_esti.h"
#include <string.h>

#include "freertos_mpool.h"             // osMemoryPool definitions
#include "freertos_os2.h"               // Configuration check and setup
#include "task\user_task.h"
#include "error_detect.h"
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */

#ifdef DEBUG
BMI088_t bmi088;
IST8310_t ist8310;

AHRS_t gimbal_ahrs;
AHRS_Eulr_t imu_eulr;

pid_type_def 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

const pid_param_t imu_temp_pid_param = {
    .p = 1600.0f,
    .i = 1.5f,
    .d = 0.0f,
    .i_limit = 4700.0f,
    .out_limit = 4700.0f,
};
//static int temp_flag;
/* Private function  -------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */


/**
 * \brief 姿态解算
 *
 * \param argument 未使用
 */
void Task_AttiEsti(void *argument) {
  (void)argument; /* 未使用argument,消除警告 */

//   temp_flag = 0;//当温度达到期望值该标志位置1
  /* 初始化设备 */
	BMI088_Init(&bmi088,&task_runtime.config.cali_088);

  /* 初始化姿态解算算法 */
  AHRS_Init(&gimbal_ahrs, &ist8310.magn, BMI088_GetUpdateFreq(&bmi088));

  /* 初始化IMU温度控制PID,防止温漂 */
	PID_init(&imu_temp, PID_POSITION,
            &imu_temp_pid_param);
  /* IMU温度控制PWM输出 */
  BSP_PWM_Start(BSP_PWM_IMU_HEAT);

  while (1) {
#ifdef DEBUG
    /* 记录任务所使用的的栈空间 */
    task_runtime.stack_water_mark.atti_esti =
        osThreadGetStackSpace(osThreadGetId());
#endif		
    /* 等待IMU新数据 */
		BMI088_calibration(&bmi088);
		BMI088_WaitNew();

    /* 开始数据接收DMA,加速度计和陀螺仪共用同一个SPI接口,
     * 一次只能开启一个DMA
     */
    BMI088_AcclStartDmaRecv();
		BMI088_AcclWaitDmaCplt();

		detect_hook(BOARD_ACCEL_TOE);
		
    BMI088_GyroStartDmaRecv();
    BMI088_GyroWaitDmaCplt();
		
		detect_hook(BOARD_GYRO_TOE);
    
    /* 锁住RTOS内核防止数据解析过程中断,造成错误 */
    osKernelLock();
    /* 接收完所有数据后,把数据从原始字节加工成方便计算的数据 */
    BMI088_ParseAccl(&bmi088);
    BMI088_ParseGyro(&bmi088);
    // IST8310_Parse(&ist8310);

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

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

    osMessageQueueReset(task_runtime.msgq.imu.accl);
    osMessageQueuePut(task_runtime.msgq.imu.accl, &bmi088.accl, 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);
    osKernelUnlock();
		
    /* PID控制IMU温度,PWM输出 */
    BSP_PWM_Set(BSP_PWM_IMU_HEAT,
                PID_calc(&imu_temp,bmi088.temp, 40.0f)/5000);
  }
}