\input{ccs_gyak_defs}
\author{Homework 3}

\tikzstyle{block} = [draw, rectangle, minimum height=1em, minimum width=1em]
\tikzstyle{sum} = [draw, circle, node distance=1cm]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]

\begin{document}
\begin{center}
\textbf{Computer Controlled Systems} \\
\theauthor \\
Submission deadline: November 16. 2017. 10:00/12:00 (end of the seminar)
\end{center}

\begin{center}
All solutions are expected to be calculated by hand, also all figures have to be drawn by hand. Computer programs can be used for self-verification, but all problems have to contain the detailed steps of solutions!
\end{center}

\subsubsection*{Frequency response analysis and controller design for a HDD head%
    \footnote{Model is borrowed from \href{https://www.mathworks.com/help/control/ug/hard-disk-readwrite-head-controller.html}{MathWorks}}
}

Consider a HDD read/write head with dynamics described by the following differential equation: \\
\begin{minipage}{0.69\textwidth}
    \[
        J\ddot{\theta} + b \dot{\theta} + k_1 \theta = k_2 u
    \]
    where $\theta$ denotes the angular position of the head, while $u$ is the input.

    The names and values of the system constants are:

    \begin{tabular}{ll}
    $J=0.01~kgm^2$ & inertia of the head assembly \\
    $b=0.004~Nm/(rad/sec)$  & viscous damping coefficient of the bearings \\
    $k_1=10~ Nm/rad$ &  return spring constant \\
    $k_2=0.05~Nm/rad$ &  motor torque constant
    \end{tabular}
\end{minipage}\hfill
\begin{minipage}{0.29\textwidth}
	\includegraphics[width=\textwidth]{fig/HDD.png}
\end{minipage}

\noindent

By choosing the angular position as output variable ($y=\theta$), the input-output model of the HDD head is:
\[
\ddot{y}(t)+0.4 \dot{y}(t) +1000 y(t) = 5u(t)
\]
The head has a (damped) oscillatory behaviour, which is unacceptable. Moreover, it has to be positioned to prescribed values in a short time. To fulfill these needs, a PID controller is applied to the system as depicted in the figure below.

	% \begin{figure}[!h]
	% \centering
	% \includegraphics[scale=0.6]{fig/pid.pdf}
	% \end{figure}

\begin{center}
    \begin{tikzpicture}[auto, node distance=1cm,>=latex']
        % We start by placing the blocks
        \node [coordinate, name=input] {};

        \node [sum, right of=input,node distance=2cm] (sum) {};
        \node [block,minimum height=1cm, fill=green, right=of sum] (C) {Controller: $C(s)$};
        \node [block,minimum height=1cm, fill=yellow, right=of C] (G) {HDD head: $H(s)$};
        \node [coordinate, right of=G, node distance=4cm] (output) {};
        \node [coordinate, below of=C] (tmp) {};

        \draw [draw,->] (input) -- node[pos=0.3] {$r(t)$} node[pos=0.9] (r) {} (sum);
        \draw [->] (sum) -- node {$e(t)$} (C);
        \draw [->] (G) -- node [pos=0.3] (y) {} node [pos=0.7] {$y(t)$} (output);
        \draw [->] (y) |- (tmp) -| node[pos=0.95]{-}(sum);
        \draw [->] (C) -- node (u) {$u(t)$} (G);

        \draw
            node [red, below of=u, node distance=1.7cm] (Ge_label) {$G(s)$}
            node [draw=red!30,inner sep=8pt,rounded corners=2mm,fit=(C) (r) (Ge_label.mid) (y) (tmp)] (Ge) {}
            ;
    \end{tikzpicture}
\end{center}

\begin{enumerate}
\item Determine the transfer function $H(s)$ of the input-output model by Laplace-transform!
\item Check the poles of $H(s)$! Is the system stable? What refers to the oscillatory behaviour?
\item Give the frequency response function $H(j\omega)$ of the system! Give its gain $k(\omega)$ and phase $\phi(\omega)~$ at $\omega=0~rad/s$ and $\omega=32~ rad/s$~! What is the response $y(t)$ to the periodic input $u(t)=10 \sin(32t)$~?
\item Give the transfer function $G(s)$ of the closed loop system!
\item Determine the tuning parameters $K_P, K_D$ and $K_I$ in such a way that all the poles of the closed loop system are equal to $-20~$!
\item Determine the DC gain of the closed loop system! Does this controlled system follow a constant reference (head positioning) signal?
\end{enumerate}

\newpage
\begin{qboxr}

\textbf{Extra homework.} Implement and simulate the behaviour of HDD read/write head dynamics.
    \begin{enumerate}
        \item Implement both the controlled system ($H(s)$) and the PID controller ($C(s)$) as a ``Transfer Fcn'' (Figure \ref{TransferFcn2}, or as you want).
        \item Be aware that, in case of a ``Transfer Fcn'', the degree of the denominator must be at least the same as the degree of the denominator. E.g. instead of the improper transfer function $\frac{s^2 K_D + s K_P + K_I}{s}$ use its semi-proper approximate $\frac{s^2 K_D + s K_P + K_I}{\varepsilon s^2 + s}$, where $\varepsilon$ is small enough (e.g. $\varepsilon = 0.001$).
        \item Define a reference signal. You may use the ``Signal Builder'' block (Figure \ref{SignalBuilder}, Figure \ref{SignalBuilder_dialog}).
        \item In the real life, an actuator cannot execute arbitrarily large input signal, e.g. a voltage source cannot generate arbitrarily high voltage. Therefore, before we connect the design control input computed by $C(s)$ to the plant $H(s)$, we should limit the range of the input signal to the interval $u(t) \in [-1000,1000]$. In order to implement this you can use the ``Saturation'' block (Figure \ref{Saturation}).
        \item Plot the response of the control loop alongside the reference signal (Figure \ref{Scope}, Figure \ref{Scope_dialog}).
    \end{enumerate}

    You are allowed to solve the problem as you prefer. However, to compute a signal's derivative, let me advise you against using the ``Derivative'' block (it is quite erroneous). Instead, use a semi proper transfer function $D(s) = \frac{s}{\varepsilon s + 1}$, where $\varepsilon$ is small enough (e.g. $\varepsilon = 0.001$) to give a proper approximate of the signal's derivative.

    \begin{minipage}{0.2\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/Derivative.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{Derivative}
    \end{minipage}\hfill
    \begin{minipage}{0.2\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/Gain.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{Gain}
    \end{minipage}\hfill
    \begin{minipage}{0.5\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/Saturation.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{Saturation}
    \end{minipage}\hfill
    \begin{minipage}{0.3\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/Scope_ry.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{Scope}
    \end{minipage}\hfill
    \begin{minipage}{0.25\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/SignalBuilder.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{SignalBuilder}
    \end{minipage}\hfill
    \begin{minipage}{0.25\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/TransferFcn.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{TransferFcn}
    \end{minipage}\hfill
    \begin{minipage}{0.4\textwidth}
        \centering
        \includegraphics{fig/Simulink_Blocks/TransferFcn2.png}\\
        Figure \refstepcounter{figure}\thefigure.
        \label{TransferFcn2}
    \end{minipage}\hfill

    \begin{minipage}{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{fig/Simulink_Blocks/SignalBuilder_dialog.png} \\
        Figure \refstepcounter{figure}\thefigure.
        \label{SignalBuilder_dialog}
    \end{minipage}\hfill
    \begin{minipage}{0.49\textwidth}
        \centering
        \includegraphics[width=\textwidth]{fig/Simulink_Blocks/Scope_dialog.png} \\
        Figure \refstepcounter{figure}\thefigure.
        \label{Scope_dialog}
    \end{minipage}
\end{qboxr}

\end{document}



    % Control loop WITH actuator fault
    \begin{tikzpicture}[auto, node distance=1cm,>=latex']
        % We start by placing the blocks
        \node [coordinate, name=input] {};

        \node [sum, right of=input,node distance=2cm] (sum) {};
        %\node [sum2, right of=sum] (sum2) {};

        \node [block,minimum height=1cm, fill=green, right of=sum, node distance=2.5cm] (C) {Controller: $C(s)$};
        \node [sum, right of=C, node distance=3cm] (sum2) {};
        \node [block,minimum height=1cm, fill=yellow, right of=sum2, node distance=3cm] (G) {HDD head: $H(s)$};
        \node [coordinate, right of=G, node distance=4cm] (output) {};
        \node [coordinate, below of=C] (tmp) {};
        \node [above of=sum2, node distance=1.2cm] (d) {$d(t)$};

        % Once the nodes are placed, connecting them is easy.
        \draw [draw,->] (input) -- node[pos=0.3] {$r(t)$} node[pos=0.9] (r) {} (sum);
        %\draw [draw,->] (sum) -- node{} (sum2);
        \draw [->] (sum) -- node {$e(t)$} (C);
        \draw [->] (G) -- node [pos=0.3] (y) {} node [pos=0.7] {$y(t)$} (output);
        \draw [->] (y) |- (tmp) -| node[pos=0.95]{-}(sum);
        \draw [->] (C) -- node {$v(t)$} (sum2);
        \draw [->] (sum2) -- node {$u(t)$} (G);
        \draw [->] (d) -- (sum2);

        \draw
            node[red] at (d -| sum) {$G(s)$}
            node [draw=red!30,inner sep=8pt,rounded corners=2mm,fit=(r) (d) (y) (tmp)] (Ge) {}
            ;
    \end{tikzpicture}




