I'm trying to implement a PID controller in C for an embbeded system. This is the first time. I think I haven't fully understand the theory. For the proportional term, we have an output from PID controller that is P = Kp*err Suppose I have a power supply with a variable output voltage. The power supply output is connected to a low-value resistor that dissipates power and transfer heat to some water. I need to reach and maintain a temperature of the water by increasing/decreasing voltage across the resistor (i.e., the dissipated power). The loop is closed by the temperature measurement. Goal = 80�C. Kp = 0.1V/�C. FIRST STEP T = 20�C. Error = 60�C. P = 6V. Should I set the power supply output voltage to 6V? I think so. SECOND STEP T = 30�C. Error = 50�C. P = 5V. Should I set Vout=5V or increase Vout by 5V so setting it to 11V? The mathematics say the output of PID controller *is* Kp*err, but I'm not sure. With the increase of temperature, the errore decreases to a minimum, so the Vout. At the steady state the error should be zero, but the voltage from the power supply can't be zero. So I'm thinking to implement the proportional term as: Vout += kp*err (note +=) but this isn't what I found in literature.
PID controller: dummy questions
Started by ●June 25, 2020
Reply by ●June 25, 20202020-06-25
Am 25.06.2020 um 19:55 schrieb pozz:> Goal = 80�C. Kp = 0.1V/�C. > > FIRST STEP > T = 20�C. Error = 60�C. P = 6V. > Should I set the power supply output voltage to 6V? I think so. > > SECOND STEP > T = 30�C. Error = 50�C. P = 5V. > Should I set Vout=5V or increase Vout by 5V so setting it to 11V?5 V> The mathematics say the output of PID controller *is* Kp*err, but I'm > not sure. With the increase of temperature, the errore decreases to a > minimum, so the Vout. At the steady state the error should be zero, but > the voltage from the power supply can't be zero.It can be, if your heater overshoots --- for a while. Then your water will begin to loose heat, and the P controller will reactivate your heater. Lather, rinse, repeat. Voila, oscillation: your P controller devolved into the even simpler 2-point controller: Apply heat when too cold, otherwise don't. That's it But more likely, it'll just reach a state a couple degrees below your target, where Kp times that difference yields a heating power that just about compensates the heat loss. In the absence of actual disturbances, that's the state this system will remain at indefinitely. In other words: congratulations, you've just understood what they mean when they say a pure P controller will never actually reach a steady zero-error state. And if the control is sign-constrained (you only have a heater, but you cannot cool), that may apply even more strongly. One somewhat hand-wavy possibility to avoid the latter trap is to artificially bias your control. E.g. define that "control output zero" means "50% of the actual swing of the output", so negative control outputs can have an effect, too. That 50% point may later profit from some more strategic positioning, a.k.a. tweaking.
Reply by ●June 25, 20202020-06-25
I'm not an expert, but I've gone through this puzzle in the past, so... pozz <pozzugno@gmail.com> writes:> For the proportional term, we have an output from PID controller that is > > P = Kp*errYup!> FIRST STEP > T = 20°C. Error = 60°C. P = 6V. > Should I set the power supply output voltage to 6V? I think so.Yup!> SECOND STEP > T = 30°C. Error = 50°C. P = 5V. > Should I set Vout=5V or increase Vout by 5V so setting it to 11V?5V.> The mathematics say the output of PID controller *is* Kp*err,As a first step, yes. You're not done though. Even with a pure P controller, you can just set Kp high enough that the error is small. I.e. if your Kp was 10V/V, you'd get a 10V swing from 79C to 81C, which may be enough. If you set Kp REALLY high, you effectively get an on-off control like a mechanical thermostat. Ideally, you'd set Kp as high as you can get away with, but not so high that you get runaway oscillations in the output.> but I'm not sure. With the increase of temperature, the errore > decreases to a minimum, so the Vout. At the steady state the error > should be zero, but the voltage from the power supply can't be zero.P = Kp * err (note = not += ) Vout = P So far you've done the "P" part of PID. Next you need to do the "I" part of PID. The P portion quickly (hopefully) gets you close to the goal, with some steady-state error. The next step is to integrate the remaining error over time, which undoes any "offset" in the system. I += Ki * err (note += not = ) Vout = P + I So now, if the Kp isn't enough to get you to goal, the system will slowly boost the output to remove the offset. This is a PI controller. Note that the accumulated I value is often limited in magnitude to prevent runaway. Now for the "D" term. The D term is designed to prevent overshoot by "putting on the brakes" if you're approaching the goal too quickly. D = Kd * (err[t] - err[t-1]) (i.e. how fast is the error changing) Vout = P + I + D; Now you have a PID controller :-) There's one more term that's often used to optimize the system - a "feed forward" term. F = Kf * Tgoal I.e. if your system is of the type where (approximately) input voltage = output temperature, you can estimate this with an F term, and use the PID control just for precision.
Reply by ●June 25, 20202020-06-25
On 6/25/20 1:55 PM, pozz wrote:> I'm trying to implement a PID controller in C for an embbeded system. > This is the first time. I think I haven't fully understand the theory. > > For the proportional term, we have an output from PID controller that is > > � P = Kp*err >Basic bit of terminology, You used the Term PID controller, but you only have a P term. The equation for a PID controlloer would be more like: P = Kp*err + Ki*Integral(err) + Kd*Derivative(err) I.E. have a P, and I, and a D term. Often, you will use the I term to eliminate the DC error, but with a cost of needing to control oscillations, as integral loops have a habit of wanting to overshoot. And the D term can provide damping to reduce overshoot. You example of the loop where P became the sum of the previous P and the new Kp*err was basically just an integral loop, where you took the sum of the errors times the control constant (using the distributive property).
Reply by ●June 26, 20202020-06-26
On 25/06/2020 21:29, DJ Delorie wrote:> > I'm not an expert, but I've gone through this puzzle in the past, so... > > pozz <pozzugno@gmail.com> writes: >> For the proportional term, we have an output from PID controller that is >> >> P = Kp*err > > Yup! > >> FIRST STEP >> T = 20°C. Error = 60°C. P = 6V. >> Should I set the power supply output voltage to 6V? I think so. > > Yup! > >> SECOND STEP >> T = 30°C. Error = 50°C. P = 5V. >> Should I set Vout=5V or increase Vout by 5V so setting it to 11V? > > 5V. > > >> The mathematics say the output of PID controller *is* Kp*err, > > As a first step, yes. You're not done though. Even with a pure P > controller, you can just set Kp high enough that the error is small. > I.e. if your Kp was 10V/V, you'd get a 10V swing from 79C to 81C, which > may be enough. If you set Kp REALLY high, you effectively get an on-off > control like a mechanical thermostat. > > Ideally, you'd set Kp as high as you can get away with, but not so high > that you get runaway oscillations in the output. > >> but I'm not sure. With the increase of temperature, the errore >> decreases to a minimum, so the Vout. At the steady state the error >> should be zero, but the voltage from the power supply can't be zero. > > P = Kp * err (note = not += ) > > Vout = P > > So far you've done the "P" part of PID. Next you need to do the "I" > part of PID. > > The P portion quickly (hopefully) gets you close to the goal, with some > steady-state error. The next step is to integrate the remaining error > over time, which undoes any "offset" in the system. > > I += Ki * err (note += not = ) > > Vout = P + I > > So now, if the Kp isn't enough to get you to goal, the system will > slowly boost the output to remove the offset. This is a PI controller. > Note that the accumulated I value is often limited in magnitude to > prevent runaway. > > Now for the "D" term. The D term is designed to prevent overshoot by > "putting on the brakes" if you're approaching the goal too quickly. > > D = Kd * (err[t] - err[t-1]) > (i.e. how fast is the error changing) > > Vout = P + I + D; > > Now you have a PID controller :-) > > There's one more term that's often used to optimize the system - a "feed > forward" term. > > F = Kf * Tgoal > > I.e. if your system is of the type where (approximately) input voltage = > output temperature, you can estimate this with an F term, and use the > PID control just for precision. >There's a few clarifications I'd like to add to this. Sometimes in examples or literature you'll see a different form of the integral term. Instead of writing: I += Ki * err you write: S += err I = Ki * S This makes more sense to me, and many people - you integrate the error over time, and then apply a scale factor when you use it. It also means your windup limits are consistent while you play with different Ki factors to get a nice regulation. It's worth noting that Kd here is negative, while Kp and Ki are positive. And sometimes the scaling is written with a single K and time factors for the I and D parts. Ultimately this is all the same in the end, but it can be useful for the OP to understand when he reads about it online. Finally, the F term you have here can be a function, not just a simple scale. You might base it on a table, or from a linear function (scale and offset).
Reply by ●June 26, 20202020-06-26
Il 25/06/2020 19:55, pozz ha scritto:> I'm trying to implement a PID controller in C for an embbeded system. > This is the first time. I think I haven't fully understand the theory.I read all your answers, but I didn't get the point. I'm thinking of a system where a process variable (PV, such as a liquid flow) must match a desired set point (SP, such as 1 lt/min). The controller can change a control variable (CV, such as a voltage applied to an electronic valve). If we need 1 lt/min we start opening the valve increasing the voltage control. When we see 1 lt/min on a flowmeter, we stop increasing the voltage that stay indefinitely at, for example, 1.0V. At the steady state, when the error is zero, the control voltage *is not* zero, but 1.0V. The proportional term (P) of a PID controller can't mimic this "manual control", because when the error is zero (we found the right voltage on the valve), the P term drops to zero and the valve would close. When I use my brain, I will do: - the error is high, rotate the knob fast - the error is low (we are approaching the desidered value), rotate the knob slowly - the error is zero, ok stop rotating the knob The P term of a PID controller works in a different way: - the error is high, high voltage control, open the valve high - the error decreases, low voltage control, the valve starts closing - the error is zero, close the valve completely (P=0) Maybe my brain works as an integrative-only controller?
Reply by ●June 26, 20202020-06-26
On 26/06/2020 10:59, pozz wrote:> Il 25/06/2020 19:55, pozz ha scritto: >> I'm trying to implement a PID controller in C for an embbeded >> system. This is the first time. I think I haven't fully understand >> the theory. > > I read all your answers, but I didn't get the point. > > I'm thinking of a system where a process variable (PV, such as a > liquid flow) must match a desired set point (SP, such as 1 lt/min). > The controller can change a control variable (CV, such as a voltage > applied to an electronic valve). > > If we need 1 lt/min we start opening the valve increasing the > voltage control. When we see 1 lt/min on a flowmeter, we stop > increasing the voltage that stay indefinitely at, for example, 1.0V. > At the steady state, when the error is zero, the control voltage *is > not* zero, but 1.0V.You are describing an "I" controller, not a "P" controller. For long-term steady state regulation, the "I" bit of "PID" is typically the most important. But lacking a "P" part means you will react slowly to changes. And if you lack the "D" part, you need to keep the "I" part slow to avoid oscillations.> > The proportional term (P) of a PID controller can't mimic this > "manual control", because when the error is zero (we found the right > voltage on the valve), the P term drops to zero and the valve would > close.That is correct. That is why a pure "P" controller is often not a suitable choice. It can never achieve a zero-error steady state.> > When I use my brain, I will do: > > - the error is high, rotate the knob fast - the error is low (we are > approaching the desidered value), rotate the knob slowly - the error > is zero, ok stop rotating the knob > > The P term of a PID controller works in a different way: > > - the error is high, high voltage control, open the valve high - the > error decreases, low voltage control, the valve starts closing - the > error is zero, close the valve completely (P=0) > > > Maybe my brain works as an integrative-only controller?Brains are not consistent enough to be classified as PID controllers of any kind.
Reply by ●June 26, 20202020-06-26
On Friday, June 26, 2020 at 4:59:13 AM UTC-4, pozz wrote:> Il 25/06/2020 19:55, pozz ha scritto: > > I'm trying to implement a PID controller in C for an embbeded system. > > This is the first time. I think I haven't fully understand the theory. > > I read all your answers, but I didn't get the point. > > I'm thinking of a system where a process variable (PV, such as a liquid > flow) must match a desired set point (SP, such as 1 lt/min). The > controller can change a control variable (CV, such as a voltage applied > to an electronic valve). > > If we need 1 lt/min we start opening the valve increasing the voltage > control. When we see 1 lt/min on a flowmeter, we stop increasing the > voltage that stay indefinitely at, for example, 1.0V. > At the steady state, when the error is zero, the control voltage *is > not* zero, but 1.0V. > > The proportional term (P) of a PID controller can't mimic this "manual > control", because when the error is zero (we found the right voltage on > the valve), the P term drops to zero and the valve would close. > > When I use my brain, I will do: > > - the error is high, rotate the knob fast > - the error is low (we are approaching the desidered value), rotate the > knob slowly > - the error is zero, ok stop rotating the knob > > The P term of a PID controller works in a different way: > > - the error is high, high voltage control, open the valve high > - the error decreases, low voltage control, the valve starts closing > - the error is zero, close the valve completely (P=0) > > > Maybe my brain works as an integrative-only controller?You seem to be only talking about the proportional control function while the PID controller has also an integral term and a differential term. To reach a point where the error is zero regardless of the setting requires the addition of the integral term. This term can cause oscillations however. Keeping it small relative to the proportional term will reduce that. Judicious use of the differential term will also help to give an even response with minimal over shoot. PID controlers are simple in concept, but difficult to use in practice because of stability issues. Is your application a real design or is this just for learning? -- Rick C. - Get 1,000 miles of free Supercharging - Tesla referral code - https://ts.la/richard11209
Reply by ●June 26, 20202020-06-26
On 6/26/20 4:59 AM, pozz wrote:> Il 25/06/2020 19:55, pozz ha scritto: >> I'm trying to implement a PID controller in C for an embbeded system. >> This is the first time. I think I haven't fully understand the theory. > > I read all your answers, but I didn't get the point. > > I'm thinking of a system where a process variable (PV, such as a liquid > flow) must match a desired set point (SP, such as 1 lt/min). The > controller can change a control variable (CV, such as a voltage applied > to an electronic valve). > > If we need 1 lt/min we start opening the valve increasing the voltage > control. When we see 1 lt/min on a flowmeter, we stop increasing the > voltage that stay indefinitely at, for example, 1.0V. > At the steady state, when the error is zero, the control voltage *is > not* zero, but 1.0V. > > The proportional term (P) of a PID controller can't mimic this "manual > control", because when the error is zero (we found the right voltage on > the valve), the P term drops to zero and the valve would close. > > When I use my brain, I will do: > > - the error is high, rotate the knob fast > - the error is low (we are approaching the desidered value), rotate the > knob slowly > - the error is zero, ok stop rotating the knob > > The P term of a PID controller works in a different way: > > - the error is high, high voltage control, open the valve high > - the error decreases, low voltage control, the valve starts closing > - the error is zero, close the valve completely (P=0) > > > Maybe my brain works as an integrative-only controller?It makes a big difference in what you think of as the 'Plant' and the 'Control Variable'. If your input is 'The speed I am turning the knob', then your control law is a Proportional control system. If you input is 'How much the valve is open', then you control law in an Integral control system. Note, the difference is because position is the integral of Velocity. One important aspect of a system is how many poles (or zeros) it has at zero (or near zero). These will vastly affect what type of control loop you want to put around the system.
Reply by ●June 26, 20202020-06-26
On Friday, June 26, 2020 at 12:29:27 PM UTC-4, Richard Damon wrote:> On 6/26/20 4:59 AM, pozz wrote: > > Il 25/06/2020 19:55, pozz ha scritto: > >> I'm trying to implement a PID controller in C for an embbeded system. > >> This is the first time. I think I haven't fully understand the theory. > > > > I read all your answers, but I didn't get the point. > > > > I'm thinking of a system where a process variable (PV, such as a liquid > > flow) must match a desired set point (SP, such as 1 lt/min). The > > controller can change a control variable (CV, such as a voltage applied > > to an electronic valve). > > > > If we need 1 lt/min we start opening the valve increasing the voltage > > control. When we see 1 lt/min on a flowmeter, we stop increasing the > > voltage that stay indefinitely at, for example, 1.0V. > > At the steady state, when the error is zero, the control voltage *is > > not* zero, but 1.0V. > > > > The proportional term (P) of a PID controller can't mimic this "manual > > control", because when the error is zero (we found the right voltage on > > the valve), the P term drops to zero and the valve would close. > > > > When I use my brain, I will do: > > > > - the error is high, rotate the knob fast > > - the error is low (we are approaching the desidered value), rotate the > > knob slowly > > - the error is zero, ok stop rotating the knob > > > > The P term of a PID controller works in a different way: > > > > - the error is high, high voltage control, open the valve high > > - the error decreases, low voltage control, the valve starts closing > > - the error is zero, close the valve completely (P=0) > > > > > > Maybe my brain works as an integrative-only controller? > > It makes a big difference in what you think of as the 'Plant' and the > 'Control Variable'. If your input is 'The speed I am turning the knob', > then your control law is a Proportional control system. If you input is > 'How much the valve is open', then you control law in an Integral > control system. Note, the difference is because position is the integral > of Velocity. > > One important aspect of a system is how many poles (or zeros) it has at > zero (or near zero). These will vastly affect what type of control loop > you want to put around the system.The realities of thermal control are complex with the heating device introducing an element of integrated response in addition to the thing being heated. Formal analysis is often impossible. So the resulting design process is to twiddle the PID coefficients until you get a feel for the optimum settings. Alternatively the "plant" response can be measured by applying inputs and analyzing the outputs. Complex to do in practice for many systems. -- Rick C. + Get 1,000 miles of free Supercharging + Tesla referral code - https://ts.la/richard11209