# Discrete PID controller simulation

62 views (last 30 days)
Stepan Podhorsky on 11 Sep 2019
Hello,
I have implemented a discrete PID controller in C language via the Simulink
S-function. Here is my C code which I have inserted into the text field in the
Outputs tab in the S-function Builder
double tmp; // temporary control value
double satU;
double dup; // increment of proportional part
double dui; // increment of integral part
double dud; // increment of derivative part
double du; // increment of control value
dup = Kp*(xD - xD); // dup(k) = Kp*[e(k) - e(k-1)]
dui = (Kp*T)/(2*Ti)*(xD + xD); // dui(k) = (Kp*T)/(2*Ti)*[e(k) + e(k-1)]
dud = (Kp*Td)/T*(xD - 2*xD + xD); // dud(k) = (Kp*Td)/T*[e(k) - 2*e(k-1) + e(k-2)]
du = dup + dui + dud; // du(k)
tmp = xD + du; // u(k) = u(k-1) + du(k)
// antiwind-up
if(tmp > Umax){
satU = Umax;
}else if(tmp < Umin){
satU = Umin;
}else{
satU = tmp;
}
y0 = satU;
Here is the C code which I have inserted into the text field in the
Discret Update tab in the S-function Builder
xD = u0 - u1; // e(k) = sp - cv
xD = xD; // e(k-2) = e(k-1)
xD = xD; // e(k-1) = e(k)
xD = u2; // bumpless transition: u(k-1) = tr
I have prepared following simulation for the testing purposes i.e. controller controls FOPDT system with transfer function and transport delay . Parameters of the
PID controller are set in following manner: .
It is worthwile to say that the control value of the PID controller
is saturated in range . I have chosen sampling period in respect to the system time constant (the sampling period
is used by the PID algorithm and also by the ZOH blocks).
Simulation output (without switching between controller output and manually set
control value) is given below The controlled variable oscillates and I don't understand why. The same setting of
the controller behaves correctly in analog case with PID controller block from Simulink library.
I have tried smaler sampling period values but results are same. Can anybody give me an advice
what I am doing wrong?

R2015a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!