# PI Controller in MCU

Status
Not open for further replies.

#### garimella

##### Full Member level 5
Hi

I have some clarification regarding implementing PI Controller in MCU. I have seen two approaches
First one is direct translation of PI controller equations in software and second one is converting continuous domain to discrete domain and then getting corresponding difference equation, which can be put back to action. I was interested to know which one is better?

#### FvM

##### Super Moderator
Staff member
Software controller is time discrete by nature. In so far z domain implementation is the straightforward approach and more exact than "simulated" continuous PI.

In can still make sense to use popular PI parameters for controller setup and calculate parameters of time discrete transfer function internally. Most commercial digital PID controllers do.

#### gary36

##### Full Member level 3
Hi FvM
I was referring to this link in implementing PI controller in C for MCU's https://batchloaf.wordpress.com/2013/06/11/simple-pi-control-using-the-dspic30f4011/
I do not understand how the final output works. For example if we consider only proportional controller, the output=kp*error. Well this is just an open loop. closed loop TF should be kp/kp+1. I have seen similar such formulas all over the place. I do not see feedback parameter getting into the equation.

#### FvM

##### Super Moderator
Staff member
I don't understand what's the problem with this simple PI code. It's a "simulated" continuous PI, implemented as difference equation. To get the exact transfer function, you should analyze it in z domain.

What do you mean by feedback parameter? The loop is closed in hardware outside the code. The overall closed loop transfer function also depends on the controlled "plant" and the RC lowpass used to average the PWM output.

#### gary36

##### Full Member level 3
Ok I got it. Model gets closed from outside.

#### gary36

##### Full Member level 3
I was trying to implement closed loop PI controller within MCU. No inputs from field. The TF laplace domain is skp+ki/s(kp+1)+ki. I translated this to z domain using Tustin method and converted to difference equation. Assuming Kp=100 and Ki=1000 and T=0.001, i get y=.0099y(n-1)+0.9901x-0.0098x(n-1). The input is simply a constant 1.0 and I expect output to rise and stay at 1.0. I get same answer in simulink. But looking at difference equation, i really do not understand the meaning of x(n-1) because there are no previous values of x. Since input is constant x(n-1) will always be 1.0. So what Am i doing wrong?

Status
Not open for further replies.