I am designing a PID temperature controler; as far as I managed to write it so far, you find the code below.
Can you please help me check if Integrator wind up and other PID elements are configured please?
When i run the code at some point close to the set point , it starts behaving like an ON and OFF code
I don't know if this is a valid question but im asking it anyways .
and if you can make any edits . i will be glad to check them out
// programing the pid
const int pin = 3; // PWM analog output
double cumerror = 0; // Initialising the cumulator of the integrator to 0
double input, setpoint, integral, proportional, piderror, pidvalue;
// Input values
double kp = 25; // seting proportional at 25
double ki = 0; // seting the integral at 0
void setup() {
setpoint = 37; // seting the setpoint at 37
Serial.begin(9600);
}
void loop() {
double inputvalue = map(analogRead(A0), 0, 1023, 0,
100); // reading the values from the analog signals
// and mapping it to a 100 deg scale
piderror = setpoint - inputvalue; // calculating the error
// calculating the integral ((Naturally ki= cumerror dt), we do not have to
// multiply by dt, since dt = 0 because i am using the sample time delay(1000)
// every time )
cumerror += piderror;
integral = ki cumerror;
// calculating the proportional error
proportional = kp * piderror; // calculating the proportional error
// changing the P and I parameters as the error approaches the set point
if (3 < piderror < 10) {
kp = 10;
ki = 0.5;
cumerror += (1 * piderror);
integral = ki * cumerror;
}
// Making the parameters =0 as the error approaches more 0
if (piderror < 3) {
proportional = 0;
pidvalue = 0;
integral = 0;
}
pidvalue = proportional + integral;
if (pidvalue <= 0) {
pidvalue = 0;
}
if (pidvalue > 255) {
pidvalue = 255;
}
analogWrite(pin, pidvalue); // sending the pid value to the heater
delay(1000);
cumerror=cumerror;
}
Serial.print()lines as they (should) have no affect on the operation - unless you suspect there is a problem there (in which case you should explain why you think there is a problem or why we need to see them). Remove spaces before punctuation,or;and add spaces after punctuation for legibility and clarity. i.e, Make it look professional. – Transistor Jul 14 '20 at 17:11delay(1000);The value of this delay should have been calculated based on the speed of thermal response. If the system heats up quickly, the temperature would have overshot the target in1000ms, leading to an ON-OFF type response. – AJN Jul 15 '20 at 01:59is it putting the heater on power supply and calculate the amount of time it takes to change from for example 0 deg to 10 deg ?This is what we would call a step response. It will give you an idea what delay to use and how big/small yourKpshould be. How did you arrive at the current values ofKp,KdandKi? trial and error ? It will be good if you can post a graph of the controller output and sensor input waveforms here. Also a step response as you indicated above. As mentioned by others, start with simpleKpalone.Kiand gain switching etc can be implemented later. – AJN Jul 15 '20 at 05:56