Browse Source

更新

添加宏定义处理
avery 6 months ago
parent
commit
1a1a5c0fd5

+ 2 - 2
User/Application/main.c

@@ -11,8 +11,8 @@ bool data IsTick = false;
 bool data isCtrlPowOn = false;
 
 Curr_Offset_t xdata currOffset;
-Motor_Control_t motorControl;
-
+Motor_Control_t xdata motorControl;
+Motor_Fault_t xdata motorFault;
 
 CurrentVarible     xdata   mcCurVarible;
 FOCCTRL            xdata   mcFocCtrl;

+ 10 - 10
User/Function/FocControl.c

@@ -68,7 +68,7 @@ void Motor_Control_State(void)
                             ((currOffset.IvOffset > 19959) || (currOffset.IvOffset < 10107)))
                         {
                             currOffset.OffsetCalib = CALIB_INIT;
-                            mcFaultSource = FaultIbusOffset;
+                            mcFaultSource = FaultOffsetError;
                             FaultProcess();
                         }
                         else
@@ -259,7 +259,7 @@ void Motor_Control_State(void)
             
             break;
             
-        case mcStart:   // 配置电机启动参数,进入mcRun状态。
+        case mcStart:
             Motor_Open();
             break;
             
@@ -267,7 +267,7 @@ void Motor_Control_State(void)
             break;
             
         case mcStop:
-            #if (StopBrakeFlag)
+            #if (STOP_BREAK)
             if (motorControl.ActualSpeed < MOTOR_STOP_SPEED)
             {
                 MOE = 0;
@@ -279,18 +279,17 @@ void Motor_Control_State(void)
                 ClrBit(DRV_CR, OCS);  // OCS = 0, DRV_COMR;OCS = 1, FOC/SVPWM/SPWM
                 MOE = 1;
                 mcState  = mcBrake;
-                mcFocCtrl.State_Count = StopWaitTime;
+                mcFocCtrl.State_Count = BREAK_WAIT_TIME;
             }
             
             #else
-            {
-                mcState = mcReady;
-                FOC_CR1 = 0x00;
-                ClrBit(DRV_CR, FOCEN);   //关闭FOC
-                MOE = 0;
-            }
+            mcState = mcReady;
+            FOC_CR1 = 0x00;
+            ClrBit(DRV_CR, FOCEN);   //关闭FOC
+            MOE = 0;
             #endif
             break;
+            #if (STOP_BREAK)
             
         case mcBrake:
             if (isCtrlPowOn)
@@ -306,6 +305,7 @@ void Motor_Control_State(void)
             }
             
             break;
+            #endif
             
         case mcFault:
             if (mcFaultSource == FaultNoSource)

+ 57 - 41
User/Function/Protect.c

@@ -445,14 +445,14 @@ void Fault_IPMOverTemp(void)
 {
     if (mcFaultSource == FaultNoSource)
     {
-        if (motorControl.IGBTTemp > TempProtValue)
+        if (motorControl.IGBTTemp > TEMP_PROT_VALUE)
         {
             mcFaultDect.OverTempDetecCnt++;
             
             if (mcFaultDect.OverTempDetecCnt > 20)
             {
                 mcFaultDect.OverTempDetecCnt = 0;
-                mcFaultSource             = FaultIpmTemp;
+                mcFaultSource             = FaultIPMOTErr;
                 FaultProcess();
             }
         }
@@ -461,43 +461,9 @@ void Fault_IPMOverTemp(void)
             mcFaultDect.OverTempDetecCnt = 0;
         }
     }
-    
-    /*******温度保护恢复*********/
-    if ((mcState == mcFault) && (mcFaultSource == FaultIpmTemp))
-    {
-        if (motorControl.IGBTTemp < TempRecoverValue)
-        {
-            mcFaultDect.TempRecoverCnt++;
-            
-            if (mcFaultDect.TempRecoverCnt > 200)
-            {
-                mcFaultSource           = FaultNoSource;
-                mcFaultDect.TempRecoverCnt = 0;
-            }
-        }
-        else
-        {
-            mcFaultDect.TempRecoverCnt = 0;
-        }
-    }
 }
 
 
-/*压机偏置电压错误恢复*/
-void IOffsetRecoverEnable()
-{
-    if ((mcFaultSource == FaultIbusOffset) && (mcProtectTime.IbusOffsetProtectTimes < IbusOffsetRestartTimes))
-    {
-        mcFaultDect.IbusOffsetRecoverCnt++;
-        
-        if ((mcFaultDect.IbusOffsetRecoverCnt) >= IbusOffsetRecoverTime)//IbusOffsetRecoverTime*1ms
-        {
-            mcFaultDect.IbusOffsetRecoverCnt = 0;
-            mcProtectTime.IbusOffsetProtectTimes++;
-            mcFaultSource = FaultNoSource;
-        }
-    }
-}
 void Fault_UdcMax_Min(void)
 {
     if ((mcFaultSource == FaultNoSource) && (mcState == mcRun))
@@ -575,11 +541,6 @@ void Fault_Detection(void)
             Fault_OverUnderVoltage();
         }
         #endif
-        #if (IbusOffsetRecoverEnable)  //偏执电压保护恢复
-        {
-            IOffsetRecoverEnable();
-        }
-        #endif
     }
     else if (mcFaultDect.segment == 2)
     {
@@ -626,3 +587,58 @@ void Fault_Detection(void)
     #endif
 }
 
+
+void Motor_Fault_Recovery(void)
+{
+    switch (mcFaultSource)
+    {
+        case FaultOverVoltage:
+            if (motorControl.DCBus < OVER_RECOVER_VALUE)
+            { RECOVER_DELAY(motorFault.RecoverDelay.dcDelay); }
+            
+            break;
+            
+        case FaultUnderVoltage:
+            if (motorControl.DCBus > UNDER_RECOVER_VALUE)
+            { RECOVER_DELAY(motorFault.RecoverDelay.dcDelay); }
+            
+            break;
+            
+        case FaultIPMOTErr:
+            if (motorControl.IGBTTemp <= TEMP_RECOVER_VALUE)
+            { RECOVER_DELAY(motorFault.RecoverDelay.otDelay); }
+            
+            break;
+            
+        case FaultLossPhase:
+            RECOVER_DELAY(motorFault.RecoverDelay.phaseDelay);
+            break;
+            
+        case FaultStall:
+        case FaultStart:
+            RECOVER_DELAY(motorFault.RecoverDelay.stallDelay);
+            break;
+            
+        case FaultSoftOVCurrent:
+        case FaultHardOVCurrent:
+            RECOVER_DELAY(motorFault.RecoverDelay.ocDelay);
+            break;
+            
+        case FaultOverPower:
+            RECOVER_DELAY(motorFault.RecoverDelay.opDelay);
+            break;
+            
+        case FaultPWMLoss:
+            if (motorControl.PWMFreq > 300)
+            { mcFaultSource = FaultNoSource; }
+            
+            break;
+            
+        case FaultOffsetError:
+            RECOVER_DELAY(motorFault.RecoverDelay.offsetDelay);
+            break;
+    }
+}
+
+
+

+ 2 - 4
User/include/Customer.h

@@ -116,7 +116,6 @@
 // ------------------------------------------------------------------------------------------------------------------
 // 6.环路相关
 
-
 #define OUT_LOOP_CONTROL                (1)                                     // 外环控制使能
 #define LOOP_MODE                       (SPEED_CONTROL_MODE)                    // 外环配置
 #define CONTROL_MODE                    (NONE_MODE)                              // 控制模式
@@ -147,9 +146,8 @@
 
 #define TARGET_SPEED_SET                _Q15(3000.0/MOTOR_SPEED_BASE)           // 测试转速
 
-
-#define StopBrakeFlag                   (0)
-#define StopWaitTime                    (2000)                                  // (ms) 刹车等待时间
+#define STOP_BREAK                      (0)
+#define BREAK_WAIT_TIME                 (2000)                                  // (ms) 刹车等待时间
 
 
 // ------------------------------------------------------------------------------------------------------------------

+ 62 - 2
User/include/Definition.h

@@ -242,6 +242,47 @@
 #define DATA_OFFSET                     0x06                        // 头(1B) + 地址(2B) + 功能码(1B) + 长度(2B)
 #define CRC_OFFSET                      0x02                        // 校验字(2B)
 
+/**
+ * @brief 故障恢复延时计数宏
+ * @param cnt  对应故障的延时计数器变量
+ *
+ * @note  用于故障恢复逻辑,当检测条件已恢复正常后,
+ *        不会立刻清除故障,而是等待计数超过
+ *        PROTECT_RECOVER_TIME 后才解除故障,
+ *        以避免抖动恢复。
+ */
+#define RECOVER_DELAY(cnt)              \
+    do {                                \
+        (cnt)++;                        \
+        if ((cnt) > PROTECT_RECOVER_TIME) { \
+            (cnt) = 0;                  \
+            mcFaultSource = FaultNoSource; \
+        }                               \
+    } while(0)
+
+
+/**
+ * @brief 通用的故障计数检测宏
+ * @param cnt        计数器变量
+ * @param cond       检测条件 (true 表示异常)
+ * @param threshold  连续超过阈值次数才判定故障
+ * @param faultCode  触发的故障源代码
+ */
+#define FAULT_COUNT_UPDOWN(cnt, cond, threshold, faultCode) \
+    do { \
+        if (cond) { \
+            if (++(cnt) > (threshold)) { \
+                (cnt) = 0; \
+                mcFaultSource = (faultCode); \
+            } \
+        } else if (cnt) { \
+            (cnt)--; \
+        } \
+    } while (0)
+
+
+#define IGBT_TEMP_VALUE(Xn0)            _Q15(5.0*Xn0/(10+Xn0)/HW_ADC_REF)
+
 // ------------------------------------------------------------------------------------------------------------------
 // 4.结构体定义与声明 函数声明
 
@@ -291,7 +332,7 @@ typedef struct
     // 数据处理
     uint16_t ACBusMax;
     uint16_t ACBusMin;
-    
+    uint16_t PWMFreq;
     // 预充电状态
     enum
     {
@@ -338,7 +379,26 @@ typedef struct
     uint16_t BackEMF;
 } Motor_Control_t;
 
-extern Motor_Control_t motorControl;
+extern Motor_Control_t xdata motorControl;
+
+
+
+typedef struct
+{
+    struct
+    {
+        uint16_t dcDelay;       // 母线电压恢复延时
+        uint16_t phaseDelay;    // 缺相恢复延时
+        uint16_t otDelay;       // 过温恢复延时
+        uint16_t ocDelay;       // 过流恢复延时
+        uint16_t stallDelay;    // 堵转恢复延时
+        uint16_t opDelay;       // 过功率恢复延时
+        uint16_t offsetDelay;   // 偏置异常恢复延时
+    } RecoverDelay;
+} Motor_Fault_t;
+
+
+extern Motor_Fault_t xdata motorFault;
 
 typedef struct
 {

+ 43 - 42
User/include/Myproject.h

@@ -9,7 +9,7 @@
 
 
 // math.h包含算术运算的数学函数
-#include <math.h>                       
+#include <math.h>
 #include <Definition.h>
 
 typedef enum
@@ -25,7 +25,7 @@ typedef enum
     mcStop      = 8,
     mcFault     = 9,
     mcBrake     = 10
-}MotStaType;
+} MotStaType;
 
 
 
@@ -46,7 +46,7 @@ void SPI_Init(void);
 void WatchDogConfig(uint32 value, uint8 Status);
 
 
-unsigned short CRC_Check(unsigned char start_sector , unsigned char offset_sector);
+unsigned short CRC_Check(unsigned char start_sector, unsigned char offset_sector);
 
 
 extern void FOC_Init(void);
@@ -57,19 +57,19 @@ extern void Motor_Open(void);
 #if (FiledWeakenCompEnable)
 typedef struct
 {
-    uint16    FieldWeakenActualUS_Q15;    
+    uint16    FieldWeakenActualUS_Q15;
     uint16    FieldWeakenActualDcbus_Q15;
     int16     FieldWeakenIsTheta;
-	  int16     mcIqref;                       
+    int16     mcIqref;
     int16     mcIdref;
-}FieldWeakeningTypeDef;
+} FieldWeakeningTypeDef;
 
-FieldWeakeningTypeDef xdata mcFieldWeaken;	
-void GetFieldWeakenUsAndUdc_Q15(int16 Ud, int16 Uq , uint16 Dcbus);
+FieldWeakeningTypeDef xdata mcFieldWeaken;
+void GetFieldWeakenUsAndUdc_Q15(int16 Ud, int16 Uq, uint16 Dcbus);
 void GetFieldWeakenIdRefAndIqRef_Q15(int16 Is);
 void GetFieldWeakenIsTheta_Q15(void);
 int16 mcFieldWeakenPI(int16 feedbackvalue, int16 referencevalue);
-void  FiledWeakenControl(int16 Ud, int16 Uq , uint16 Dcbusk,int16 Is);
+void  FiledWeakenControl(int16 Ud, int16 Uq, uint16 Dcbusk, int16 Is);
 #endif
 
 #define P_Power(POWER)                  _Q15(POWER*2/HW_BOARD_CURR_BASE/HW_BOARD_VOLTAGE_BASE/1.5)
@@ -86,16 +86,16 @@ typedef struct
 
 typedef struct
 {
-    int16  mcDcbusFlt;	// 母线电压
-    int16  mcDcbus_max;	// 母线电压最大值
-    int16  mcDcbus_min;	// 母线电压最小值
-    int16  mcDcbus_chazhi;	// 母线电压最小值
-    int16  Powerlpf;	// 功率滤波后的值
+    int16  mcDcbusFlt;  // 母线电压
+    int16  mcDcbus_max; // 母线电压最大值
+    int16  mcDcbus_min; // 母线电压最小值
+    int16  mcDcbus_chazhi;  // 母线电压最小值
+    int16  Powerlpf;    // 功率滤波后的值
     
-    int16  UQFlt;	// UQ滤波后的值
-    int16  UDFlt;	// UD滤波后的值
+    int16  UQFlt;   // UQ滤波后的值
+    int16  UDFlt;   // UD滤波后的值
     
-    uint16 State_Count;	// 电机各个状态的时间计数
+    uint16 State_Count; // 电机各个状态的时间计数
 } FOCCTRL;
 
 extern CurrentVarible xdata   mcCurVarible;
@@ -121,10 +121,11 @@ typedef enum
     FaultLossPhase     = 5,                // 缺相保护
     FaultStall         = 6,                // 堵转保护
     FaultStart         = 7,                // 启动保护
-	FaultIpmTemp       = 9,                // IPM温度保护
-	FaultIbusOffset    = 11,               // pianzhi
-	FaultshuruLoss     = 14,
-	
+    FaultIPMOTErr       = 9,                // IPM温度保护
+    FaultOffsetError    = 11,               // pianzhi
+    FaultOverPower   = 12,
+    FaultshuruLoss     = 14,
+    FaultPWMLoss = 15,
 } FaultStateType;
 
 
@@ -136,44 +137,44 @@ typedef struct
     uint16 CurrentPretectTimes;        // 过流保护次数
     uint8  StartFlag;                  // 启动保护的标志位,用于判断哪个方法起作用
     uint8  StallFlag;                  // 堵转保护的标志位,用于判断哪个方法起作用
-	  uint8  IbusOffsetProtectTimes;     //偏执电压保护次数
-	  uint8  shurulossTimes;
-	
-}ProtectVarible;
+    uint8  IbusOffsetProtectTimes;     //偏执电压保护次数
+    uint8  shurulossTimes;
+    
+} ProtectVarible;
 
 typedef struct
 {
     uint8 segment;                    // 分段执行
-
+    
     //voltage protect
     uint16 OverVoltDetecCnt;           // 过压检测计数
     uint16 UnderVoltDetecCnt;          // 欠压检测计数
     uint16 VoltRecoverCnt;             // 过压恢复计数
-	  uint16 OverTempDetecCnt;           // 过温检测计数
-	  uint16 OverHuantempDetecCnt;           // 过温检测计数
-	  uint16 OverGuantempDetecCnt;           // 过温检测计数
-	  uint16 TempRecoverCnt;             // 过温恢复计数
-	  uint16 HuantempRecoverCnt;             // 过温恢复计数
-	  uint16 GuantempRecoverCnt;             // 过温恢复计数
+    uint16 OverTempDetecCnt;           // 过温检测计数
+    uint16 OverHuantempDetecCnt;           // 过温检测计数
+    uint16 OverGuantempDetecCnt;           // 过温检测计数
+    uint16 TempRecoverCnt;             // 过温恢复计数
+    uint16 HuantempRecoverCnt;             // 过温恢复计数
+    uint16 GuantempRecoverCnt;             // 过温恢复计数
     uint32 CurrentRecoverCnt;          // 过流保护恢复计数
-
-	  uint8  IbusOffsetRecoverCnt;     ///偏执电压保护恢复次数
+    
+    uint8  IbusOffsetRecoverCnt;     ///偏执电压保护恢复次数
     //stall protect
     uint16 StallDelayCnt;              // 堵转延迟判断计时
     uint16 StallDectEs;                // method 1,与ES相关
-	  uint16 StallDectEs2;                // method 1,与ES相关
+    uint16 StallDectEs2;                // method 1,与ES相关
     uint16 StallDectSpeed;             // method 2,与速度相关
     uint32 StallReCount;               // 堵转保护恢复计数
-	
+    
     //Loss Phase protect
     uint16 Lphasecnt;                  // 缺相保护计时
     uint16 AOpencnt ;                  // A缺相计数
     uint16 BOpencnt ;                  // B缺相计数
     uint16 COpencnt ;                  // C缺相计数
     uint16 mcLossPHRecCount;           // 缺相恢复计数
-		uint16 shurulossRecCount;           // 缺相恢复计数
-		int16  shurulossCnt;
-		
+    uint16 shurulossRecCount;           // 缺相恢复计数
+    int16  shurulossCnt;
+    
     int32 CurrentASum;
     int32 CurrentBSum;
     int32 CurrentCSum;
@@ -182,14 +183,14 @@ typedef struct
     int16 CurrentBAlign;
     int16 CurrentCAlign;
     
-		//start protect
+    //start protect
     int16 StartESCount;                // 启动保护判断ES的计数
     int16 StartEsCnt;                  // 启动保护判断ES的计时
     int16 StartDelay;                  // 启动保护判断ES的延迟
     int16 StartFocmode;                // 启动保护判断FOCMODE状态的计时
     int16 StartSpeedCnt;               // 启动保护判断速度和ES的计时
-		uint32 commu_time;
-}FaultVarible;
+    uint32 commu_time;
+} FaultVarible;
 
 extern FaultStateType     xdata    mcFaultSource;
 extern ProtectVarible     xdata    mcProtectTime;

+ 4 - 4
User/include/ProtectSet.h

@@ -40,6 +40,8 @@
 #define OVER_RECOVER_VALUE              _Q15(680.0 / HW_BOARD_VOLT_MAX)
 #define UNDER_RECOVER_VALUE             _Q15(130.0 / HW_BOARD_VOLT_MAX)
 
+// 保护恢复时间 (s)
+#define PROTECT_RECOVER_TIME     (180)
 
 
 /*缺相保护值设定*/
@@ -52,15 +54,13 @@
 
 /*IPM温度保护值设定*/
 
-#define TempProtValue                  (1050)                     // 扩大10倍(度1050
-#define TempRecoverValue               (700)                      // 扩大10倍(度)800
+#define TEMP_PROT_VALUE          IGBT_TEMP_VALUE(0.783) // (kΩ) 过温阈值 (≈95°C)
+#define TEMP_RECOVER_VALUE       IGBT_TEMP_VALUE(1.249) // (kΩ) 恢复阈值 (≈80°C)
 
 /*过流恢复*/
 #define CurrentRecoverEnable           (1)                        // 过流保护使能位, 0,不使能;1,使能
 #define OverCurrentRecoverTime         (20000)                     // (ms) 过流保护恢复时间
 
-#define IbusOffsetRecoverEnable        (1)                        //压机偏执恢复
-#define IbusOffsetRecoverTime          (20000)                      // (ms) 恢复时间
 
 /*启动恢复次数*/
 #define StartProtectRestartTimes        (200)