# digital PID controller implementation

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

• From simple inspection we can immediately see something is clearly wrong with your Ki nd Kd values since the units don't make sense. Ki is in units of frequency, and Kd in units of time. Dimensionless as you show them is clearly wrong and therefore can't possibly be converted to the Z domain in any meaningful sense. – Olin Lathrop Jun 27 '12 at 21:40
• @OlinLathrop I can see where you are coming from, but these are formulas I have obtained from the wikibooks link. Are there other ways I can transform my time domain to z? – suzu Jun 27 '12 at 22:03
• let us continue this discussion in chat @OlinLathrop – Kortuk Jun 28 '12 at 14:13
• @OlinLathrop, I think your chat works, if it does not we can figure a different way to communicate. comments here are not the most productive. – Kortuk Jun 28 '12 at 14:20
• @Kortuk: I followed your link and got to a chat page. It had blocks for things you said and I said, but none of the text was filled in. Chat has never worked for me yet. – Olin Lathrop Jun 28 '12 at 16:06

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.

You are of by one order of magnitude. $a_2 = \frac{K_d}{T} \rightarrow T = \frac{K_d}{a_2} = \frac{0.005}{500} = 10^{-5}$ and not (your) $T = 10^{-6}$ so your values need to be adjusted.

• oops, i think i meant to write $10*10^{-6}$. – suzu Jun 27 '12 at 21:43
• @suzu Was that causing the error? – suha Jun 28 '12 at 7:26