Hacker News new | past | comments | ask | show | jobs | submit login
PID Controller: A Simple Control Loop Mechanism (vaclavkosar.com)
115 points by vackosar on May 28, 2021 | hide | past | favorite | 37 comments




I worked with a PID controller in college. It's a concept that's stuck with me to this day.

https://en.wikipedia.org/wiki/PID_controller

Proportional–integral–derivative

P: proportional, this one is obvious. Water is WAY too cold, turn the knob WAY up

I: integral, how long does it take for feedback to take effect. You don't want to turn the knob up and up each second if it takes 10 seconds for the change to take effect.

D: derivative, a bit trickier.

What's really interesting is seeing how humans apply these concepts, which are so intuitive to things like balance, we don't even think about them. In face we'll apply them to where they don't apply or are faulty. You'll see it when people learn to back up a car for the first time or start winning at blackjack.


Your description for Integral is how the Derivative term is used.

Proportional: set the control based on a multiple of the difference between the current and the target.

Integral: adjust that setting based on cumulative difference over time. This handles the case where the proportional setting isn't providing enough of a change.

Derivative: adjust the setting based on how fast the current value is approaching the target value. This provides damping when the error is decreasing rapidly to minimize overshoot and oscillation around the target.

What's interesting is that a lot of controllers leave out the derivative term. Car cruise controls are often only PI, for instance. This works best when it's ok to approach the target slowly anyway, so oscillation is minimal.


> What's interesting is that a lot of controllers leave out the derivative term

This is because a PD controller controlling system X is equivalent to a PI controller for the system (dX/dt). For example, a PI controller adjusting force to control velocity is equivalent to a PD controller adjusting force to control position.

If your system has a strong "momentum-like" term (larger than its damping), you need a derivative term to stabilize it. So a force-position controller needs D, but a room heater does not, because when you stop heating the room, the temperature doesn't keep coasting upwards (usually - there can be some phase lag if your sensor is far from your heater, or if your room is on fire).

A force-velocity controller (cruise control) also does not have "momentum", as you can see from the fact that if you stop applying force, velocity stops increasing immediately.


Speaking of room heater, I remembered when I was studying on control theory in university and learned PID, I was thinking "this thing is so simple and yet so effective, it gotta be everywhere!".

It's a little bit disappointing when I later realized that most of heater/air conditioner basically just use on/off control with a deadband. I guess the limiting factor here isn't the controller but the actuator part, but still a let-down to see them being so primitive.


On-off or "bang-bang" control actually makes a lot of sense for home thermostats. For example consider:

- With a home thermostat you probably want it to reach the desired temperature as quickly as possible. PID will never reach the setpoint as fast as bang-bang control since bang-bang control will always use the maximum possible power.

- PID might help you avoid overshooting the desired temperature but that's not really an important issue for a home furnace. In fact it might be better if it overshoots the desired temperature a bit on very cold days.

- The air in the home acts like a big thermal battery that helps negate the need for a sophisticated controller.

PID works very good for a wide variety of situations but it's important to remember it is not always the best tool for every job. In that way it is kind of like "quicksort".


Well.. that and your heater / ac state is binary. The heater is either on or off, and the ac is either on or off. PID loops require a continuous variable to set.

If the only thing you can do is turn something on and off, a bang-bang controller is the only thing that works.


Although overkill for a heater, you can often pulse-modulate a binary output to simulate a continuous output variable. This can be used together with a PID controller.


That's how AC motors are controlled, but generally wouldn't recommend it for mechatronic systems (EM emittance, wear and tear etc.)


You can often run into equipment reliability issues with this approach because you are constantly short-cycling your systems. I know on HN we’re generally focused on the software aspect but in the real world it’s important to consider the overall system


My point is about how "binary" signals are controlled via modulation to emulate an analog variable for use with a continuous controller, not a recommendation to run your gas furnace this way. (That might have been lost because of the line the conversation followed).

In a real system this is exactly how MOSFETS are used to control a voltage; you would almost never use an analog amplifier for high power because of the losses, so voltage control is binary on/off, but that doesn't restrict you to just bang-bang control despite the GP's statement.

In fact, an electric heater can and often will be controlled via a PWM method with a continuous controller rather than via hysteresis.


Probably will literally kill the heater, haha


Yeah, hysteresis control aka "bang bang" control can also be unreasonably effective.

It's a natural choice when your furnace can't throttle, or can but operates most efficiently at one output level anyway.

Since the system always pushes as hard as it can, it automatically reaches the setpoint in the minimum possible time. It's almost like a PID controller with infinite gain. But the tradeoff is jitter -- all the error gets pushed up to the highest frequency bands. As long as you don't care about that in your system, it's a great choice and effortless to tune.

It can also be a bad choice if there's any cost to changing or toggling the output, like if your furnace wears out from switching on and off a lot.


Many hot water circulating heating systems use bang-bang for the call-for-heat actuation but vary the circulating water temperatures in response to the outdoor temperature (via an “outdoor reset” input to the system).

Properly set, this dampens the swings of temperature in the rooms as the heat gain is more closely matched to the heat loss, increasing comfort and economy of the system.

https://en.m.wikipedia.org/wiki/Hot_water_reset


From what I can recall from my optimal controls course using on/off is actually an optimal controller.

Edit: It's called a bang-bang controller [0]

[0]: https://en.wikipedia.org/wiki/Bang%E2%80%93bang_control


On larger hydronic systems, you’ll occasionally see a PI controller on the mixing valve. The burner is kept near constant and the mixing valve is used to maintain water temp setpoint by tempering boiler water temp


Interestingly, I've seen mechanical on/off controllers with a tiny heater on the sensor, turning it into a time proportioning controller.


Another thing the integral handles is a systematic error in the controls.

For example, suppose you have a vehicle that is suppose to automatically follow a straight line painted on the road. You have a sensor that can tell you how far you are from the line, and you can set the steering angle.

The car is supposed to go straight when the steering angle is 0, but if there is a misalignment so that to go straight you actually have to set the steering angle to some constant non-zero value, if you just do PD you can end up with the car following a fixed distance to the side of the line instead of centered on the line like you want.

Make it a PID controller instead of just PD, and the I term will fix this.


another way to think of it:

P some value multiplied by how far off you are from the setpoint.

Integral term is just that from calculus... adding stuff up over time. some value (gain) multiplied by the error every N (min/milliseconds/seconds) and sent to the output. usually clamped at some value to avoid runaway errors. aka turn up the heat a little more periodically to eventually reach the setpoint.

another math term(derivative being the rate of change of something with respect to something else basically)... the derivative term is supposed to be a way to dampen the output so you don't overshoot it and oscillate around the output value. if you can critically dampen the output you hit the setpoint without overshooting it and quickly. the trick is knowing which to use when because all systems are a little bit different with differing responses to the outputs and some systems are cascading and rely on other pid loops which is another bag of worms.


Another interpretation:

P is a spring (the spring equation is k * delta x, same as the P term, but in terms of the error). Adding a P term will make the controller “want” to eliminate any error.

D term is a mass damper (again, equation is the same). It will fight oscillations, but it is extremely sensitive to noise in your input (calculating discrete derivatives in noisy signals is hard).

The I term makes your output converge to the desired value. Your system will have an inherent friction or stiction which can’t be overcome with just a P term. The I helps with that but introduces other issues like windup.


Saw PID in college and as a course project designed one to control room heating systems. Years later worked on one to control ads spending.


What parameters were trying to control and using as controlling factors using PID on ad spending? Would you have done it differently if you were selling a different product/service?


Ad spending, very interesting application!


Wasn't there an HN article recently which linked to a useful site on PID controller tuning? That was interesting. PID controllers are easy to create but hard to tune.



There's an interesting crypto protocol called Reflexer[1] that uses a PID loop to alter interest rates and incentives to maintain the stability of a stablecoin/debt coupon called Rai.

This allows the asset to remain relatively stable[2] in response to actual market forces. At the time of posting there are over $35m worth of Rai in circulation[3].

[1]: https://reflexer.finance/faq

[2]: https://stats.reflexer.finance/

[3]: https://coinmarketcap.com/currencies/rai/


I've used PID controllers for some things in game development, but mostly for simple stuff like specifying a target velocity.

What I always have problems with, is using pid controllers for angular movements. Like turning an object to a desired angle or approaching a desired angular velocity. Are PID controllers suited for this task? For example: You want to turn right and specify a large angular velocity. But now, after a few turns the integral part adds up to a huge value. Then you want to turn back, but now the response is very slow, because of the large integral part. Never got around this problem. How would you approach this?


1) this is the same problem like controlling the angle of a quad copter, for which I have used PID. You can calculate the error of the position, which is in the range -180..179. Applying one PID stage to the error of the orientation yields a "desired rate of change" - it increases if the error is large or held for a long time and it's sign describes the direction of the rate of change of orientation. You can use another PID stage to give a step count power iteration (how much to turn the object). 2) integral windup is commonly prevented by just capping it. You can also use a smoother capping function which bounds the size of the integral, or lowers it on each iteration.


Capping and/or gradually lowering the the integral part seems like an elegant solution. Thanks for your input! This is something which is never told but should be part of every PID controller resource on the web.


An alternative to capping is to avoid adding anything to the integral term in situations where the actuator is already at 0% or 100% of the possible range.


PID controllers are used to do exactly this with quadcopters. There is a lot of finesse on top to deal with oddities and problems like I term windup. Common ones are zeroing it out when it’s no longer valid or clamping it within specific bounds.

For games you might just get away with a PD controller and not need to worry about the I term at all.


When you've figured out PIDs then its time to try a Linear Quadratic Regulator:

https://twitter.com/The___Missile/status/1378190268520927234


LQR does not include integral action by default


Extending it to LQI is not difficult


I remember using https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method

in college to tune PID controllers in C. Fun times.


I'm tempted to try this for our logistics forecasting. PID seems perfect but it really breaks the KISS principle.


Why it breaks the KISS principle?




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

Search: