#ifndef __MOTOR_H
#define __MOTOR_H

// #include "PID.h"
#include "PID_Gen.h"
#include "math_utils.h"

#include "OBS_MOT.h"

#include "MOT_Dev_Config.h"
#include "ParaMge_Common.h"

#if MOT_DEV_TARGET == STM32_TARGET_A12_F4
#include "IMU_Pubilc.h"
#endif

//用于暂存校准后的数据
extern float offset_Temp;
extern float mge_direction_Temp;
extern float zeroElectricAngleOffset_Temp;
extern KFP kfp_ang;

//Motor Pre Setting
#define MOTOR_PP 7

//==OLD Part
extern LowPassFSTD_s velLP_Config;

//==Current Part
// extern int8_t motor_PP ; // 极对数

extern float Set_Velocity; // 目标转速
// extern LowPassFSTD_s velLP_Config;

extern float Target_Angle; 
extern float angleIMU_traget;

typedef enum
{
    MOTOR_IMU_DIRECTION_CW      = 1,  
    MOTOR_IMU_DIRECTION_CCW     = -1, 
    MOTOR_IMU_DIRECTION_UNKNOWN = 0   
} MOTOR_IMU_DIRECTION;

typedef enum
{
    CALIBRATION_FAILED = 0,     
    CALIBRATION_PROCESSING ,    
    CALIBRATION_CMPLETE ,    
} CALIBRATION_STATE;


// CUT##        索引枚举

typedef enum{
    __MOTOR_GENPID_INDEX_VEL = 1,
    __MOTOR_GENPID_INDEX_ANG = 2,
}__MOTOR_GENPID_INDEX;


extern MOTOR_IMU_DIRECTION mot_Imu_dir;
extern CALIBRATION_STATE calibration_State; //校准状态

// CUT##     电机参数设置

/// @brief 电机的PP值,须>0,否则设置不会被应用
/// @param PP 整数,(>0)
/// @return 返回操作结果 , 0 - 成功 , -1 - 失败(输入非法)
int8_t motor_SetPP(uint8_t PP);
int8_t motor_GetPP();
int8_t motor_SetUpLimit(float AngLimit);
float motor_GetUpLimit();
int8_t motor_SetDownLimit(float AngLimit);
float motor_GetDownLimit();

// /// @brief 返回当前角度PID的设置,以结构体的形式
// /// @return 返回角度PID的GenPID_CONFIG结构体
// GenPID_CONFIG motor_angGenPID_Get();
// int motor_angGenPID_Get_Needle(GenPID_CONFIG *config);

// /// @brief 设置角度PID,以结构体的形式传入,传入结构体会赋值到最终结构体中(即需要在外部构建该结构体)
// /// @param config 角度PID设置,GenPID_CONFIG结构体
// /// @return 当前API,总是返回0
// int motor_angGenPID_Set(GenPID_CONFIG config);
// int motor_angGenPID_Set_Needle(GenPID_CONFIG *config);

// //NOTE: 速度PID设置和获取和角度PID相同
// GenPID_CONFIG motor_velGenPID_Get();
// int motor_velGenPID_Get_Needle(GenPID_CONFIG *config);
// int motor_velGenPID_Set(GenPID_CONFIG config);
// int motor_velGenPID_Set_Needle(GenPID_CONFIG *config);

//NOTE: 速度PID设置和获取和角度PID相同
// GenPID_CONFIG motor_GenPID_Get(__MOTOR_GENPID_INDEX index);
int motor_GenPID_Get_Needle(__MOTOR_GENPID_INDEX index,GenPID_CONFIG *config);
// int motor_GenPID_Set(__MOTOR_GENPID_INDEX index,GenPID_CONFIG config);
int motor_GenPID_Set_Needle(__MOTOR_GENPID_INDEX index,GenPID_CONFIG *config);

/// @brief 返回当前速度KFP滤波器的设置,以结构体的形式
/// @return 返回速度KFP滤波器的KFP_CONFIG结构体
KFP_CONFIG motor_velKFP_Get(void);
KFP_CONFIG motor_angKFP_Get(void);

/// @brief 设置速度KFP滤波器,以结构体的形式传入,传入结构体会赋值到最终结构体中(即需要在外部构建该结构体)
/// @param config KFP滤波器设置,KFP_CONFIG结构体
/// @return 当前API,总是返回0
int motor_velKFP_Set(KFP_CONFIG config);
int motor_angKFP_Set(KFP_CONFIG config);

// 电机电角度零位设置
float motor_Get_ZeroEAngleOffset();
void motor_Set_ZeroEAngleOffset(float offset);
float motor_Get_ZeroEAngleLoopOffset();
void motor_Set_ZeroEAngleLoopOffset(float offset);

// CUT##     电机功能函数

// 函数声明
__PARAMGE_DEF_MOT_CALI_STA motor_Align_Sensor(void);
uint8_t motor_start_SelfTest(void);
void motor_AutoCalibration();
void gimbal_Horizontalalign(void);
void gimbal_Axisalign(void);

void motor_InitALL(void);

#if MOT_DEV_TARGET == STM32_TARGET_A2_F1
void MOT_YandR_VelControl_Direct_GenPID_ErrIn(float err);
#endif

#if MOT_DEV_TARGET == STM32_TARGET_A12_F4
void MOT_Pitch_AngleControl_WithTCurve_GenPID(state_t *imu_AHRS_result);
#endif

void close_Angle_Control_GenPID(float Target_Angle);//目标角度闭环控制

#endif