Merge branch 'report' of github:szmoore/MCTX3420 into report
[matches/MCTX3420.git] / reports / final / chapters / Design.tex
1 \chapter{Design Implementation}
2
3 \section{Overview}
4
5 Figure \ref{software_overview.pdf} shows a high level block diagram of the software design.
6
7
8 \section{Server Program}
9
10 \subsection{Threads}
11
12 The Server Program runs as a multithreaded process under a POSIX compliant GNU/Linux operating system\footnote{Tested on Debian and Ubuntu}. Each thread runs in parallel and is dedicated to a particular task; all threads share the same memory and rescources. The three types of threads we have implemented are:
13 \begin{enumerate}
14   \item Main Thread\ref{} - A single thread which accepts and responds to HTTP requests passed to the program by the HTTP server
15   \item Sensor Thread\ref{} - Each sensor in the system is monitored by a single thread
16   \item Actuator Thread\ref{} - Each actuator in the system is controlled by a single thread
17 \end{enumerate}
18
19 In reality, threads do not run simultaneously; the operating system is responsible for sharing execution time between threads in the same way as it shares execution times between processes. Because the linux kernel is not deterministic, it is not possible to predict when a given thread is actually running. This renders it impossible to maintain a consistent sampling rate, and necessitates the use of time stamps whenever a data point is recorded.
20
21 Figure \ref{} shows a distribution of times between samples for a test sensor with the software sampling as fast as possible. 
22 Figure \ref{} shows the distribution when the sampling rate is set to 20Hz. Caution should be taken when interpreting these results, as they rely on the accuracy of timestamps recorded by the same software that is being time sliced by the operating system.
23
24
25 RTLinux is a version of the linux kernel that attempts to increase the predictability of when a process will have control\cite{rtlinux}. It was not possible to obtain a real time linux kernel for the BeagleBone. However, testing on an amd64 laptop showed very little difference in the sampling time distribution when the real time linux kernel was used.
26
27
28
29 \subsection{Safety Mechanisms}
30
31
32
33 \subsection{Sensors}
34
35 Figure \ref{sensor_thread.pdf} shows a flow chart for the thread controlling an individual sensor. This process is implemented by \verb/Sensor_Loop/ and associated helper functions.
36
37 All sensors are treated as returning a single floating point number when read. A \verb/DataPoint/ consists of a time stamp and the sensor value. \verb/DataPoint/s are continously saved to a file as long as the experiment is in process. An appropriate HTTP request (see section\ref{}) will cause the main thread of the server program to respond with \verb/DataPoint/s read back from the file. By using independent threads for reading data and transferring it to the GUI, the system does not rely on maintaining a consistent and synchronised network connection. This means that a user can safely close the GUI or even shutdown their computer and return to the experiment later without losing any data.
38
39
40
41 As the chart indicates, the processes of actually controlling sensor hardware has been abstracted out of the control loop. A \verb/Sensor/ structure is defined in \verb/sensor.h/ to represent a single sensor. When this structure is initialised, function pointers must be provided; these functions can then be called by \verb/Sensor_Loop/ as needed. All functions related to control over specific sensor hardware can be found in the files within the \verb/sensors/ sub directory.
42
43 Earlier versions of the software instead used a \verb/switch/ statement based on the \verb/Sensor/'s id number to determine how to obtain the sensor value. This was found to be difficult to maintain as the number and types of sensors supported by the software were increased.
44
45
46
47 \subsection{Actuators}
48
49 Actuators are controlled by threads in a similar way to sensors. Figure \ref{actuator_thread.pdf} shows a flow chart for these threads. This is implemented in \verb/Actuator_Loop/. Control over real hardware is seperated from the main logic in the same way as sensors (relevant files are in the \verb/actuators/ sub directory). The use of threads to control actuators gives similar advantages in terms of eliminating the need to syncronise the GUI and server software.
50
51 The actuator thread has been designed for flexibility in how exactly an actuator is controlled. Rather than specifying a single value, the main thread initialises a structure that determines the behaviour of the actuator over a period of time. The current structure represents a simple set of discrete linear changes in the actuator value. This means that a user does not need to specify every single value for the actuator. The Actuator thread stores a value every time the actuator is changed to make it easy to compare the user settings
52
53
54
55 \subsection{Data Storage and Retrieval}
56
57 Each sensor or actuator thread stores data points in a seperate binary file identified by the name of the device. When the main thread receives an appropriate HTTP request, it will read data back from the binary file. To allow for selection of a range of data points from the file, a binary search has been implemented.
58
59 Several alternate means of data storage were considered for this project. Binary files were chosen because of the significant performance benefit (see Figure \ref{}) and ease with which data can be read from any location in file and converted directly into values. A downside of using binary files is that the server software must always be running in order to convert the data into a human readable format.
60
61 \subsection{Authentication}
62
63 The \verb/Login_Handler/ function is called in the main thread when a HTTP request for authentication is received. This function checks the user's credentials and will give them access to the system if they are valid.
64
65 Whilst we had originally planned to include only a single username and password, changing client requirements forced us to investigate many alternative authentication methods to cope with multiple users.
66
67 Several authentication methods are supported by the server; the method to use can be specified as an argument when the server is started.
68 \begin{enumerate}
69   \item Unix style authentication
70   Unix like operating systems store a plain text file (/etc/shadow) of usernames and encrypted passwords. To check a password is valid, it is encrypted and then compared to the stored encrypted password. The actual password is never stored anywhere. The /etc/shadow file must be maintained by shell commands on the beaglebone.
71
72   \item Lightweight Directory Access Protocol (LDAP)
73   LDAP is a widely used data base for storing user information. A program that uses LDAP for authentication can query an LDAP server over a network; the LDAP server will respond indicating if the user and password match those stored in its database.
74
75   The UWA user management system (pheme) employs an LDAP server for storing user information and passwords. The software has been designed so that it can interface with an LDAP server configured similarly to the server on UWA's network. Unfortunately we were unable to gain permission to query this server.
76
77   \item MySQL Database
78   BLARGH
79
80   MySQL databases are vulnerable to many different security issues. Care should be taken to ensure that all these issues are addressed before deploying the system.
81 \end{enumerate}
82
83 \subsection{Server API}
84
85 \subsection{Performance}
86
87 Figure \ref{} shows the CPU and memory usage of the server program with different numbers of dummy sensor threads. This gives an idea of how well the system would scale if all sensors were run on the same BeagleBone.
88
89 \pagebreak
90 \begin{figure}[H]
91         \centering
92         \includegraphics[width=1.1\textwidth]{figures/sensor_thread.pdf}
93         \caption{Flow chart for a sensor thread} 
94         \label{sensor_thread.pdf}
95 \end{figure}
96 \pagebreak
97 \pagebreak
98 \begin{figure}[H]
99         \centering
100         \includegraphics[width=1.1\textwidth]{figures/actuator_thread.pdf}
101         \caption{Flow chart for an actuator thread} 
102         \label{actuator_thread.pdf}
103 \end{figure}
104 \pagebreak
105
106 \section{Image Processing}
107
108 \section{Client Program}
109
110 \subsection{Human Computer Interaction}
111
112 \subsection{Interaction with API}
113
114
115
116
117
118
119
120
121

UCC git Repository :: git.ucc.asn.au