Advanced PID Controller Implementation
In this digital era, PID controllers have evolved from basic textbook structure to more sophisticated algorithms. Features such as setpoint/derivative weightings and anti-windup scheme are often added to improve the closed-loop response. In our previous article Decorated PID !ontroller, we consider a PID structure with modification and additional functions as follows
•
"o lessen the effect of measurement noise, derivative pa rt is implemented as a filter with parameter
•
#ack calculation anti-windup scheme is implemented with tracking gain
•
$etpoint weightings for proportional and derivative paths can be ad%usted via and
, respectivel&
feedback diagram with this advanced PID controller is constructed using 'cos palettes as in Figure (.
Figure ( advanced PID feedback diagram In e)uation form, this controller can be described as *(+
with *+
where
,
,
and
, are reference command, plant output,
controller output, and saturated controller output, respectivel&. s described in our Discrete-time PID !ontroller Implementation article, using backward difference relationship *+
)uation *(+ can be converted to -domain as
*0+
1ewrite *0+ in terms of
*2+
"o implement this PID scheme as a computer algorithm, we have to convert *2+ to a difference e)uation. It is straightforward to show that *2+ can be rewritten as
*3+
with coefficients
*4+
$o the corresponding difference e)uation is
*5+
Response Comparison via Simulation )uation *5+ is read& for implementation on a target processor. #efore that phase, we want to make sure that our e)uation and coefficients are without error. 6ne eas& wa& is to perform simulation on 'cos and compare the response to the original continuous-time PID controller. For this purpose, we construct a model advpid7imp.cos as shown in Figure , consisting of feedback loops. "he upper loop is controlled b& discrete-time PID in the form *3+, and the lower loop contains the continuous-time PID. "he simulation results from the two closed-loop s&stems are then compared to verif& how well the& match.
Figure model advpid7imp.cos for discrete and continuous PID comparison 8ote that the discrete-time PID in the upper loop is contained in a superblock. "he internal details are shown in Figure , which corresponds to the discrete-time controller *3+.
Figure the internal details of discrete-time PID superblock lso, at the output of discrete-time PID controller, a 9PF transfer function is inserted to prevent an algebraic loop error, normall& occurred with h&brid simulation. "he 9PF pole is chosen well above the closed-loop bandwidth so the filter does not have noticeable effect on the responses. For eas& editing, all the parameters in advpid7imp.cos are initialied using a script file advpid7imp.sce. "he plant is chosen as a third-order lag transfer function *:+
which can be created in $cilab b& s = poly(0,'s'); P = syslin('c',1/(s+1)^3);
"hen, controller parameters are assigned values. "hese can be chosen as &ou like since the purpose of this simulation is to compare the responses. ;ere the PID gains are obtained from
// PID gains kp = 4.8; ki = 2.7; kd = 2.1; // filter coefficient N = 10; kt = 1.2; // back calculation gain for anti-windup // setpoint weight for proportional term p = 0.7; // setpoint weight for derivative term d = 0.1; // sampling peroid !s = 0.01;
For sampling period "s, the value should match the simulation sampling period in the clock. "he parameters left to be assigned are the limits in saturation block. Put in some reasonable values such that some saturation effect happens during transient, since we prefer response comparison with the back calculation term activated. "oo small the limit range would cause severe performance degradation. #& some trial and error, we are finall& satisfied with these values for saturation block "li# = 2000; lli# = $2000;
Finall&, the coefficients in *4+ need to be computed. =e introduce additional variables x( and x for terms that appear in several places. %1 %2 1 2 1 2 3 c1 c2 c3 c4 d1 d2 d3
= (1+N&!s); = (2+N&!s); = %2/%1; = $1/%1; = kp; = $kp&%2/%1; = kp/%1; = ki&!s; = $ki&!s/%1; = kt&!s; = $kt&!s/%1; = kd&N/%1; = $2&kd&N/%1; = kd&N/%1;
fter all parameters are assigned, interactivel& or b& executing advpid7imp.sce, we proceed b& clicking on the simulation start button. "he simulation results in Figure 0 show that the plant and controller outputs from continuous and discrete PID are almost identical. "his makes us confident that the discrete-time PID and its coefficients are derived correctl&.
Figure 0 response comparison between continuous and discrete PID
Implementation on Target Processor fter the verification process b& 'cos simulation, we are now read & to implement our advanced PID controller on a target processor, in this case a PI!0P23>!? b& >icrochip. "he plant is a D! motor with ;-bridge drive, as described in the D! >otor !ontrol Part I article. Figure 2 shows the experimental setup used. "he rightmost board is our controller protot&pe, where the PID algorithm will be downloaded and executed.
Figure 2 D! motor experimental setup "he source code is written entirel& in !. "he whole code, consisting of a couple of source files, is rather long and mess& due to supporting functions such as @1" communication, command handling, etc. #elow we discuss onl& the parts related to our PID controller implementation. "he sampling period, controller parameters and resulting coefficients are defined as global variables, with some initial values assigned // sampling period
do"l !s = 0.01;
// sampling time // -- these parameters are user-adjustable do"l *p = 1272; // proportional gain do"l *i = 8777; // integral gain do"l *d = 4; // derivative gain do"l *t = 10; // tracking gain do"l p = 0.-; // proportional weight do"l d = 0; // derivative weight int N = 20; // filter coefficient // ----- coefficients of PID algorithm --------------
do"l 1, 2, 1, 2, 3, c1, c2, c3, c4; do"l d1, d2, d3;
and also variables to keep previous values of controller inputs and outputs do"l p2, p1, p0, 1, 0, "s1, "s0, d2, d1, d0 ; do"l "2, "1, "0, "0n; // variables used in PID computation
8ow, the coefficients have to be computed before the algorithm starts, and ever& time the user changes an& parameter involved. $o it is convenient to put the computation in a function oid Pt"p(oid)
// PID coefficient setup // -- this function must be invoked anytime // -- any parameter involved is changed by user
do"l %1, %2; !1 = 0; // disable timer 1 %1 %2 1 2 1 2 3 c1 c2 c3 c4 d1 d2 d3
6
= 1 + N&!s; = 2 + N&!s; = %2/%1; = $1/%1; = *p; = $*p&%2/%1; = *p/%1; = *i&!s; = $*i&!s/%1; = *t&!s; = $*t&!s/%1; = *d&N/%1; = $2&*d&N/%1; = *d&N/%1;
!1 = 1; !15 = 0;
// enable timer 1 // reset timer 1 interrupt flag
s usual, the actual PID algorithm is placed in a timer interrupt, in this case timer (. oid tti"t((int"pt, "tops)) !1nt"pt(oid) // Timer 1 interrupt every Ts second
// perform position read from !I module of PI"#$!P#%&'"#(# p9l.:l<0 = P>1?N!@; // read lsw p9l.:l<1 = P>1A@; // read msw from hold register // position in degree dc#pos = p9l."ll&30/N?PPB%4; i (ys5lC.Pos@oop == ?@>) // closed loop PID control
"2 = "1; "1 = "0;
p2 = p1; p1 = p0; p0 = p&pc#d $ dc#pos;
1 = 0; 0 = pc#d $ dc#pos;
// weighted proportional error
// true error
// back calculation error "s1 = "s0; i (s("0) D= PBBEF) "s0 = 0; ls i ("0GPBBEF) "s0 = PBBEF $ "0; ls "s0 = $"0 $ PBBEF;
d2 = d1; d1 = d0; d0 = d&pc#d $ dc#pos;
"0 = 1&"1+2&"2+1&p0+2&p1+3&p2+c1&0+c2&1+c3&"s0+c4&"s1+d1&d0 +d2&d1+d3&d2; i ("0G=0) // positive sense i ("0 D PBBEF) PB9l = ("nsiCnd int)"0; // limit to P)' range
ls PB9l = PBBEF; H = 0;
6 ls
// negative sense "0n = $"0; i ("0n D PBBEF) PB9l = ("nsiCnd int)"0n;
// limit to P)'
range
6
ls PB9l = PBBEF; H = 1; 6 >?1H = PB9l; 6 // if *+ys,lagPos.oop ".0+!D !15 = 0; // reset interrupt flag
8ote that our ;-brige driver is commanded b& a pair of signalsA P=> and DI1, as explained in the D! >otor !ontrol Part I article. "he motor turns clockwise and counter-clockwise when DI1 B ? and (, respectivel&, and the motor speed corresponds to the dut& c&cle of P=> signal, dictated b& P=>Cal variable.
Experimental Results n initial set of PID gains is achieved b& performing some naive automatic tuning based on the
. s shown in Figure 3, "his set of PID gains with
rather high i value results in oscillar& response *dashed red+. $o we begin fine tuning b& reducing to , and , resulting in the dotted blue, and black, respectivel&. "he overshoot and oscillation lessen with decreasing
.
Figure 3 step response with values of 8ext, we tr& incresing
from
, to
Figure 4. 6scillation is reduced with increasing
, and .
, resulting in the responses in
Figure 4 step response with values of 6ur last experiment for this article is var&ing the proportional setpoint weight
gains are fixed at
responses with
reduced b& decreasing
,
,
, and
. "he PID
. Figure 5 shows the
. Interestingl&, the overshoot can be
, though the rise time becomes slower.
Figure 5 step response with 2 values of
=e leave it to an&one who implements this advanced PID controller on his/her s&stem to experiment with the anti-windup back calculation gain and derivative setpoint weighting . It is suggested in some literature that
be set to ? so that abrupt change in
command would not affect the derivative action of the controller. =e set and
for all the experimental responses in this article.
,