Hacker News new | past | comments | ask | show | jobs | submit login
Tune Your PID Controller (pidtuner.com)
251 points by tigerlily on May 26, 2021 | hide | past | favorite | 107 comments



Buddy of mine did his master thesis as at a biofuel company a decade ago or so. He was supposed to do some advanced filter modelling, but do so he needed to configure the pid regulators to actually save the sensor values, not to mention turn them on. So he ran the pid configuration software, noticed they hadnt been tuned at all, set the values to something not catastrophically shit by just looking at the signals for a few minutes, then collected his data the next few days and went home to work on the thesis. It took a month before they figured out the random thesis worker no one paid attention too was the reason output was up 80%. He got a really nice bonus and job offer as a result. Then he actually tuned the pids using basic but proper system identification, but the largest gain is always from the most basic stuff, like turning the controllers on.


I always love stories like this. When you first join a company who's product you admire, I feel like the implicit assumption is that they know what they're doing. Then after some detective work as a curious noobster you realize that everyone is just as clueless as you are!


Yeah a lot of the plc coders in the field are not too good at the pid tuning stuff actually. some are but it's treated like some dark art instead of a fairly basic thing.


Very few loops need any real tuning. For most of them, an incredibly wide range of value works perfectly well. Systems usually have a lot of slack in them.

It's hard for most field engineers to get experience tuning loops when there are so few opportunities where the time investment can be justified.

This is one of those cases however where field engineers who are around plcs all the time will say "oh I never needed that class" (process control theory) but they never needed it only because they didn't learn it, so they either don't see opportunities to apply it, or aren't assigned the task.

I think this happens in computer science a lot as well. You often can go your whole (very successful) career not needing something you didn't learn, because your career would have been different if you had learned it thoroughly.


So a "random thesis worker" was allowed to change the process config at a biofuel company without approval? Right. If this is true, I'm astonished.


There is a difference between didn't pay attention to, and gave permission.


I would have killed for stuff like this back in university.

We had written some code in an early version of matlab in the labs that implemented the complex math but few of the students actually understood in depth what was going on and why. Everyone else remained puzzled even after the exams.

I went to work in Automotive, implementing various control algos for EVs motor control and later in motorsport and finally managed to understand all the math behind the magic and to mentally visualize the impact and results even before the simulations were run :) Good times


I wish this site expanded a little to model a few things present in most real world systems:

* Limits. Nearly all systems have a maximum and minimum input value. That should be modelled so behaviours when the limit is reached can be modelled. For example, a car has a top speed - and any further pressing of the accelerator will not make it go faster, but a PID loop will keep integrating any error, causing the system to not behave as expected.

* 'Slack/Hysterisis'

* Asymmetric systems - the force applied when pushing a system may not be the same as pulling.

I really want a system where data can be thrown in like this, and it fit a model to the data and suggest not only PID values, but other types of controller which would be a better fit.


Developer here, you are right. I have also considered such systems, but my free time is limited, so for the moment I just implemented the most basic case.

Simulating limits will be included in the next release.

Hysteresis is something more difficult, so will not handle any time soon.

Asymmetric systems can be handled by gain-scheduling the PID (make a model for each region and schedule the gains), this is approachable in the middle term.


While you're here... It would be nice to have examples of common systems with little diagrams.

Eg. "Heater output to get to desired temperature", "Motor PWM to get to specified position", etc. Perhaps each with a little diagram/animation showing the input, the output, and some example data.


Maybe the youtube videos help with this? https://www.youtube.com/channel/UCkRD7FztiFOdX50BUsOkcSQ


Great work!

Could you go into a bit more detail regarding the webassembly implementation in:

pidtuner.github.io/pid/v1.0.6/pid_tuner_wasm.wasm

Is it in C, C++, rust? Could you give a brief description of the internal logic?


Thanks. It is C++ code using Armadillo for linear algebra and BLAS/LAPACK subroutines. Source code is a bit messy, but will try to clean it up and put it on a different repo when I find the time.

The most complex part is the identification algorithm, which uses this algorithm: https://math.stackexchange.com/questions/1428566/fit-sum-of-...


Oh, awesome! I’ve got to do some pid tuning this week. I’ll definitely be interested in the raw C code.

I wonder if it could be portable to a microcontroller Are the blas routines expensive or large matrices?


The code is C++ code, It could be ported to an MCU if there is a fairly recent C* compiler (C11+). But the algorithms are quite expensive, there are multiple matrix inversions, the size of the matrices been as large as the data you put in into the tool. typically between 500 and 10000 data points.


In a world where one can do so much computation, I would really like to see more adaptive control techniques.

The I in PID is the only adaptive part. You can think of it as an estimator of an additive term that arises from e.g. friction or forces due to gravity.

But what about estimating e.g. a multiplicative term? It seems we should be able to do much better than PID, but without needing to commit all the way to a model-based method or reinforcement learning.

I wish there were some materials for how to move just beyond PID, but textbook treatments of LQR or MPC cannot even handle the I in PID.


The problem with many adaptive control techniques is that is very difficult to prove the closed loop stability and performance margins, because such systems are highly nonlinear and we do not have yet the mathematics to provide simple guarantees (for example, I would not get into an aircraft where the control systems have not been rigorously, mathematically, designed)

The integral problem of LQR and MPC is indeed not the first topic discussed on textbooks, but there has been a solution for that for year, called "disturbance model". You you can have an "adaptive MPC" following some design procedures. Check out this reference:

https://folk.ntnu.no/skoge/prost/proceedings/ifac2014/media/...


Thanks for the reference. I agree that the solution to getting I action is to model the disturbance, and augment the state. The system certainly becomes uncontrollable in the augmented state space.

I've run into a conceptual problem when using this approach with [infinite-horizon] LQR to try to recover what looks like a PID controller, which IIRC is due to the control u not necessarily converging to 0. This is expected, to counteract a constant disturbance, but it means that the sum doesn't converge.

I only skimmed the reference, but I did not find a discussion of this issue.


The trick is that, even though the augmented state is uncontrollable, you still use it in the predictions, so the MPC algorithm can still compensate for it. Take a look at the before-last graph in the paper, see how that technique improves predictions after learning the real-time disturbances.


That's not what I was concerned about. The subspace that's uncontrollable is the disturbance components of the state, which I don't care to control anyway.


Not to control, but to compensate. This is what the I action in the PID does, compensate for un-modeled disturbances


You can always add an extra variable to your state space model to handle an integrated qty...


The asymmetric systems example is very true. When I did high school robotics, our robot one year had a large, heavy elevator. I made it so going up used a PID controller and going down was just a constant resistive force (since it was already too fast when falling via gravity).


You generally need some sort of state space/optimal controls theory to robustly solve problems like that.


"anti-windup" is usually implemented for PID controllers to avoid increasing the I part infinitely, and linear state-space controls do suffer from the same problem in naive implementations.


Isn’t the windup normally just a min/max type function? Any suggestions for testing an anti-windup code in an implementation?


Yes, usually just clamp the integrator's accumulator value so that when the actuator is outside the linear control regime (eg. maxing out motor current because we're in the end-stop), the output of the controller doesn't continue increasing to infinity.

(Another way is stopping integration when control output is at its physical limit, usually in addition to a numeric limit.)

As with all other PID parameters, this is something you'd have to figure out more-or-less empirically.

Testing with hardware would be checking that controller outputs stop increasing while the actuator is blocked away from its target value, usually running with much reduced speed/current etc limits. Ideally, of course, run unit tests and SIL, before trying it on actual hardware.


It's a bit more subtle than that.

What saturates is the actuator. You could choose a single minimum/maximum value for the integrator, but it could be better ye to have a value that depends dynamically on how much room there is left in the actuator output.

For example, if the actuator is saturated, the integral term should not be increasing. That could happen at different values of the integral, so there is not a single min/max.


I suggest to take a look to the canonical PID implementation of the Arduino library. It includes a simple but effective anti-windup mechanism:

http://brettbeauregard.com/blog/2011/04/improving-the-beginn...


The tuning rules from Skoge provide very decent robustness margins. (https://folk.ntnu.no/skoge/publications/2001/tuningpaper_ren...)


Before I entered management, I spent about 15 years as an engineer building "greenfield" drug manufacturing sites using automation packages such as Allen-Bradley, Emerson DeltaV and Siemens. There is nothing more time consuming than trying to tune the hundreds of PID loops in these facilities not only during site acceptance testing of equipment, but also during validation. These companies try to offer PID packages but they were always really inadequate. I wouldve loved a tool like this.

One of my biggest mistakes was tuning a loop during the winter shutdown on a large scale vaccine reactor. It dramatically increased the cells growth rate to a point where we would have had to modify our drug application. Understandably, the regulatory people had me de-tune the loop.


Ohh that's the worst. Something's out of whack and you can't fix it because it would mess with some other part of the process.


I have a Lelit espresso machine with a PID and there are many forum discussions where people share their P, I, D values and argue which ones produce the best resuls. I haven't really fiddled with those yet but maybe I should give the article a try.


What a world to live in :-)


Recteq pellet smokers also use a PID for temperature control. Probably not the only grill to do so, but it was one of the first.

https://www.recteq.com/smart-grill-technology


Developer here, ask me anything (wondered why the traffic exploded today until I opened HN).


Can you comment at all about the tuning algorithm you use? Do you monetize this? Currently work on a process control package (for the next few days, anyway) and am interested in your approach.


The tuning algo is the easiest part, the tool uses this one: https://folk.ntnu.no/skoge/publications/2001/tuningpaper_ren...

The difficult part is the identification algorithm, that is the challenging part, to accept all sorts of user data for all sorts of processes.

There is no monetization, users can be patreons, by a mug, a shirt or simply help by sharing the site. I miantain it on my free time.


How come you built this cool thing, read hacker news, but didn't submit?


I did, 3 or 4 times, but was ignored until somebody else did. I don't know how the HN algorithms work.


HN readers vote.

Sometimes wisely and sometimes less so :)


And it depends on the time of day and day of week.


I have used this tool to tune the water level PIDs of several hydroelectric substations. I passed from spending days with manual fine tuning to a couple of hours of a simple experiment and tuning with this tool.


I’ve done level control for about 30 hydro electric power plants but with relatively small headponds or reservoirs, between 20m^6 to 50,000 m^2. You can calculate the gains to achieve critical damping, it was one of the first examples in my university controls textbook.

Getting filtering and update rate right is also important.


For me the most complicated part when doing manually was the delay, because the water from the intake gates do not reach the headponds inmediatly. What I like is that the tool considers the delay in the tuning.


Sounds like a different civil arrangement than I am used to. Is there a link to one of the plants you can send me so I can see the layout? I mostly work on high head run of river plants, but some are an upper and lower arrangement where the trail race of one plant is the head pond of the next


Not sure I can (legal reasons?), but suffice to say thay feed multiple stations with multiple relatively large machines (+10MW). In some cases they have daisy-chained basins, so there are also level inner-loops to be tuned. So the approach has been using the pidtuner for tuning the inner loops first by keeping the outloops in manual. Then tune the loops outwards using the same approach. This has been the winning combination.


Thanks for the details. I have not worked on a plant that had intake gates upstream of a headpond so was curious as to what the layout was. The nice thing about hydro is that just about every plant is unique!


Needs integration with Betaflight/INAV logs.

I'd happily pay for that. Tuning PIDs is tedious and takes away from flying.


Developer here, open a detailed issue on Github and will give it a look


I can contribute some INAV/ArduPilot logs if you want. This would be a great feature.



PID controllers are so fun. I experimented with using them to animate tentacles for a game I'm working on. They're great for all sorts of controllers in fiddly physics based systems.

(https://twitter.com/LeapJosh/status/1327051175112421377)

Judging by some of the posts in this thread, it amuses me that I might have a leg up in the oil and gas industry.


This is really cool! Do you have any other animations? If you don't mind me asking, how did you use PID controllers to animate the tentacles?


My twitter is full of animations (https://twitter.com/LeapJosh) Not many other cool uses of pids specifically.

Each tentacle is rigged to a chain of bones, somewhat ironically. The bones are physics objects I apply torques to. Every node has the same error function, which is "where is the tip relative to the goal?" so, theoretically, they should all be getting torques to help the tip get to the goal.

Turns out this kind of works!

https://twitter.com/LeapJosh/status/1321219814828945411 Each particle there has a pid controller that adjusts the particles angular velocity so it'll fly towards a goal point in the form of a glowing man.

https://twitter.com/LeapJosh/status/1395762486339645443 here's a more traditional procedural animation I did recently that people seem to like.


This is fantastic and (from my POV) out of the box. I've only ever seen control theory used with cloth animation from some Pixar research papers I've skimmed so this really neat to see another application of controls in animation.

Thanks for sharing and keep up the great work


Thank you! I'm always trying to plug things into the wrong things when it seems like they might work in a fun way.

PIDs have been a remarkably versatile hammer for lots of things that aren't nails.


That is the fun part of building things. The novel application of traditional tools can often have surprising results.

> PIDs have been a remarkably versatile hammer for lots of things that aren't nails.

Love it


Woah this is cool! I recently made a little sim for a gimballed rocket that uses a PID loop. It's really helped me tune my real rocket and get some good results:

https://codesandbox.io/s/rocket-sim-final-with-position-cont...


The math behind this is linear control theory (LCT) [0], which is quite interesting. LCT and the Laplace transform techniques at its heart (this is where the "s" terms on the web site come from) are not really that daunting and once you learn them you'll know whether you even need the I and D parts of the PID and what order of each you need.

If we had an analogous "Machine Learning Theory", we'd be able to predict from first principles how many neurons we needed, how many hidden layers we needed, and what architecture (i.e. recursive, convolutional, etc.) we needed to solve a given problem. We don't have that now, but I hope we do some day. The good news is we do have such a theory for control problems (at least the linear ones).

[0] https://en.m.wikipedia.org/wiki/Control_theory


You are right, but the problem of a possible analogous "Machine Learning Theory" is that machine learning problems are highly nonlinear, and control theory has only been "solved" for linear problems. It we could solve nonlinear control that could immediately be applied to machine learning.

“Linear systems are important because we can solve them.” — Richard Feynman


Fair point. But of course the reasons for the nonlinearities are different. ML is not inherently nonlinear; ML is only nonlinear because linear neural nets are not very useful.

On the other hand linear control theory is quite useful for many real-world problems.


Odd. From my perspective, there is no "perfect" PID tuning, so this function is confusing. I fly drones, and I adjust the PID tunings depending on how I'm flying: acrobatics, racing, filming, instruction, etc. Is there a function to tune the tuner? :) What I mean is: changing P impacts responsiveness to the transmitter controls, D affects wind gusts and sudden changes (fast loops anyone?), I is more for racing, its a bit more complex than that but you get the gist. Why pick one set?

EDIT: (a) I didn't actually use the Webapp, after using it, there's more to it than my oversimplification. (b) I'm referring to the rates, not the PIDs. Thanks to the folks below ('pdituner' and 'somehnguy').


That is true, that is why there is a slider in the tool that allows you to trade-off performance for robustness. The bode plot is also an indication on how the closed loop performs in frequency domain, so you would normally choose different tunings according to specific use cases (e.g. some frequency response for tracking and a different one for just regulating)


You know, after I posted I noticed there was a "next" button and started using it (I thought it was GitHub only). You pretty much captured a lot of the tradeoffs. Very nicely, might I add: the UI/UX funneling is clever! I'm off to cram a step function into my flight controller... :)


Are you sure you don’t adjust the rates depending how you’re flying? The PID tune can be objectively good or bad, and is highly dependent on your specific hardware setup. It controls how stable the drone is, you wouldn’t want to change your PIDs based on flying style as you would just be making it fly worse. For different flying styles you adjust your rates depending on how smooth/snappy you want it to feel and the desired deg/s of rotation.


Some flight controllers have two levels of PID. One on rates, and the other on attitude angles. The higher level PID's command mixes into the lower level PID's input, perhaps along side some joystick inputs depending on the configured control mode.


It would be cool if there was like "quick tips" for how to get this data out of various PID'y things. For example how do I get data out of Marlin (3d printer controller) to tune my hotends?


There is a gcode to autotune the PID https://marlinfw.org/docs/gcode/M303.html


It is neither efficient nor optimal and could even be unsafe, but the most fun way to optimize your PID controller is on the actual plant with iterative parameter search.

Watch your device whirr and attempt to jump off your table as your algorithm is trying to optimize the P factor! Quickly estimate how good the fit is just by looking at the actuator!

(Disclaimer: think hard about safety before doing this, and make sure your device is strong enough or your motor is weak enough to ensure that no parts will be flying into people)


We actually did this with a quadcopter for my undergrad capstone project. It had a tendency to wildly flail about so we had to go to the indoor basketball court/gym when no one else was there. It took a couple of weeks, but we got it working pretty well and developed and entire system for the quadcopter to take off, track/follow, and land on a moving RC car. Fun times!


True to that, but this tool hopes to provide an educated initial guess.


In fact, just listening to the system in a quiet room can be a great way to determine whether your gains are too high, much better than simulations are likely to do.


I was actually kinda hoping for a game where you could fiddle with dials and tune a toy simulation


That's literally what you can do with the tool. Did you clicked the "Get Started" button?


Sorry, I meant for more than one dataset.

Also, this tool seems to be geared towards people who already get how PIDs work. It's completely valid assumption to make, I'm just not really in that group.


What code implementations of PID controlling do you know, to learn or base upon, to build an actual solution?

I've successfully used this blog post series from 2011, which covers the principles behind a PID library for Arduino:

http://brettbeauregard.com/blog/2011/04/improving-the-beginn...


Oh man. I wish I had the pages of manual PID tuning notes from when I was doing work with autonomous quadrotors in university to show what this thing replaces. Something like this would have saved me a massive amount of time, thanks for making it and thanks especially for wrapping it up into a nice interface.


A decent Python implementation of a PID loop controller is available here: https://github.com/pjkundert/ownercredit


I never cease to be amazed at the applications of control theory. In an optimal controls class that I took, someone presented a model that they developed to optimize the spread of information in the political space


I have an old commercial electric bakery oven with simple a simple 1 to 10 dial. I have looked into upgrading it to a digital thermostat with a PID, but have never figured out how I would set up the PID. This looks like a great resource.


You also may want to check out the BrewPi.


Perfect timing! Just last night I picked up a half-done hobby project to write a fan controller for some of my server hardware, starting by reacquainting myself with PID theory. This'll be a great help, thanks!


Might be useful for my megasquirt corrections if I figure out how to use it. Maybe I’ll get that OEM idle again


This can help setting up a thermostat, which is super annoying to do if you’re not used to it


If only the site explained wtf a PID controller was ...


This is probably not necessary here but I'll try anyway.

PID is a control loop algorithm: something which takes a desired value and an actual value from a sensor, and outputs something to some kind of actuator to try and get the actual value to match the desired value.

A simple example of a control loop algorithm is the "bang-bang" algorithm used by your thermostat. When it's too cold the furnace turns on, and when it's too hot the furnace turns off.

PID was discovered by observing boat helmspeople. It was observed that there are three major factors in their decision of how to turn the boat's wheel (the P, I, and D respectively).

First, they set the wheel to approximately where they think it needs to be to achieve the desired angle. That's the P, or proportional part.

Then they note the cumulative error over time, i.e. the distance away from the desired angle summed up over each moment that they are still waiting. As the cumulative error rises, they turn the wheel a bit further to compensate. This is the I, or integral term, which accommodates conditions where the controls are particularly dull, causing the proportional control to undershoot.

Finally they observe the rate at which they are approaching the desired angle and compensate negatively if they are approaching it more quickly than they expected. This is the D (derivative) term, which accommodates conditions where the controls are particularly sensitive and the proportional control overshoots.

PID just adds these three terms together with weights chosen for the specific application. Choosing those weights is the tuning process.


I made myself the work and created a very basic ClosedFeedbackLoop Example for java, for anyone that might want to try it out. It's old and has terrible documentation, but I also haven't touched it in quite some time. Either way, here it is:

https://github.com/RicardoMonteiroSimoes/ClosedLoopControlBl...

It contains several blocks,P,I,D,PID,PT1 and PT2, basically the ones that were teached to me


Thanks, I missed this explanation years ago when I studied industrial control systems! I truly appreciate it.

Though I wonder if that's me that got older and have better understanding of things that I now think I get it, or is it your explanation. Probably both.


In its defense...if you need to tune one, you probably know the acronym.

It's a common feedback control scheme for signal processing and engineering control more generally.

[0]https://en.m.wikipedia.org/wiki/PID_controller


You are right, but it would be nice to have a step-by-step explanation of how the tuning works. A black box is somewhat unsatisfying.

(And of course it doesn't need to explain what a Laplace transform is, as this is aimed at control engineers)


In the grandparent's defence, I thought it was about a cgroup controller: https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt.


Above the fold (and in the first five words of text) is a link to describe what a PID controller is.


> Above the fold

The entirety of the "fold" on my screen is an absurdly giant and seemingly irrelevant image, overlaid with the text "Tune your PID", "It has never been easier", and two buttons.

As I scroll down, the next thing my eyes catch is some mathematical nonsense, which tells me exactly nothing other than that this is a complex thing that is surely entirely over my head.

To people that know what a "PID controller" is, it's probably informative. I came in assuming that there was some sort of controller for OS program PIDs and was unable to grasp the context at all.


https://en.wikipedia.org/wiki/PID_controller#:~:text=A%20pro...

Heavily used for industrial automation, hackers here may know it from espresso machine hacking. (and I'm sure manyyyyy other uses)


Human beings are actually FASCINATING examples of PID controllers...especially as we learn.

I'm soon to become a dad for the first time and so my partner and I have become much more conscious and aware of babies around us and watching them learn things. It's really fun to apply control theory to that...

A friend has a young child who is learning to feed themselves. I was chatting with the friend while we drank beer and he helped the child eat dinner. using hands to pick up food (watermelon chunks) and bring it to mouth - very conservative and lossy PID-esque model of action, but you could watch small tuning improvements in real time. Then a spoon was introduced for sweet potatoes and the kid's PID controller clearly could not adapt to the change in mass in the system and we watched huge overshoots and control failures. Then it slowly improved...but isn't yet 'good'

then we tried to explain this to our partners (both reside in the 'S' in STEM)...


Can confirm. We use PIDs in espresso machines to control the temperature of the water used in brewing. Some machines have simple thermostats that over/under shoot ideal brewing temperature and a PID will help keep a consistent temperature or even let you override the temperature (if your machine runs too hot or too cold).


Do you have any insight on why PID controllers are used for this application? PID assumes a system with a continuous control input, but water boilers usually have an on/off switch. Is the input something like a PWM value?

My temperature controlled kettle does a way-more-than-optimal amount of switching when it's near the setpoint. That's consistent with using a PID controller into a PWM input.

Maybe there is a market opportunity for a water boiler that uses control techniques better suited to on/off inputs :)


A resistive electric heater can be PWM'ed to provide variable output. If you want to maintain a narrow temperature band, you can do that better by feeding the water temperature into your PID loop and have it control the PWM duty cycle, rather than to a simple on-off thermostat.


Yes! I use it to control both the amperage and temperature of a kiln.


3D printers use it to control bed and extruder temperatures.


Us Billies on the Hill use them in our stills.


For anybody interested in a simple introduction of PID control, I recommend watching Brian Douglas' videos https://www.youtube.com/playlist?list=PLUMWjy5jgHK20UW0yM22H...


BPS.space of 'model rocket version of Falcon Heavy' fame has a very good overview/introduction as part of a lecture to other model rocket builders: https://www.youtube.com/watch?v=4cw9K9yuIyU

It's a dynamic control loop.


A PID controller is a suspension system for data. And just like the shocks in your car or mountain bike, the components need to be tuned to make the ride smooth. PID controllers are used in ovens and thermostats and brushless motors and all kinds of things you probably already use.


The "Why" section, pretty much at the top of the website, has a link to the Wikipedia article.

It has been explained.


TL;DR over simple answer, it smooths the output by ramping up and down power supplied by predicting what will happen in the future based on history.

Eg: The water is about to boil so I am setting the burner to medium from max to keep it at just below a rolling boil.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: