Excellent source for PID heuristics. That being said PID control is >100 years old concept and we have done tremendous theoretical progress since then and we have access to fast numerical solvers for constrained optimization.
Look for Model Predictive control, Dynamic Matrix Control, Adaptive Control, or even Linear Quadratic Regulator.
This comment gives the impression that users should consider other methods instead of PID, but this is not the case. For 99% of applications, PID is more than enough. Many robots are just PID control and trajectory planning. If you are so new to control theory that you’d need the linked article, I want to assure you that PID control is 100% the right starting point for you. If you can’t get it to work, bring in a friend/consultant/professor with controls experience, and they’ll tell you if you need another method.
Source: I have a masters in control theory, a publication, and ~5 years industry experience
The problem exemplified in the top comment was perfectly demonstrated by my control theory classes, where we ended up with a dramatically distorted view of how you actually use the damn thing because they didn't teach us PID at all. It was treated as a special case that was trivial to analyse given the other techniques they wanted to teach us, so it got no airtime.
The hardest part of PID for folks to get a handle on is how to tune it, ideally automatically.
People often just twiddle with numbers and get frustrated
For hobbyists, the availability of correct automated tuning algorithms is sparse, even though the algorithms are well known and actually not that hard once you translate "control theory speak" into "computer science speak". Most folks would be better off autotuning for their systems.
I actually wrote and published some auto pid tuning code years ago that uses the relay method here: https://github.com/dberlin/PLC-PID-Autotuner
(It's in PLCopen, but i can give you codesys versions/etc if you want, and converting to something like arduino would take no time, but someone finally made a working arduino library i think)
Nowadays you can find a few more working libraries, but still.
One example: I use a PID to control a vacuum motor on my CNC machine being used to hold down parts, set to achieve a certain amount of PSI in clamping pressure. The system is basically undisturbed for long periods of times, but a hole gets cut in the part and the amount of vacuum needed to maintain hold-down goes up.
This is a very simple system - it needs fast response, but the response is very straightforward - there is a very direct relationship between speed of vacuum motor and hold down pressure. It can also be safely overshot, but not undershot.
(IE if it goes wild and moves the motor too fast nothing bad happens, but if it wildly undershoots it the parts move and get ruined :P)
Simple autotuning methods work great on this kind of system. I watch people twiddle with values, you name it. It takes about 2 minutes to auto-tune a pid here (mostly swapping a part with no hole and a part with a hole) and the result will be better than anything you come up with manually.
There are more complex methods, as GP mentioned, but they won't drive much better results than you are already geting for this kind of system.
The place where PIDs fall down (in systems hobbyists might use) tends to be something like controlling the temperature on a big green egg. They can maintain a steady state to within 0.1degrees or whatever once warmed up, but they get super-confused by the warm-up time. They can also oscillate into chaos very easily because of the lag time between "blowing on fire" and "temperature probe notices result" (which is highly dependent on accuracy and response tme of the probe). Overshoot is also bad because the only way to make temperature go down is to stop blowing on the fire, etc.
The result is that most smoker controllers have a warmup time/phase before they engage a PID, and then carefully adjust to avoid overshoot.
While more advanced methods might control this completely without the need to do the initial warmup phase, it's not obvious to me the complexity is worth it for that sort of thing. You are talking about going from simple loop that you can plot and reason about to often much more complex mathematical formalisms.
It can quickly become an even more magic box that you can't touch.
There are of course, places it is worth it, i just doubt most hobbyists will run into them.
To complement your comment, you anyway need a set of PIDs to control your process to your set point defined by your MPC, so good underlying PIDs are a prerequisites to the higher up algorithms (but unlike you, I am not an expert in control, only dynamic simulation...).
Generally speaking, if you have a horrifically non-linear system you still want good performance on, or you have a very predictable system and enough sensor inputs to predict its behaviour and you want to get better performance than you can get from a purely reactive form of control (though, in a lot of cases like that, PID + some form of feedforward control is almost as simple and can work really well without needing to dive into more advanced controllers). PIDs can control most any system but they are rarely the optimal controller for any given system. That gap only tends to be really large in the situations I've mentioned, though.
>if you have a horrifically non-linear system you still want good performance on
Even in that case PID is often the answer. Only that you vary the P, I, and D factors. It's called gain-scheduling [1]. You linearize your non-linearities at multiple points, and change your PID parameters to match.
Of course non-linearity is a very broad concept. So this may or may not apply to your non-linear system. The class of linear systems is easily described. The class of non-linear systems is everything else.
PID control becomes kinda useless as soon as you have a coupled high dimensional system, think something like a drone with aerodynamics or a robot a high number of DOF. There are of course patches so that you can still apply multiple PIDs but the intuition on why it won't work is that each PID doesn't know what the other PIDs are doing, and this often leads to unstable control actions. Another place where PID cannot be applied is in discrete-event systems (eg network of devices that needs to sync up using messages and do something).
- Almost anything with right half-plane singularities. Although you can balance an inverted pendulum with PID, anything beyond that, PID falls flat on its face.
- Anything where you want high performance. Lag and lead compensation is very often needed. That's not complex to do. It's often necessary (especially in analog design). PID works, but rarely optimally.
- Complex nonlinearities. Although simple ones, PID can be quite robust to (even more so than "smarter methods").
- A lot of systems which are highly multidimensional, and especially where controls isn't cleanly separable from path planning.
It's worth noting, in lower-quality control courses (e.g. a prof who specializes in another field, teaching this course on the side), a lot of incorrect things are taught here. I'm seeing some of that represented in this (broader) discussion.
humanoid robots, eg Boston dynamics. balancing is just too complex compared to a simpler 2-way 2D inverted pendulum, and PID alone just isn't going to cut it.
Let's say you're trying to control the x,y,z coordinates of a robot arm. It has three pivots. Rotating the third pivot can increase or decrease x depending on the first two.
That's a complex, multidimensional, nonlinear system (albeit with relatively simple dynamics). PID won't do it, since you don't even know which way the PID terms should go. There are approaches which mix PID with other methods, though.
A lot of analog systems too, such as some audio amplifiers, use more complex control techniques simply because they achieve higher performance. If you want low distortion, you need a lot of gain in your feedback loop. Most use rather simple controllers, but some go very fancy.
Some have complex linear dynamics. A system with a right half plane zero means that if you push right, the system goes left than right. That's very difficult to control. An example (called a boost amplifier) works like this, in analogy:
You have a bucket of water. You can either turn on a spigot to fill the bucket at a fixed rate, or one to empty it from a hole in the bottom (so at a rate proportional to how full it is). You'd like water to come out at a given rate. What is the ratio of time between filling and emptying?
- In the short term, if you empty for less time, less water comes out, because duh.
- In the long term, though, ALL the water which goes in must come out. If you empty for less time, you fill for more time, so it gets fuller, water pressure goes up, and more water comes out.
The above is actually completely analogous, mathematically, to how a USB device might take a 5V power rail and make a -12V power rail from it. You connect an inductor to 5V, charge it up with a current, and have it drive the -12V rail down.
If you're controlling more complex systems, you might have a mix of all of the above: nonlinearity AND complex dynamics.
Is your nick accurate? If so, you should already know this.
Not only that. The most important aspect is that you can have explicit constraints in the outputs, so that your system does not go outside the operating envelope it is designed for. For example you can state that I want you to stabilize my system given that my motor can only apply up to this force. And by the way do not exceed system velocity of x km/h.
We can also have objective to explicitly define how much more important to us accurate tracking vs stabilization speed.
PID has been a super power in my career. I've used it to solve problems others couldn't even approach (it helps being the only EE in rooms full of CS people). I can claim to have made hundreds of thousands of dollars by just typing in the control loop from my undergraduate textbooks.
Likewise. I have made hundreds of thousands of dollars building entire embedded execution environments designed just to host and run a 25-line PID function .. its surprising just how valuable a well designed PID can be ..
Most recently, as a manager, I had someone on my team use it to control our memory usage in a garbage collector. I've used it to control temperatures in multiple pieces of high-end scientific equipment. I've used it consulting. It's not a stretch to say I have been a hero multiple times.
CS majors (I was EE) aren't generally taught PID, and I have milked my arcane knowledge. It's very profitable to type in the control loop, do some Ziegler-Nichols, and go home early.
EDIT Oh, I forgot the most recent time I've used PID was with the high-school robotics team I coach. We used PID for part of our robot. I didn't go too deep with the students, it's really just a P controller, but it solved the problem, and dramatically simplified the code.
At my first real Engineering job, I used this paper to both understand and implement a PID control system. Then by using the Ziegler Nichols method of tuning, the system was controlled way better than anyone had expected. Since then I have used it a handful of times with minor tweaks and each time it feels like magic when things work well.
The Arduino PID library write up covers more real-world instances such as derivative kick, and set point adjustment. Props to Brett Beauregard for documenting how the code evolves as features are added.
Tim Westcott is a great author and this is a solid article.
I'll add, I've been meaning to write up my own "realities of PID", one day I'll get around to it.
What is NOT readily apparent when you look all these treatments of PIDs is what happens with the poles-zeros of your control-loop (PID) and the interplay with the system or plant.
I have consistenly found all these "it's so simple, integrator removes long-term error, differentiation improves response time" ignores the fact that if you don't pay attentions to the poles-zeros of your overall response you get some very puzzling results.
In a nutshell, understanding the s-domain (Bode) view of your plant and control-loop means you can usually use PID to sovle control-loop problems. But most people don't have a model of their plant (like some of the examples DannyBee has listed) which means you can't easily auto-tune a PID or most control-loops.
Examining the step-response is probably the first clue to your plant, assuming you won't physically break something with that stimulus...
I am new to control theory, since it was not taught in CS curriculum. Other than reading Maxwell's On Governors and Norbert Wiener to get a sense of the history, there is a lot to explore. I have recently been working with ControlSystems.jl in Julia, and can recommend it as a rather intuitive modeling toolkit. Control math can get involved, but there is no reason why the programming should be. There is a package for discrete PIDs, but also for more advanced analyses noted by other commenters.
The topic is "Control Theory." The author's book "Applied Control Theory for Embedded Systems" may be a useful addition if you're going to apply this stuff. I found the book helpful, but I personally didn't find it as clear and easy to read as "PID Without a PhD".
PID control is probably what you'll see in an introductory undergraduate engineering control theory course (which I haven't taken). I think you'd typically study the dynamics using numerical simulation and Laplace transforms. You might think about making a practical project (I made a PID reflow oven controller).
After that you're off to the races with State Space methods (e.g. Chen's "Linear System Theory and Design".) and you can think about problems like stabilizing a quadrocopter based on inertial measurements.
... and so on. PID is nice since it's idiot-friendly.
(And even with a Ph.D and a complete understanding of all of the above, it's often more fun or efficient to be an idiot and just use PID; I've never seen a place where PID was the right answer, but it's oh-so-often a good-enough answer)
Honestly, I wish folks would stop recommending PID as the go-to tool for control design. Like some others have said, it is never the right answer in any non-trivial system. The problem with newcomers blindly incorporating it into their design for quick satisfaction is that they have just done so at the expense of making someone else's job harder down the line. In a system where many things are interconnected or nested, introducing artificial dynamics via feedback in one part often leads to unpredictable and undesired behaviour in another. Then the next junior engineer comes along and thinks the weird behaviour can be solved with more PID loops, repeating the process until you end up with a un-tuneable mess with a precariously narrow operating region.
If you are working in this field, no you don't need a PhD, but for the love of god invest some time into learning more appropriate techniques for the work. Start with learning to work with MIMO systems in state-space, then learn to use LQR (literally a one-liner in MATLAB) and reduced-order observers, then some nonlinear techniques like Lyapunov functions and integrator backstepping for those tricky nested loops. I'm happy to suggest resources for anyone interested.
> Start with learning to work with MIMO systems in state-space, then learn to use LQR (literally a one-liner in MATLAB) and reduced-order observers, then some nonlinear techniques like Lyapunov functions and integrator backstepping for those tricky nested loops
In my experience, there are a lot of applications that are "trivial" enough that PID works fine. I'm reasonably comfortable with state space modeling for systems with more variables. But
OK, I'll bite - always open to picking up something new. What resources would you suggest for someone who wants to learn more about nonlinear controls?
Without knowing any specifics I would say the most universally useful tool to have in your nonlinear controls belt is Lyapunov control design and its extensions (if you're familiar with Lyapunov equations in linear systems that's where the connection starts). It leads to useful methods that are applicable to many systems, and allows you to handle some particularly tricky situations. Wikipedia's articles on these topics are surprisingly decent:
https://en.wikipedia.org/wiki/Control-Lyapunov_functionhttps://en.wikipedia.org/wiki/Backstepping
as well as Stanford's lecture notes:
https://web.stanford.edu/class/ee363/lectures/lyap.pdf
Check out Robust Nonlinear Control Design by Freeman and Kokotovic for more on this.
Compared to linear control theory, nonlinear controls is much more fractured and domain-specific. No concept is as widely applicable as ones from linear controls. Khalil's Nonlinear Systems book is generally considered a top reference, but it does start to get into the PhD-level stuff and I don't recommend it to a non-specialist unless you are really into this stuff. I can give more specific recommendations if you have a particular industry or application in mind.
1) One of the key things in control systems is to deal with uncertainty. If you have a hundred trains, none of them should crash. You want a control system which you can understand to work even in corner cases. ML is bad at that.
2) You need to train an ML. Unless you have a very good model of the thing you're controlling (generating which is usually harder than designing an optimal control system), training an RL means crashing airplanes, burning out amplifiers, or destroying self-driving cars as the system "learns." Even so, when moving from model to reality, it's likely you didn't capture some oddball part of the system.
3) In many cases, there is some give-and-take between control system design and system design. In an extreme case, the PID can be purely mechanical or electrical (e.g. an op-amp). In a less extreme case, if you can design the mechanical system to avoid having e.g. a right-half-plane singularity, controls becomes easier. RL isn't at the level where you can do that kind of codesign.
4) It's a lot more complex.
So "in general," no. In some specific cases, yes.
RL is mostly beneficial for rather complex systems, such as walking robots or anytime a word like "nonholonomic" comes up. For relatively linear, relatively low-dimensional systems, classical methods will give better results for less effort.
One more -- and this comes up a lot -- anything where latency or a delay matters. An analog circuit can have response times in the tens or hundreds of picoseconds. A digital PID on something like an FPGA in the nanoseconds. One on a basic microcontroller (the kind without an OS -- PIC, AVR, ESP32, etc.), in the microseconds.
An RL algorithm running on a general-purpose computer, you'll be lucky to hit tens of milliseconds.
That makes a big difference.
For those unfamiliar with control systems, delay matters a lot. Something like an audio amplifier (where you can't hear anything <20kHz) will generally have a bandwidth in the tens of megahertz, and more is better. A rule-of-thumb is:
In other words, if you'd like to bring down error by a factor of 10,000, your loop will generally like to be around 10,000x faster than whatever it's controlling. If something has a time constant of 1 second, you still might want to be able to react at the speed of microseconds. That's not a hard-and-fast rule per se (I can give all sorts of ways to work around it), but what is true is the more general principle that minimizing delays can be very, very helpful (even for things which otherwise seem pretty slow).
One thing I see missing from some other replies to your comment is a discussion of determinism. That is given the same input, always produce the same output.
Determinism is often valued in safety critical systems. The system can't work _sometimes_. The system can't even work correctly _eventually_. It must produce the desired result in the time allotted. Actuating the thing 20ms too late could be as bad doing it incorrectly depending on the application.
I'm no ML expert, but that seems to be a problem for those types of solutions in some spaces.
It was interesting, and while I will never be in a situation where I’ll have to actually do the maths, I think it’s important to understand that it exists.
The whole concept never really ‘clicked’ until I watched some demonstrations, like this [1] video. After seeing that one in particular, I realised that it’s everywhere… not everything is just a robotic on/off, or a directly proportional response based on feedback from some input.
Yeah I doubt there's much point without insane amounts of data, and if you do have the extra onboard compute a particle filter can probably leverage it more effectively.
Thankfully. The original had some highly questionable design choices: four parallel columns of text (each ~30 characters wide), images inserted mid-paragraph, and code samples in proportional font.
Yeah the title is kinda meh. Teenagers on the robotics team I mentor have figured out PID loops and calibrating them (and how funny robots are when you don’t)
I was on one of those robotics teams, and I recall this document being passed around as suggested reading on that team. It’s probably worth considering that this was written in 2000, when it was a more niche topic, and was probably itself a part of what made PID programming more accessible in the first place.
I mean, Control Theory has been part of engineering cirricula for a lot longer than that. But the internet wasn't nearly as pervasive then, and Wikipedia wouldn't be founded until the next year.
Look for Model Predictive control, Dynamic Matrix Control, Adaptive Control, or even Linear Quadratic Regulator.