PID controller: dummy questions

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.

FIRST STEP

Should I set the power supply output voltage to 6V? I think so.

SECOND STEP

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.

Reply to
pozz
Loading thread data ...

Am 25.06.2020 um 19:55 schrieb pozz:

5 V

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 to
Hans-Bernhard Bröker

Yup!

Yup!

5V.

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.

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 to
DJ Delorie

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 to
Richard Damon

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 to
David Brown

Il 25/06/2020 19:55, pozz ha scritto:

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 to
pozz

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.

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.

Brains are not consistent enough to be classified as PID controllers of any kind.

Reply to
David Brown

You seem to be only talking about the proportional control function while t he PID controller has also an integral term and a differential term. To re ach 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. Judic ious use of the differential term will also help to give an even response w ith minimal over shoot.

PID controlers are simple in concept, but difficult to use in practice beca use of stability issues.

Is your application a real design or is this just for learning?

--
  Rick C. 

  - Get 1,000 miles of free Supercharging 
 Click to see the full signature
Reply to
Rick C

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 to
Richard Damon

The realities of thermal control are complex with the heating device introd ucing an element of integrated response in addition to the thing being heat ed. Formal analysis is often impossible. So the resulting design process is to twiddle the PID coefficients until you get a feel for the optimum se ttings.

Alternatively the "plant" response can be measured by applying inputs and a nalyzing the outputs. Complex to do in practice for many systems.

--
  Rick C. 

  + Get 1,000 miles of free Supercharging 
 Click to see the full signature
Reply to
Rick C

Am 26.06.2020 um 10:59 schrieb pozz:

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.

Reply to
Hans-Bernhard Bröker

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).
Reply to
Richard Damon

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.

Reply to
Paul Rubin

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.

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.

Reply to
DJ Delorie

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 temperatu re. After that is achieved by the integral term ramping up as needed the p roportional term minimizes fluctuations from short term variations in the h eat 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 hea t 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 in tegral ramps down until it reaches a minimum with the temperature too low a gain. With the proportional gain set much higher than the integral gain, t hese overshoots will diminish, but never be eliminated. Adding in the deri vative term with the right setting will allow the temperature to ramp up in a critically damped manner without overshoot... in theory. I've always se ttled 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 
 Click to see the full signature
Reply to
Rick C

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.

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.

Reply to
Paul Rubin

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.

Reply to
Paul Rubin

,
.

g.

Yes, you are missing that the error variable can be both positive and negat ive and by definition will balance the I variable to a value that makes err or 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 dra wn 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 poin t 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 er ror. 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 capac itors and resistors. Have a resistor in line with the output voltage from your controller feeding the capacitor and add a resistor to ground to repre sent the heat losses in the water tank. Add a second resistor with a switc h to represent drawing water out of the tank. Then you can watch the volta ge on the capacitor rise and fall as the circuit charges it up and the swit ched resistor empties it out.

The controller can be made of op amps. One with an integrator cap to be th e... integrator. One with a series cap on the input to be the differentiat or and one with no cap to be the proportional term. A fourth op amp will c ombine 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 
 Click to see the full signature
Reply to
Rick C

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 mai ntain the temperature of the take once it reaches the set point. So the er ror will shoot past the zero point and become negative quickly. The integr ator doesn't make the temperature rise to gently reach the set point. At t he 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 ba ck to zero from the other direction.

Increasing the gain on the proportional term and keeping the gain on the in tegral term low will minimize the ripples, but never eliminate them. So th e 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 som e real numbers.

If LTspice is too complicated for you, you can use a spread sheet to make c alculations in a stepwise manner. Write the equations for the three contro ller terms and one for the combined heat output based on the previous value s. Write the equations for the heat output (losses) based on present tempe rature and the intermittent water draw. Write the equations for the temper ature 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 tem perature 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 
 Click to see the full signature
Reply to
Rick C

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
Reply to
Tauno Voipio

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.