/**
    @copyright None
    @file      control.c
    @author    Comment Vivre
    @date      2025-12-29
    @brief     None
*/
#include <MyProject.h>

Start_Test_t xdata startTest;
/**
    @function     Motor_Start_Test
    @brief        电机启动测试
    @date         2025-12-23
*/
void Motor_Start_Test(void)
{
    // 有故障 结束测试
    if (FaultSource != SYS_NO_FAULT)
    {
        startTest.State = TEST_FAULT;
        isCtrlPowOn = false;
        startTest.RunTime = 0;
        startTest.StopTime = 0;
    }
    
    switch (startTest.State)
    {
        // 停止
        case TEST_MOTOR_STOP:
            isCtrlPowOn = false;
            loopCtrl.TargetRef = 0;
            
            if (++startTest.StopTime > MOTOR_STOP_TIME)
            {
                startTest.StopTime = 0;
                startTest.State = TEST_MOTOR_START;
            }
            
            break;
            
        // 启动
        case TEST_MOTOR_START:
            isCtrlPowOn = true;
            loopCtrl.TargetRef = MOTOR_START_TEST_SPEED;
            
            if (++startTest.RunTime > MOTOR_RUN_TIME)
            {
                startTest.RunTime = 0;
                startTest.State = TEST_MOTOR_STOP;
            }
            
            break;
            
        case TEST_FAULT:
            break;
    }
}

/**
    @function     Compressor_Speed_Control
    @brief        压缩机速度控制
    @date         2025-12-29
*/
void Compressor_Speed_Control(void)
{
    static uint8_t powONHoldCnt  = 0;   // 开机确认计数
    static uint8_t powOFFHoldCnt = 0;   // 关机确认计数
    
    if (isCtrlPowOn)
    {
        // 关机判定：频率小于下限或大于上限
        if ((inputCapture.OutFreq < OFFPWMFreq) || (inputCapture.OutFreq > OFFPWMFreqHigh))
        {
            if (++powOFFHoldCnt > 50)
            {
                powOFFHoldCnt = 0;
                isCtrlPowOn = false;
            }
            
            powONHoldCnt = 0;
        }
    }
    else
    {
        // 开机判定：频率在有效范围内
        if ((inputCapture.OutFreq > ONPWMFreq) && (inputCapture.OutFreq < OFFPWMFreqHigh))
        {
            if (++powONHoldCnt > 50)
            {
                powONHoldCnt = 0;
                isCtrlPowOn = true;   // 开机
            }
            
            powOFFHoldCnt = 0;
        }
    }
    
    if (isCtrlPowOn)
    {
        switch (loopCtrl.CompState)
        {
            case COMP_RETURN_OIL:
                loopCtrl.TargetRef = RETURN_OIL_SPEED;
                
                if (++loopCtrl.ReturnOilCnt > RETURN_OIL_TIME)
                {
                    loopCtrl.ReturnOilCnt = 0;
                    loopCtrl.CompState = COMP_RUN;
                }
                
                break;
                
            case COMP_RUN:
                loopCtrl.TargetRef = SPEED_FREQ_K * inputCapture.OutFreq;
                
                // 限幅
                if (loopCtrl.TargetRef > MOTOR_SPEED_MAX_RPM)
                { loopCtrl.TargetRef = MOTOR_SPEED_MAX_RPM; }
                else if (loopCtrl.TargetRef < MOTOR_SPEED_MIN_RPM)
                { loopCtrl.TargetRef = MOTOR_SPEED_MIN_RPM; }
                
                break;
        }
    }
    else
    { loopCtrl.TargetRef = 0; }
}


/**
    @function     Get_Target_Ref
    @brief        速度给定
    @date         2025-11-01
*/
void Get_Target_Ref(void)
{
    #if (SPEED_MODE == NONEMODE)
    isCtrlPowOn = true;
    loopCtrl.TargetRef = _Q15(2000.0 / MOTOR_SPEED_BASE);
    #elif (SPEED_MODE == PWMMODE)
    Input_Capture_Calc();
    Compressor_Speed_Control();
    #elif (SPEED_MODE == STARTTEST)
    Motor_Start_Test();
    #endif
}



