Electronic – digital PID controller implementation

control systemmicrocontrollerpid controller

I am trying to implement a PID controller in the z-domain. What I have done so far is found some values of \$K_p\$, \$K_i\$, and \$K_d\$ that work in the time domain. I have found some information on how to translate these values into the z-domain, but when I run the new simulation with the digital PID controller, it no longer seems to work.

For your reference, the \$K_p\$, \$K_i\$, and \$K_d\$ values I have are 0.25, 0.6, 0.005, and when i converted these to the canonical form (in z domain) I had \$a_0 = 500.25\$, \$a_1 = -1000.25\$ and \$a_2 = 500\$. (used \$T = 10^{-6}\$. When i plot these two things (using a sinusoidal input), the outputs are different and I can't quite figure out where I am going wrong with this.

EDIT: I guess as a more direct question, how can i take time domain parameters\$K_p\$, \$K_i\$, and \$K_d\$ and turn that into a digital/ z domain difference equation for a microcontroller?
TIA

Best Answer

Since you changed your question, here is my second answer:

You have your coefficients \$K_p\$, \$K_i\$ and \$K_p\$. The transfer function is \$D(z) = K_p + K_i \frac{T}{2}\left[ \frac{z+1}{z-1}\right] + K_d \left[\frac{z-1}{Tz}\right] =\frac{a_0 + a_1 z^{-1} + a_2 z^{-2}}{1 + b_1 z^{-1} + b_2 z^{-2}}\$. When you compare the two sides of the equation you will find can calculate everything you need. I assume you already have determined \$K_p\$, \$K_i\$, \$K_p\$ and \$T\$ either by measurement or from the requirements.

  1. Calculate the coefficients

    • \$a_0 = K_p + \frac{K_iT}{2} + \frac{K_d}{T}\$
    • \$a_1 = -K_p + \frac{K_iT}{2} - \frac{2K_d}{T}\$
    • \$a_2 = \frac{K_d}{T}\$
    • \$b_1 = -1\$
    • \$b_2 = 0\$
  2. Negative exponents for z basically mean a shift. The output is $$y[n] = x[n]a_0 + x[n-1]a_1 + x[n-2]a_2 + y[n-1]$$

The index \$n\$ is the current value, \$n-1\$ the previous one and so on.