\chapter{Design and Implementation}\label{chap:design}
This chapter outlines and further describes in details the design decisions behind the end-product of this project, as well as the specifics on the implementation of the project.

\section{Phases of Development}
The following phases of development are not grouped chronologically over the span of the project schedule, rather into conceptual groups.

\subsection{Preparing Development and Build Environment}
Setting up the development environment for this project was a non-trivial task, due to a lack of pre-compiled binaries for the intended architecture, the ARM chipset of the Raspberry Pi.

The Raspberry Pi 4B used in this project was delivered with a Quad Core Cortex-A72 64-bit SOC. However, a 32-bit kernel OS was used in this project, due to the netHAT drivers only delivered as 32-bit compiled binaries.

\subsubsection{Intel RealSense SDK}
The Intel RealSense SDK, or librealsense, is a cross-platform library provided by Intel for use with their RealSense devices. Pre-compiled binaries for 32-bit ARM were not provided, and therefore must be compiled\cite{realsense_git}.

\subsubsection{ZeroMQ}
ZeroMQ is a lightweight asynchronous messaging library which extends the traditional socket interfaces\cite{zeromq_git}. In this project, ZeroMQ is used in order to:
\begin{itemize}
\item Broadcast raw sensor data to remote controllers using a publish/subscribe model
\item Exchange configuration parameters between the remote controller and the local processor using a request/reply model
\end{itemize}

In this project, TCP sockets are used for communications.

ZeroMQ was chosen to provide the aforementioned functionalities for the following reasons:
\begin{itemize}
\item Simple to implement
\item Data can be transferred as binary data, instead of requiring it to be serialized
\item Availability on numerous platforms, as well as pre-compiled binaries
\end{itemize}

\subsubsection{Real-Time Kernel Patch}
Traditionally, the Linux kernel only allows one process to preempt another process in specific circumstances. This means, that even a high-priority thread may not preempt kernel code, until the kernel explicitly yields control.

This is particularly disadvantageous for any operations requiring real-time performance. In order to circumvent this, the \verb|CONFIG_PREEMPT_RT| Kernel Patch is used in order to allow kernel code to be preempted\cite{rtwiki}.

In the case of this project, this means that the local processor can process and deliver data in a more deterministic fashion\todo{maybe insert some test data here}.

\subsubsection{GUI with Qt}
The Qt GUI framework was used in order to create a GUI for the remote controller. This allowed for the sensor data to be more easily calibrated and aligned, as well as providing a consistent interface for end-user configuration.\todo{insert image of the interface}

\subsection{Main Functionality}
This subsection elaborates the fundamental building blocks of the program.

\subsubsection{Transmission of Raw Sensor Data}

\subsubsection{Calibration of Sensor Data}

\subsubsection{Configuration for Processing}

\subsubsection{Transmission of Configuration Parameters}


\subsection{Testing and Validation}

\section{Process Overview}
\begin{figure}[h]
	\centering
	\includegraphics[width=0.8\textwidth]{./design/ProcessOverview}
	\caption{Overview of the communication and processing process between the remote controller and the local processor.}
\end{figure}

\section{Software Architecture}\label{sec:softarch}
\begin{figure}[h]
	\centering
	\includegraphics[width=0.7\textwidth]{./design/SoftwareArchitecture}
	\caption{Overview of the interactions between the various software components and their communication.}
\end{figure}

What is being referred to \textit{the program} for the entirety of this document actually refers a two-part software suite consisting of:
\begin{itemize}
\item \textbf{FlowPi:} The local processing software that runs on the Raspberry Pi, and
\item \textbf{FlowRemote: } The remote control software that is meant to run on an external PC for configuration purposes
\end{itemize}

\subsection{Development Language Choice}
The program is written in \cpp\ for compatibility and performance reasons. All the device drivers provide libraries in either \clang\ or \cpp, while some drivers such as the CIFX library for the netHAT are only provided in \clang. 

The topic of performance between languages and systems are a topic of much heated debate, however \cpp\ was chosen for this project do to the ability to program comfortably in a higher-level language, while having the ability to \textit{\enquote{drop-down}} into \clang. The \clang\ Programming Language is often the benchmark for higher-level programming languages when programming for Real-Time Systems due its predictability and the ability to run operations with few layers of abstraction on memory directly\cite{pizlo2010}. 

Furthermore, since the scale of the processing unit of the program is relatively small, the benefits that come from using a higher-level programming language --- such as increased productivity, organization, and re-usability\cite{pizlo2010} --- are not strictly necessary.

\begin{figure}[H]
	\centering
    \includegraphics[width=0.8\textwidth]{./design/ProcessingLibrary}
    \caption{Representation of the how the processing unit is called by different components of the program.}
    \label{fig:processinglib}
\end{figure}

As shown in \autoref{fig:processinglib}, the main functionality of the processing unit which include:

\begin{itemize}
\item Image Pre-Processing i.e. rotation, skew, cropping
\item Curve-Fitting
\item Cross-Correlation
\end{itemize}

are implemented in \clang\ as much as possible. This is then encapsulated by a \cpp\ wrapper. This provides ease-of-use on the remote side, where processing is not real-time critical, while still allowing the local side to directly call the \clang\ processing functions.

\subsection{FlowRemote -- Remote Control GUI}

\begin{figure}[h]
	\centering
	\includegraphics[width=0.9\textwidth]{./design/FlowRemote}
	\caption{Overview of the FlowRemote features and configurable parameters.}
\end{figure}




\section{Data Processing Details}