
PID Without a PhD (2016) [pdf] - anonu
https://www.wescottdesign.com/articles/pid/pidWithoutAPhd.pdf
======
markjdb
If anyone is interested in their application to system software, the FreeBSD
kernel uses a PID loop to regulate memory reclamation:
[https://svnweb.freebsd.org/base/head/sys/kern/subr_pidctrl.c...](https://svnweb.freebsd.org/base/head/sys/kern/subr_pidctrl.c?view=markup)
[https://svnweb.freebsd.org/base/head/sys/vm/vm_pageout.c?vie...](https://svnweb.freebsd.org/base/head/sys/vm/vm_pageout.c?view=markup#l2064)

It does a pretty good job of maintaining system responsiveness and latency
when there's sustained memory pressure, at least much better than the simpler
hysteresis loops that are commonly used for this sort of thing.

~~~
metaphor
I'll be the first to admit that I hastily presumed you were conflating the
control theory PID acronym with "process ID", then I looked at the code and
had a genuine WT-actual-glorious-F moment.

------
Animats
It's an OK explanation of a PID controller. They're easy to code; tuning is
the hard part. There are lots of approaches to tuning. This author offers a
simple one.

I note that he cites "James Clerk Maxwell: “On Governors”. Proceedings of the
Royal Society, #100,1868".[1]

Yes, 1868. Yes, that Maxwell, of Maxwell's Equations. That paper is worth
reading. Right there, linear control theory was born. And that's about where
the state of the art was until 1950 or so.

Modern control theory is closely tied to machine learning. Except that the
control theory people want solutions that don't suddenly do funny stuff for
some inputs. The math is way beyond me. Even control theory PhDs have trouble
now.

[1]
[https://www.maths.ed.ac.uk/~v1ranick/papers/maxwell1.pdf](https://www.maths.ed.ac.uk/~v1ranick/papers/maxwell1.pdf)

~~~
jbay808
I think there are some important steps between Maxwell and 1950 that are
worthy of mention. Black's paper on negative feedback amplifiers, for one; the
invention of the PID controller itself, for another...

~~~
Animats
Maxwell had P and I, but not D. Derivatives are hard to do mechanically, tend
to be noisy, need filtering, and thus have lag. An aircraft vertical speed
indicator is one of the few widely used mechanical differentiators.

Enough gain to allow throwing it away with negative feedback to get linearity
was a ways off in 1868.

------
ragona
One of my coworkers (colmmacc@ on twitter) has been giving talks [1][2] for
years on how PID loops and control systems theory apply to software, and that
I’m totally convinced that it’s an undermined vein of excellent thought. I’m
working on applying this to a piece of software I’m building and I’m very
excited.

1: [https://www.infoq.com/presentations/pid-
loops/](https://www.infoq.com/presentations/pid-loops/)

2:
[https://m.youtube.com/watch?v=O8xLxNje30M](https://m.youtube.com/watch?v=O8xLxNje30M)

~~~
GlenTheMachine
Controls PhD here...

He’s not wrong, but the fundamental problem is that control theory has a very
limited view of “correct behavior”: stability around a desired point or
trajectory. If you can define your desired system state as a fixed or time
varying value that can be determined ahead of time, and you would like to
prove that your system stays close to that point for some definition of
“close”, then this tool may be for you.

But if you can’t, it’s much harder to see how to apply it. And a lot of
software problems can’t be defined in such strictly mathematical terms.

Also, if your system is linear or approximately linear then we have good
tools. If your system is really honestly nonlinear it gets more dicey. You
have to either hope that somebody has already derived a controller for systems
of the class you are using, or you have to do novel numerical analysis in
order to derive a controller that will stabilize it.

~~~
patrick5415
I did my PhD in controls too, and I’d pretty much agree with all of that.
There is a ton of really heavy math flying around with little to show for it.
If your system is anything beyond weakly nonlinear, you are basically screwd.
The best and effectively only tool we have otherwise was invented over a 100
years ago by lyapunov. And using it requires, I’m not joking, nearly divine
inspiration.

~~~
miketery
Can you expand on what this tool is?

~~~
creatornator
Lyapunov functions indicate Lyapunov stability. A system that is Lyapunov
Stable will settle into a global minimum state and stay there stably
indefinitely. Kind of like a ball in a bowl, where all gradients point in to
the middle. Non linear systems can "refuse to settle". I may be misremembering
something from my controls courses

------
TimWescott
FYI, if you're a visual learner, check out my YouTube channel:
[https://www.youtube.com/channel/UC923b-omXUs0dnWOTDD7FhA](https://www.youtube.com/channel/UC923b-omXUs0dnWOTDD7FhA),
but be warned there's not as much there as I'd like.

I do have all the material from "PID Without a PhD" in there -- just watch the
three videos with "PID" in the title.

I also recommend Brian Douglas's channel:
[https://www.youtube.com/user/ControlLectures](https://www.youtube.com/user/ControlLectures)
if for no other reason than because I haven't posted a new video for three
years -- but then, he's been posting on some Matlab channel, so you'll want to
start with his and then move to that one.

~~~
michaelt
Those look great!

What would you recommend if the feedback is noisy enough that a derivative
term would be mostly noise, but using a simple FIR filter to remove the noise
introduces a delay which makes the system less responsive?

~~~
TimWescott
FIR filters are the last thing you want in closed loop -- for the amount of
filtering they do, they have way excessive phase delay.

I'd use a derivative term with bandlimiting, and I'd accept that if I couldn't
get the response I needed using feedback in that case then I'd either
investigate using feedforward, or I'd work on changing the sensors on the
plant so that they're less noisy.

------
leon_sbt
This is a well explained and concise guide on theory and implementation behind
it.

But like many things, there's no replacement for getting your hands on a real
life system and gaining real life intuition.

The best way to learn this? Get/make a test bench with safe-ish motor and
encoder system. Play with values and measure at the performance in the system.
Increasing the P value gains, makes the system "stiffer" but at the expense of
stability. Put your hand on the flywheel and "fight it" safely. You can feel
the I (Integral) value winding up against your steady state errors. It's quite
magical.

As you start to chase the highest performance for your system, you start to
think about alternative control strategies. Eventually you will dream up the
concept of feedforward control loop. Try to automate the the picking of PID
values, then you start to learn about Ziegler–Nichols tuning method. Gain
Scheduling. Non Linear Control theory. It never stops.

(If you do any of this, please make sure the motor/flywheel won't kill you if
it goes unstable, and please wire a E-stop that's easy to access when it's
unstable)

~~~
pietroglyph
This is how I teach middle school and high school students basic controls.
They implement and tune a proportional controller, then the derivative part,
and then I give them a feedforward model for the plant (usually a brushed DC
motor.) The PD controller and feedforward is _very_ easy to write and is quite
approachable once you see how simple it is, and it can be very exciting to see
a motor “do as you command” before your eyes.

I avoid integral control because it’s less effective and much harder to deal
with than good feedforward. Most of the mechanisms we use (in the _FIRST_
Robotics context) have a feedforward model and good system-identification
tools so this works out well.

------
flyinglizard
I designed control systems for brushless servo motors and more recently did
the power control for very power hungry ASICs of a very well known company.

Tuning is important but when you come to design a control system, you need to
look at the overall system - and this is something textbooks don't really tell
you. Control systems are all about reacting quickly enough and accurately
enough. Delay and poor incoming/outgoing signal quality are the enemies of the
control system:

1\. The delay from sensor to controller (e.g. SPI bus incurred delay between
your gyro sensor and your processor)

2\. The bandwidth of each component. If your gyro sensor is only 10hz
bandwidth, it'd be difficult to control anything reasonably fast. Note that
bandwidth is often given in frequency to 3dB attenuation (half the amplitude)
- that's not enough, you need to understand the phase distortion: in other
words, as you approach the bandwidth limit, does your signal get distorted in
time? because that's very bad for a control system.

3\. The resolution of your actuator - if you end up using an on-off actuator,
it'd be impossible to do any precision control. Do you want to position a
motor accurately? Make sure you use more than 8-bit PWM.

4\. Static/dynamic response: things are usually not nicely linear. Things at
standstill take a different amount of effort to move than once they are in
motion.

My experience has shown time and time again that if you get the system
fundamentals correct, implementing and tuning the control loop is reasonably
trivial. The secret is having a system that's an order of magnitude faster
(higher bandwidth) than what you're trying to control.

You really need to think about control systems in the frequency domain: "what
I'm controlling requires the bandwidth of X, I need to sample with with n * X,
my processor is this fast, my output signal is that accurate, this is the end
to end delay".

Someone once said "any non-linear system becomes linear when you sample it
quickly enough". I don't know who said it or whether it's even remotely true
for all parts of life - for control systems though, it's definitely a rule to
go by.

------
andyjpb
This is great.

It's called "PID Without a PhD" but it still assumes at least an undergrad
level training.

I wish it had been around when I was studying EE-2-Control. That was not one
of my favourite courses but is one of the bits of theory that I have reached
for the most since and this guide has been a very useful reminder of much of
it.

~~~
babygoat
But “PID with an undergrad” doesn’t rhyme :)

~~~
TimWescott
Had I considered that title I would have discarded it for that reason.

------
daveguy
This is one of my favorite PID blog posts. Improving the beginners pid:

[http://brettbeauregard.com/blog/2011/04/improving-the-
beginn...](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-
pid-introduction/)

~~~
themeiguoren
As a controls engineer, I’ve pointed people towards that series of posts more
than once. Great explanations.

------
harry8
Control theory text.

'PID Control The "PID" in "PID Control" stands for "Proportional, Integral,
Derivative". These three terms describe the basic elements of a PID
controller.'

~~~
moron4hire
Classic case of a really simple concept being obfuscated by jargon. If you've
done any sort of game development and implemented smooth movement and
animations, you've probably accidentally implemented a PID controller.

~~~
dahart
I bumped into PID controllers doing game dev. Trying to write an AI actor to
drive a car that was subject to physics is what revealed the problem.

But, I had been writing smooth movement and animation for years and never
needed a PID controller before, or implemented one accidentally. In fact using
animation smoothing is what got me into trouble with the AI driving. In
retrospect, I would call basic animation smoothing a “P” controller, you do
something in proportion to your goal. To make it smoother, you just turn down
the proportion. I tried making the AI driver use a P-controller to steer in
order to stay on the optimal racing line.

When you introduce a delay in the system between input and reaction - which is
what happens when your car is subject to physics, and when steering changes
are rate limited - this is when animation smoothing (P-controller) causes a
really surprising behavior. The lower you turn down the P value, the more
unstable the system becomes. I was confused at first... why would reducing the
input cause the system to go from oscillating to divergent? But when someone
suggested reading up on PID controllers, it all made sense, and I was a bit
stunned that I hadn’t even heard about them during my undergrad CS years.

------
glouwbug
My prof spent 8 whole months teaching us systems modeling and PID mathematics
only to tell us there's no reasonable way to set your PID values... You gotta
guess and check!

~~~
pietroglyph
What? If you have a good model of your system then you can (relatively easily)
turn it into an optimization problem and use a LQR to choose gains for you.
It’s true, this method still gives you knobs you might have to tune (Q and R),
but these are easily conceptualized because they slide the cost of state
excursions and control effort along a Pareto boundary. That means that you can
get _optimal_ PID gains just by saying how much you penalize a state excursion
vs. how much you penalize control effort (e.g. distance from your reference
point v.s. fuel use, or something like that.)

It’s also certainly true that a LQR won’t help you if you have no a priori
knowledge of your system, but for many of the mechanisms we need to control
this isn’t a problem.

~~~
patrick5415
Your whole argument is based around “if you have good model...”. This where
most all control theory falls on its face. Getting a _good_ is hard. For lqr
that model better be mostly linear.

Oh, you system model isn’t first order? Now you need an estimator/Kalman
filter. Or more sensors. That’s just more complexity for questionable benifit,
which is why lqr is beloved by academics [0]. For anything that would be
adaquetly controlled with pid, stick with that. After about 2 hours fiddling
with the knobs, it’ll be close enough.

This whole idea of optimality is based on bad intuition. Which states do you
care about? Why? Is that more valuable than control effort? Why? Who is doing
the economic analysis to determine what ultimately costs us more money? In the
end, this thing are tuned just like pid: you stop when the step response looks
nice. Besides all that, for a lot of systems, and in particular flexible
structures with a lot of states, “penalizing state excursions” isn’t really
useful intuition to begin with for almost all state space represtations. You
are better off with a pid and notch filter.

[0] I decided to complete my PhD in controls so could make statements like
that with at least marginal credibility.

------
carapace
I can recommend also "Feedback Control for Computer Systems, Introducing
Control Theory to Enterprise Programmers" by Philipp Janert
[http://shop.oreilly.com/product/0636920028970.do](http://shop.oreilly.com/product/0636920028970.do)

------
wycx
I have also found this useful:
[http://brettbeauregard.com/blog/2011/04/improving-the-
beginn...](http://brettbeauregard.com/blog/2011/04/improving-the-beginners-
pid-introduction/)

~~~
CamperBob2
This appears to be a really nice series of articles, but wow, what an annoying
format. Just give me a .PDF or single .html page, don't make me click 'Next'
thirty times. :(

He's not even doing that to show ads. He's just doing it because, hey, it's
not like _he_ wants to read it.

------
aguki
In some cases especially hardware, it is _essential_ that PID tuning is done
safely.

I've implemented this reinforcement learning algorithm in C++ for safely
tuning a PID controller on a hardware system with successful results i.e. has
been successfully deployed at customer sites for in-situ tuning.

[https://github.com/befelix/SafeOpt](https://github.com/befelix/SafeOpt)

[http://papers.nips.cc/paper/6692-safe-model-based-
reinforcem...](http://papers.nips.cc/paper/6692-safe-model-based-
reinforcement-learning-with-stability-guarantees)

------
RhysU
[https://github.com/RhysU/helm/blob/master/README.md](https://github.com/RhysU/helm/blob/master/README.md)
is an implementation of such a thing.

------
louwrentius
For what it's worth: I use PID to control the fan speed of my DIY NAS.

It uses the temperature of the hottest drive to determine if it needs to spin
up the fans or not. Works very very well.

The end result is an extremely quiet NAS even with 24 drives.

[https://github.com/louwrentius/storagefancontrol](https://github.com/louwrentius/storagefancontrol)

~~~
carlob
I assembled a controller for my smoker from a cheap PID from aliexpress, a
relay and a fan. It works like a charm and it's an order of magnitude cheaper
than buying a complete solution.

------
JadeNB
I know it must be a foolish question, but Wiki gives too many candidates and
the linked PDF seems to assume you know. What is PID?

~~~
mehrdadn
It's not a foolish question :) see
[https://news.ycombinator.com/item?id=22753980](https://news.ycombinator.com/item?id=22753980)

~~~
JadeNB
Thanks!

------
baylessj
This guide is good at going in depth without being too heavy on the jargon.
I've found that it's still a bit much for younger students learning about the
concept and related math for the first time. George Gillard's intro to PID [1]
has been a staple of VEX robotics instruction because it fills this niche --
it's an excellent resource for teaching high school students and younger.

1\. [http://georgegillard.com/documents/2-introduction-to-pid-
con...](http://georgegillard.com/documents/2-introduction-to-pid-controllers)

------
annoyingnoob
I read this in a magazine years ago, super helpful. I learned the most when I
sat down and worked though it by hand. In practice, I've never needed D, PI
has been enough to do the job.

~~~
ssivark
In the typical pedagogy, D is only necessary if you feel the PI isn’t
responding fast enough (for rapid control). D, being very sensitive to high
frequencies, helps with that.

~~~
themeiguoren
And the reason you’d leave it out is because a PI controller is guaranteed to
be stable with positive gains. Throw the D term in there and you have to start
checking pole positions to make sure your system doesn’t blow up.

------
dang
Discussed at the time:
[https://news.ycombinator.com/item?id=16257156](https://news.ycombinator.com/item?id=16257156)

------
pidtuner
It is very easy to tune PID now a days with free tools like
[https://pidtuner.com](https://pidtuner.com)

------
data_ders
I'm currently in Georgia Techs OMSCS program and taking Sebastian Thrun's AI
for Robotics class on Udacity (free to access). I thought the Lesson on PID
controller was really solid. Here's the first lecture.
[https://www.youtube.com/watch?v=-8w0prceask&feature=emb_logo](https://www.youtube.com/watch?v=-8w0prceask&feature=emb_logo)

------
TimWescott
Heh. My website has crashed due to excessive bandwidth -- I'm working on it.

------
redis_mlc
Linux used to have a bunch of control theory-type heuristics implemented in
the scheduler by Con Kolivas. It was removed quite a while ago.

[https://en.wikipedia.org/wiki/Con_Kolivas](https://en.wikipedia.org/wiki/Con_Kolivas)

------
JCharante
Ah I remember reading this in high school. It's a recommended read/resource
for kids learning to control their FIRST Robotics Competition (FRC) robots in
autonomous situations or non-drivetrain mechanisms.

------
srean
HN folks don't miss out on the older discussion here on HN

[https://news.ycombinator.com/item?id=16257156](https://news.ycombinator.com/item?id=16257156)

------
RMPR
> Bandwidth Limit Exceeded

> The server is temporarily unable to service your request due to the site
> owner reaching his/her bandwidth limit. Please try again later.

------
tuckerpo
This is a very very common control scheme in the embedded world. I'm surprised
this is so new to so many.

~~~
TimWescott
I wrote that article in 2000, and the really scary thing for me is that I'm
now seeing code that obviously either came right out of it, or is only a few
copy-pasta steps away from it that's just taken as "the way things are done".

For the record -- it's _one_ way to do it, but the code in there was primarily
written to be as easy to understand as possible, not to be the World's Best
Controller Implementation.

~~~
mehrdadn
Oh wow, so the article was written in 2000 and not 2016?

And that's funny/scary. Hopefully those cases still end up being an
improvement over not using PID at all?

------
totorovirus
It is quite surprising that this article not related to CS made up to top news
in hacker news. There must be a lot of non-CS engineers here.

~~~
creatornator
PID is very related to CS, especially in the domain of digital signal
processing. And yup, I would agree that there are a lot of non-CS engineers
here, we are pretty common :)

~~~
jbn
any large-ish system is going to have admission control and stability issues,
better design those than leave them to chance...

