# PID Autotuning using Relay Method

PID controllers are widely-used nowadays due to their ease of implementation and deployment, with many commercial products available on the market. To achieve desired performance, the controller gains need to be adjusted properly. Among many tuning schemes in the literature, the so-called Ziegler Nichols Frequency Domain (ZNFD) approach is one that can easily be understood and applied to a physical system. In this article we demonstrate simulation of manual ZNFD PID tuning, and then extend to an autotuning procedure using relay feedback. Basic theory can be read from [1] and our previous article Digital PID Controllers*.

*The result below is simply the Scilab version of the simulation in that article, using the same plant, but now with Xcos model files provided for your convenience.

### Manual ZNFD Method

Consider a third-order lag plant with transfer function

(1)

with PID feedback control in parallel form

(2)

Using Xcos model pid_feedback.zcos shown in Figure 1, we can simulate step response of this closed-loop system. In order to do so, a user may want to achieve a good-enough set of PID parameters to begin with.

Figure 1 an Xcos PID simulation diagram [pid_feedback.zcos]

A manual tuning method generally requires the user to adjust the controller parameters by herself and measure a couple of quantities related to plant dynamics, then compute PID gains using those values.

For the ZNFD method, the manual procedure is as follows

1. Turn off the I and D terms by setting an to zero. The control law reduces to just a proportional gain; i.e., .
2. Increase from a small value and observe the step response. Try to obtain the value that causes plant output oscillation. At this point the closed-loop system is marginally stable. Further increment destabilizes the system. Write down this value as ultimate gain .
3. Measure the oscillation period as ultimate period .
4. With the resulting values of and , compute the controller gains using Table 1.
Controller Form
P
PI
PID

Table 1: PID parameter adjustment using ZNFD method

We can experiment this ZNFD procedure on the plant (1) using pid_feedback.zcos. Double click on the PID controller to set PID parameters. Put value 0 in the Integral and Derivation fields. Put in some value for Proportional and simulate. Try adjusting proportional gain until the plant output oscillates indefinitely (not growing larger or dying out) as shown in Figure 2.

Figure 2 plant output oscillation when

You would find that the ultimate gain for this proportional feedback system equals 8. Now measure the oscillation period by zooming in the plot. This assessment does not have to be very precise. From Figure 3 we see that the period is approximately 3.5.

Figure 3 oscillation period

So from Table 1 we achieve . This set of parameters yields the step response in Figure 3. It is well-known that ZNFD method normally yields a response with large overshoot. If that becomes a problem one may want to reduce .

Figure 4 step response from PID controller tuned by ZNFD method

One obvious problem with manual ZNFD procedure is to find the ultimate gain. Cranking a little more results in closed-loop instability, which might be hazardous. We would prefer to obtain plant output oscillation without destabilizing the system. A relay feedback helps us achieve just that.

### PID Autotuning based on ZNFD method

When a relay is put in place of a controller, it could cause the plant output to oscillate. This is true for a plant with its Nyquist plot crossing the negative real axis. If this is not the case, a relay with hysteresis may be used. For the third-order lag plant (1), a simple relay can be applied.

A relay is a nonlinear device. To analyze relay feedback as a linear system, a technique called describing function can be applied. See our previous article, or [1] for detailed analysis. Here we only summarize the result.

For a non-hysteresis relay, its describing function is

(3)

Where and are the relay amplitude and plant output magnitude of oscillation, respectively. At the oscillation point, the Nyquist curve must intersect the negative axis at . It concludes that the ultimate gain equals . can then be measured as before.

Figure 5 shows an Xcos diagram of a modified PID feedback. pid_autotuning.zcos, equipped with a relay that can be switched in place of controller during tuning phase. Note that the relay is built using SIGN block in series with a gain block to adjust its output magnitude. At the relay input a small bias is needed so that the relay operation could start initially when the plant output is zero.

Figure 5 PID control with relay autotuning function

Autotuning is performed when the switch is connected to input 1. Setting the relay output amplitude to 1; i.e., , yields the oscillation in Figure 6 (adjust the y-axis of scope if necessary to see the full oscillation waveform.)

Figure 6 plant output oscillation induced by a relay

From figure 6, the plant output magnitude after the oscillation is steady is roughly measured as . So from (3)

(4)

which matches the value from manual ZNFD obtained earlier. By zooming in the oscillation waveform, it can be verified that oscillation period is close to 3.5 sec. So PID parameters can be computed form Table 1 as before.

To summarize, the relay autotuning method helps us achieve the same and values as the manual ZNFD, without the risk of destabilizing the system. In a real application, and can be computed from data captured in real-time during the tuning phase, and the PID parameters computed according to Table 1. C code example is provided in our previous article [2].