用博图SCL语言写模糊PID算法
时间: 2023-08-10 18:02:42 浏览: 667
以下是用博图SCL语言写的模糊PID算法的示例代码:
```
FUNCTION_BLOCK Fuzzy_PID
VAR_INPUT
PV : REAL; // 过程量值
SP : REAL; // 设定量值
END_VAR
VAR_OUTPUT
MV : REAL; // 控制量值
END_VAR
VAR
// 模糊控制器输入变量
e : REAL; // 误差
ec : REAL; // 误差变化率
// 模糊控制器输出变量
u : REAL; // 模糊控制器输出
// PID控制器参数
Kp : REAL := 1.0; // 比例系数
Ki : REAL := 0.0; // 积分系数
Kd : REAL := 0.0; // 微分系数
// PID控制器状态变量
e_prev : REAL := 0.0; // 上一次误差
e_int : REAL := 0.0; // 误差积分
END_VAR
// 模糊控制器输入变量的模糊集定义
FUZZIFY E
TERM NB := (SP - PV) / (SP - 0.8 * PV); // 负大
TERM NM := (SP - PV) / (SP - 0.6 * PV); // 负中
TERM NS := (SP - PV) / (SP - 0.4 * PV); // 负小
TERM ZO := (SP - PV) / (SP + 0.4 * PV); // 零
TERM PS := (SP - PV) / (SP + 0.6 * PV); // 正小
TERM PM := (SP - PV) / (SP + 0.8 * PV); // 正中
TERM PB := (SP - PV) / PV; // 正大
END_FUZZIFY
// 模糊控制器输入变量的模糊集定义
FUZZIFY EC
TERM NB := -1.0; // 负大
TERM NM := -0.6; // 负中
TERM NS := -0.2; // 负小
TERM ZO := 0.0; // 零
TERM PS := 0.2; // 正小
TERM PM := 0.6; // 正中
TERM PB := 1.0; // 正大
END_FUZZIFY
// 模糊控制器输出变量的模糊集定义
DEFUZZIFY U
TERM NB := -1.0; // 负大
TERM NM := -0.5; // 负中
TERM NS := -0.2; // 负小
TERM ZO := 0.0; // 零
TERM PS := 0.2; // 正小
TERM PM := 0.5; // 正中
TERM PB := 1.0; // 正大
METHOD : COG; // 采用中心重心法
DEFAULT := (SP - PV) / SP; // 默认输出值
END_DEFUZZIFY
// 模糊规则
RULEBLOCK Fuzzy_PID_Rules
AND : MIN; // 采用最小值原则
ACT : MIN; // 采用最小值原则
ACCU : MAX; // 采用最大值原则
RULE 1 : IF E IS NB AND EC IS NB THEN U IS NB;
RULE 2 : IF E IS NB AND EC IS NM THEN U IS NB;
RULE 3 : IF E IS NB AND EC IS NS THEN U IS NM;
RULE 4 : IF E IS NB AND EC IS ZO THEN U IS NS;
RULE 5 : IF E IS NB AND EC IS PS THEN U IS ZO;
RULE 6 : IF E IS NB AND EC IS PM THEN U IS PS;
RULE 7 : IF E IS NB AND EC IS PB THEN U IS PM;
RULE 8 : IF E IS NM AND EC IS NB THEN U IS NB;
RULE 9 : IF E IS NM AND EC IS NM THEN U IS NM;
RULE 10: IF E IS NM AND EC IS NS THEN U IS NS;
RULE 11: IF E IS NM AND EC IS ZO THEN U IS ZO;
RULE 12: IF E IS NM AND EC IS PS THEN U IS PS;
RULE 13: IF E IS NM AND EC IS PM THEN U IS PM;
RULE 14: IF E IS NM AND EC IS PB THEN U IS PB;
RULE 15: IF E IS NS AND EC IS NB THEN U IS NB;
RULE 16: IF E IS NS AND EC IS NM THEN U IS NS;
RULE 17: IF E IS NS AND EC IS NS THEN U IS ZO;
RULE 18: IF E IS NS AND EC IS ZO THEN U IS PS;
RULE 19: IF E IS NS AND EC IS PS THEN U IS PM;
RULE 20: IF E IS NS AND EC IS PM THEN U IS PB;
RULE 21: IF E IS NS AND EC IS PB THEN U IS PB;
RULE 22: IF E IS ZO AND EC IS NB THEN U IS NM;
RULE 23: IF E IS ZO AND EC IS NM THEN U IS NS;
RULE 24: IF E IS ZO AND EC IS NS THEN U IS ZO;
RULE 25: IF E IS ZO AND EC IS ZO THEN U IS ZO;
RULE 26: IF E IS ZO AND EC IS PS THEN U IS ZO;
RULE 27: IF E IS ZO AND EC IS PM THEN U IS PS;
RULE 28: IF E IS ZO AND EC IS PB THEN U IS PM;
RULE 29: IF E IS PS AND EC IS NB THEN U IS ZO;
RULE 30: IF E IS PS AND EC IS NM THEN U IS PS;
RULE 31: IF E IS PS AND EC IS NS THEN U IS PM;
RULE 32: IF E IS PS AND EC IS ZO THEN U IS PM;
RULE 33: IF E IS PS AND EC IS PS THEN U IS PB;
RULE 34: IF E IS PS AND EC IS PM THEN U IS PB;
RULE 35: IF E IS PS AND EC IS PB THEN U IS PB;
RULE 36: IF E IS PM AND EC IS NB THEN U IS NS;
RULE 37: IF E IS PM AND EC IS NM THEN U IS ZO;
RULE 38: IF E IS PM AND EC IS NS THEN U IS PS;
RULE 39: IF E IS PM AND EC IS ZO THEN U IS PM;
RULE 40: IF E IS PM AND EC IS PS THEN U IS PB;
RULE 41: IF E IS PM AND EC IS PM THEN U IS PB;
RULE 42: IF E IS PM AND EC IS PB THEN U IS PB;
RULE 43: IF E IS PB AND EC IS NB THEN U IS ZO;
RULE 44: IF E IS PB AND EC IS NM THEN U IS PS;
RULE 45: IF E IS PB AND EC IS NS THEN U IS PM;
RULE 46: IF E IS PB AND EC IS ZO THEN U IS PM;
RULE 47: IF E IS PB AND EC IS PS THEN U IS PB;
RULE 48: IF E IS PB AND EC IS PM THEN U IS PB;
RULE 49: IF E IS PB AND EC IS PB THEN U IS PB;
END_RULEBLOCK
// 模糊PID控制器主程序
MV := Kp * (SP - PV) + Ki * e_int + Kd * (e - e_prev);
e_prev := e;
e := SP - PV;
ec := e - e_prev;
Fuzzy_PID_Rules(e, ec, u);
e_int := e_int + e;
MV := MV + u;
```
在这个示例代码中,我们定义了一个名为Fuzzy_PID的函数块,在其中实现了模糊PID控制器的逻辑。该函数块的输入变量为过程量值PV和设定量值SP,输出变量为控制量值MV。在函数块内部,我们定义了模糊控制器的输入变量e和ec,以及输出变量u。我们还定义了PID控制器的比例系数Kp、积分系数Ki和微分系数Kd,以及状态变量e_prev、e_int。在FUZZIFY和DEFUZZIFY中,我们分别定义了模糊控制器输入、输出变量的模糊集,并通过RULEBLOCK定义了模糊规则。最后,在主程序中,我们通过模糊PID控制器计算出控制量值MV,并将其输出。
阅读全文