# PID

PID is **P**roportional – **I**ntegral – **D**erivative control. It is the very commonly used in process control, heating control and motion control.

The PID function calculates the “error’ value between a measured process variable and the desired set-point. It adjusts an output in an attempt to minimize the error. This adjustment occurs on a regular period. Each time an adjustment is made, it takes into account the magnitude of the error (Proportional), the accumulated total of the error over time (Integral), and the rate of change of the error (Derivative).

If you are new to PID there is a good description of PID on Wikipedia.

In vBuilder, **PID control is a background task**. That means that you Start it and it will continue to run in the background, constantly calculating the error, integrating it and determining its rate of change and updating the output based on these factors combined with the constants that you set up. Once started, the PID will operate until it is Paused

The basic PID equation used in the PID function of vBuilder is :

O = P*E + I * ∫ E dt + D * ΔE/dt

where:

- O: Output
- P: Proportional constant (sometimes referred to as the Gain)
- I: Integral constant (sometimes referred to as Reset)
- D: Derivative constant (sometimes referred to as Rate)
- E: Error
- dt: change in time

### Start/Continue

PID only has to be started once. It will continue to operate until it is Paused.

Start/Continue uses the following parameters:

**Output:**This is what the PID is actually adjusting. The Output should have a direct impact on the value of the Process Variable. For example, a butterfly valve controlling the gas supply to a boiler’s burners has a direct impact on the temperature of the boiler.**Output Max:**The maximum value allowable for the Output. Regardless of the result of the PID calculation, the Output will be restricted to no more than this value. [any variable type except Bit and ui8]**Output Min:**The minimum value allowable for the Output. Regardless of the result of the PID calculation, the Output will be restriced to no less than this value. [any variable type except Bit and ui8]**Process Variable:**This is the measurement of the parameter that you are trying to control.**Set Point:**This is the desired value of the Process Variable at this time**Input Max:**The maximum value of the Process Variable measurement that will be used for the PID calculation. If the actual measurement goes above this value, this value will be used.**Input Min:**The minimum value of the Process Variable measurement that will be used for the PID calculation. If the actual measurement goes below this value, this value will be used.**Proportional:**The constant multiplier to the error.**Integral:**The constant multiplier to the integral of the error.**Derivative:**The constant multiplier to the derivative of the error.**Sample rate:**The amount of time between each PID calculation/adjustment, in milliseconds.**Freeze bias:**This is a selectable option. If selected, it is used to limit the impact of integral portion of the equation to no more than the entire Output range. This is useful in preventing overreaction after a time period where some factor prevented the actual control of the Process Variable [which could possibly result in a huge integral value].

When you Start a PID, it will continue operating at the defined sample rate, behind the scene. In other words, **once you start it, you don’t need to continue to execute PID Start/Continue blocks for operation**. It doesn’t hurt anything if you do. Its just not necessary.

### Pause

A PID Pause block stops the PID from operating.

Select the Output variable of the PID that you want to pause, as shown on the right.

### Reset

A PID Reset sets the Integral value to the value required to produce an Output equal to the Reset Value. You should do when starting a PID. The Start/Continue does not initialize the Integral value, because it does not know whether this is the actual Start or whether it is a Continue. When you place a PID Reset in a flow chart, select the Output variable of the PID that you want to reset, as shown on the right. Select the value of the PID output to start the PID with.

### In Ladder: How to execute tasks only once at startup?

Useful for defining the starting value of certain tags or for background tasks.

Note: Since some tools are** background tasks**, it is not necessary to run them every PLC cycle. If you run it every cycle, the tool will not work properly because you are constantly configuring it.

Just add a "BIT" that will be turned ON right after.

All your lines that only need to start once at startup :
- must be behind the normally closed "ColdStart" contact.
- before the line where the "ColdStart" coil is set to 1.
Note: You can use any name for the ColdStart bit. |

### PID with digital output?

It’s not useful to connect a PID to a digital output, because the PID produces a continuous (versus discrete) signal.

You need some way to make that continuous output relevant to the hardware components. This is most commonly done with PWM – pulse width modulation – in which the output is turned on for a varying length of time depending on the PID output. However, some hardware components don’t like being switched on and off quickly, so you may need to limit the rate at which the valve switches (by running the control loop at a low frequency).

https://www.support.aceautomation.eu/knowledge-base/subroutines-to-download-for-vbuilder/ See “PID to PWM conversion”

With the digital output, you will lose some of the advantage of the PID. You have to compare the output for on/off with the set-point. Essentially, it often takes a long on/off cycle time to protect the hardware components.

### A short history to understand the principle of regulation

Imagine you are driving your car on the motorway and you want to stabilise your speed at 130 km/h.
In practice, you can do this without too much trouble. In this example, it is important to understand how your mind intuitively regulates your speed. |

#### Proportional Action

First of all, the most intuitive thing you do when you want to drive at a certain speed on the motorway is to say to yourself: «the slower I drive compared to the speed I want to drive, the more I press the accelerator pedal». The pressure on the accelerator is therefore proportional to the mistake you make. That is, proportional to the difference between the desired speed and the actual speed.

With this method, if you are at 50 km/h, you press the accelerator pedal down fully. Your speed increases rapidly and approaches the speed limit. Once the speed reaches 130 km/h the error becomes zero, so you let go of the accelerator. Then, due to the inertia effect of the car, you have exceeded the set speed.

When you let go of the accelerator, the car starts to slow down until it falls below the set speed again. So you start to press the accelerator again, and then more and more as you go along to try to reach 130 km/h. Finally, you manage to stabilise your speed at a lower speed than the one you have chosen, say 120km/h.

Proportional action allows the speed of response of the process to be influenced.
A good compromise must therefore be found between speed and stability. |

#### Integral Action

That’s when you think: «Damn! I can’t get the speed I want. I have to add another rule to my reasoning! » . So you decide that if your speed stays below the target for a long time, you will accelerate more and more. So you decide that in addition to accelerating in proportion to the error made, you should also memorise this error over time. The greater the overall error, the faster you accelerate.

So, when you stabilise your speed at 120km/h, the overall error increases and you start to press harder and harder on the accelerator until you reach 130km/h. and exceed it! Indeed, at 130km/h, the overall error is positive, so you continue to press on the accelerator.

Once you reach 130km/h, the error is negative and therefore the overall error decreases. You therefore lift your foot off the accelerator more and more strongly until you return to 130 km/h. At 130km/h, the error is negative again and you continue to decelerate. So you accelerate again and again until you finally stabilise at 130km/h after multiple oscillations.

The integral action cancels out the static error (the difference between measurement and setpoint).
Here again, a good compromise between speed and stability must be found. |

#### Derivative Action

At 130km/h, you think: «I’ve got it! But I wasn’t very efficient… Shouldn’t we add a third rule to be more efficient? » . That’s when you decide to anticipate your speed. The closer your speed is to the optimum speed, the less you accelerate and the closer it is to the optimum speed, the more you accelerate!

So, if you are approaching 130 km/h quickly, you will be quick to lift your foot off the accelerator so that you do not exceed 130 km/h too quickly. So by anticipating, you reduce the oscillation phenomenon and you quickly stabilise at the desired speed!

There you go, you have intuitively made a PID type regulation!

The derivative action is anticipatory. It adds a term that takes into account the rate of change of the deviation. This makes it possible to anticipate by accelerating the response of the process when the deviation increases. On the contrary, it slows it down when the deviation decreases.
Here again, a good compromise must be found between speed and stability. |

### How to tune a PID? (Proportional, Integral, Derivative)

#### Manual method of setting PID parameters

- Set the Integral (I) and Derivative (D) values to zero.
- Then increase the Proportional gain (P) until the output oscillates.
- Then increase the Integral gain until the oscillation stops.

#### Another way

- Set the Integral (I) and Derivative (D) values to zero.
- Raise the proportional value to 100.0
- Increase the integral value to 100
- Slowly lower the integral value and observe the system’s response
- Since the system will be maintained around setpoint, change setpoint and verify if system corrects in an acceptable amount of time. If not acceptable or you would like a quick response, continue lowering the integral value.
- If the system begins to oscillate again, record the integral value and raise value to 100. Just like me,you got a little greedy trying to get the quickest response.