[HN Gopher] Tune Your PID Controller
       ___________________________________________________________________
        
       Tune Your PID Controller
        
       Author : tigerlily
       Score  : 204 points
       Date   : 2021-05-25 06:21 UTC (1 days ago)
        
 (HTM) web link (pidtuner.com)
 (TXT) w3m dump (pidtuner.com)
        
       | MrLeap wrote:
       | 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 industry, it amuses me that
       | I might have a leg up in the oil and gas industry.
        
       | jedimastert wrote:
       | I was actually kinda hoping for a game where you could fiddle
       | with dials and tune a toy simulation
        
       | valyagolev wrote:
       | This can help setting up a thermostat, which is super annoying to
       | do if you're not used to it
        
       | SavantIdiot wrote:
       | 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').
        
         | pidtuner wrote:
         | 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)
        
           | SavantIdiot wrote:
           | 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... :)
        
         | somehnguy wrote:
         | 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.
        
       | polishdude20 wrote:
       | 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...
        
       | tonetheman wrote:
       | If only the site explained wtf a PID controller was ...
        
         | pidtuner wrote:
         | For anybody interested in a simple introduction of PID control,
         | I recommend watching Brian Douglas' videos
         | https://www.youtube.com/playlist?list=PLUMWjy5jgHK20UW0yM22H...
        
         | verytrivial wrote:
         | 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.
        
         | sigmaprimus wrote:
         | 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.
        
         | [deleted]
        
         | shawnz wrote:
         | 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.
        
           | hawski wrote:
           | 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.
        
           | Redimo wrote:
           | 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
        
         | sokoloff wrote:
         | Above the fold (and in the _first five words_ of text) is a
         | link to describe what a PID controller is.
        
           | McGlockenshire wrote:
           | > 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.
        
         | lbotos wrote:
         | 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)
        
           | sigmaprimus wrote:
           | Us Billies on the Hill use them in our stills.
        
           | avs733 wrote:
           | 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)...
        
           | cassianoleal wrote:
           | 3D printers use it to control bed and extruder temperatures.
        
           | trench0 wrote:
           | 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).
        
             | blt wrote:
             | 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 :)
        
               | repiret wrote:
               | 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.
        
           | rakah wrote:
           | Yes! I use it to control both the amperage and temperature of
           | a kiln.
        
         | cassianoleal wrote:
         | The "Why" section, pretty much at the top of the website, has a
         | link to the Wikipedia article.
         | 
         | It has been explained.
        
         | perpetualpatzer wrote:
         | 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
        
           | daenney wrote:
           | In the grandparent's defence, I thought it was about a cgroup
           | controller:
           | https://www.kernel.org/doc/Documentation/cgroup-v1/pids.txt.
        
           | amelius wrote:
           | 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)
        
             | [deleted]
        
         | dahart wrote:
         | 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.
        
       | qrohlf wrote:
       | 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.
        
       | naoru wrote:
       | Needs integration with Betaflight/INAV logs.
       | 
       | I'd happily pay for that. Tuning PIDs is tedious and takes away
       | from flying.
        
         | [deleted]
        
         | pidtuner wrote:
         | Developer here, open a detailed issue on Github and will give
         | it a look
        
           | StavrosK wrote:
           | I can contribute some INAV/ArduPilot logs if you want. This
           | would be a great feature.
        
       | juangburgos wrote:
       | 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.
        
         | rhodozelia wrote:
         | 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.
        
           | juangburgos wrote:
           | 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.
        
             | rhodozelia wrote:
             | 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
        
       | leejoramo wrote:
       | 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.
        
         | mhb wrote:
         | You also may want to check out the BrewPi.
        
       | Scramblejams wrote:
       | 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!
        
       | theamk wrote:
       | 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)
        
         | jbay808 wrote:
         | 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.
        
         | pidtuner wrote:
         | True to that, but this tool hopes to provide an educated
         | initial guess.
        
       | spookthesunset wrote:
       | 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?
        
         | volker48 wrote:
         | There is a gcode to autotune the PID
         | https://marlinfw.org/docs/gcode/M303.html
        
       | 5440 wrote:
       | 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.
        
         | taneq wrote:
         | 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.
        
       | culopatin wrote:
       | Might be useful for my megasquirt corrections if I figure out how
       | to use it. Maybe I'll get that OEM idle again
        
       | ChuckNorris89 wrote:
       | 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
        
       | pjkundert wrote:
       | A decent Python implementation of a PID loop controller is
       | available here: https://github.com/pjkundert/ownercredit
        
       | ThePhysicist wrote:
       | 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.
        
         | fho wrote:
         | What a world to live in :-)
        
       | londons_explore wrote:
       | 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.
        
         | pidtuner wrote:
         | 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.
        
           | DoingIsLearning wrote:
           | 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?
        
             | pidtuner wrote:
             | 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-...
        
               | elcritch wrote:
               | 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?
        
           | londons_explore wrote:
           | 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.
        
             | pidtuner wrote:
             | Maybe the youtube videos help with this?
             | https://www.youtube.com/channel/UCkRD7FztiFOdX50BUsOkcSQ
        
         | gugagore wrote:
         | 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.
        
           | salty_biscuits wrote:
           | You can always add an extra variable to your state space
           | model to handle an integrated qty...
        
         | hatsunearu wrote:
         | You generally need some sort of state space/optimal controls
         | theory to robustly solve problems like that.
        
           | dognotdog wrote:
           | "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.
        
             | elcritch wrote:
             | Isn't the windup normally just a min/max type function? Any
             | suggestions for testing an anti-windup code in an
             | implementation?
        
               | gugagore wrote:
               | 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.
        
               | dognotdog wrote:
               | 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.
        
           | pidtuner wrote:
           | The tuning rules from Skoge provide very decent robustness
           | margins. (https://folk.ntnu.no/skoge/publications/2001/tuning
           | paper_ren...)
        
         | [deleted]
        
       | pidtuner wrote:
       | Developer here, ask me anything (wondered why the traffic
       | exploded today until I opened HN).
        
         | dokem wrote:
         | How come you built this cool thing, read hacker news, but
         | didn't submit?
        
           | pidtuner wrote:
           | I did, 3 or 4 times, but was ignored until somebody else did.
           | I don't know how the HN algorithms work.
        
             | usr1106 wrote:
             | HN readers vote.
             | 
             | Sometimes wisely and sometimes less so :)
        
         | dls2016 wrote:
         | 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.
        
           | pidtuner wrote:
           | 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.
        
       | dreamcompiler wrote:
       | 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
        
       | midjji wrote:
       | 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.
        
         | weaksauce wrote:
         | 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.
        
         | pseudosudoer wrote:
         | 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!
        
           | [deleted]
        
       ___________________________________________________________________
       (page generated 2021-05-26 23:01 UTC)