*rest*of us.

### PID Basics

PID controls are used to control something. Vague, right? Let's define some things first, by using a super simple example (that actually has some very complex boundary issues, but for 90% of our scenarios is pretty straight forward).The problem? We want to heat up something. Maybe a BBQ pit, or a kettle of water, or maybe a coop full of baby chickens or rabbits. Normal "bang-bang" thermostats are actually horrible at temperature control. They slam the temperature all over the place, like this:

Set an oven temp to 350F. The thermostat turns on the heater element until temp reaches 350, and turns off. This actually means the temp will shoot up to around 360, since the element is very hot, and the process of cooling down means the oven gets hotter. When the temperature hits 349, turn on, which takes a few minutes, so the oven temp hits 340 quickly, until the element gets hot again. That's a 20 degree swing in temp!

Now, lets see how a pretty simple "process control" PID does this. Lets define a few terms and look at a real world example first:

#### Definitions:

**SP**: Set Point - What we define as homeostasis (temp = 350F, speed = 20 RPM, etc)**PV**: Process Value - What the actual value of what we are measuring is (330F, 18 RPM, etc)**PE**: Process Error - How far away from SP is PV? So PE=SP-PV. Easy Peasy.**Pt, It, Dt**- the Term, or each part of the PID controls contribution to the output.**ADC**: Analog to Digital Converter - Converts analog measurements like temp and rpm to a digital number. A 10 bit ADC has 10 bits of resolution, or 1024 discrete "steps". For an ADC that reads 0-5 volts, 0v = 0, and 5v = 1023, and 2.5v = 512.

#### OK, lets look at the PID algorithm now:

**P: Proportional**- In many control systems, the process control isn't on or off, it has a range of states, like a valve that can be on a tiny bit, on half way, or on full blast. That's proportional control. In the PID world you need the machine to determine how much to open a valve (or for on/off things like heaters, how long to turn it on for).*Boiled down even further, the farther away from*The calculation is very simple:**SP**that**PV**is, the higher**P**needs to be to get**PV=SP**.**Pt**=**PE*****pGain**. pGain is described below. Back to the example of trying to get an oven to 350F:**SP**= 350F**PV**= 100F**PE**=**SP**-**PV**: 350 - 100 = 250, so**PE**= 250**pGain**, what?**pGain**is a constant that is chosen so as to scale the control output to a range usable by the ADCs and output controls by multiplying it with the**PE**. Two examples, both using a**pGain**of 2:**SP**= 350**PV**= 100**PE**= 250**Pt**= (350 - 100) * 2 = 500**Pt**= 500 so we need a Proportional term of 500 of some type of control output to get our oven up to temp. Compare that to:**SP**=350F**PV**=340F**PE**= 10,**Pt**= (350 - 340) * 2 = 20, so we need a lot less output from our oven element to get**PV=SP**. The bigger the**PE**, the higher**Pt**needs to be to get**SP = PV**. The secret sauce is the**pGain**: too high, you overshoot the temp, too low, your heater takes too long and cakes don't rise, and chickens freeze to death, and beer tastes skunky.**pGain**is adjusted by you to get the right amount of**Pt**for your application. Start with**pGain**of 1.

Another real world example is cruise control on your car - the cruise makes very slight adjustments when you are near

**SP**, say 65 MPH, but when you are traveling at 35 MPH, the cruise control 'floors' the gas to get to

**SP**as quickly as possible.

**Pt**is the amount of artificial "foot" the controller adds to the gas pedal to get to speed.

Proportional control (

**PE***

**pGain**) isn't enough to maintain stasis on systems like temperature control. Since

**Pt**decreases the closer we get to

**SP**, It will never actually get there on its own. To solve that, we need to add

**It, Integral**, (and later maybe

**D, Derivative**to stabilize).

**I: Integral -**It's a fancy term for cumulative error. It lets the PID loop "remember" previous errors, by summing the errors as the algorithm runs. That means the**I**will change less and less as**PE**approaches**SP**, which means the integrator's value increase will slow over time, meaning the control loop is getting stable. In the previous example of the oven,**I**is 250 on the first pass of the loop, then as the temp rises, the**PE**is added to**I**, so the next pass**I**=**I**+**PE**(10), or 260, and as the**PV**approaches**SP**,**I**stops growing as fast, so less and less**It**is added to the system, causing the system to "settle".**iGain**: is another constant that is multiplied with**I**to get a control output (**It**), which is then added to**P**and sent to the control.

**Integral**can stabilize a system, it can also cause Windup, which is when your error is very high at the beginning of the process loop, like when the kettle is cold, or the motors are off, etc. You can saturate your Integral such that it's outside the bounds of what you can push out as control (heat, speed, etc), so you have to have a safe upper and lower limit for

**I**. The problem with

**I**is it usually decreases no faster than it rose, because the growth and reduction of

**I**is fixed at the sample rate of your PID loop. If it took 500 loops to get over saturated and overshoot

**SP**by a wide margin, it's gonna take just as long to get it back down.

**I**lowers because when you overshoot, your

**PE**goes negative, so when added to each loop,

**I**starts to decrease. i.e. if

**PE**looks like: 10,9,8,7,6,5,2,-1,-2,-3, etc, you see how

**I**grows and shrinks based on accumulated

**PE**). The good part about controlling windup in your control algorithm is to just limit the output to the maximum output of the process that is driving SP. If it's a motor, it may be speed or current, just limit it to the max speed and current the motor is capable of. It's as easy as:

If

**It**> cMAX, then**It**= cMAX
If

**It**< cMIN, then**It**= cMIN.
If your system under control is inherently stable, you can raise/reduce cMin and cMax even inside the lower and upper control limits.

**iGain**is usually a very small number, like .002**D: Derivative****-**if P deals with the current state of the system under control, and I deals with the past performance of the system, guess what D does? Yep, it tries to predict the future state of the system to prevent overshoot. Again, Derivative is a fancy term for a pretty simple calculation:

NOTE: There are two versions of D in controls. The one I'm using is better for systems like heaters, in that it subtractsDt=(CurrentPV- PreviousPV) *dGain.

*control away from the system as the rate of*

**change slows via**

*PV***Pt**and

**It.**The other method of deriving

**Dt**involves the rate of change of

**, which can cause "derivative kick" since the**

*PE***PE**is very high at the beginning of the control loop (when the PID loop is first started). Error is high, but

**PV**change is relatively stable and slow. Subtle yet significant in the beginning of the process.

The amount of change of

**PV**each loop gives you a bit of insight on what the next value of PV is going to be, or its slope/rate of change. For example, if our oven PV was 340 F last loop, and 341 F this loop, what do you think PV will be next loop? Again, easy peasy. The steeper the slope, the higher the delta, the higher probability of an overshoot, which

**Dt**helps correct.

**dGain**: this is the value multiplied with

**D**to do predictive control.

**I**,

**P**are summed, and then

**D**is subtracted, which eliminates overshooting when

**PE**and is very high and as the rate of change slows,

**Dt**subtracted from

**Pt+It**will make the approach to

**SP**even more stable.