/*
 * var.c
 *
 *  Created on: 2023年5月17日
 *      Author: wangd
 */
#include "var.h"   // DSP2803x Examples Include File
#ifndef TRUE
#define FALSE 0
#define TRUE  1
#endif


union FAULTFLAG_UNI FaultFlag;
Uint16 DCbus_voltage=0;
Uint32 IsrTime = 0;
_iq test =0;
int16 test2 =0;
int16 test3 =0;
int16 test4 =0;
int16 Tmotor = 0;
PI_CONTROLLER pid1_idc = PI_CONTROLLER_DEFAULTS;
PI_CONTROLLER pid1_spd = PI_CONTROLLER_DEFAULTS;
RMPCNTL rc1 = RMPCNTL_DEFAULTS;

int32 pwmlimit = 32768;
_iq SpeedRef=_IQ(0.0);//初始速度
int16 Direction = 1;
_iq BemfA=0;
_iq BemfB=0;
_iq BemfC=0;
_iq Iphase=0;
_iq DC_current_real = 0;
_iq DC_current_filer = 0;
int32 DC_current_avr = 0;
int32 DC_current_filter_avr = 0;
int16 Tvot = 0;
_iq CurrentSet = _IQ(0.0);
Uint16 Fault_clear = 0;
int32 XintTime = 60000000;
volatile Uint16 EnableFlag = FALSE;
volatile Uint16 Enable_ALLOW = FALSE;
void InitVar(void){
    FaultFlag.all = 0;
    // Initialize the PI module for dc-bus current
    pid1_idc.Kp = _IQ(0.05);
    pid1_idc.Ki   = _IQ(0.0002);
    pid1_idc.Umax = _IQ((float)pwmlimit/32767);
    pid1_idc.Umin = _IQ((float)(-pwmlimit)/32767);
    // Initialize the PI module for speed
    pid1_spd.Kp   = 16384;
    pid1_spd.Ki   = 1638;
    pid1_spd.Umax = _IQ(1.0);
    pid1_spd.Umin = _IQ(0.0);

    // Initialize RMPCNTL module
    rc1.RampDelayMax = 5;
    rc1.RampLowLimit = _IQ(-1.0);
    rc1.RampHighLimit = _IQ(1.0);



}
void FaultTreat(void){
    if(hall1.HallGpio == 0 || hall1.HallGpio == 7){
        FaultFlag.bit.HallFlag =1;
    }
    OverVoltage();
    OverMotorRpm();
    OverMotCtrTemp();
    SoftOverCurFlag();
    Fault_clear_fuc();
}
//I真实 = adc * 3.3 / 4096 /0.1
void SoftOverCurFlag(void){
    static int16 cnt = 0;
    if(DC_current_filer>((Uint32)SOFTCURlIMIT<<12)){
        cnt++;
        if(cnt>10000/MAINLOOPTIME){// us
            cnt = 10000/MAINLOOPTIME+1;
            FaultFlag.bit.SoftOverCurFlag = 1;
            t4 = DC_current_filer;
        }
    }else{
        cnt = 0;
    }

}
void OverVoltage(void)
{
//过压不可恢复  欠压可恢复

    static int16 j = 0;

    if (DCbus_voltage >= VOLTAGEUP)
    {
        FaultFlag.bit.OverVolFlag = 1;
    }
    else if (DCbus_voltage <= VOLTAGELOW)
    {
        j = MAINLOOPFREQ;
        FaultFlag.bit.LowVolFlag = 1;
    }
    else
    {
            if (FaultFlag.bit.LowVolFlag == 1)
        {
            j--;
            if (j <= 0)
            {
                j = 0;
                FaultFlag.bit.LowVolFlag = 0;
            }
        }
        else
        {
            j = 0;
        }
    }
}
void OverMotorRpm(void)
{//飞车不可恢复
    if (speed1.BaseRpm >= SPEEDUP)
    {
        FaultFlag.bit.OverRpmFlag = 1;
    }

}
void OverMotCtrTemp(void)
{//过温可恢复

    if (Tmotor >= MOTORTEMPOVER)
    {
        FaultFlag.bit.OverMotTempFlag = 1;
    }
    else if (Tmotor < MOTORTEMPLIMIT)
    {
        FaultFlag.bit.OverMotTempFlag = 0;
    }
    if (Tvot >= IGBTTEMPOVER)
    {
        FaultFlag.bit.IgbtTempFaultFlag = 1;
    }
    else if (Tvot < 50)
    {
        FaultFlag.bit.IgbtTempFaultFlag = 0;
    }

}
void Fault_clear_fuc(void){
    if(Fault_clear == 1) {
         static int16 fault_cnt = 0;
         GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1;
         FaultFlag.bit.OverCurFlag = 0;
         fault_cnt++;
         if(fault_cnt>MAINLOOPFREQ){
         Fault_clear = 0;
         fault_cnt = 0;
         OpenPwm();
         if (speed1.BaseRpm < 700)
         {
            FaultFlag.bit.OverRpmFlag = 0;
         }
         if(GpioDataRegs.GPADAT.bit.GPIO16==1){
               FaultFlag.bit.Ipmfault = 0;
          }

        }
         if(FaultFlag.bit.OverVolFlag == 1 && DCbus_voltage<320){
             FaultFlag.bit.OverVolFlag = 0;
         }
         FaultFlag.bit.HallFlag =0;

     }else{
         GpioDataRegs.GPBSET.bit.GPIO32 = 1;
     }
    if(EnableFlag == 0 && pwm1.DutyFunc==0){
         FaultFlag.bit.SoftOverCurFlag = 0;//失能清软件过流
    }
}
int16 LimitMotCtrTemp(int32 period){
    static int16 result = 32767;
    static int32 cnt1=0;
    static int32 cnt2=0;

    if(Tmotor>MOTORTEMPLIMIT){
        cnt1++;
        cnt2 = 0;
        if(cnt1>period){
            result = (int32)result*31128>>15;
            FaultFlag.bit.LimitMotTempFlag = 1;
            cnt1 =0;
        }

    }else{
        cnt1 =0;
        if(result<31128){
            cnt2++;
            if(cnt2>period){
                cnt2 = 0;
                int32 tmp = (int32)result*34491>>15;
                if(tmp>32767){
                    result = 32767;
                }else{
                    result = tmp;
                }
            }

        }else{
            result = 32767;
            FaultFlag.bit.LimitMotTempFlag = 0;
            cnt2 = 0;
        }

    }
    //test = result;
    return result;
}
