/**
 * @copyright None
 * @file      Definition.h
 * @author    Comment Vivre
 * @date      2025-11-03
 * @brief     None
 */
#ifndef __DEFINITION_H_
#define __DEFINITION_H_

// ------------------------------------------------------------------------------------------------------------------
// 1.可选配置项定义

// 转向
#define CW                              (0)
#define CCW                             (1)

// 基准电压
#define VREF3_0                         (0xA0)
#define VREF4_0                         (0xB0)
#define VREF4_5                         (0xC0)
#define VREF5_0                         (0xD0)

// 驱动电平
#define HIGH_LEVEL                      (0xA0)
#define LOW_LEVEL                       (0xB0)
#define UP_H_DOWN_L                     (0xC0)
#define UP_L_DOWN_H                     (0xD0)

// 电流采样模式
#define Single_Resistor                 (0xA0)
#define Double_Resistor                 (0xB0)
#define Three_Resistor                  (0xC0)

// 内部PGA放大倍数选择
#define AMP2x                           (2)
#define AMP4x                           (4)
#define AMP8x                           (8)
#define AMP16x                          (16)

// 运放模式选择
#define AMP_NOMAL                       (0xA0)
#define AMP_PGA_DUAL                    (0xB0)

// 浮点转整数定义
#define _Q7(A)                          (int8) ((A) * 0x7f)         // Q7  128*1  = 128
#define _Q8(A)                          (int16)((A) * 0xff)         // Q8  128*2  = 256
#define _Q9(A)                          (int16)((A) * 0x1ff)        // Q9  128*4  = 512
#define _Q10(A)                         (int16)((A) * 0x3ff)        // Q10 128*8  = 1024
#define _Q11(A)                         (int16)((A) * 0x07ff)       // Q11 128*16 = 2048
#define _Q12(A)                         (int16)((A) * 0x0fff)       // Q12 128*32 = 4096
#define _Q13(A)                         (int16)((A) * 0x1fff)       // Q13 128*64 = 8192
#define _Q14(A)                         (int16)((A) * 0x3fff)       // Q14 128*128= 18383
#define _Q15(A)                         (int16)((A) * 0x7fff)       // Q15 128*256= 32767
#define _Q16                            (65535.0)                   // Q16 
#define _2PI                            (3.1415926 * 2)

// 双电阻采样周期配置
#define DouRes_1_Cycle                  (0xA0)                      // 周期采样完 ia, ib
#define DouRes_2_Cycle                  (0xB0)                      // 交替采用ia, ib, 2周期采样完成

// SVPWM模式
#define SVPWM_5_Segment                 (0xA0)
#define SVPWM_7_Segment                 (0xB0)

// 估算器
#define SMO                             (0xA0)
#define PLL                             (0xB0)
#define AO                              (0xC0)

// 开环启动模式选择
#define Open_Start                      (0xA0)
#define Omega_Start                     (0xB0)
#define Open_Omega_Start                (0xC0)

//调速模式
#define UARTMODE                        (0xA0)
#define NONEMODE                        (0xB0)

// 外环
#define POWER_CONTROL_MODE              (0xA0)
#define SPEED_CONTROL_MODE              (0xB0)

// ------------------------------------------------------------------------------------------------------------------
// 2.电机控制参数定义与配置

#define MCU_CLOCK                       (24.0)

#define DLL_TIME                        (1.3)            // 双电阻最小脉宽设置(us),建议值为死区时间值+0.2us以上
#define OVERMOD_TIME                    (2.0)            // 三电阻过调制时间(us)，建议值2.0
#define DT_TIME                         (1.0)            // 死区补偿时间(us)适用于双电阻和三电阻，建议值是1/2死区时间
#define GLI_TIME                        (0.5)            // 桥臂窄脉宽消除(us),建议值0.5

#define PWM_LEVEL_MODE                  (HIGH_LEVEL)

// DQ轴最大限幅值
#define DOUTMAX                         _Q15(0.99)
#define DOUTMIN                         _Q15(-0.99)
#define QOUTMAX                         _Q15(0.99)
#define QOUTMIN                         _Q15(-0.99)



#define LPF_K(K)                        _Q15(_2PI / 1000 * K)
#define VC1                             (1.0)
#define RV                              ((RV1 + RV2 + RV3) / RV3)


// 电流基准
#define HW_BOARD_CURR_MAX               (HW_ADC_REF / 2 / HW_AMPGAIN / HW_RSHUNT)
#define HW_BOARD_CURR_MIN               (-HW_BOARD_CURR_MAX)
#define HW_BOARD_CURR_BASE              (HW_BOARD_CURR_MAX * 2)

// 母线电压采样分压电路
#define HW_BOARD_VOLT_MAX               (HW_ADC_REF * RV)
#define HW_BOARD_VOLTAGE_BASE           (HW_BOARD_VOLT_MAX / 1.732)
#define HW_BOARD_VOLTAGE_VC             ((RV1 + RV2 + RV3 * VC1) / (RV3 * VC1))
#define HW_BOARD_VOLTAGE_BASE_Start     (HW_ADC_REF * HW_BOARD_VOLTAGE_VC / 1.732)

#define I_ValueX(Curr_Value)            (Curr_Value * HW_RSHUNT * HW_AMPGAIN / (HW_ADC_REF))
#define I_Value(Curr_Value)             _Q15(I_ValueX(Curr_Value))

// 速度带宽的滤波值，经典值为5.0-40.0
#define SPD_BW                          (25.0)
#define ATT_COEF                        (0.95)
#define OBS_KSLIDE                     _Q15(0.85)											// SMO算法里的滑膜增益值

#define PWM_CYCLE                       (1000.0 / PWM_FREQUENCY)							// 周期us
#define SAMP_FREQ                       (PWM_FREQUENCY * 1000)								// 采样频率(HZ)
#define TPWM_VALUE                      (1.0 / SAMP_FREQ)									// 载波周期(S)
#define PWM_VALUE_LOAD                  (uint16)(MCU_CLOCK * 1000 *2 / PWM_FREQUENCY) 		// PWM 定时器重载值

#define PWM_LOAD_DEADTIME               (PWM_DEADTIME * MCU_CLOCK)             				// 死区设置值
#define PWM_OVERMODULE_TIME             (OVERMOD_TIME * MCU_CLOCK / 2)         				// 过调制时间
#define PWM_DLOWL_TIME                  (DLL_TIME * MCU_CLOCK / 2)             				//下桥臂最小时间

#define PWM_TS_LOAD                     (uint16)(_Q16 / PWM_CYCLE * MIN_WIND_TIME / 16)		// 单电阻采样设置值
#define PWM_DT_LOAD                     (uint16)(_Q16 / PWM_CYCLE * DT_TIME / 16)			// 死区补偿值
#define PWM_TGLI_LOAD                   (uint16)(_Q16 / PWM_CYCLE * (GLI_TIME + PWM_DEADTIME) / 16)  // 最小脉冲

#define BASE_FREQ                       ((MOTOR_SPEED_BASE / 60) * Pole_Pairs)				// 基准频率


#define MAX_BEMF_VOLTAGE                ((MOTOR_SPEED_BASE*Ke)/(1000.0))
#define MAX_OMEG_RAD_SEC                ((float)(_2PI*BASE_FREQ))

#define OBS_FBASE                       _Q15(BASE_FREQ*TPWM_VALUE)
#define OBS_KLPF                        _Q15(_2PI*BASE_FREQ*TPWM_VALUE)
#define SPEED_KLPF                      _Q15(_2PI*SPD_BW*TPWM_VALUE)
#define OBS_EA_KS                       _Q15((2*MOTOR_SPEED_SMOMIN_RPM*_2PI*BASE_FREQ*TPWM_VALUE)/MOTOR_SPEED_BASE)


// 过调制
#define OverModulation                  (0)

#define EstimateAlgorithm               (AO)
#define Open_Start_Mode                 (Omega_Start)
#define Shunt_Resistor_Mode             (Double_Resistor)

#define CalibENDIS                      (1)
#define SVPMW_Mode                      (SVPWM_7_Segment)
#define DouRes_Sample_Mode              (DouRes_1_Cycle)


#if (EstimateAlgorithm==AO)
    #define OBS_K1T                         _Q11((3.0/(125.0*1.0))*(LQ/TPWM_VALUE)*(HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE))
    #define OBS_K2T                         _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
    #define OBS_K2T_Actual                  _Q11(RS*HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE)
    #define OBS_K3T                         _Q8(2.5)
    #define OBS_K4T                         _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
#else
    #define OBS_K1T                         _Q15(LD/(LD+RS*TPWM_VALUE))
    #define OBS_K2T                         _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE_Start/HW_BOARD_CURR_BASE))
    #define OBS_K2T_Actual                  _Q13((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(HW_BOARD_VOLTAGE_BASE/HW_BOARD_CURR_BASE))
    #define OBS_K3T                         _Q15((TPWM_VALUE/(LD+RS*TPWM_VALUE))*(MAX_BEMF_VOLTAGE/HW_BOARD_CURR_BASE))
    #define OBS_K4T                         _Q15(((LD-LQ)*TPWM_VALUE*MAX_OMEG_RAD_SEC)/(LD+RS*TPWM_VALUE))
#endif

// AO SMO 估算器增益
#define OBSW_KP_GAIN                    _Q12(2*_2PI*ATT_COEF*ATO_BW/BASE_FREQ)
#define OBSW_KI_GAIN                    _Q15(_2PI*ATO_BW*ATO_BW*TPWM_VALUE/BASE_FREQ)

#define OBSW_KP_GAIN_RUN                _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN/BASE_FREQ)
#define OBSW_KI_GAIN_RUN                _Q15(_2PI*ATO_BW_RUN*ATO_BW_RUN*TPWM_VALUE/BASE_FREQ)

#define OBSW_KP_GAIN_RUN1               _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN1/BASE_FREQ)
#define OBSW_KI_GAIN_RUN1               _Q15(_2PI*ATO_BW_RUN1*ATO_BW_RUN1*TPWM_VALUE/BASE_FREQ)

#define OBSW_KP_GAIN_RUN2               _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN2/BASE_FREQ)
#define OBSW_KI_GAIN_RUN2               _Q15(_2PI*ATO_BW_RUN2*ATO_BW_RUN2*TPWM_VALUE/BASE_FREQ)

#define OBSW_KP_GAIN_RUN3               _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN3/BASE_FREQ)
#define OBSW_KI_GAIN_RUN3               _Q15(_2PI*ATO_BW_RUN3*ATO_BW_RUN3*TPWM_VALUE/BASE_FREQ)

#define OBSW_KP_GAIN_RUN4               _Q12(2*_2PI*ATT_COEF*ATO_BW_RUN4/BASE_FREQ)
#define OBSW_KI_GAIN_RUN4               _Q15(_2PI*ATO_BW_RUN4*ATO_BW_RUN4*TPWM_VALUE/BASE_FREQ)

// PLL估算器增益
#define OBSE_PLLKP_GAIN1                _Q11(((2*ATT_COEF*_2PI*E_BW1*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
#define OBSE_PLLKI_GAIN1                _Q11((_2PI*E_BW1*_2PI*E_BW1*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)

#define OBSE_PLLKP_GAIN2                _Q11(((2*ATT_COEF*_2PI*E_BW2*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
#define OBSE_PLLKI_GAIN2                _Q11((_2PI*E_BW2*_2PI*E_BW2*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)

#define OBSE_PLLKP_GAIN3                _Q11(((2*ATT_COEF*_2PI*E_BW3*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
#define OBSE_PLLKI_GAIN3                _Q11((_2PI*E_BW3*_2PI*E_BW3*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)

#define OBSE_PLLKP_GAIN4                _Q11(((2*ATT_COEF*_2PI*E_BW4*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
#define OBSE_PLLKI_GAIN4                _Q11((_2PI*E_BW4*_2PI*E_BW4*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)

#define OBSE_PLLKP_GAIN5                _Q11(((2*ATT_COEF*_2PI*E_BW5*LD - RS)*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)
#define OBSE_PLLKI_GAIN5                _Q11((_2PI*E_BW5*_2PI*E_BW5*LD*TPWM_VALUE*HW_BOARD_CURR_BASE)/HW_BOARD_VOLTAGE_BASE)


// ------------------------------------------------------------------------------------------------------------------
// 3.其他宏定义

#define LED_PIN                         (GP03)
#define LED_ON                          (LED_PIN = 0)               // LED 点亮
#define LED_OFF                         (LED_PIN = 1)               // LED 熄灭
#define LED_OFF_STATE                   (LED_PIN == 1)              // LED 熄灭状态
#define LED_TOGGLE                      (LED_PIN = ~LED_PIN)        // LED 翻转



#define VDCBUS_ADC                      ADC2_DR                     // 高压直流
#define VACBUS_ADC                      ADC3_DR                     // 高压交流
#define NTC1_ADC                        ADC6_DR                     // NTC1
#define NTC2_ADC                        ADC7_DR                     // NTC2
#define IGBT_NTC_ADC                    ADC11_DR                    // IGBT

#define PRE_DRIVER_RST                  GP04                        // 驱动复位
#define MAIN_RELAY                      GP51                        // 母线主继电器



// ------------------------------------------------------------------------------------------------------------------
// 4.结构体定义与声明 函数声明


// LED控制
typedef struct
{
    enum
    {
        LED_STATE_OFF,                              // 熄灭
        LED_STATE_ON,                               // 常亮
        LED_STATE_BLINK_FAULT,                      // 故障闪烁
        LED_STATE_POST_FAULT_DELAY                  // 延时等待
    } LedState ;
    
    uint16_t PauseDelayCnt;
    uint8_t BlinkCnt;
} LED_Control_t;
extern volatile LED_Control_t ledControl;
void LED_State_Display(uint8_t Xn0);


// 上电控制
typedef  struct
{
    enum
    {
        DELAY_POWER_ON,                     // 等待上电
        DELAY_FREE_RST,                     // 等待释放复位
        POWER_RUN                           // 上电完成
    } PowerSate;
    uint16_t PowerInCnt;                    // 上电时间计数
} Power_Control_t;

extern volatile Power_Control_t powerControl;
void Power_In_Control(void);


// 电机控制
typedef struct
{
    // 模拟数据
    uint16_t DCBus;
    uint16_t ACBus;
    uint16_t IGBTTemp;
    
    // 数据处理
    uint16_t ACBusMax;
    uint16_t ACBusMin;
    
    // 速度相关
	enum
	{
		OPEN_MODE,
		CLOSE_MODE
	}LoopState;
	uint8_t LoopTime;
	
    float RampInc;
    float RampDec;
	
    int16_t TargetRef;
    float ActualRef;
    int16_t ActualSpeed;
	
    int16_t ActualQOutValue;
    int16_t QOutRef;
    
    int16_t IQRef;
    int16_t IDRef;
    int16_t ISRef;
	
    // 保护相关
    uint16_t BackEMF;
    // 测试
    int16_t uPhaseCurr;
    int16_t vPhaseCurr;
} Motor_Control_t;

extern Motor_Control_t motorControl;

typedef struct
{
    enum
    {
        CALIB_INIT,
        GET_OFFSET,
        OFFSET_READY
    } OffsetCalib;
    // U相
    int16_t IuOffset;
    int32_t IuOffsetSum;
    // V相
    int16_t IvOffset;
    int32_t IvOffsetSum;
    // W相
    int16_t IwBusOffset;
    int32_t IwBusOffsetSum;
    int16_t OffsetCount;
} Curr_Offset_t;

extern Curr_Offset_t xdata currOffset;

// ------------------------------------------------------------------------------------------------------------------
// 5.硬件初始化与调用声明

void Driver_Init(void);

void VREF_Config_Init(void);
void ADC_Init(void);
void Get_ADC_Value(void);
void AMP_Init(void);
void GPIO_Init(void);
void PreDriver_Falut_Init(void);

void Sys_Tick(void);
// 串口初始化
void UART1_Init(void);
void UART2_Init(void);

extern uint8_t xdata DebugDat[64];
void Dabug_Data_Update(void);


// 增量式硬件PI控制器初始化
void HW_Zero_PI_Init(void);
void HW_One_PI_Init(void);
void HW_Two_PI_Init(void);
void HW_Three_PI_Init(void);
// 增量式硬件PI计算
int16_t HW_Zero_Calc(int16_t Xn0);
int16_t HW_One_Calc(int16_t Xn0);
int16_t HW_Two_Calc(int16_t Xn0);
int16_t HW_Three_Calc(int16_t Xn0);

// 低通滤波计算
int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K);
int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K);
int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K);



// ------------------------------------------------------------------------------------------------------------------
// 6.函数声明 变量声明

extern bool data IsTick;
extern bool data isCtrlPowOn;

void Tick_Task(void);
void Motor_Control_State(void);

void Loop_Control(void);
void Ref_Ramp(void);
void Get_Target_Ref(void);
#endif


