Engineering background

PID controller

PID controller continuously calculates an error value e(t) as the difference between a desired setpoint (SP) and a measured process variable (PV) and applies a correction based on proportional, integral, and derivative terms (denoted P, I, and D respectively), hence the name.

Standard form

U(t) = MV(t) =
  K_{i}\int _{0}^{t}{e(\tau )}{d\tau }+
  K_{d}{\frac {d}{dt}}e(t)


K_{p} the proportional gain, a tuning parameter,

K_{i} is the integral gain, a tuning parameter,

K_{d} is the derivative gain, a tuning parameter,

e(t)=SP-PV(t) is the error (SP is the setpoint, and PV(t) is the process variable),

t is the time or instantaneous time (the present), τ is the variable of integration (takes on values from time 0 to the present t.

Ideal form

K_{p} gain is applied to the I_{out}, and D_{out} terms, yielding:

MV(t) =
    \frac{1}{T_{i}}\int _{0}^{t}{e(\tau )}\,{d\tau }+
    T_{d}{\frac {d}{dt}}e(t)


T_{i} is the integral time and T_{d} is the derivative time

the gain parameters are related to the parameters of the standard form through K_{i}={\frac {K_{p}}{T_{i}}} and K_{d}=K_{p}T_{d}


previous_error = 0
integral = 0
  error = setpoint - measured_value
  integral = integral + error * dt
  derivative = (error - previous_error) / dt
  output = Kp * error + Ki * integral + Kd * derivative
  previous_error = error
  goto loop