MATLAB Answers

Discrete PID controller simulation

62 views (last 30 days)
Stepan Podhorsky
Stepan Podhorsky on 11 Sep 2019
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[0]*(xD[0] - xD[1]); // dup(k) = Kp*[e(k) - e(k-1)]
dui = (Kp[0]*T[0])/(2*Ti[0])*(xD[0] + xD[1]); // dui(k) = (Kp*T)/(2*Ti)*[e(k) + e(k-1)]
dud = (Kp[0]*Td[0])/T[0]*(xD[0] - 2*xD[1] + xD[2]); // dud(k) = (Kp*Td)/T*[e(k) - 2*e(k-1) + e(k-2)]
du = dup + dui + dud; // du(k)
tmp = xD[3] + du; // u(k) = u(k-1) + du(k)
// antiwind-up
if(tmp > Umax[0]){
satU = Umax[0];
}else if(tmp < Umin[0]){
satU = Umin[0];
satU = tmp;
y0[0] = 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[0] = u0[0] - u1[0]; // e(k) = sp - cv
xD[2] = xD[1]; // e(k-2) = e(k-1)
xD[1] = xD[0]; // e(k-1) = e(k)
xD[3] = u2[0]; // 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?

Answers (0)

Community Treasure Hunt

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

Start Hunting!