Browse Source

feat:功能添加

1.配置FG信号 每个机械周期12个 4对极 对应每个电周期3个
2.添加保护恢复
3.调整转速曲线
4.环路配置 调整系数 添加过功率限制
Comment.Vivre 1 year ago
parent
commit
5fb0d5c564

+ 2 - 2
README.md

@@ -9,8 +9,8 @@
 > Ubase = RV*Vref/1.732
 
 #### FOC__POW 功率寄存器
-> FOC__POW / 32767 * Ibase * Ubase * 1.5 = POWER
+> FOC__POW / 32767 * Ibase * Ubase * 1.5 / 2 = POWER
 > 实际会对FOC__POW进行放缩
 
 #### 功率转换计算系数
-> fact = 1/1.5*8*32767/Ubase/Ibase
+> fact = 1/1.5*8*32767/Ubase/Ibase*2

+ 21 - 4
User/Include/Customer.h

@@ -196,13 +196,13 @@
  * @param (UQ_POWER_CONTROL)			UQ环+功率补偿
  */
 #define MOTOR_CTRL_MODE                 (UQ_POWER_CONTROL)    // 闭环方式选择
-#define LOOP_TIME                       (2)                     // (ms) 外环环调节周期,默认为 1ms
+#define LOOP_TIME                       (1)                     // (ms) 外环环调节周期,默认为 1ms
 
 #define SKP                             _Q12(0.5)               // 外环KP     
 #define SKI                             _Q15(0.002)             // 外环KI
 #define SKD                             _Q12(0.0)               // 外环KD
 
-#define SOUTMAX                         I_Value(0.9)            // (A) 外环电流最大限幅值
+#define SOUTMAX                         I_Value(1.0)            // (A) 外环电流最大限幅值
 #define SOUTMIN                         I_Value(0.0)            // (A) 外环电流最小限幅值
 
 #define RAMP_INC                        S_Value(1)              // (RPS) 每秒爬坡递增量
@@ -222,8 +222,17 @@
 /**
  * @brief VSP调速开关机设置
  */
-#define VSP_OFF_MIN						704		// 0.15V		// 小于该电压关机
-#define VSP_ON_MIN						1173	// 0.25V		// 大于该电压开机
+#define VSP_OFF_MIN						UADC_Value(0.15) 		// (V)关机电压
+#define VSP_ON_MIN						UADC_Value(0.25)		// (V)开机电压
+
+#define GearMinVol						UADC_Value(0.50)		// (V) 调速下限电压
+#define GearA							UADC_Value(2.40)
+#define GearB							UADC_Value(3.20)
+#define GearC							UADC_Value(4.00)
+#define GearD							UADC_Value(4.50)
+#define GearE							UADC_Value(4.80)
+
+
 
 /**
  * @brief 定速模式速度给定
@@ -335,6 +344,14 @@
 #define Motor_F5SEG_Speed               S_Value(1200)           // 速度大于这个后切换成五段式
 #define Motor_F7SEG_Speed               S_Value(1000)           // 速度低于这个值,切换成七段式,主要是起动的时候
 
+/**
+ * FG输出使能
+ * @param (Disable)      禁止
+ * @param (Enable)       使能
+ */
+#define FG_Enable                       (Enable)                      ///<FG输出使能
+#define FG_K                            (3)                           ///<FG频率输出倍数
+
 /**
 ---------------------------------------------------------------------------------------------------------------------------------------------
     @brief 测试相关

+ 2 - 2
User/Include/Declaration.h

@@ -153,10 +153,10 @@ void Set_DBG_DMA(uint16 DMAAddr);
     @brief 定时器
 */
 #define TIM3_Fre                       (750000.0)                                // TIM0计数频率750KHz
-#define TIM4_Fre                       (6000.0)                                  // TIM0计数频率6KHz
+#define TIM4_Fre                       (12000000.0)                                  // TIM0计数频率6KHz
 void Timer2_Init(void);
 void Timer3_Init(void);
-
+void TIM4_Init(void);
 
 /* ------------------------------------------- 功能 ------------------------------------------------------------- */
 /**

+ 2 - 1
User/Include/Parameter.h

@@ -151,5 +151,6 @@
 #define E_BW                           (450.0)          ///< PLL算法里的反电动势滤波值
 
 #define UADC_Value(Xn0)					_Q15(Xn0/HW_ADC_REF)
-#define PROTECT_POWER(Xn0)				((float)Xn0*235.091)
+#define PROTECT_POWER(Xn0)				((float)Xn0*235.09)
+#define FG_Value                        (TIM4_Fre / BASE_FREQ / FG_K)
 #endif

+ 7 - 8
User/Include/Protect.h

@@ -37,22 +37,22 @@
 ---------------------------------------------------------------------------------------------------------------------------------------------
 */
 // 软件过流
-#define OC_RecoveryTimes				(0)
+#define OC_RecoveryTimes				(255)
 #define OC_RecoveryDelayTime			(3000)
 // 过欠压
-#define OV_RecoveryTimes				(0)
+#define OV_RecoveryTimes				(255)
 #define OV_RecoveryDelayTime			(1200)
 // 缺相
-#define LP_RecoveryTimes				(0)
+#define LP_RecoveryTimes				(255)
 #define LP_RecoveryDelayTime			(500)
 // 过温
-#define OT_RecoveryTimes				(0)
+#define OT_RecoveryTimes				(255)
 #define OT_RecoveryDelayTime			(500)
 // 堵转
-#define Stall_RecoveryTimes				(0)
+#define Stall_RecoveryTimes				(255)
 #define Stall_RecoveryDelayTime			(500)
 // 过功率
-#define OP_RecoveryTimes				(0)
+#define OP_RecoveryTimes				(255)
 #define OP_RecoveryDelayTime			(500)
 
 /**
@@ -95,14 +95,13 @@
 #define EsThresholdValueH				(1200.0)
 #define EsThresholdSpeed				S_Value(1000)			// (RPM) 电机转速
 
-
 // NTC过温保护
 #define TemperatureProtectTime          (1000)					// (ms) 温度保护检测时间
 #define OVER_Temperature                UADC_Value(1.0)			// (V) 过温保护阈值
 #define UNDER_Temperature               UADC_Value(1.5)			// (V) 过温保护恢复阈值
 
 // 限制功率
-#define POWERLPFLIMIT                   PROTECT_POWER(90.0)		// (W)
+#define POWERLPFLIMIT                   PROTECT_POWER(100.0)	// (W)
 // 功率保护
 #define OverPowerValue					PROTECT_POWER(120.0)	// (W) 功率保护阈值
 

+ 2 - 2
User/Include/SMDU.h

@@ -77,8 +77,8 @@ typedef enum
 
 
 /* SMDU控制 */
-extern int16  HW_One_PI(int16 Xn1);
-extern int16  HW_One_PI2(int16 Xn1);
+int16 HW_One_PI(int16 Xn1);
+int16 HW_TWO_PI(int16 Xn1);
 extern int16  HW_One_PI3(int16 Xn1);
 extern int16  LPFFunction(int16 Xn1, int16 Xn0, int8 K);
 extern int16  Atan_Us_MDU(int16 Xn1, int16 Xn0);

+ 2 - 1
User/Include/VaribleDef.h

@@ -380,7 +380,8 @@ typedef struct
     short			IqRef;				// Q轴给定电流
     short			IdRef;				// D轴给定电流
     short			IqSpeedRef;			// Q轴速度给定电流
-    unsigned short	LoopCalcValue;
+	unsigned short	LoopCalcInValue;
+	short			LoopCalcOutValue;
     
     short			ExtDec;
     short			LimitIqOut;			// 限制功率输出

+ 129 - 121
User/Source/AddFunction.c

@@ -212,22 +212,25 @@ void GetSrefKeyGear(void)
     //转速曲线计算
     if (isCtrlPowOn)
     {
+		// VSP测试代码
+		// mcFocCtrl.Ref = mcFocCtrl.SREFValue;
+		
         // 计算公式为
         // Ref = ( 区间UQ差 / 区间调速电压差) * (VSP - 调速下限) + 区间最小UQ值
-        if (mcFocCtrl.SREFValue <= 1408)
-        { mcFocCtrl.Ref = _Q15(0.120); }
-        else if (mcFocCtrl.SREFValue <= 11640 )
-        { mcFocCtrl.Ref = 0.864 * (mcFocCtrl.SREFValue -  1408) + _Q15(0.120); }
-        else if (mcFocCtrl.SREFValue <= 15530 )
-        { mcFocCtrl.Ref = 1.238 * (mcFocCtrl.SREFValue - 11640) + _Q15(0.390); }
-        else if (mcFocCtrl.SREFValue <= 19480 )
-        { mcFocCtrl.Ref = 1.543 * (mcFocCtrl.SREFValue - 15530) + _Q15(0.537); }
-        else if (mcFocCtrl.SREFValue <= 21962 )
-        { mcFocCtrl.Ref = 1.755 * (mcFocCtrl.SREFValue - 19480) + _Q15(0.723); }
-        else if (mcFocCtrl.SREFValue <= 23368 )
-        { mcFocCtrl.Ref = 2.004 * (mcFocCtrl.SREFValue - 21962) + _Q15(0.856); }
-        else
-        { mcFocCtrl.Ref = _Q15(0.942); }
+		if(mcFocCtrl.SREFValue <=GearMinVol)
+		{ mcFocCtrl.Ref = 3094; }
+		else if(mcFocCtrl.SREFValue <=GearA)
+		{ mcFocCtrl.Ref = 0.462 * (mcFocCtrl.SREFValue - GearMinVol) + 3094; }
+		else if(mcFocCtrl.SREFValue <=GearB)
+		{ mcFocCtrl.Ref = 0.734 * (mcFocCtrl.SREFValue - GearA) + 9454; }
+		else if(mcFocCtrl.SREFValue <=GearC)
+		{ mcFocCtrl.Ref = 0.949 * (mcFocCtrl.SREFValue - GearB) + 13303; }
+		else if(mcFocCtrl.SREFValue <=GearD)
+		{ mcFocCtrl.Ref = 0.795 * (mcFocCtrl.SREFValue - GearC) + 18280; }
+		else if(mcFocCtrl.SREFValue <=GearE)
+		{ mcFocCtrl.Ref = 1.011 * (mcFocCtrl.SREFValue - GearD) + 20887; }
+		else
+		{ mcFocCtrl.Ref = 22875; }
     }
     else
     { mcFocCtrl.Ref = 0; }
@@ -306,135 +309,140 @@ void GetTargetRef(void)
 }
 
 /**
-    @brief        外部闭环控制函数,示例代码提供 电流环,速度环,功率环,UQ控制示例代码,可根据需要自行修改
-                 建议使用默认1ms周期运行
-    @date         2022-07-14
+ * @function     LoopResponse
+ * @brief        闭环控制函数
+ * @param[in]    None
+ * @return       None
+ * @date         2024-08-28
 */
 void LoopResponse(void)
 {
     static int16 refRampOut = 0;
     
-
-        switch (mcFocCtrl.CtrlMode)
+    switch (mcFocCtrl.CtrlMode)
+    {
+        case 0:
         {
-            case 0:
+            if (mcFocCtrl.SpeedFlt > MOTOR_LOOP_RPM)
             {
-                if (mcFocCtrl.SpeedFlt > MOTOR_LOOP_RPM)
+                mcFocCtrl.Mode0HoldCnt++;
+                
+                if (mcFocCtrl.Mode0HoldCnt > 10)
                 {
-                    mcFocCtrl.Mode0HoldCnt++;
-                    
-                    if (mcFocCtrl.Mode0HoldCnt > 10)
-                    {
-                        FOC_QKP = QKP;
-                        FOC_QKI = QKI;
-                        FOC_DKP = DKP;
-                        FOC_DKI = DKI;
-                        // FOC_THECOMP = _Q15(-25.0 / 180.0); // SMO 估算补偿角
-                        // 启动电流环与外环给定衔接
-                        #if (MOTOR_CTRL_MODE == SPEED_LOOP_CONTROL)
-                        LoopRefRamp.Out = mcFocCtrl.SpeedFlt;
-                        #elif (MOTOR_CTRL_MODE == POWER_LOOP_CONTROL)
-                        LoopRefRamp.Out = mcFocCtrl.Power;
-                        #elif (MOTOR_CTRL_MODE == UQ_LOOP_CONTROL)
-                        LoopRefRamp.Out = mcFocCtrl.UqFlt;
-                        #elif (MOTOR_CTRL_MODE == UQ_POWER_CONTROL)
-                        LoopRefRamp.Out = mcFocCtrl.UqFlt;
-                        #endif
-                        mcFocCtrl.LoopTime = LOOP_TIME;
-                        LoopRefRamp.Inc = RAMP_INC;
-                        LoopRefRamp.Dec = RAMP_DEC;
-                        mcFocCtrl.IqRef = FOC_IQREF;
-                        FOC_IDREF = ID_RUN_CURRENT; // D轴启动电流
-                        PI1_UKH = mcFocCtrl.IqRef;
-                        PI2_Init(); // PI初始化
-                        // 弱磁
-                        #if (MotorFiledWeakenEn)
-                        FiledWeakenInit();
-                        #endif
-                        // 电压补偿
-                        VoltageComp.Undervoltage_flag = 0;
-                        VoltageComp.IncVoltage = _Q15(40.0 / HW_BOARD_VOLT_MAX);
-                        VoltageComp.LineAngel =  LinearCompensationAngel;
-                        VoltageComp.LineAngelMax =  LinearCompensationAngel_MAX;
-                        VoltageComp.LineAngelMin =  LinearCompensationAngel_MIN;
-                        VoltageComp.VCDelayCnt =  VoltageCompensationDelayCnt;
-                        mcFocCtrl.CtrlMode = 1;
-                    }
+                    FOC_QKP = QKP;
+                    FOC_QKI = QKI;
+                    FOC_DKP = DKP;
+                    FOC_DKI = DKI;
+                    // FOC_THECOMP = _Q15(-25.0 / 180.0); // SMO 估算补偿角
+                    // 启动电流环与外环给定衔接
+                    #if (MOTOR_CTRL_MODE == SPEED_LOOP_CONTROL)
+                    LoopRefRamp.Out = mcFocCtrl.SpeedFlt;
+                    #elif (MOTOR_CTRL_MODE == POWER_LOOP_CONTROL)
+                    LoopRefRamp.Out = mcFocCtrl.Power;
+                    #elif (MOTOR_CTRL_MODE == UQ_LOOP_CONTROL)
+                    LoopRefRamp.Out = mcFocCtrl.UqFlt;
+                    #elif (MOTOR_CTRL_MODE == UQ_POWER_CONTROL)
+                    LoopRefRamp.Out = mcFocCtrl.UqFlt;
+                    #endif
+                    mcFocCtrl.LoopTime = LOOP_TIME;
+                    LoopRefRamp.Inc = RAMP_INC;
+                    LoopRefRamp.Dec = RAMP_DEC;
+                    mcFocCtrl.IqRef = FOC_IQREF;
+                    FOC_IDREF = ID_RUN_CURRENT; // D轴启动电流
+                    PI1_UKH = mcFocCtrl.IqRef;
+                    PI2_Init(); // PI初始化
+                    // 弱磁
+                    #if (MotorFiledWeakenEn)
+                    FiledWeakenInit();
+                    #endif
+                    // 电压补偿
+                    VoltageComp.Undervoltage_flag = 0;
+                    VoltageComp.IncVoltage = _Q15(40.0 / HW_BOARD_VOLT_MAX);
+                    VoltageComp.LineAngel =  LinearCompensationAngel;
+                    VoltageComp.LineAngelMax =  LinearCompensationAngel_MAX;
+                    VoltageComp.LineAngelMin =  LinearCompensationAngel_MIN;
+                    VoltageComp.VCDelayCnt =  VoltageCompensationDelayCnt;
+                    mcFocCtrl.CtrlMode = 1;
                 }
-                else
-                { mcFocCtrl.Mode0HoldCnt = 0; }
-                
-                break;
             }
+            else
+            { mcFocCtrl.Mode0HoldCnt = 0; }
+            
+            break;
+        }
+        
+        case 1:
+        {
+            mcFocCtrl.LoopTime++;
             
-            case 1:
+            if (mcFocCtrl.LoopTime >= LOOP_TIME)
             {
-                mcFocCtrl.LoopTime++;
-                
-                if (mcFocCtrl.LoopTime >= LOOP_TIME)
+                mcFocCtrl.LoopTime = 0;
+                refRampOut = LoopRamp(mcFocCtrl.Ref); // 控制命令爬坡函数,用于实现调速信号之间平滑过渡
+                #if (MOTOR_CTRL_MODE == CURRENT_LOOP_CONTROL)
                 {
-                    mcFocCtrl.LoopTime = 0;
-                    refRampOut = LoopRamp(mcFocCtrl.Ref); // 控制命令爬坡函数,用于实现调速信号之间平滑过渡
-                    #if (MOTOR_CTRL_MODE == CURRENT_LOOP_CONTROL)
-                    {
-                        mcFocCtrl.IqRef = refRampOut;
-                        FOC_IQREF = mcFocCtrl.IqRef;
-                    }
-                    #elif (MOTOR_CTRL_MODE == SPEED_LOOP_CONTROL)
-                    {
-                        mcFocCtrl.IqSpeedRef = HW_One_PI(refRampOut - mcFocCtrl.SpeedFlt);
-                        mcFocCtrl.LimitIqOut = HW_One_PI2(mcFocCtrl.PowerLimitValue - mcFocCtrl.Power); // 限制功率
-                    
-                        if ((mcFocCtrl.LimitIqOut < mcFocCtrl.IqSpeedRef)) // 限制输出电流
-                        {
-                            mcFocCtrl.ExtDec = (mcFocCtrl.IqRef - mcFocCtrl.LimitIqOut) / 3;
-                            mcFocCtrl.IqRef -= mcFocCtrl.ExtDec;
-                        }
-                        else
-                        { mcFocCtrl.IqRef = mcFocCtrl.IqSpeedRef; }
-                    
-                        #if (MotorFiledWeakenEn)
-                        FileWeakenControl();
-                        #else
-                        FOC_IQREF = mcFocCtrl.IqRef;
-                        #endif
-                    }
-                    #elif (MOTOR_CTRL_MODE == POWER_LOOP_CONTROL)
-                    {
-                        mcFocCtrl.IqRef = HW_One_PI(refRampOut - mcFocCtrl.Power);
-                        FOC_IQREF = mcFocCtrl.IqRef;
-                    }
-                    #elif (MOTOR_CTRL_MODE == UQ_LOOP_CONTROL)
-                    {
-                        mcFocCtrl.IqRef = HW_One_PI(refRampOut - mcFocCtrl.UqFlt);
-                        FOC_IQREF = mcFocCtrl.IqRef;
-                    }
-                    #elif (MOTOR_CTRL_MODE == UQ_POWER_CONTROL)
+                    mcFocCtrl.IqRef = refRampOut;
+                    FOC_IQREF = mcFocCtrl.IqRef;
+                }
+                #elif (MOTOR_CTRL_MODE == SPEED_LOOP_CONTROL)
+                {
+                    mcFocCtrl.IqSpeedRef = HW_One_PI(refRampOut - mcFocCtrl.SpeedFlt);
+                    mcFocCtrl.LimitIqOut = HW_One_PI2(mcFocCtrl.PowerLimitValue - mcFocCtrl.Power); // 限制功率
+                
+                    if ((mcFocCtrl.LimitIqOut < mcFocCtrl.IqSpeedRef)) // 限制输出电流
                     {
-                        // 反馈计算
-                        mcFocCtrl.LoopCalcValue = mcFocCtrl.UqFlt * 0.65 + mcFocCtrl.Power * 0.62;
-                    
-                        // 限幅
-                        if (mcFocCtrl.LoopCalcValue > 32440)
-                        { mcFocCtrl.LoopCalcValue = 32440;}
-                    
-                        mcFocCtrl.IqRef = HW_One_PI(refRampOut - mcFocCtrl.LoopCalcValue);
-                        FOC_IQREF = mcFocCtrl.IqRef;
+                        mcFocCtrl.ExtDec = (mcFocCtrl.IqRef - mcFocCtrl.LimitIqOut) / 3;
+                        mcFocCtrl.IqRef -= mcFocCtrl.ExtDec;
                     }
+                    else
+                    { mcFocCtrl.IqRef = mcFocCtrl.IqSpeedRef; }
+                
+                    #if (MotorFiledWeakenEn)
+                    FileWeakenControl();
+                    #else
+                    FOC_IQREF = mcFocCtrl.IqRef;
                     #endif
                 }
-                
-                #if (SVPWM_5_Segment_Run_Enale == 1) // 开启五段式
+                #elif (MOTOR_CTRL_MODE == POWER_LOOP_CONTROL)
+                {
+                    mcFocCtrl.IqRef = HW_One_PI(refRampOut - mcFocCtrl.Power);
+                    FOC_IQREF = mcFocCtrl.IqRef;
+                }
+                #elif (MOTOR_CTRL_MODE == UQ_LOOP_CONTROL)
                 {
-                    if (mcFocCtrl.SpeedFlt > Motor_F5SEG_Speed)
-                    { SetBit(FOC_CR2, F5SEG); }
-                    else if (mcFocCtrl.SpeedFlt < Motor_F7SEG_Speed)
-                    { ClrBit(FOC_CR2, F5SEG); }
+                    mcFocCtrl.IqRef = HW_One_PI(refRampOut - mcFocCtrl.UqFlt);
+                    FOC_IQREF = mcFocCtrl.IqRef;
+                }
+                #elif (MOTOR_CTRL_MODE == UQ_POWER_CONTROL)
+                {
+					mcFocCtrl.LoopCalcInValue = mcFocCtrl.UqFlt*0.45 + mcFocCtrl.Power*0.65;
+                    mcFocCtrl.LoopCalcOutValue = HW_One_PI(refRampOut - mcFocCtrl.LoopCalcInValue);
+					mcFocCtrl.LimitIqOut = HW_TWO_PI(mcFocCtrl.PowerLimitValue - mcFocCtrl.Power);
+					// 限制输出电流
+                    if ((mcFocCtrl.LimitIqOut < mcFocCtrl.LoopCalcOutValue)) 
+                    {
+                        mcFocCtrl.ExtDec = (mcFocCtrl.IqRef - mcFocCtrl.LimitIqOut) / 3;
+                        mcFocCtrl.IqRef -= mcFocCtrl.ExtDec;
+                    }
+                    else
+                    { mcFocCtrl.IqRef = mcFocCtrl.LoopCalcOutValue; }
+					
+                    FOC_IQREF = mcFocCtrl.IqRef;
                 }
                 #endif
-                break;
             }
+            
+            #if (SVPWM_5_Segment_Run_Enale == 1) // 开启五段式
+            {
+                if (mcFocCtrl.SpeedFlt > Motor_F5SEG_Speed)
+                { SetBit(FOC_CR2, F5SEG); }
+                else if (mcFocCtrl.SpeedFlt < Motor_F7SEG_Speed)
+                { ClrBit(FOC_CR2, F5SEG); }
+            }
+            #endif
+            break;
         }
+    }
 }
 
 /**

+ 1 - 0
User/Source/DRIVER.c

@@ -67,3 +67,4 @@ void Driver_Init(void)
     ClrBit(DRV_CR , DRPE);	// 计数器比较值预装载使能 0-->Disable		        1-->Enable
     SetBit(DRV_CR , DRVOE);	// Driver输出使能0-->Disable		1-->Enable
 }
+

+ 1 - 1
User/Source/MotorControl.c

@@ -262,7 +262,7 @@ void TickCycle(void)
         mcFocCtrl.Is = Sqrt_alpbet(FOC__IA, FOC__IBET);
         
         if (mcState == mcRun)
-        { mcFocCtrl.Power = LPFFunction(FOC__POW << 3, mcFocCtrl.Power, 10); }
+        { mcFocCtrl.Power = LPFFunction(FOC__POW << 2, mcFocCtrl.Power, 50); }
         
         mcFocCtrl.UqFlt = LPFFunction(FOC__UQ, mcFocCtrl.UqFlt, 50);
         mcFocCtrl.UdFlt = LPFFunction(FOC__UD, mcFocCtrl.UdFlt, 50);

+ 3 - 0
User/Source/MotorControlFunction.c

@@ -35,6 +35,9 @@ void FOC_Init(void)
     FOC_RTHECNT = 0;                 // 清零 爬坡次数
     FOC_THECOMP = _Q15(-15.0 / 180.0); // SMO 估算补偿角
     FOC_THECOR = 0x04;               // 误差角度补偿
+	#if (FG_Enable)
+    FOC_KFG = FG_Value;
+    #endif
     /* 电流环参数配置 */
     FOC_DMAX = DOUTMAX;
     FOC_DMIN = DOUTMIN;

+ 26 - 30
User/Source/PIInit.c

@@ -1,14 +1,10 @@
-/* --------------------------- (C) COPYRIGHT 2021 Fortiortech ShenZhen -----------------------------
-    File Name      : PIInit.c
-    Author         : Fortiortech  Appliction Team
-    Version        : V1.0
-	Editor         : Leo.li
-    Date           : 2021-04-11
-    Description    : This file contains PI initial function used for Motor Control.
-----------------------------------------------------------------------------------------------------  
-                                       All Rights Reserved
-------------------------------------------------------------------------------------------------- */
-/* Includes -------------------------------------------------------------------------------------*/
+/**
+ * @copyright None
+ * @file      PIInit.c
+ * @author    Comment Vivre
+ * @date      2024-08-28
+ * @brief     None
+ */
 #include <MyProject.h>
 
 /* -------------------------------------------------------------------------------------------------
@@ -19,14 +15,14 @@
 ------------------------------------------------------------------------------------------------- */
 void PI_Init(void)
 {
-    PI1_KP          = SKP;
-    PI1_KI          = SKI;
-    PI1_EK1         =0;
-    PI1_EK          =0;
-    PI1_UKH         =0;
-    PI1_UKL         =0;
-    PI1_UKMAX       = SOUTMAX;
-    PI1_UKMIN       = SOUTMIN;
+    PI1_KP = SKP;
+    PI1_KI = SKI;
+    PI1_EK1 = 0;
+    PI1_EK = 0;
+    PI1_UKH = 0;
+    PI1_UKL = 0;
+    PI1_UKMAX = SOUTMAX;
+    PI1_UKMIN = SOUTMIN;
 }
 /**
     @brief        PI2用于母线限流
@@ -36,9 +32,9 @@ void PI2_Init(void)
 {
     PI2_KP = _Q12(0.1);                //限制功率调节
     PI2_KI = _Q15(0.008);
-	  PI2_UKMAX = SOUTMAX;
-    PI2_UKMIN = SOUTMAX >>2;
-	  PI2_UKH   = PI2_UKMAX;            //输出从最大开始调节
+    PI2_UKMAX = SOUTMAX;
+    PI2_UKMIN = SOUTMAX >> 2;
+    PI2_UKH   = PI2_UKMAX;            //输出从最大开始调节
 }
 /**
     @brief        PI3用于弱磁增速
@@ -46,13 +42,13 @@ void PI2_Init(void)
 */
 void PI3_Init(void)
 {
-//    PI3_KP          = AKP;
-//    PI3_EK1         = 0;
-//    PI3_EK          = 0;
-//    PI3_KI          = AKI;
-//    PI3_UKH         = 0;
-//    PI3_UKL         = 0;
-//    PI3_UKMAX       = A_Value(AMAX);
-//    PI3_UKMIN       = A_Value(AMIN);
+    //    PI3_KP          = AKP;
+    //    PI3_EK1         = 0;
+    //    PI3_EK          = 0;
+    //    PI3_KI          = AKI;
+    //    PI3_UKH         = 0;
+    //    PI3_UKL         = 0;
+    //    PI3_UKMAX       = A_Value(AMAX);
+    //    PI3_UKMIN       = A_Value(AMIN);
 }
 

+ 2 - 2
User/Source/SMDU.c

@@ -29,12 +29,12 @@ int16 HW_One_PI(int16 Xn1)
     return PI1_UKH;
 }
 /*  -------------------------------------------------------------------------------------------------
-    Function Name : int16 HW_One_PI2(int16 Xn1, int16 Yn0, int16 Xn2)
+    Function Name : int16 HW_TWO_PI(int16 Xn1, int16 Yn0, int16 Xn2)
     Description   : PI控制
     Input         : Xn1--E(K)
     Output        : PI_UK--当前PI输出值,执行时间us
     -------------------------------------------------------------------------------------------------*/
-int16 HW_One_PI2(int16 Xn1)
+int16 HW_TWO_PI(int16 Xn1)
 {
     PI2_EK =  Xn1;                                                                               //填入EK
     SMDU_RunBlock(2, PI);

+ 59 - 0
User/Source/TIMER.c

@@ -63,3 +63,62 @@ void Timer3_Init(void)
     ClrBit(TIM3_CR0, T3MOD); // 0-->Timer模式       1-->输出模式
     SetBit(TIM3_CR1, T3EN);  // TIM3使能    0-->Disable  1-->Enable
 }
+/**
+ * @function     TIM4_Init
+ * @brief        TIM4初始化配置 用于FG
+ * @param[in]    None
+ * @return       None
+ * @date         2024-01-20
+*/
+void TIM4_Init(void)
+{
+    // 先停止计数,配置完寄存器后,最后启动计数
+    ClrBit(TIM4_CR1, T4EN);            // 0 - 停止计数;1 - 使能计数
+    SetBit(PH_SEL, T4SEL);              // 1 - P0.1作为Time4的输入输出
+	// 配置P12为定时器输入输出
+	SetReg(PH_SEL1,T4CT1|T4CT0,T4CT1);
+    // 时钟分频设置(T4PSC)
+    // 000:cpuclk(24MHz)       001:cpuclk/2^1(12MHz)   010:cpuclk/2^2(6MHz)    011:cpuclk/2^3(3MHz)
+    // 100:cpuclk/2^4(1.5MHz)  101:cpuclk/2^5(750KHz)  110:cpuclk/2^6(375KHz)  111:cpuclk/2^7(187.5KHz)
+    SetReg(TIM4_CR0, T4PSC0 | T4PSC1 | T4PSC2,  T4PSC0);
+    // 输入输出模式选择
+    // T4MOD = 0 输入Timer模式
+    // T4MOD = 1 输出模式
+    SetBit(TIM4_CR0, T4MOD);
+    // ClrBit(TIM4_CR0, T4MOD);
+    // 输出模式:比较模式选择
+    // T4OCM = 0      TIM4__CNTR <= TIM4__DR 输出 “0”  TIM4__CNTR > TIM4__DR 输出 “1”
+    // T4OCM = 1      TIM4__CNTR <= TIM4__DR 输出 “1”  TIM4__CNTR > TIM4__DR 输出 “0”
+    // 输入Timer模式   :周期沿选择
+    // T4OCM = 0      相邻两个上升沿为1个周期,上升沿到下降沿为高电平脉宽
+    // T4OCM = 1      相邻两个下降沿为1个周期,下降沿到上升沿为低电平脉宽
+    // SetBit(TIM4_CR0, T4OCM);  //输出比较器匹配模式,TIM3_CNTR≤TIM3_DR: 0;
+    ClrBit(TIM4_CR0, T4OCM);
+    // 滤波时间
+    // 00 - 不滤波;       01 - 8个时钟周期
+    // 10 - 16个时钟周期; 11 - 32个时钟周期
+    SetReg(TIM4_CR1, T4NM1 | T4NM0, T4NM1 | T4NM0);
+    // 清除中断标志位
+    // 禁止PWM周期检测中断使能
+    // 使能计数器上溢中断使能
+    ClrBit(TIM4_CR1, T4IR | T4IP | T4IF);         // 清除中断标志位
+    ClrBit(TIM4_CR1, T4IFE | T4IPE);              // 输入有效边沿变化中断使能和基本计数器上溢使能
+    ClrBit(TIM4_CR0, T4IRE);                      // 輸出模式:比較匹配中斷使能  輸入模式:脈寬檢測中斷使能
+    // 单次模式配置
+    // 输出模式:计数器上溢事件
+    // 输入模式:PWM周期检测或计数器上溢事件
+    // T4OPM = 0       发生上述事件时,计数器不停止
+    // T4OPM = 1       发生上述事件时,计数器停止(清除T4EN)
+    ClrBit(TIM4_CR0, T4OPM);
+    // 定时器4中断优先级配置及芯片中断总使能
+    // 中断优先级控制值从0-3依次表示优先级从最低到最高,共4级优化级控制
+	ClrBit(IP2, PTIM41);
+	ClrBit(IP2, PTIM40);
+	
+    // 配置周期值、比较值、计数值
+    TIM4__DR   = 12000;                          // 输入模式,DR和ARR的值由硬件写;
+    TIM4__ARR  = 24000.;
+    TIM4__CNTR = 0;
+    // 启动计数
+    SetBit(TIM4_CR1, T4EN);                       // 使能计数器,启动计数
+}

+ 4 - 0
User/Source/main.c

@@ -89,6 +89,10 @@ static void HardwareInit(void)
     GPIO_Init();
     ADC_Init(); // 初始化需要在Driver之前
     Driver_Init();
+	#if (FG_Enable)
+    // 端口GP12
+    TIM4_Init();
+    #endif
     AMP_Init();
     // 温度配置初始化
     TSD_Init();