Back to Part 1.

So, we’ve got the following equation:

$u_k = K_p e_k + K_i \sum_{i=0}^{k}{e_i} + K_d (e_k - e_{k-1})$,

which gives the controller output at each instant $k$. To implement this controller, one thing to do is to consider the $\Delta u_k$, i.e. the increment of the control signal $u_k$ from one instant to the other. So if we define:

$\Delta u_k = u_k - u_{k-1}$,

we get

$\Delta u_k = K_p e_k + K_i \sum_{i=0}^{k}{e_i} + K_d (e_k - e_{k-1}) - (K_p e_{k-1} + K_i \sum_{i=0}^{k-1}{e_i} + K_d (e_{k-1} - e_{k-2}) )$,

which yields

$\Delta u_k = K_p (e_k - e_{k-1}) + K_i e_k + K_d (e_k -2e_{k-1} + e_{k-2})$.

Grouping the addenda by the k-instant we get the almost final version of the algorithm:

$\Delta u_k = (K_p + K_i + K_d) e_k + (-K_p - 2K_d)e_{k-1} + K_d e_{k-2}$,

which can be better formulated to ease implementation in code as follows:

$K_1 = K_p + K_i + K_d$,
$K_2 = -K_p - 2K_d$,
$K_3 = K_d$,
$e_k = r_k - y_k$,
$\Delta u_k = K_1 e_k + K_2 e_{k-1} + K_3 e_{k-2}$,
$u_k = u_{k-1} + \Delta u_k$.

Ok! Now that should do, it’s time to code and test; check Part 3, now!

The PID Controller – Part 2 of 4