EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

PID controller: dummy questions

Started by pozz June 25, 2020
Am 26.06.2020 um 10:59 schrieb pozz:
> > The proportional term (P) of a PID controller can't mimic this "manual > control",
Nobody ever claimed it could. A P-only controller simply won't meet that goal. That's a large part of the reason why everybody routinely talks about full PID-controllers. People know better than to just react linearly. You would be doing a good deal of D- and I-type controlling, too, and most likely some more that doesn't fit into the PID model at all. Especially if it's not the first time ever you've been turning that knob. You will have got a "feel" for it after just a few tries. Nobody in their right mind ever claimed the human brain was as bluntly stupid as a P controller. A PID controller is still way simpler than what you would do yourself. We use them not because they're better at the job than humans, but rather because they're cheaper and more reliable in the long run. All said, I believe your apparent frustration at the answer you've found has more to do with asking the wrong question than with anything else.
On 6/26/20 3:00 PM, Rick C wrote:
> 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. >
Defining the plant doesn't necessarily mean fully knowing its dynamics, but more importantly, what the controls actually mean. Am I controlling how fast I am turning the valve know, or am I controlling the valve position. The shift from position to speed effectively added an integrator into the system. Yes, thermal systems tend to be fairly heavily low pass filter systems, and you often don't have a detailed provided response (and it might easily change over time and ambilant conditions). What isn't hard to get is an idea of your zero frequency pole/zero situation. Imagine a constant, small signal excitiation to the system. The system is likely to do one of three things (assuming a system that is at least somewhat linear): 1) It can go unstable and run off to infinity. This is indicative that you have a pole at zero (or very near it) and means you may not need an integrator in your controller. 2) It can go to some finite, non-zero state. This indicated neither a pole or zero near zero. A proportional controller will have a finite error result, so you may want an integral term in the system. 3) It can go to a zero output response. This indicates that you have a zero in the plant at DC (basically something is AC Coupled). Such systems can't maintain a DC output condition (at least not with that input).
Richard Damon <Richard@Damon-Family.org> writes:
> 2) It can go to some finite, non-zero state. This indicated neither a > pole or zero near zero. A proportional controller will have a finite > error result, so you may want an integral term in the system.
Slightly tangential but can someone explain if the integral term is supposed to decay? Otherwise, can't it potentially keep getting bigger and bigger without bound? E.g. in a thermal system, you have to keep adding heat to account for losses, or for (say) hot water being drawn out of a water heater, and being replaced by incoming cold water.
Paul Rubin <no.email@nospam.invalid> writes:
> Slightly tangential but can someone explain if the integral term is > supposed to decay? Otherwise, can't it potentially keep getting bigger > and bigger without bound?
Typical systems will limit the I term to some range so it won't go out of bounds (or "wind up" as they say). It doesn't decay on its own, but the system can cause its value to reduce to zero due to the Ki math.
> E.g. in a thermal system, you have to keep adding heat to account for > losses, or for (say) hot water being drawn out of a water heater, and > being replaced by incoming cold water.
Yes, but the amount of extra energy you put in is constant in that case, and when the extra energy balances so that you stay at your set point, the I term stops increasing, because the error is now zero. Drain more hot water, the I term goes up. Insulate the tank, the I term goes down.
On Saturday, June 27, 2020 at 12:41:31 AM UTC-4, Paul Rubin wrote:
> Richard Damon <Richard@Damon-Family.org> writes: > > 2) It can go to some finite, non-zero state. This indicated neither a > > pole or zero near zero. A proportional controller will have a finite > > error result, so you may want an integral term in the system. > > Slightly tangential but can someone explain if the integral term is > supposed to decay? Otherwise, can't it potentially keep getting bigger > and bigger without bound? E.g. in a thermal system, you have to keep > adding heat to account for losses, or for (say) hot water being drawn > out of a water heater, and being replaced by incoming cold water.
You are missing the point that the output of the controller is the rate of added heat, i.e. heat flow, not some quantity of heat added. The integral term accounts for the base heat flow required to maintain the set temperature. After that is achieved by the integral term ramping up as needed the proportional term minimizes fluctuations from short term variations in the heat flow out of the system. In the water heater example the integral term will be ramping up the entire time the heater is not up to temperature. The rate of increase of the heat flowing in will lower until the set point is achieved at which point the integral term will be at a maximum, but too high with the water temperature continuing to rise. Once the temperature rises above the set point the integral ramps down until it reaches a minimum with the temperature too low again. With the proportional gain set much higher than the integral gain, these overshoots will diminish, but never be eliminated. Adding in the derivative term with the right setting will allow the temperature to ramp up in a critically damped manner without overshoot... in theory. I've always settled for the damped oscillations rather than trying to deal with the added noise from a derivative term. -- Rick C. -- Get 1,000 miles of free Supercharging -- Tesla referral code - https://ts.la/richard11209
DJ Delorie <dj@delorie.com> writes:
> Typical systems will limit the I term to some range so it won't go out > of bounds (or "wind up" as they say). It doesn't decay on its own, but > the system can cause its value to reduce to zero due to the Ki math.
Do you mean it saturates (controller stops increasing it once it reaches some maximum)? Ok, but won't it reach the maximum and stay there? The Ki math I think was: I += Ki * err (note += not = ) Vout = P + I So if the error is almost always positive, I will keep increasing.
>> E.g. in a thermal system, you have to keep adding heat > > Yes, but the amount of extra energy you put in is constant in that case, > and when the extra energy balances so that you stay at your set point, > the I term stops increasing, because the error is now zero. Drain more > hot water, the I term goes up. Insulate the tank, the I term goes down.
I'm imagining a tank starting with 20 degree water and you want to heat it to 50 degrees. Now the error is 30 degrees, or am I interpreting the wrong thing as the error? You turn on the heat and the error decreases as the water gets hotter. Eventually the water reaches 50 degrees and the error is 0, but I is some positive number (the integral of the error over the time it took to reach 50 degrees). Now you draw out some hot water, say to wash dishes; you replace it with more 20 degree water, so the system is say at 40 degrees now (error is 10). You turn the heat back on, but again, I keeps increasing. You might overshoot a little (say to 51 degrees, error is -1) in which case I might decrease a little until the tank cools back down. But really, the error will always be either positive or near 0. So I keeps increasing. I have to be missing something. Thanks.
Rick C <gnuarm.deletethisbit@gmail.com> writes:
> In the water heater example the integral term will be ramping up the > entire time the heater is not up to temperature. The rate of increase > of the heat flowing in will lower until the set point is achieved at > which point the integral term will be at a maximum, but too high with > the water temperature continuing to rise.
Let's say the water heater is insulated, but not perfectly (which is impossible). That means heat steadily escapes from the system and you have to keep adding energy to maintain the temperature. That is, the error (difference between measured temperature and goal temperature) is always positive or zero, except maybe for a few moments when you overshoot slightly. Therefore, the integral of the error should keep increasing. I tried reading the Wikipedia article about PID and thought I understood it, but still come away from it with the same impression. I'm clearly missing something.
On Saturday, June 27, 2020 at 2:12:21 AM UTC-4, Paul Rubin wrote:
> DJ Delorie <dj@delorie.com> writes: > > Typical systems will limit the I term to some range so it won't go out > > of bounds (or "wind up" as they say). It doesn't decay on its own, but > > the system can cause its value to reduce to zero due to the Ki math. > > Do you mean it saturates (controller stops increasing it once it reaches > some maximum)? Ok, but won't it reach the maximum and stay there? The > Ki math I think was: > > I += Ki * err (note += not = ) > Vout = P + I > > So if the error is almost always positive, I will keep increasing. > > >> E.g. in a thermal system, you have to keep adding heat > > > > Yes, but the amount of extra energy you put in is constant in that case, > > and when the extra energy balances so that you stay at your set point, > > the I term stops increasing, because the error is now zero. Drain more > > hot water, the I term goes up. Insulate the tank, the I term goes down. > > I'm imagining a tank starting with 20 degree water and you want to heat > it to 50 degrees. Now the error is 30 degrees, or am I interpreting the > wrong thing as the error? You turn on the heat and the error decreases > as the water gets hotter. Eventually the water reaches 50 degrees and > the error is 0, but I is some positive number (the integral of the error > over the time it took to reach 50 degrees). > > Now you draw out some hot water, say to wash dishes; you replace it with > more 20 degree water, so the system is say at 40 degrees now (error is > 10). You turn the heat back on, but again, I keeps increasing. You > might overshoot a little (say to 51 degrees, error is -1) in which case > I might decrease a little until the tank cools back down. But really, > the error will always be either positive or near 0. So I keeps increasing. > > I have to be missing something. Thanks.
Yes, you are missing that the error variable can be both positive and negative and by definition will balance the I variable to a value that makes error return to zero. Once the water in your heater reaches the set temperature the error will be zero but the heat sent to the water heater won't be zero. If water is drawn the temperature will drop and the error term will go positive again, the integral term will increase to restore the error to zero, but at that point the heat into the tank will be higher than before so that the temperature will rise above the set point and the error will go negative. It is a mistake to think there will be more positive error than negative error. Once the water is warmed to the appropriate temperature the integral value will be back at the value it had before cold water was added. Try drawing this circuit in LTspice or a similar program. You will need to get creative to model the thermal properties, but you can do it with capacitors and resistors. Have a resistor in line with the output voltage from your controller feeding the capacitor and add a resistor to ground to represent the heat losses in the water tank. Add a second resistor with a switch to represent drawing water out of the tank. Then you can watch the voltage on the capacitor rise and fall as the circuit charges it up and the switched resistor empties it out. The controller can be made of op amps. One with an integrator cap to be the... integrator. One with a series cap on the input to be the differentiator and one with no cap to be the proportional term. A fourth op amp will combine the three inputs to produce the control output. It would be an interesting circuit to watch operate. -- Rick C. -+ Get 1,000 miles of free Supercharging -+ Tesla referral code - https://ts.la/richard11209
On Saturday, June 27, 2020 at 2:18:44 AM UTC-4, Paul Rubin wrote:
> Rick C <gnuarm.deletethisbit@gmail.com> writes: > > In the water heater example the integral term will be ramping up the > > entire time the heater is not up to temperature. The rate of increase > > of the heat flowing in will lower until the set point is achieved at > > which point the integral term will be at a maximum, but too high with > > the water temperature continuing to rise. > > Let's say the water heater is insulated, but not perfectly (which is > impossible). That means heat steadily escapes from the system and you > have to keep adding energy to maintain the temperature. That is, the > error (difference between measured temperature and goal temperature) is > always positive or zero, except maybe for a few moments when you > overshoot slightly. Therefore, the integral of the error should keep > increasing. > > I tried reading the Wikipedia article about PID and thought I understood > it, but still come away from it with the same impression. I'm clearly > missing something.
Your mistake is thinking the integral term will only overshoot "slightly". Once the integral term rises to heat the added water it is too high to maintain the temperature of the take once it reaches the set point. So the error will shoot past the zero point and become negative quickly. The integrator doesn't make the temperature rise to gently reach the set point. At the point the temperature crosses the set point it increasing the fastest it will move. So now the error has to be negative for a while to bring it back to zero from the other direction. Increasing the gain on the proportional term and keeping the gain on the integral term low will minimize the ripples, but never eliminate them. So the error will always wing positive and negative. You need to keep in mind that if the integral term is high, the heat output will be high. Thinking the set point will only be passed a little bit is not accurate. You need to stop "feeling" the design and try looking at some real numbers. If LTspice is too complicated for you, you can use a spread sheet to make calculations in a stepwise manner. Write the equations for the three controller terms and one for the combined heat output based on the previous values. Write the equations for the heat output (losses) based on present temperature and the intermittent water draw. Write the equations for the temperature of the tank in terms of the previous temperature and the heat inputs and outputs. Copy each of these from one row to the next for many rows and watch the temperature change across the rows. I evaluate DSP algorithms this way. You can even chart the columns to see temperature and the heat flows change and watch the oscillations of temperature and heat output. -- Rick C. +- Get 1,000 miles of free Supercharging +- Tesla referral code - https://ts.la/richard11209
On 27.6.20 07:41, Paul Rubin wrote:
> Richard Damon <Richard@Damon-Family.org> writes: >> 2) It can go to some finite, non-zero state. This indicated neither a >> pole or zero near zero. A proportional controller will have a finite >> error result, so you may want an integral term in the system. > > Slightly tangential but can someone explain if the integral term is > supposed to decay? Otherwise, can't it potentially keep getting bigger > and bigger without bound? E.g. in a thermal system, you have to keep > adding heat to account for losses, or for (say) hot water being drawn > out of a water heater, and being replaced by incoming cold water.
The integral term is supposed to approach a stable value, to compensate for the steady-state error of pure proportional control. What is supposed to decay is the total error term. -- -TV

The 2024 Embedded Online Conference