Scroll back to top

    PID

    Overview

    The proportional-integral-derivative or PID controller is a simple, widely-used, and often very effective way to make a dynamic system do what you want.

    Suppose this dynamic system has exactly one real-valued input and one real-valued output . Suppose our goal is to choose the input so that the output converges to a desired reference value . Define the error as

    The PID controller is

    for some choice of constant gains , , and . The first term in this controller is “proportional feedback,” the second term is “integral feedback,” and the third term is “derivative feedback.”

    How to compute the proportional feedback term

    We assume the controller is given and has access to a measurement of , so computing the term

    is just arithmetic.

    How to compute the integral feedback term

    To compute the term

    we need to find the integral

    at the current time . In most cases, we cannot find this integral exactly even if we wanted to, because we do not know — and, therefore, — at all times , but rather only at sampled times

    for some time step . For this reason, it is common to use the right-hand rectangle method of numerical integration:

    Here is another way of thinking about this. Suppose we define a new variable

    to contain the integral of the error. It is equivalent to say that is described by the first-order ordinary differential equation

    with the initial condition

    Then, it is common to use the backward Euler method

    to compute an approximation to . Applying the backward Euler method to find is exactly the same as applying the right-hand rectangle method to find the integral that represents.

    To implement the backward Euler method with code, you need to define a variable to store the prior value of the error integral, and you need to update the value of this variable at each time step. You will have to initialize the error integral to something when you start your controller — it is common to start with a value of zero.

    How to compute the derivative feedback term

    To compute the term

    we need to find the derivative

    at the current time . Just like for the integral term, we usually cannot find this derivative exactly. So, just like it is common to use the backward Euler method to find the integral of the error, it is common to use the backward finite difference method to find the derivative of the error:

    To implement the backward finite difference method with code, you need to define a variable to store the prior value of the error, and you need to update the value of this variable at each time step. Again, you will have to initialize this prior error to something when starting your controller — it is common to start with a value of zero.