/**
    @copyright None
    @file      SMDU.c
    @author    Comment Vivre
    @date      2025-11-03
    @brief     None
*/
#include <MyProject.h>
// ------------------------------------------------------------------------------------------------------------------
// 1. PI控制器初始化
/**
    @function     HW_Zero_PI_Init
    @brief        0号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Zero_PI_Init(void)
{
    PI0_KP = SKP;
    PI0_KI = SKI;
    PI0_EK = 0;
    PI0_EK1 = 0;
    PI0_UKMAX = SOUTMAX;
    PI0_UKMIN = SOUTMIN;
    PI0_UKH = 0;
    PI0_UKL = 0;
}
/**
    @function     HW_One_PI_Init
    @brief        1号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_One_PI_Init(void)
{
    PI1_KP = FiledWeakenKp;
    PI1_KI = FiledWeakenKi;
    PI1_EK = 0;
    PI1_EK1 = 0;
    PI1_UKMAX =  0;
    PI1_UKMIN =  _Q15((float) -35.0 / 180.0) ;
    PI1_UKH = 0;
    PI1_UKL = 0;
}
/**
    @function     HW_Two_PI_Init
    @brief        2号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Two_PI_Init(void)
{
    PI2_KP = 0;
    PI2_KI = 0;
    PI2_KD = 0;
    PI2_EK = 0;
    PI2_EK1 = 0;
    PI2_EK2 = 0 ;
    PI2_UKMAX = 0;
    PI2_UKMIN = 0;
    PI2_UKH = 0;
    PI2_UKL = 0;
}

/**
    @function     HW_Three_PI_Init
    @brief        3号硬件PI控制器初始化
    @date         2025-11-01
*/
void HW_Three_PI_Init(void)
{
    PI3_KP = 0;
    PI3_KI = 0;
    PI3_KD = 0;
    PI3_EK = 0;
    PI3_EK1 = 0;
    PI3_EK2 = 0 ;
    PI3_UKMAX = 0;
    PI3_UKMIN = 0;
    PI3_UKH = 0;
    PI3_UKL = 0;
}

// ------------------------------------------------------------------------------------------------------------------
// 2. PI计算
/**
    @function     HW_Zero_Calc
    @brief        0号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Zero_Calc(int16_t Xn0)
{
    PI0_EK = Xn0;
    SMDU_RunBlock(0, 7);
    return PI0_UKH;
}

/**
    @function     HW_One_Calc
    @brief        1号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_One_Calc(int16_t Xn0)
{
    PI1_EK = Xn0;
    SMDU_RunBlock(1, 7);
    return PI1_UKH;
}

/**
    @function     HW_Two_Calc
    @brief        2号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Two_Calc(int16_t Xn0)
{
    PI2_EK = Xn0;
    SMDU_RunBlock(2, 7);
    return PI2_UKH;
}

/**
    @function     HW_Three_Calc
    @brief        3号PI计算
    @param[in]    Xn0: [输入/出]
    @return       PI计算值
    @date         2025-11-01
*/
int16_t HW_Three_Calc(int16_t Xn0)
{
    PI3_EK = Xn0;
    SMDU_RunBlock(3, 7);
    return PI3_UKH;
}

// ------------------------------------------------------------------------------------------------------------------
// 3.LPF 低通滤波计算

/**
    @function     LPF_Zero_Update
    @brief        0号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_Zero_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF0_K = K;
    LPF0_X = Xn1;
    LPF0_YH = Xn0;
    SMDU_RunBlock(0, 6);
    return LPF0_YH;
}

/**
    @function     LPF_One_Update
    @brief        1号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_One_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF1_K = K;
    LPF1_X = Xn1;
    LPF1_YH = Xn0;
    SMDU_RunBlock(1, 6);
    return LPF1_YH;
}

/**
    @function     LPF_Two_Update
    @brief        2号低通滤波计算
    @param[in]    Xn1: [输入/出]
**           Xn0: [输入/出]
**           K: [输入/出]
    @return       滤波结果
    @date         2025-11-03
*/
int16_t LPF_Two_Update(int16_t Xn1, int16_t Xn0, int16_t K)
{
    LPF2_K = K;
    LPF2_X = Xn1;
    LPF2_YH = Xn0;
    SMDU_RunBlock(2, 6);
    return LPF2_YH;
}
