Howdy,

In my previous post I introduced the PID controller; this time I’m gonna illustrate how to formulate the algorithm in a way which simplify the coding on board our EV3. This post will be saved as a static page here.

I know, I know, there’s some more math; but I think it’s worth it.

So, we’ve got the following equation representing the canonical form of the discrete PID controller:

$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! Wait for my next post!

The PID Controller – Part 2
Tagged on: