1
$\begingroup$

I need the equation of a discrete PID controller and I find different answers from different websites. For example, here the answer is:

$$u[k] = u[k-1] + (K_p + K_i \frac{T_s}{2} + \frac{K_d}{T_s}) e[k] + (-K_p + K_i \frac{T_s}{2} - 2 \frac{K_d}{T_s}) e[k-1] + \frac{K_d}{T_s} e[k-2]$$

and that's the equation that is used in this paper, for example. If I write that equation in function of $T_i$ ($T_i = K_p/K_i$) and $T_d$ ($T_d = K_d/K_p)$, I get:

$$u[k] = u[k-1] + K_p \left[ (1+\frac{T_s}{2 T_i} + \frac{T_d}{T_s}) e[k] + (-1 + \frac{T_s}{2 T_i} - 2 \frac{T_d}{T_s}) e[k-1] + \frac{T_d}{T_s} e[k-2]\right]$$

But in wikipedia and in this paper (although they say they're using the written equation above) they use the following equation:

$$u[k] = u[k-1] + (K_p + \frac{K_d}{T_s}) e[k] + (-K_p - 2 \frac{K_d}{T_s}) e[k-1] + \frac{K_d}{T_s} e[k-2]$$

and I think that here they use the same equation.

So, what can I do? Which do I implement? Any demonstration?

Thanks

$\endgroup$
1
  • $\begingroup$ Please edit your third term to actually match the Wikipedia article -- you left out the $K_i$ term entirely. $\endgroup$
    – TimWescott
    Mar 27, 2019 at 21:15

2 Answers 2

1
$\begingroup$

The second equation is just the first equation with $K_i=0$. So the second equation would be a PD controller, while the first is PID controller.

One can derive the first equation by starting with common approximations for the integral $i[k]$ and derivative $d[k]$ terms

\begin{align} i[k] &= i[k-1] + T_s\,\frac{e[k] + e[k-1]}{2}, \tag{1} \\ d[k] &= \frac{e[k] - e[k-1]}{T_s}. \tag{2} \end{align}

The control signal could then also be constructed using

$$ u[k] = K_p\,e[k] + K_i\,i[k] + K_d\,d[k]. \tag{3} $$

Your first equation can be obtained using the difference of the control signal and substituting $(1)$ and $(2)$

$$ u[k] - u[k-1] = \left(K_p\,e[k] + K_i\left(i[k-1] + T_s\,\frac{e[k] + e[k-1]}{2}\right) + K_d\,\frac{e[k] - e[k-1]}{T_s}\right) - \left(K_p\,e[k-1] + K_i\,i[k-1] + K_d\,\frac{e[k-1] - e[k-2]}{T_s}\right), \tag{4} $$

namely expanding and simplifying this yields the same as your first equation

$$ u[k] - u[k-1] = \left(K_p + \frac{K_i\,T_s}{2} + \frac{K_d}{T_s}\right) e[k] + \left(-K_p + \frac{K_i\,T_s}{2} - \frac{2\,K_d}{T_s}\right) e[k-1] + \frac{K_d}{T_s} e[k-2]. \tag{5} $$

The advantage of using $(5)$ over the combination of $(1)$, $(2)$ and $(3)$ is that $(5)$ uses less computations and memory. However for debugging the other implementation might be more convenient, since $i[k]$ and $d[k]$ would be directly available. This would make it for example easier to check for integral windup or whether the derivative is staying within certain desired bounds.


However as TimWescott mentioned, the derivative term from $(2)$ can also be approximated with

$$ d[k] = \frac{1 + \alpha}{T_s} (e[k] - e[k-1]) - \alpha\,d[k-1], \tag{6} $$

with $-1\leq\alpha\leq1$. Namely for values close to zero the magnitude of the transfer function associated with $(6)$ stays closer to that of a true derivative but the phase does drop significantly at high frequencies, while for values close to one the phase stays closer to 90° but the magnitude can increase a lot at high frequencies. It can be noted that $\alpha=1$ is equivalent to the bilinear transform. For negative values the transfer function looks like a high pass filter (pure derivative filtered by a low pass filter), which has the advantage of not amplifying high frequency noise. A similar thing could also be done for the integral, however the effects of an integral should only affect low frequencies, which is hardly affected by changing the parameter $\alpha$.

Using $(6)$ instead of $(2)$ does not allow for $(4)$ to be simplified down to $(5)$, due to the added pole from $(6)$, however one can write it into the following combined difference equation

$$ u[k] + (\alpha - 1)\,u[k-1] - \alpha\,u[k-2] = \left(K_p + \frac{K_i\,T_s}{2} + \frac{K_d\,(1 + \alpha)}{T_s}\right) e[k] + \left(K_p\,(\alpha - 1) + \frac{K_i\,T_s\,(\alpha + 1)}{2} - \frac{2\,K_d\,(1 + \alpha)}{T_s}\right) e[k-1] + \left(-K_p\,\alpha + \frac{K_i\,T_s\,\alpha}{2} + \frac{K_d\,(1 + \alpha)}{T_s}\right) e[k-2]. \tag{7} $$

$\endgroup$
0
$\begingroup$

I prefer none of the above. This works better, in my experience. If you work out the math, you'll find that it's equivalent if you set $d_d = 0$.

$$u_d[k] = (1-d_d)(e[k]-e[k-1]) + d_d u_d[k-1] \\ u_i[k] = e[k] + u_i[k] \\ u[k] = k_i u_i[k] + k_p u[k] + k_d u_d[k]$$

Note that I've introduced the $d_d$ term -- this has the effect of band-limiting the derivative term (the closer $d_d$ is to 1, the slower the derivative), which will save you a world of grief from high-frequency oscillations.

$\endgroup$
4
  • $\begingroup$ and how did you get to that equation? $\endgroup$
    – Unnamed
    Mar 28, 2019 at 17:16
  • $\begingroup$ It's a set of difference equations that go with a controller with the transfer function $\frac{k_i z}{z - 1} + k_p + k_d \frac{(1-d)(z - 1)}{z - d}$. It comes from almost 30 years of designing motion control systems that Actually Work(TM). $\endgroup$
    – TimWescott
    Mar 28, 2019 at 19:38
  • $\begingroup$ But can you please share with me any link or something where that equation is demonstrated? $\endgroup$
    – Unnamed
    Mar 30, 2019 at 17:09
  • $\begingroup$ An article. It's up on the Embedded Systems Programming site, somewhere, maybe, but they keep moving it so I keep one on my site. Also these three videos: What is a PID controller?; PID Controller in Software; and PID Tuning. $\endgroup$
    – TimWescott
    Mar 30, 2019 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.