# Automatic Design of Target Motor Control

In this section, we develop an automatically tuned digital PIDF position controller for the T1 target system and show how to export the design to a C header file for use on the target system. As an example, we use the parameters of the T1a target system, but this process applies to any T1 target system (see section A.2).

## Automatic Design of Controllers

The design method of section 8.4 can be applied to the target motor. The method introduced in this section, automatic design of controllers, sometimes called “automatic tuning,” is a technique for automatically determining a controller transfer function. There are a wide variety of automatic design methods. Some require a sufficiently accurate mathematical model of the plant dynamics, and others characterize those dynamics by introducing a known input to the plant and observing the response. From the plant dynamics, controller gains can be adjusted to optimize performance criteria, such as robustness. These criteria can be evaluated by means of simulation, experimentation, analysis, or some combination thereof.

Most methods of designing or tuning controllers can be automated. However, there is an important limitation to most automatic design tools. For a given controller type (e.g., PID), feedback architecture, and plant, there may not be a set of gains that meet the performance criteria. No amount of optimization can change that. It can be the case that the controller type, feedback architecture, or plant must be augmented to meet the design criteria. For instance, a controller with an integrator, rate feedback, a sensor with less noise, or an actuator with more power could be required. Although the design of many controller types can be automated, the automatic tuning of PID or similar controllers is most common. A further limitation of this design tool is that in most cases, there is no consideration for control effort; therefore, proper use of it requires checking that there is sufficient control effort available for a given design.

Perhaps the simplest tuning algorithms are the Ziegler-Nichols “step response” and “frequency response” methods for PID controllers (Åström and Hägglund 2006, sec. 6.2). These were originally developed for manual tuning, but they can be adapted for automatic tuning. These methods do not necessarily require a mathematical model of the plant, the dynamics of which can be estimated from response measurements. The step response method includes the measurement of the open-loop response of the plant to a step input. The frequency response method includes the closed-loop response measurement for increasing proportional gain \(K_P\) (\(K_I = K_D = 0\)) to determine at what gain the output becomes continuously sinusoidal. These tuning methods are relatively simple but can have severe performance limitations.

Most automatic design techniques were developed first for adaptive control, which adapts a controller to improve performance. Automatic design methods could be considered adaptive controllers that stop adapting once they have been tuned because, in applications for which automatic design is effective, the plant’s dynamics don’t change significantly. If the plant’s dynamics do change, the controller can be retuned.

With an accurate plant model, design (automatic or otherwise) can be
performed in software, independent of the plant itself. Automatic design
in a software environment can be performed with a custom algorithm or
with an existing software library. The MATLAB Control Systems Toolbox
has an automatic design function called `pidtune()`

,
which can tune a variety of PID controllers, including PIDF. For
instance, for a transfer function model `GP`

of a plant, a PIDF controller transfer
function `GC`

can be tuned with

```
wc = 30; % rad/s, controlled bandwidth
GC = pidtune(GP, 'PIDF', wc) % tuned PIDF transfer function
```

The parameter `wc`

is the open-loop
\(0\) dB gain crossover frequency
target value for the tuning algorithm. This is approximately equal to
the closed-loop bandwidth, which is related to the closed-loop rise time
and which will be discussed in the next section.

astrom2006

## Controller bandwidth

The method that we will use here works best when we set a design controller bandwidth. Although we have not considered the frequency response control theory techniques required to fully understand how to choose a controller bandwidth, some basic background will suffice to motivate our bandwidth selection process.

A closed-loop bandwidth \(\omega_\text{BW}\) is a measure of the closed-loop system’s speed of response: a higher bandwidth corresponds to an ability to track higher frequencies. Higher speeds correspond to a lower rise time \(T_r\), lower peak time \(T_p\), and lower settling time \(T_s\). Two distinct bandwidths are relevant: that of the open-loop transfer function and that of the closed-loop transfer function. However, these are usually near each other, so adjusting one typically has a similar effect on the other.

A common definition of the bandwidth of a transfer function is the interval of frequencies within which the magnitude of its frequency response function is not less than \(-3\) dB relative to its DC (zero-frequency) magnitude. Consider the magnitude Bode plot of a transfer function \(L(s)\), shown in figure 8.8. This could be the open-loop or closed-loop transfer function. It passes frequencies within the bandwidth, but it attenuates higher frequencies. For first-order linear systems with time constant \(\tau\), the bandwidth corresponds to \(1/\tau\). For second-order linear systems, it is near the natural frequency, \(\omega_n\). This suggests that for higher bandwidths, a faster transient response will be observed.

Another closely related concept is the gain crossover frequency \(\omega_c\): for a system with open-loop transfer function \(L(s)\), the frequency at which the open-loop magnitude \(\left|L(s)\right|\) is 1 (\(0\) dB on a Bode plot) and is greater than 1 at lower frequencies. Those familiar with the gain margin and the phase margin, which are defined in Nyquist frequency-domain control theory, will recognize the \(\omega_c\) as the frequency where the phase margin is defined (Nise 2019, sec. 10.6). The gain crossover frequency and the maximum frequency in the bandwidth differ, but they tend to be close in value. For our purposes, assuming that they are similar is reasonable.

It can be shown that the closed-loop bandwidth \(\omega_\text{BW}\) is related to the
closed-loop rise time \(T_r\) via the
approximation^{1} \[
T_r \approx \frac{2}{\omega_\text{BW}}.
\qquad{(1)}\] This heuristic will help us design a PIDF
controller, which we will discuss in the following section.

## Using MATLAB to tune a PIDF controller

We are now ready to use MATLAB to tune a PIDF controller, discretize it, and export it to a C header file for use on the target system.

### Defining the continuous system model

From eq. **¿eq:motor-tf-is?**, the electromechanical
system transfer function including the amplifier gain \(K_a\) (A/V) is \[
G_P(s) = \frac{\Omega_J(s)}{I_S(s)} = \frac{K_a
K_M}{B}\cdot\frac{1}{\tau s + 1}\ ,
\] where \(K_M\) is the motor
constant (N\(\cdot\)m/A), \(B\) is the damping coefficient, \(J\) is the moment of inertia (kg\(\cdot\)m\(^2\)), and \(\tau
= J/B\) is the time constant.

### Getting the T1a Parameters

The T1a specific target system parameters are available in a `.mat`

file at the Uniform Resource Locator
(URL) https://github.com/rtc-book/source/blob/main/matlab/elmech_params/elmech_params_T1a.mat
Load the file `filename`

with

`load(filename);`

Variable `p`

, a `struct`

ure, will
be loaded into the current workspace. The system parameters are members
of `p`

and
can be accessed with, for instance, `p.R`

for the motor armature resistance. See the variable `p_doc`

for
documentation of the parameters.

For other target system parameters, see the following repository: https://github.com/rtc-book/source/tree/main/matlab/elmech_params

With the parameters loaded into the `struct`

`p`

, the transfer
function can be defined as

```
tau = p.J / p.B; % time constant
G = tf([p.Ka * p.Km / p.B], [tau, 1]); % electromech. system
% transfer function
```

We can integrate `G`

, which is the
transfer function \(\Omega_J(s)/I_S(s)\) to the flywheel
angular velocity \(\Omega_J\), with a
factor of \(1/s\) to yield an output of
angular position \(\Theta_J\) and
assign this to `GP`

with

```
s = tf([1, 0], [1]); % make s a tf object
GP = G / s; % plant tf, Theta/I_S
H = tf([1], [1]); % unity feedback
```

### PIDF controller
design with `pidtune()`

Let us design for a closed-loop rise time \(T_r\) of just under \(30\) ms. We know from eq. 1 that \(\omega_\text{BW} \approx 2/T_r\); in order
to be conservative of the control effort, we start the design with a
slightly longer target rise time of \(40\) ms. The MATLAB function `pidtune()`

accepts as arguments the open-loop transfer function `GP*H`

,
a controller “type” (e.g., `'PI'`

),
and an open-loop gain crossover frequency `wc`

(approximately equal to \(\omega_\text{BW}\)) as follows:

```
Tr = 0.04; % s, design closed-loop rise time (conservative)
wc = 2 / Tr; % rad/s, control bandwidth (gain crossover frequency)
options = pidtuneOptions('DesignFocus', 'reference-tracking');
GC = pidtune(GP * H, 'PIDF', wc, options);
GCL = GC * GP / (1 + GC * GP * H); % closed-loop tf Theta/U_a
```

We have chosen a “design focus” of reference tracking because we will be tracking planned reference path in lab 8 (see section 8.6 for an introduction to path planning).

### Discretizing the transfer functions

To implement the controller `GC`

in lab 8, we must first discretize the model. Simulation of
the discrete system requires discrete plant and feedback transfer
functions as well. Using Tustin’s method,

```
T = 5e-3; % s, sample period (BTI)
GCd = c2d(GC, T, 'Tustin'); % using Tustin's method
GPd = c2d(GP, T, 'Tustin');
Hd = c2d(H, T, 'Tustin');
GCLd = GCd * GPd / (1 + GCd * GPd * Hd); % discrete closed-loop tf
```

### Simulation and evaluation

To evaluate the performance of the control system, we should simulate its step response. Let’s use a command of \(25\) degrees:

```
R_deg = 25; % deg, command angular position
R_rad = R_deg * pi / 180; % rad
t = 0:T:0.2;
Theta = R_rad * step(GCL, t);
ThetaT = R_rad * step(GCLd, t);
```

The results are shown in the upper plot of figure 8.9.
We see that the discrete controller performs similarly to the continuous
controller. The steady-state error is zero and the rise time can be
verified with `stepinfo()`

to
be \(T_r \approx 28.1\) ms, close to
the design requirement of just under \(30\) ms.

Although we will not be applying steps of this size in lab 8, it is interesting to see if the required control
effort is within the capabilities of the current amplifier. The transfer
function for the control effort \(U\)
over the command \(R\) is \[
\frac{I_S(s)}{\Theta_R(s)} = \frac{U(s)}{R(s)} = \frac{G_C(s)}{1 +
G_C(s) G_P(s) H(s)}.
\] Using the discrete versions and defining the transfer function
as `U_R`

,
we can simulate the control effort with

```
U_R = GCd / (1 + GCd * GPd * Hd); % V, control effort cl tf, ampl. in.
u = R_rad * step(U_R, t); % V, amplifier input voltage
u_c = u * p.Ka; % A, amplifier output current
```

The scaling by `R_rad`

, the
magnitude of the input step in radians, uses superposition to give the
amplifier input voltage. Recall that the amplifier gain is included in
the model, so to compute the amplifier output, we must scale `u`

by the
amplifier gain `p.Ka`

to get the amplifier output current `u_c`

. This is
shown in the lower plot of figure 8.9. The required current
is well below the maximum current of the amplifier.

The amplifier’s output voltage is limited by its power supply
voltage, so we should also compute the required voltage. This is given
by the loop equation \[
v_S = v_R + v_L + v_M = I_S R + L \frac{d I_S}{d t} + K_M \Omega_J.
\] In MATLAB, we can estimate the derivative with the `diff()`

function
such that

```
u_v = p.R * u_c(1:end - 1) + ... % amplifier output voltage
p.L * diff(u_c)/T + ...
p.Km * diff(ThetaT)/T; % OmegaT = diff(ThetaT)/T
```

The amplifier output voltage `u_v`

is also
shown in the lower plot of figure 8.9. The peak voltage
required is \(9.19\) V, which is below
the source voltage.

Therefore, all the controller design requirements have been achieved with our design for the T1a specific target system. While the particularities of the design differ for specific T1 systems, the design process should proceed in a similar manner.

*Advanced PID Control*. ISA Instrumentation, Systems; Automation Society.

*Control System Engineering*. 8th ed. Wiley.

See astrom2006. The MATLAB documentation for

`pidtune()`

has a similar approximation: the “response time” is approximately \(1/\omega_c\).↩︎

## Online Resources for Section 8.5

No online resources.