ソースを参照

feat:调整控制量递增函数

1.优化控制量递增函数
2.结构体定义对齐处理
3.优化部分代码
Comment.Vivre 1 年間 前
コミット
c89adb77e8

+ 5 - 2
User/Include/Declaration.h

@@ -19,6 +19,11 @@ void FiniteStateManger(void);
 */
 void TickCycle(void);
 
+/**
+    @brief 目标参数爬坡函数
+*/
+int16 LoopRamp(int16 Xn0);
+
 /**
     @brief 环路响应
 */
@@ -44,8 +49,6 @@ void Motor_TailWind(void);
 void McTailWindDealwith(void);
 void TailWindDealwith(void);
 
-
-int16 Motor_Ramp(int16 ref);
 void VSPSample(void);
 void ONOFF_Starttest(void);
 void ATORamp(void);

+ 1 - 1
User/Include/Protect.h

@@ -53,7 +53,7 @@
 #define SW_OC_CurrentVal                    I_Value(1.2)                            ///< (A)软件过流值
 #define SW_OC_DectTime                      (10)                                    ///< (ms)软件过流检测时间
 
-/* -----偏置电压保护----- */
+// 偏置电压保护
 #define GetCurrentOffsetValue              _Q14(0.05)                              // (单位:100%)偏置电压保护误差范围,超过该范围保护   
 
 /* -----功率保护----- */

+ 46 - 50
User/Include/VaribleDef.h

@@ -194,8 +194,8 @@ typedef struct
     s32tos16 mcDcbusFlt2Sum;
     s32tos16 UQVALUESum;
     s32tos16 UDVALUESum;
-    int16	 UQVALUEAVERAGE;
-    int16	 UDVALUEAVERAGE;
+    int16    UQVALUEAVERAGE;
+    int16    UDVALUEAVERAGE;
     uint16 mcDcbusFlt2cnt;
     uint16 cpscnt;
     uint16 Uqcps;
@@ -350,19 +350,17 @@ typedef struct
 typedef struct
 {
     uint8  CheckCount;                 //
-    uint8 TSD_Enable_Flag;             //    温度检测使能标志位	
-	uint8 TSD_Trigger_Flag;            //    温度检测触发标志位
-
-    uint8 ActualTemper;               // 
-    uint16 Trigger_Value;              // 
+    uint8 TSD_Enable_Flag;             //    温度检测使能标志位
+    uint8 TSD_Trigger_Flag;            //    温度检测触发标志位
+    
+    uint8 ActualTemper;               //
+    uint16 Trigger_Value;              //
     uint16 TSD_Dec_Time;               //    温度检测时间计数
-	
-}MCUTemperature;
+    
+} MCUTemperature;
 
 extern MCUTemperature xdata TSDTemperature;
 
-
-extern uint8                        xdata   mcPOSTErrSource;
 extern FaultVarible                 xdata   fault;
 extern FaultCurrentVarible          idata   mcCurVarible;
 extern FaultRecoverTypedef          xdata   Restart;
@@ -370,43 +368,41 @@ extern FaultRecoverTypedef          xdata   Restart;
 
 typedef struct
 {
-    uint8 CtrlMode;        // 控制模式
-    
+    unsigned char	CtrlMode;			// 控制模式
+    unsigned char	ChargeStep;			// 预充电的步骤
+    unsigned char	Start_Mode;			// 启动方式
+	
+    unsigned char	FR_SET;
+    unsigned char	FR;
+    unsigned char	Flg_ATORampEnd;
     
-    uint8  ChargeStep;      // 预充电的步骤
-    uint8  Start_Mode;      // 启动方式
+    short			Ref;				// 控制目标给定
+    short			IqRef;				// Q轴给定电流
+    short			IdRef;				// D轴给定电流
+    short			IqSpeedRef;			// Q轴速度给定电流
+    unsigned short	LoopCalcValue;
     
-    uint8  FR_SET;
-    uint8  FR;
-    uint8  Flg_ATORampEnd;
-	
-    int16  Ref;         // 控制目标给定
-    int16  IqRef;       // Q轴给定电流
-    int16  IdRef;       // D轴给定电流
-    int16  IqSpeedRef;   // Q轴速度给定电流
-    unsigned short LoopCalcValue;
-	
-    int16 ExtDec;
-    int16 LimitIqOut; // 限制功率输出
-    uint16 PowerLimitValue; // 功率限制的值
+    short			ExtDec;
+    short			LimitIqOut;			// 限制功率输出
+    unsigned short	PowerLimitValue;	// 功率限制的值
     
-    uint8  Mode0HoldCnt;    // MODE0切MODE1延迟时间计数
-    uint16  EMFsquare;      // 当前FOC_ESQU滤波后的值
-    uint16 RunStateCnt;     // 运行状态计数
-    uint8 LoopTime;         // 外环控制周期计数
-    uint16 State_Count;     // 电机各个状态的时间计数
-    int16  mcPosCheckAngle;	// 位置检测的角度
-    uint8  MCU_TEMP;        //MCU超过70°后,读取当前温度值
-    int16  Angle;            //弱磁角度
+    unsigned char	Mode0HoldCnt;		// MODE0切MODE1延迟时间计数
+    unsigned short	EMFsquare;			// 当前FOC_ESQU滤波后的值
+    unsigned short	RunStateCnt;		// 运行状态计数
+    unsigned char	LoopTime;			// 外环控制周期计数
+    unsigned short	State_Count;		// 电机各个状态的时间计数
+    short			mcPosCheckAngle;	// 位置检测的角度
+    short			Angle;				// 弱磁角度
     
-    uint16 mcDcbusFlt;      // 母线电压
-    int16 Power;            // 当前功率
-    int16 SpeedFlt;         // 当前速度滤波后的值
-    int16 UqFlt;            // Q轴电压
-    int16 UdFlt;			// D轴电压
-    int16 NTCValue;
-	int16 Is;				// 母线电流
-	int16 SREFValue;
+    unsigned short	mcDcbusFlt;			// 母线电压
+    short			Power;				// 当前功率
+    short			SpeedFlt;			// 当前速度滤波后的值
+    short			UqFlt;				// Q轴电压
+    short			UdFlt;				// D轴电压
+    short			NTCValue;
+    unsigned char	MCU_TEMP;			// MCU超过70°后,读取当前温度值
+    short			Is;					// 母线电流
+    short			SREFValue;
 } FOCCTRL;
 extern FOCCTRL xdata mcFocCtrl;
 
@@ -415,12 +411,12 @@ extern FOCCTRL xdata mcFocCtrl;
 */
 typedef struct
 {
-    int16  RefValue;
-    float  OutValue_float;
-    float  IncValue;
-    float  DecValue;
-} MCRAMP;
-extern MCRAMP data mcRefRamp;
+    int16 In;
+    float Out;
+    float Inc;
+    float Dec;
+} RefRamp;
+extern RefRamp data LoopRefRamp;
 
 /**
     @brief 脉宽计算定义

+ 44 - 67
User/Source/AddFunction.c

@@ -1,13 +1,10 @@
 /**
-    @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
-    @file      AddFunction.c
-    @author    Fortiortech  Appliction Team
-    @since     Create:2022-07-13
-    @date      Last modify:2022-07-14
-    @note      Last modify author is Marcel He
-    @brief     This file contains main function used for Motor Control.
-*/
-
+ * @copyright None
+ * @file      AddFunction.c
+ * @author    Comment Vivre
+ * @date      2024-08-28
+ * @brief     None
+ */
 #include <MyProject.h>
 
 /* Public variables --------------------------------------------------------- */
@@ -15,9 +12,11 @@
 bit isCtrlPowOn = false;           ///< 开关机控制
 PWMINPUTCAL xdata mcPwmInput;            ///< PWM捕获结构体变量
 FOCCTRL xdata mcFocCtrl;                 ///< FOC电机控制相关结构体变量
-MCRAMP data mcRefRamp;                   ///< 控制指令爬坡结构体相关变量
 debugONOFFTypeDef xdata debug_ONOFFTest; ///< ONOFF启停测试小工具结构体变量
 
+
+RefRamp data LoopRefRamp;                   ///< 控制指令爬坡结构体相关变量
+
 /**
     @brief        对变量取16位的绝对值
     @param[in]    value
@@ -315,8 +314,7 @@ void LoopResponse(void)
 {
     static int16 refRampOut = 0;
     
-    if ((mcState == mcRun) || (mcState == mcStop))
-    {
+
         switch (mcFocCtrl.CtrlMode)
         {
             case 0:
@@ -334,17 +332,17 @@ void LoopResponse(void)
                         // FOC_THECOMP = _Q15(-25.0 / 180.0); // SMO 估算补偿角
                         // 启动电流环与外环给定衔接
                         #if (MOTOR_CTRL_MODE == SPEED_LOOP_CONTROL)
-                        mcRefRamp.OutValue_float = mcFocCtrl.SpeedFlt;
+                        LoopRefRamp.Out = mcFocCtrl.SpeedFlt;
                         #elif (MOTOR_CTRL_MODE == POWER_LOOP_CONTROL)
-                        mcRefRamp.OutValue_float = mcFocCtrl.Power;
+                        LoopRefRamp.Out = mcFocCtrl.Power;
                         #elif (MOTOR_CTRL_MODE == UQ_LOOP_CONTROL)
-                        mcRefRamp.OutValue_float = mcFocCtrl.UqFlt;
+                        LoopRefRamp.Out = mcFocCtrl.UqFlt;
                         #elif (MOTOR_CTRL_MODE == UQ_POWER_CONTROL)
-                        mcRefRamp.OutValue_float = mcFocCtrl.UqFlt;
+                        LoopRefRamp.Out = mcFocCtrl.UqFlt;
                         #endif
                         mcFocCtrl.LoopTime = LOOP_TIME;
-                        mcRefRamp.IncValue = RAMP_INC;
-                        mcRefRamp.DecValue = RAMP_DEC;
+                        LoopRefRamp.Inc = RAMP_INC;
+                        LoopRefRamp.Dec = RAMP_DEC;
                         mcFocCtrl.IqRef = FOC_IQREF;
                         FOC_IDREF = ID_RUN_CURRENT; // D轴启动电流
                         PI1_UKH = mcFocCtrl.IqRef;
@@ -376,7 +374,7 @@ void LoopResponse(void)
                 if (mcFocCtrl.LoopTime >= LOOP_TIME)
                 {
                     mcFocCtrl.LoopTime = 0;
-                    refRampOut = Motor_Ramp(mcFocCtrl.Ref); // 控制命令爬坡函数,用于实现调速信号之间平滑过渡
+                    refRampOut = LoopRamp(mcFocCtrl.Ref); // 控制命令爬坡函数,用于实现调速信号之间平滑过渡
                     #if (MOTOR_CTRL_MODE == CURRENT_LOOP_CONTROL)
                     {
                         mcFocCtrl.IqRef = refRampOut;
@@ -437,7 +435,6 @@ void LoopResponse(void)
                 break;
             }
         }
-    }
 }
 
 /**
@@ -448,34 +445,17 @@ void LoopResponse(void)
     @return       爬坡结果(int16)
     @date         2022-07-14
 */
-int16 Motor_Ramp(int16 ref)
+int16 LoopRamp(int16 Xn0)
 {
-    mcRefRamp.RefValue = ref; // 爬坡函数输入
+    LoopRefRamp.In = Xn0; // 爬坡函数输入
     
-    if (mcRefRamp.OutValue_float < mcRefRamp.RefValue)
-    {
-        if (mcRefRamp.OutValue_float + mcRefRamp.IncValue < mcRefRamp.RefValue)
-        {
-            mcRefRamp.OutValue_float += mcRefRamp.IncValue;
-        }
-        else
-        {
-            mcRefRamp.OutValue_float = mcRefRamp.RefValue;
-        }
-    }
-    else
-    {
-        if (mcRefRamp.OutValue_float - mcRefRamp.DecValue > mcRefRamp.RefValue)
-        {
-            mcRefRamp.OutValue_float -= mcRefRamp.DecValue;
-        }
-        else
-        {
-            mcRefRamp.OutValue_float = mcRefRamp.RefValue;
-        }
-    }
+    // 数据爬坡处理
+    if ((LoopRefRamp.Out + LoopRefRamp.Inc) < LoopRefRamp.In)
+    { LoopRefRamp.Out += LoopRefRamp.Inc; }
+    else if ((LoopRefRamp.Out - LoopRefRamp.Dec) > LoopRefRamp.In)
+    { LoopRefRamp.Out -= LoopRefRamp.Dec; }
     
-    return (int16)mcRefRamp.OutValue_float; // 输出浮点数取整
+    return (int16)LoopRefRamp.Out;
 }
 
 /**
@@ -484,29 +464,26 @@ int16 Motor_Ramp(int16 ref)
 */
 void ATORamp(void)
 {
-    if (mcState == mcRun)
+    if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 2))
     {
-        if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 2))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN1; // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN1; // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count == ((ATO_RAMP_PERIOD << 1) + ATO_RAMP_PERIOD))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN2; // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN2; // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 1))
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN3; // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN3; // 估算器里的PI的KI
-        }
-        else if (mcFocCtrl.State_Count <= ATO_RAMP_PERIOD && mcFocCtrl.Flg_ATORampEnd == 0)
-        {
-            FOC_EKP = OBSW_KP_GAIN_RUN4;  // 估算器里的PI的KP
-            FOC_EKI = OBSW_KI_GAIN_RUN4;  // 估算器里的PI的KI
-            mcFocCtrl.Flg_ATORampEnd = 1; // ATO 爬坡结束
-        }
+        FOC_EKP = OBSW_KP_GAIN_RUN1; // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN1; // 估算器里的PI的KI
+    }
+    else if (mcFocCtrl.State_Count == ((ATO_RAMP_PERIOD << 1) + ATO_RAMP_PERIOD))
+    {
+        FOC_EKP = OBSW_KP_GAIN_RUN2; // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN2; // 估算器里的PI的KI
+    }
+    else if (mcFocCtrl.State_Count == (ATO_RAMP_PERIOD << 1))
+    {
+        FOC_EKP = OBSW_KP_GAIN_RUN3; // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN3; // 估算器里的PI的KI
+    }
+    else if (mcFocCtrl.State_Count <= ATO_RAMP_PERIOD && mcFocCtrl.Flg_ATORampEnd == 0)
+    {
+        FOC_EKP = OBSW_KP_GAIN_RUN4;  // 估算器里的PI的KP
+        FOC_EKI = OBSW_KI_GAIN_RUN4;  // 估算器里的PI的KI
+        mcFocCtrl.Flg_ATORampEnd = 1; // ATO 爬坡结束
     }
 }
 

+ 14 - 4
User/Source/MotorControl.c

@@ -263,11 +263,18 @@ void TickCycle(void)
         
         if (mcState == mcRun)
         { mcFocCtrl.Power = LPFFunction(FOC__POW << 3, mcFocCtrl.Power, 10); }
+        
+        mcFocCtrl.UqFlt = LPFFunction(FOC__UQ, mcFocCtrl.UqFlt, 50);
+        mcFocCtrl.UdFlt = LPFFunction(FOC__UD, mcFocCtrl.UdFlt, 50);
     }
     else
     {
         mcFocCtrl.SpeedFlt = 0;
+        mcFocCtrl.EMFsquare = 0;
+        mcFocCtrl.Is = 0;
+        mcFocCtrl.UqFlt = 0;
         mcFocCtrl.Power = 0;
+        mcFocCtrl.UdFlt = 0;
     }
     
     // DCbus的采样获取值并滤波
@@ -295,16 +302,19 @@ void TickCycle(void)
     #endif
     mcFocCtrl.NTCValue = LPFFunction(ADC8_DR, mcFocCtrl.NTCValue, 100);
     mcFocCtrl.SREFValue = LPFFunction(ADC9_DR, mcFocCtrl.SREFValue, 100);
-    mcFocCtrl.UqFlt = LPFFunction(FOC__UQ, mcFocCtrl.UqFlt, 50);
-    mcFocCtrl.UdFlt = LPFFunction(FOC__UD, mcFocCtrl.UdFlt, 50);
     // 获取调速信号,不同调速模式(PWMMODE,NONEMODE,SREFMODE)的目标值修改
     GetTargetRef();
+    
     // 启动ATO控制,环路响应,如速度环、转矩环、功率环等
-    LoopResponse();
+    if ((mcState == mcRun) || (mcState == mcStop))
+    { LoopResponse(); }
+    
     //故障保护函数功能,如过欠压保护、启动保护、缺相、堵转等
     Fault_Detection();
+    
     // 电机启动ATO爬坡函数处理
-    ATORamp();
+    if (mcState == mcRun)
+    { ATORamp(); }
     
     // 电机状态机的时序处理
     if (mcFocCtrl.State_Count > 0)

+ 1 - 1
User/Source/MotorControlFunction.c

@@ -432,7 +432,7 @@ void VariablesPreInit(void)
     memset(&fault, 0, sizeof(FaultVarible)); // FaultVarible变量清零
     McStaSet.SetMode = 0;
     mcFocCtrl.CtrlMode = 0;
-    mcRefRamp.OutValue_float = 0;
+    LoopRefRamp.Out = 0;
     mcFocCtrl.ChargeStep = 0;
     mcFocCtrl.Flg_ATORampEnd = 0;
 }

+ 0 - 1
User/Source/MotorProtect.c

@@ -9,7 +9,6 @@
 #include <Myproject.h>
 
 FaultStateType data mcFaultSource; ///< 故障类型
-uint8 xdata mcPOSTErrSource;       ///< 自检故障类型
 FaultVarible xdata fault;          ///< 故障检测相关结构体变量
 FaultRecoverTypedef xdata Restart; ///< 故障恢复重启控制相关结构体变量