Basic PID


Often when dealing with various applications, controlling a certain quantity to be around a constant value is useful. The quantity to be controlled could be a variety of things, such as liquid levels, velocity, or even altitude just to name a few examples. PID or Proportional, Integral and Derivative is one method by which a quantity can be controlled in a robust manner if the computer is sufficiently fast or an analog controller is used. In the following explanation, a time domain analysis of PID is done for a more intuitive explanation if you are not familiar with transfer functions s-domain and z-domain analysis methods.

Basic Operation

The main feature of PID is the "feedback" of a measured error to generate a forcing output to control system states.

One familiar example of PID-like control is the method by which people use to control their vehicle speed on the road. If we are stopped and a red light turns green, we accelerate quickly initially to build up speed (large forcing), but when we see that the speed is close to what we deem "adequate" we start to ease off the accelerator (reduce forcing). PID controllers basically use a set formula that embodies these characteristics, applying forcing aggressively if the error is large, and easing off if the error is small.

Analytical Implementation

In order to understand this section, you will have to have a basic working knowledge of linear differential equations and damping. This section is not necessary to create an implementation but is helpful in showcasing how and why this method is robust (assuming continuous time) even without much knowledge of system parameters.

For example, if we consider a car with drag traveling in a straight line, its motion can be described by the below equation

$$ F_{motor} - F_{drag} = mx'' $$

Suppose that we have a means to control the force output of the motor. Therefore we can let \(u = F_{motor}\); in this simple example I will assume that drag is described by \(F_{drag} = Cx'\).

The control input, defined as \(u\), takes the following form in the PID method.

$$ u = K_p e + K_i \int edt + K_d \frac{de}{dt} $$

where \(K_p, K_i,\) and \(K_d\) are all positive constants

\(u\) in general provides forcing to a system that can be used to indirectly control a quantity

By substitution

$$  K_p e + K_i \int edt + K_d \frac{d}{dt}e  - Cx' = mx'' $$

Since speed is to be controlled the substitution \(e = r - v\) is made

$$  K_p (r - v) + K_i \int (r - v)dt + K_d \frac{d(r - v)}{dt}  - Cv = mv' $$

The equation becomes more intuitive and interesting when the derivative is taken, it should look familiar if you have been acquainted with 2nd order ODE's.

$$ K_ir = (m+K_d)v'' + (C+K_p)v' + K_i v $$

The equation is second order in velocity, which means the velocity will behave as a simple damped oscillator with constant forcing. Furthermore, the steady state particular solution is \(v=r\) meaning the desired effect is achieved.

In this particular system it is very easy to "optimize" the response time since the PID constants can be selected to achieve critical damping. It is important to note that the system need not be linear for PID to work; in non-linear systems however there are performance draw backs of using PID which are discussed in the Wikipedia article here. The strength of the method is it is easy to use and is very prominent in industry. In general, it is not always necessary to have proportional, integral and derivative components.

Numerical Implementation

In a realistic setting, there must be some sort of sensor that can be used to determine the value of control variable \(x\) at any time. Using the previous example, suppose that the vehicle is to maintain a velocity of \(v=5\). Suppose that we have a speedometer that gives us a means of accurately measuring \(v\).

Initially the three error terms are given below. The subscripts refer to the discrete time step.

$$ e_0 = r-v_0 $$

$$ E_0 = 0 $$

$$ e'_0= 0 $$

Then in general at time \(t\) we use the below formulas

$$ e_t = r-v_t $$

$$ E_t= E_{t-1} + e_{t-1}\cdot dt $$

$$ e'_t = \frac{e_t-e_{t-1}}{dt} $$

Now all that's to be done is to combine the signal into \(u\) with the appropriate constants. Recall that the model takes the form below

$$ u = mx'' + Cx' $$

Note that this model is to simulate the system's response only. In practice, this model although helpful and often necessary for robust optimal control, is not actually necessary for the target metric to eventually converge to the reference value. Optimization of a system however, does require either a model of the system or the application of some algorithm to identify the system and tune constants.

The following graphs are simulations of the PID controlled vehicle with randomized constants \(m,C,K_p,K_i,K_d\). You can download the spreadsheet below and alter the constants to see what effects they have. You can also click "recalculate" to randomly generate new scenarios.