模糊pid控制温度的c语言
时间: 2023-09-24 16:05:42 浏览: 83
以下是模糊PID控制温度的C语言示例代码:
```c
#include <stdio.h>
// 模糊 PID 控制器参数
#define Kp 1.0
#define Ki 0.5
#define Kd 0.1
// 模糊参数
#define NB 5
#define NM 7
#define NS 9
#define ZO 11
#define PS 13
#define PM 15
#define PB 17
// 模糊输入域和输出域
#define TEMP_MIN 0
#define TEMP_MAX 100
#define POWER_MIN 0
#define POWER_MAX 100
// 模糊集合函数
double fuzzySet[NB+NM+NS+1+PS+PM+PB];
// 模糊控制规则
double fuzzyRules[NB][NB+NM+NS+1+PS+PM+PB];
// 模糊PID控制器
double fuzzyPIDController(double error, double last_error, double sum_error) {
// 计算误差的模糊值
int nb = error < -10 ? NB : (error < -5 ? (NB+NM)/2 : (error < 0 ? (NB+NM+NS)/3 : (error < 5 ? (NB+NM+NS+ZO)/4 : (error < 10 ? (NB+NM+NS+ZO+PS)/5 : PB))));
int nm = error < -10 ? NB : (error < -5 ? (NB+NM)/2 : (error < 0 ? (NB+NM+NS)/3 : (error < 5 ? (NB+NM+NS+ZO)/4 : (error < 10 ? (NB+NM+NS+ZO+PS)/5 : PB))));
int ns = error < -10 ? NB : (error < -5 ? (NB+NM)/2 : (error < 0 ? (NB+NM+NS)/3 : (error < 5 ? (NB+NM+NS+ZO)/4 : (error < 10 ? (NB+NM+NS+ZO+PS)/5 : PB))));
int zo = error < -5 ? (NB+NM)/2 : (error < 0 ? (NB+NM+NS)/3 : (error < 5 ? (NB+NM+NS+ZO)/4 : (error < 10 ? (NB+NM+NS+ZO+PS)/5 : PM)));
int ps = error < 0 ? (NB+NM+NS+ZO)/4 : (error < 5 ? (NB+NM+NS+ZO+PS)/5 : (error < 10 ? (NB+NM+NS+ZO+PS+PM)/6 : (NB+NM+NS+ZO+PS+PM+PB)/7));
int pm = error < 0 ? (NB+NM+NS+ZO)/4 : (error < 5 ? (NB+NM+NS+ZO+PS)/5 : (error < 10 ? (NB+NM+NS+ZO+PS+PM)/6 : (NB+NM+NS+ZO+PS+PM+PB)/7));
int pb = error < 5 ? PM : (error < 10 ? (PM+PB)/2 : PB);
// 计算误差的模糊输出
for (int i = 0; i <= PB; i++) {
if (i < nb) {
fuzzySet[i] = 0;
} else if (i < nm) {
fuzzySet[i] = (double)(i-nb)/(nm-nb);
} else if (i < ns) {
fuzzySet[i] = (double)(ns-i)/(ns-nm);
} else if (i < zo) {
fuzzySet[i] = (double)(zo-i)/(zo-ns);
} else if (i < ps) {
fuzzySet[i] = (double)(i-zo)/(ps-zo);
} else if (i < pm) {
fuzzySet[i] = (double)(pm-i)/(pm-ps);
} else if (i < pb) {
fuzzySet[i] = (double)(pb-i)/(pb-pm);
} else {
fuzzySet[i] = 0;
}
}
// 计算模糊控制规则
for (int i = 0; i < NB; i++) {
for (int j = 0; j <= PB; j++) {
if (j < nb) {
fuzzyRules[i][j] = 0;
} else if (j < nm) {
fuzzyRules[i][j] = (double)(j-nb)/(nm-nb);
} else if (j < ns) {
fuzzyRules[i][j] = (double)(ns-j)/(ns-nm);
} else if (j < zo) {
fuzzyRules[i][j] = (double)(zo-j)/(zo-ns);
} else if (j < ps) {
fuzzyRules[i][j] = (double)(j-zo)/(ps-zo);
} else if (j < pm) {
fuzzyRules[i][j] = (double)(pm-j)/(pm-ps);
} else if (j < pb) {
fuzzyRules[i][j] = (double)(pb-j)/(pb-pm);
} else {
fuzzyRules[i][j] = 0;
}
}
}
// 计算模糊输出
double output = 0;
double sum_weight = 0;
for (int i = 0; i <= PB; i++) {
double weight = fuzzySet[i];
double delta_error = error - last_error;
double power = (double)i/(PB-PM)*100;
double control = Kp*fuzzyRules[nm][i] + Ki*fuzzyRules[ns][i] + Kd*fuzzyRules[pm][i]*(delta_error/sum_error);
output += weight*control*power;
sum_weight += weight;
}
output /= sum_weight;
// 输出控制器参数
printf("fuzzySet:");
for (int i = 0; i <= PB; i++) {
printf(" %.2f", fuzzySet[i]);
}
printf("\n");
printf("fuzzyRules:\n");
for (int i = 0; i < NB; i++) {
for (int j = 0; j <= PB; j++) {
printf("%.2f ", fuzzyRules[i][j]);
}
printf("\n");
}
return output;
}
int main() {
// 模拟温度控制
double target_temp = 50;
double current_temp = 20;
double last_error = 0;
double sum_error = 0;
while (1) {
double error = target_temp - current_temp;
double control_power = fuzzyPIDController(error, last_error, sum_error);
// 更新温度
current_temp += control_power/10;
last_error = error;
sum_error += error;
// 输出温度和控制力
printf("temp=%.2f, power=%.2f\n", current_temp, control_power);
}
return 0;
}
```
注意:以上代码仅供参考,实际应用中需要根据具体需求进行修改和优化。
阅读全文