我想通过使用带有 PI 控制器的 Mosfet 驱动器将电阻器温度保持在 45C。 我有两种方法,但我不知道哪一种是正确的?你能帮我这个吗?
#define lm35_Pin A0
#define PWM_pin 9
#define reference_temperature 45.0
double kp = 50.0;
double ki = 0.1;
float temp_voltage = 0.0, temperature = 0.0;
double error,pwm_output, P_Term , I_Term, lastError = 0 , integral = 0;
void setup() {
Serial.begin(9600);
pinMode(PWM_pin, OUTPUT);
}
void loop() {
double inputData = analogRead(lm35_Pin);
temp_voltage = (inputData / 1023.0)*5000;
temperature = temp_voltage /10.0;
error = reference_temperature - temperature;
METHOD 1
P_Term = ki * error;
I_Term += kp * error;
if (I_Term <0) I_Term = 0;
if (I_Term > 255) I_Term = 255;
pwm_output = P_Term + I_Term;
METHOD 2
P_Term = ki * error;
integral += error;
I_Term = ki * integral;
if (integral <0) integral = 0;
if (integral > 255) integral = 255;
pwm_output = P_Term + I_Term;
if (pwm_output < 0) pwm_output = 0;
if (pwm_output > 255) pwm_output = 255;
analogWrite(PWM_pin, pwm_output);
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print("C PWM: ");
Serial.print(pwm_output);
Serial.print(" PWM Ratio ");
Serial.println((100*pwm_output)/255);
delay(100); //(fs=10 Hz)
}
你能帮助我通过 PI Control 来提高自己吗?
您可以尝试以下代码:
#define lm35_Pin A0
#define PWM_pin 9
#define reference_temperature 45.0
double kp = 50.0;
double ki = 0.1;
float temp_voltage = 0.0, temperature = 0.0;
double error, pwm_output, P_Term, I_Term, lastError = 0, integral = 0;
void setup() {
//Serial.begin(9600);
pinMode(PWM_pin, OUTPUT);
}
void loop() {
double inputData = analogRead(lm35_Pin);
temp_voltage = (inputData / 1023.0) * 5000;
temperature = temp_voltage / 10.0;
error = reference_temperature - temperature;
// METHOD 1
// P_Term is proportional to the error, and I_Term is the integral of the error
P_Term = kp * error;
I_Term += ki * error;
// Ensure I_Term is within limits
if (I_Term < 0) I_Term = 0;
if (I_Term > 255) I_Term = 255;
pwm_output = P_Term + I_Term;
// METHOD 2
// P_Term is proportional to the error, and I_Term is the integral of the error
P_Term = kp * error;
integral += error; // Fix: Accumulate error in the integral term
I_Term = ki * integral;
// Ensure I_Term is within limits
if (integral < 0) integral = 0;
if (integral > 255) integral = 255;
pwm_output = P_Term + I_Term;
// Ensure pwm_output is within limits
if (pwm_output < 0) pwm_output = 0;
if (pwm_output > 255) pwm_output = 255;
analogWrite(PWM_pin, pwm_output);
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print("C PWM: ");
Serial.print(pwm_output);
Serial.print(" PWM Ratio ");
Serial.println((100 * pwm_output) / 255);
delay(100); // (fs = 10 Hz)
}