I'm doing the implementation of the PID with derivative filter using the Euler's backward method, but I got stuck in this part since I have z in the denominator of most terms.

I didn't realize yet how I could get the inverse transform to obtain the recursive equation for the uC. Does anyone see any solution or can give me any advice?


K: is the proportional gain

Ti: Integrative constant

Td: Derivative constant

N: Derivative filter constant

PID enter image description here

Just to be more specific: my doubt is how to get the inverse transform of a term like this:

\begin{align} &&&\frac{N\cdot T_d}{z(N\cdot h + T_d) - T_d}(z^2)\\ \end{align}

That $z$ in the denominator is driving nuts, because if I hadn't that term I could just do

\begin{align} u(k) = &&&\frac{N\cdot T_d}{(N\cdot h + T_d) - T_d}e(k+2)\\ \end{align}

  • 1
    $\begingroup$ I'll try to answer tonight. But usually we don't implement the PID as a transfer function even though it is. If you want to implement anti-windup, it is much easier to treat the integrator separately. $\endgroup$
    – Ben
    Dec 16, 2021 at 14:27
  • 1
    $\begingroup$ Hey, your notiation is a bit unclear. What is capital $Z$ and small $z$? is it really different? When you write things like $Ti$, what's that? Where does the $d$ factor used all over come from? $\endgroup$ Dec 16, 2021 at 14:34
  • 1
    $\begingroup$ I tried to put what what I got from your equation into a $\LaTeX$ equation within your question, but I don't know whether that represents what you're actually doing. Please check, and fix if necessary! $\endgroup$ Dec 16, 2021 at 14:36
  • $\begingroup$ Opss! My bad, thank you so much!! I'll rewrite everything to get in this final expression $\endgroup$ Dec 16, 2021 at 14:39
  • 1
    $\begingroup$ ah, is it possible that you mean some constants $T_i$ and $T_d$ when you write $Ti$ and $Td$? $\endgroup$ Dec 16, 2021 at 15:03

1 Answer 1


Your second real mistake happens when you multiply both sides of the equation by $z - 1$. This is both unnecessary and it makes the resulting problem difficult to solve.

Instead, start two equations up. Here it is with the normalization of each term modified a bit so it'll be convenient later: $$U(z) = K\left[ 1 + \frac{h}{T_i}\frac{z}{z - 1} + \frac{N\,T_d}{N\,h+T_d}\frac{z-1}{z - \frac{T_d}{N\,h + T_d}}\right] E(z) \tag 1$$

I am not going to work the coefficients out for you, but if you persist in your approach, you want to find $U(z)$ in the form

$$U(z) = \frac{b_0 + z^{-1}b_1 + z^{-2}b_2}{1 + z^{-1}a_1 + z^{-2}a_2} E(z). \tag 2$$

Now multiply both sides by the entire denominator:

$$\left (1 + z^{-1}a_1 + z^{-2}a_2 \right) U(z) = \left( b_0 + z^{-1}b_1 + z^{-2}b_2 \right) E(z). \tag 3$$

You ought to be able to take the inverse $z$ transform of this by inspection:

$$u[n] + a_1\ u[n - 1] + a_2\ u[n-2] = b_0\ e[n] + b_1\ e[n-1] + b_2\ e[n-2] \tag 4$$

Now solve that for $u[n]$, and you have your difference equation. Or find a discussion of the various types of direct-form filters and choose the one that's best for you.

But your first real mistake is implementing a PID controller as a second-order generic filter. The two big reasons this is a mistake is because, one, you can run into problems with the integrator pole not staying right at $z = 1$ because of coefficient precision, two, there's no easy way to turn a stage (like the integrator or the proportional gain) off, and third, you should always implement integrator anti-windup any time you have an integrator inside a control loop.

What you want to do is recast (1) as

$$U(z) = \left [ k_p + k_i\frac{z}{z - 1} + k_d\frac{z-1}{z - d_d}\right] E(z) \tag 1$$

(In your code, if you want to use the traditional form that's specified by $T_i$, $T_d$, $N$ and $K$, then just convert from those to the gains above -- I find it's easier to tune using the gains directly).

Then implement the proportional, integral, and derivative separately in your code, and add them up at the end. If you have your generic PID controller and you find that you don't need integral action -- set $k_i$ to zero. Ditto, if you find you don't need derivative action* you set $k_d$ to zero.

As part of this, implement integrator anti-windup. This answer is getting pretty long, and you didn't ask -- so just do a web search on that term. The short reason is that actuators saturate, integrator anti-windup is a good way to compensate for that, and there's a ton of different ways to implement anti-windup, each of which is best in some particular way -- so it's good to know a lot of them so that you can pick the best for any given control loop.

* This is pretty common -- derivative action can be problematic, you only want to use it if you must.


Your Answer

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

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