# PID question

Discussion in 'Electronic Design' started by [email protected], Nov 22, 2006.

1. ### Guest

I'm working on a linear bench power supply using a microcontroller. My
plan is to have a DAC set the output voltage, which will be regulated
by a simple op-amp circuit.
using this value to provide current limiting, via a PID algorithm
controlling the set voltage. Some general PID tuning advice in a
Microchip PDF says to first set Kp (with Ki and Kd set to 0) so the
system is as stable as possible.

With only Kp set, the voltage output just follows the error. Not too
helpful. Things got better when I added Ki. I'm not sure if I need
Kd. This seems like a somewhat difficult problem because anything can

I've searched the Internet, but nobody seems to be using PID in this
way. I'm sure it is being done, however. What is some general advice
on tuning this PID system? So much information out there seems to be
related to motors and heaters.

2. ### John PopelishGuest

In general, I think a current control loop has to react much
faster than a voltage control loop. One way to accomplish
this is to have two PID controls, a fast one that controls
current, and a slow one than controls voltage, by modifying
the setpoint for the current loop. This is called a cascade
controller (a single output controlled by two measured
variables, one slaved to the other. You implement current
limit by having a low selector at the current controller
setpoint. In other words, the actual current controller
setpoint is the lower of, the current limit value, or the
output of the voltage controller. Both the setpoint for the
voltage controller, and the current limit value can come

As to tuning the controllers, a visual method is quite
effective, if you have a load stepper that provides the
loops with clean, repeatable steps that you can sync a scope
to. If you need help with the tuning concepts based on
observing the time response of a PID control loop, I wrote a
tutorial (focused on industrial process control, but
applicable to any PIC controller) available at:
http://www.tcnj.edu/~rgraham/PID/popelish.html

You would tune the current controller, first, with only the
limit setpoint in effect, then tune the voltage controller
supplying the setpoint for the current controller.

3. ### GenomeGuest

Seriously...... PID is for shitboys. Proportional, Integral,
Derivative....... these are terms that relate to the response of various
things in your loop. Since you are dealing with a loop you should analyse it
to determine its control to output behaviour. Then you can decide what sort
of sums your microprocessor should do so you can achieve a stable loop with
'optimum' performance.

Fiddling about with Kp, Ki and Kd is like an old fat bloke fiddling with his
knob. He can't see what he is doing, it's a source of frustration and
nothing good happens.

Let's say the input to the power section of your supply is effectively a
source follower. That means that whatever voltage you put on the input
appears at its output.

The control to output response of your power stage, dVpout/dVpin, is 1V/V.

Now things get a little tricky but not too much. The chances are, and if you
haven't then you will, that the output of your supply has some filtering
capacitance on it. Let's be nice and assume it is a pure capacitance.

Let's be nicer and assume that you are going to cross over your loop at a
frequency where that capacitor is effectively a short circuit. It also
dominates whatever the load might be.

You sense the current at its output with a sense resistor. (perhaps using
one of those nasty analog differential amplifier things). The voltage across
the sense resistor works out to be dVrsns/Rsns. Because your capacitor is
shorting things then dVrsns is the output of your power stage so that makes
things dVpout/Rsns.

Multiply things together and you get an output from the sense resistor in
response to an input to your power stage of dVrsns/dVpin = 1/Rsns. If you
have a differential amplifier in there then you can include its gain to get
dVcdiff/dVpin = Gcdiff/Rsns.

If your 'pass' device was not a follower...... then you might write
dVcdiff/dVpin = GpowGcdiff/Rsns but then you would have to start worrying,
and you do have to, about just what Gpow really is.... You also get to worry

Anyway..... surprisingly you have managed to define the control to output
response of the stuff external to your microprocessor as dVcdiff/dVpin =
Gcdiff. In this 'ideal world there are no frequency dependent terms so it is
flat from DC to light (or even gamma).

That means that if you set Kp in your software to some value and your
digibollocks stuff was also ideal then the system would never cross over.
Gauranteed to be stable but unfortunately something in the real world is
likely to bugger things up. I'm not a ditigal jock but I am sure there are

So setting Kp to some value that seems to work and then fiddling with Ki and
Kd is like pissing in the dark with a deaf freind holding your knob.... At
best you'll get wet trousers. What you want to do is force the loop to cross
over at a frequency that you define that might also take care of other
problems that might be lieing about the place.

In this case you don't want to be fiddling about with Kp. You should be
messing about with Ki....... Don't forget all of those K things are crap
because they are simpleton mong for 'I'm a digital Johnny and I don't know
what I am doing'. They do relate to the 'real' analog world but only if you
express them proper like.

Having said that it's not as if you don't get analog Johnnys wibbling about
in the same way!

Ki is an integrator which means its response falls at 20dB/decade and it has
a constant 90 degree phase lag. This is cool because if you add one of those
then your loop will cross over at some frequency and its phase margin will
be 90 degrees so it will be funky and stable......

So, all you have to do is pick your crossover frequency.

Having taken the piss a bit that leads me into the digital domain so I will
now flap about the place. You are that bloke with the 100Ksps in a DSP
thing.

I'll be a bastard and assume that it is also doing other stuff so, in fact,
you can only service stuff at 20KHz and then, because I'm not too hot at
digital sampling that is going to make your cross over frequency 5KHz. Of
course, if you know better you can adjust the numbers.

Putting in some hypothetical bits. Let's say your current sense resistor is
100mR and your differential amplifier has a gain of 10. Surprisingly that
works out as a control to output response of 1V/V or 0dB. So, if you want
your loop to cross over at 5KHz, you need the gain of your integrator to be
1 at 5KHz as well.

I got ahead of myself. Part of your digital stuff will be the A/D convertor
that looks at what Vcdiff is doing and the D/A converter which is going to
generate dVpin. I suppose this is why they use 2.56V references. Let's say
the numbers that come in and go out are the same as the voltages they
represent.

If they were not then you would have to add Kad and Kda as gain terms for
your converters. Shit!, I have just started making up K terms. However these
are defined K terms and mean something...... I know.... I shall call them

Now, on to the stuff that will hurt my head. Ki is an integrator so what
you'll be doing in your microprocessor is adding up the difference between
the output of your D/A and the set point... integrators sum things.

Now I get the chance to look stupid.

In analog terms I would have an inverting op-amp configuration with an input
resistor, RIN, and a feedback capacitor, CF. I can pick RIN as 10K and then
work out what CF needs to be to give me unity gain at 5KHz..... That's when
its impedance is 10K which gets me a value for CF as C=1/2pifR or 3.18nF.

If I put 1V on the end of RIN then the current through it is 1/10K or 100uA.
In one second CF will integrate that charge up to It/CF or 314V, give or
take a bit.

OK, so the A/D doesn't do exactly what I said before. Let's say that 1V
works out to be 100. You get to sample that 100 20 thousand times in one
second and the answer is 2 million.

Frightening stuff but hopefully the loop will sort things out so things
don't get too big and you can probably do something else in software to take
care of the blorks.

Strange to say, linear loops have the same sort of problems.

Ho-Hum.... so in order to make my digital integrator behave like my analog
integrator I have to divide its answer by 6369 before I shunt the answer out
to the D/A converter.

At this point my head blows up....... It seems right and I think it is but
there has got to be some hard thinking about how the numbers play the game
as the loop is controlled.

There you go, quite simple really.

PID...... shit more like

DNA

4. ### JoergGuest

The task at hands is going to be similar to motors and heaters, just in
different timing ranges. One of the engineers here in the NG (Tim
Wescott) has written a nice article about it:

http://www.embedded.com/2000/0010/0010feat3.htm

5. ### Guest

Wow, that's a lot of stuff. I hate to say it, but I don't even
understand what a "pole" is. PID wasn't all that interesting to me in
college. Now I might look at it a little differently.

6. ### Jim ThompsonGuest

A "pole" is someone native to Poland ;-)

...Jim Thompson

7. ### Guest

Keep in mind the DAC provides a reference for a regulator, so I'm not
digitally regulating voltage. I'll be able to set the voltage via a
user interface (as well as current), but the PID controller will have
the capability of forcing the voltage lower to meet a current setpoint.

Does your response still apply? I'm a bit confused about how to apply
maximum value the accumulating error can reach, the "I" part.
Digitally, the output voltage can be set 0 - 2500. Right now, the
accumulating error is limited in software from exceeding +/- 28 000;
kind of a random value I chose. Is there a methodology to setting this
limit? It seems to me now that +/- 28K is a little high and will cause
sluggish response after settling into a "maximum error" condition for
long enough to hit this max.

8. ### JoergGuest

Hello Jim,

[ ... ]
There are actually two poles. The one in the north is used to mail your
Christmas wishes to ;-)

9. ### John PopelishGuest

If you picture the 2 dimensional (one dimension is
sinusoidal frequency, one is exponential) frequency response
of a system as a surface, a pole is a point on this surface
that has infinite response. If you think of the response
surface as an elastic tent canvas, the point of infinite
response can be imagined as being propped up by a pole of
infinite height. Hence, a pole.

10. ### John PopelishGuest

Understood. In PID controller terminology, the DAC produces
a setpoint for the voltage regulator. The regulator acts as
a follower to copy the reference as the voltage output
(possibly with a scaling factor).
I'm not sure what you are talking about, here, but it sounds
a lot like preventing integral wind up in the master control
of a cascade control strategy (the voltage control, in this
case) when it loses control of the slave (the current
controller). In effect, you need some way to limit the
error at the input of the error amplifier, once the output
ceases to have an effect (the moment the current limit
process takes over), so that voltage control resumes
smoothly and quickly, once the current falls below the limit
value. Limiting the integral windup causes the voltage
controller output to match the current limit setpoint at the
moment that the current falls below the current limit value.

11. ### GenomeGuest

No worries, I haven't explained too well.... especially when it got to the
digital stuff. You probably didn't pay much attention to PID at college
because it wasn't related to the real world. Certainly when you get people
suggesting that you fiddle bout with things and look at the response you
have to guess that they are guessing.

However you'll probably find the systems they are dealing with are extremely
complex and any answer will do. That's not to say that you should not try
and analyse your system.... You most certainly should. If you can you'll get

I'll try to slap together a web page.

Ultimately it is sort of simple stuff if you have a method and it gets
easier if you have the tools to visualise it. Get hold of LTspice

http://www.linear.com/designtools/softwareRegistration.jsp

And don't be ashamed to use it as a tool to model the system you are dealing
with.
Many systems can be broken down into and analysed as electrical/electronic
models.

Back in the bad old days hard men wrote big equations that don't make much
sense without a lot of experience. If you are just beginning then it is a
great help to see what is going on. Once you are over the initial hurdle you
will be able to do it in your head or on a bit of paper. You just need to
get over the first hurdles.

DNA

12. ### Guest

Yes, integrator windup, I just read about that after I wrote the email.
That part I now understand. But I'm unclear about a couple things in
the topology. Here's a drawing of how I understand it:
http://www.yourfilehost.com/media.php?cat=image&file=Drawing1.jpg

I wasn't sure about where the feedback goes. One problem, if the
feedback path is correct as I have it, is that my current range is
0-2500 and the current range is 0-4000. Maybe it just has to be scaled.

13. ### John PopelishGuest

The block you call Voltage setpoint would be one DAC output.

The block you call Current setpoint would be the second DAC
output.

The PID current control drives the pass element that

The measure of load current is the feedback to the PIC
current controller.

The measure of voltage across the load is the feedback to
the PID voltage controller.

Does that make more sense?

14. ### Guest

Well, not really. I was going to have only one DAC that provides the
ref voltage for the regulator, then use that, along with the current
sense value, to limit current. Remember the PID is being done in a
micro, and the only analog I/O is one DAC out and one A/D measuring

15. ### Tim WescottGuest

Joerg beat me to it. That article will, indeed, tell you how to tune a
PID loop.

In this case I would seriously consider either:

(a) Use the DAC to drive a transistor amplifier, probably a gain stage
followed by an emitter-follower, and use dual PID loops for current and
voltage control. This has the advantage that you can do a lot to smooth
out the transition from current to voltage control and back, but the
disadvantage that you'll want to sample _fast_ to make it work well.
Its accuracy depends on the DAC and its voltage reference, of course.

(b) Use the DAC to drive a command to a 723 regulator, and use the 723's
current limit. This will (potentially) give you faster response and
immunity from software glitches, at the expense of a non-adjustable
current limit.

check out my book (see below). I think you'd do fine with that article
for starters, but if you really want to make something that's production
quality you'll want to understand the material in the book.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html

16. ### Tim WescottGuest

That's just what Kp is supposed to do. By itself it reduces the error
but doesn't eliminate it. The point of increasing Kp by itself is to
find out just how high you can push it before the system starts
misbehaving. Once you've found that level, then you start increasing Ki.
Almost every regulated power supply on the planet uses a PI or PID loop.
It's just so firmly buried that you don't find it in a search, just by
looking at the details.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html

17. ### John PopelishGuest

I lost some of those details.

So the DAC provides the analog signal representing the
output of low selector, (the setpoint for the analog PID
measurement into the software PID voltage controller.
You will still need an analog current measurement to provide
feedback for the analog PID current controller.

The only question left in my mind is whether or not the
micro has the bandwidth to perform the PID voltage
controller and low selector with adequate response.

18. ### Guest

that reads load voltage, which is a little redundant. The micro is 16
bit, with a 100ksps A/D and a some DSP type functionality. It seems to
handle it ok; it's just that some loads causes the voltage output to
jump all over the place. For example, pure resistive loads are fine.
If I put a small motor on as a load with some current limiting, it has
problems. The voltage output jumps all over.

19. ### John PopelishGuest

The one that reads current cannot be involved in the analog
current regulation, without slowing it down, though it is
fine to use it for monitoring the average current for other
purposes. You will have to use the analog signal it
digitizes as the feedback for the analog PID current regulator.
Every regulator has an upper frequency limit. You have to
decide what is fast enough. If you add capacitance across
the output, it can help stabilize the voltage for
frequencies higher than the regulator can keep up with, but
then you lose the high frequency response with the current
limit. This is a trade off.

20. ### Michael A. TerrellGuest

And some "poles" are EEs, as well. A friend of mine that dies a few
years ago was Polish, and served as a Merchant Marine Radio Operator
during WWII. he was aboard one of the first ships to arrive at the ports
of Japan after the two nuclear warheads were dropped.

--
Service to my country? Been there, Done that, and I've got my DD214 to
prove it.
Member of DAV #85.

Michael A. Terrell
Central Florida