\chapter{Control loops}
\label{B}

\section{Control theory}
The theory of controls is a branch of science and technology which studies how to drive a given dynamical system. This is generally characterized by inputs and outputs and the former can be manipulated to obtain a desired output, which is chosen by a reference setpoint. The design and the technology involved depends on the system, but in general they imply a sensing section, a software section which can modify some features of the input and a feedback section to check that the manipulation of the input signal gives the output as set by the reference.
\subsection{Principles: control loops}
The collection of all the sections forms a control loop and manages the behaviour of a given variable under exam. Control loops can be:
\begin{itemize}
\item{Open-loop}: the control action is independent from the output.
\item{Closed-loop}: the control action depends on the desired output conditions. This kind of loop uses feedback loops, that assure that the process is correctly going on, i.e. the value of the variable under exam is that of the setpoint.
\end{itemize}

\noindent
In order to design a control loop, we need to build all the subsystems up: the main one is the \textbf{plant}, which is the physical parameter to be controlled; the plant is measured by one or more \textbf{sensors}, which detect a deviation (if any) in the signal from the reference setpoint. An error signal is then produced and processed by a \textbf{controller} into a correction signal that is sent to an \textbf{actuator}. The actuator applies the correction to the plant.


\subsection{How to: block diagrams}
Block diagrams are useful graphical instruments to describe, study and build a control loop. Each element of the control system is represented by a block and each block is joined by lines with arrows showing the sequence of controls. Following the logical steps stated before, we can then draw the block diagram of a basic system to be controlled as in Fig. \ref{blockB}.

\begin{figure}[h!]
\centering
\includegraphics[scale=0.8]{images/block.png}
\caption[Basic block diagram of a control loop.]{Basic block diagram of a control loop. The setpoint is injected as an input.}
\label{blockB}
\end{figure}

\noindent
Every variable of interest at the output of each block can be evaluated by \textit{solving} the diagram. Referring to Fig. \ref{blockB}, solving a block diagram means solving the system of equations involving the variable under exam and each block. The product of the components of the block diagram give the total gain of the loop.

\section{Control analysis}
Once the control loop has been schematically drafted, it needs to be finalized: the software section implies instructions. These are given by a computation of the transfer functions of the whole system, which gives the response in the frequency domain of the output to a given input. The computed (and measured) transfer function will then be modified with suitable filters to make the output adjust to the reference setpoint.
\subsection{Transfer functions}
Every dynamical system is characterized by equations of motion in the time domain. In order to study the system in the frequency domain, it is possible to apply a Laplace transform. So, for any function f(t) in the time domain, the Laplace transform is defined as:

\begin{equation}
\centering
F(s) = \int e^{-st}f(t)dt
\end{equation}

\noindent
where s = a + i$\omega$.\\
A transfer function is defined as the ratio between the output and the input, in the frequency domain. For any input X(s) and output Y(s), the transfer function is:

\begin{equation}
\centering
T(s) = \dfrac{Y(s)}{X(s)}
\end{equation}

\noindent
and so the output is characterized by the product between the transfer function and the input signal in the Laplace domain, which is the Laplace transform of the convolution of the two functions in the time domain\footnote{An interesting demonstration of this statement can be found in \cite{wiki}}.\\
Since, in general, a function can be written as a product of polynomials, the transfer function is also in the form:

\begin{equation}
\centering
T(s) = C \Pi_{i,j} \dfrac{(s-z_i)^j}{(s-p_i)^j}
\end{equation}

\noindent
where z and p are the $i$th zeros and poles of the polynomial, of order $j$ and C is the amplitude. This form is useful to study the stability of the system:
\begin{itemize}
\item{Re(p) $<$ 0}: the system is asymptotically stable;
\item{Re(p) = 0 with j = 1}: the system is marginally stable;
\item{Re(p) = 0 with j $>$ 1 or Re(p) $>$ 1}: the system is unstable.
\end{itemize}

\noindent
The gain of the system is defined as the ratio between the amplitudes of the input and output, i.e. it's the absolute value of the transfer function:

\begin{equation}
\centering
G = \dfrac{\mid Y(s)\mid}{\mid X(s)\mid} = \mid T(s)\mid
\end{equation}

\noindent
and the phase is $\varphi$ = arg(T(s)).\\
In the frame of control loops, the transfer function is given by the gain contributions of all the subsystems of the loop. So, recalling the previous block diagram, if we want to know the transfer function of the system, we need to solve the system of equations:

\begin{equation*}
\centering
\left\{\begin{split}
&X_{err} = IN+OUT\\
&x_f = C \cdot X_{err}\\
&X_a = A \cdot X_f\\
&X = P \cdot X_a\\
&OUT =  S \cdot X
\end{split}\right.
\end{equation*}

\begin{equation}
\centering
OUT = S \cdot P \cdot A \cdot C \cdot (IN+OUT) = G \cdot (IN+OUT),
\end{equation}

\noindent
where G = S P A C. The transfer function is then:

\begin{equation}
\centering
\dfrac{OUT}{IN} = \dfrac{G}{1-G},
\end{equation}

\noindent
which is also called the \textit{open loop gain}. Since normally the variable we want to control is $X_{err}$, we want to solve the closed loop:

\begin{equation}
\centering
X_{err} = IN + G \cdot X_{err} = \dfrac{IN}{1-G}.
\end{equation}

\noindent
The term 1/(1-G) is called \textit{closed loop gain}.

\subsection{Phase and magnitude interpretation: the Bode plot}
The Bode plot is a graph representing the response in frequency of the magnitude and phase of the system under exam. It is largely used to define the marginal conditions for the stability of the loop. The magnitude is expressed in dB = 20$\log_{10} (x)$ and it is computed as the absolute value of the transfer function:

\begin{equation}
\centering
\mid T(s) \mid = \sqrt{T \cdot T^*}.
\end{equation}

\noindent
The phase is expressed in degrees (deg) and it is computed as:

\begin{equation}
\centering
\varphi =  -\arctan \left(\dfrac{ImT(s))}{Re(T(s))}\right).
\end{equation}

\noindent
In the frame of loops, the closed-loop gain was:

\begin{equation*}
\centering
G_{CL} = \dfrac{1}{1-G},
\end{equation*}

\noindent
where G is a pole for this relation. This means that if G = 1, G$_{CL}$ diverges and the loop is unstable. On the phase plot, this corresponds to $\varphi$ = 180$^{\circ}$. In general, when the trace on the phase plot approaches this value at certain frequencies, it means that the loop that we are building is unstable in that region.

\subsection{Spectral density}
Spectral densities are views of a signal in a frequency spectrum. It is a useful tool to detect effects on the signal during processing, like peaks due to harmonics, or resonances. The physical parameter used in this study is the power spectral density, which measures the power of a signal as a function of frequency and has units of W/Hz$^{-1/2}$. When there is no direct power associated to the measurement (like in case of Volts) the units are in terms of the square of the signal per Hz. In some cases, an Amplitude Spectral Density (ASD), defined as the square root of the power spectral density, is used when the shape of the signal is quite constant; in this case the units are in the form of 1/Hz$^{-1/2}$ and the variations in the ASD will then be proportional to the variations of the signal itself.

\subsection{Coherence}
The coherence is a statistic relation between two signals or data sets x and y. It is defined as the ratio between the cross spectral density of the two functions and the product of the spectral densities of each function:

\begin{equation}
\centering
C_{xy}(f) = \dfrac{\mid ASD_{xy}(f) \mid ^2}{ASD_{xx}(f)ASD_{yy}(f)}.
\end{equation}

\noindent
Coherence is a useful parameter to estimate the correlation between the two signals. In control loops, it can be used to verify how much the output can be predictable by the input. The values of the coherence lies between 0 and 1 and two signals are considered optimally correlated if their coherence approaches 1.

\newpage
\section{The French fry factory}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.35]{images/fries.jpg}
\caption[The French fry factory.]{The French fry factory: basic principles of a control loop as explained by Dr. Jenne Driggers during a private conversation at LIGO Hanford. Original photo.}
\end{figure}

