final commit
[des2015.git] / marsrover / document / robot.tex
index bf0d685..27efa44 100644 (file)
@@ -1,14 +1,28 @@
 \section{Robot architecture}
 \subsection{Tools}
-\emph{LeJOS}~\cite{lejos_team_lejos_2015}
-
-\emph{XText}
-
-\emph{eclipse}
-
-\emph{Antlr}
-
-\emph{Xtend}
+\paragraph{\emph{LeJOS}~\cite{lejos_team_lejos_2015}} is an alternative
+operating system for the \textsc{lego}$^{\small\textcopyright}$ \textsc{EV3}
+bricks. By default the bricks come preinstalled with their own operating system
+in which you can write programs. The programs have to be written in a
+\textsc{C} dialect or in a graphical programming tool. \emph{LeJOS} on the
+other hand is a constantly developed toolkit to write the programs in Java.
+Because of this we can reuse Java standard libraries and program in a familiar
+language that allows enough detail for the task.
+
+\paragraph{\emph{XText}, \emph{XTend} and \emph{Antlr}} are a collection of
+tools working together to provide the functionality for writing, parsing,
+generating and validating the DSL. \emph{XText} is the format used to write the
+concrete syntax in. The \emph{XText} syntax is converted to a so called
+\emph{ecore} model which can be read by \emph{XTend} programs. \emph{XTend}
+program can do code generation and validation. All the parsing is done by the
+parser generator library \emph{Antlr}.
+
+\paragraph{\emph{eclipse}} is the integrated development environment that ties
+all the tools together. The DSL is developed in \emph{eclipse} and when the
+language infrastructure is generated you can open a new instance of
+\emph{eclipse} to write the actual DSL code in. In this way the user of the
+child-instance of \emph{eclipse} has no view on the underlying mechanisms
+generating the code and processing the data.
 
 \subsection{Design patterns}
 \subsubsection{Producer-Consumer}
@@ -28,13 +42,13 @@ limitation of the Bluetooth this is very hard or even impossible to achieve.
 The \emph{producer-consumer} paradigm always requires to certain parameters to
 be set. It could very well be the case that the \emph{Producer} produces more
 then the \emph{Consumer} can process. There are several strategies one could
-use.  For example we could send the sensor data from the \emph{Producer} all
+use. For example we could send the sensor data from the \emph{Producer} all
 the time, even when there are no updates. Since all communication happens via
 Bluetooth it could be the case that the bandwidth is not high enough and
 reading are queued and therefore reach the \emph{Consumer} too late. Another
 approach would be to only send the differences in sensor values. In this way we
 limit the needed bandwidth and still the \emph{Producer} can send its data
-immediately without having to queue a lot.  To keep the \emph{Producer} as dumb
+immediately without having to queue a lot. To keep the \emph{Producer} as dumb
 as possible we do all the interpretation of the sensor values on the
 \emph{Consumer}.
 
@@ -100,28 +114,14 @@ actuators when asked for.
        \caption{Subsumption architecture}\label{fig:sub}
 \end{figure}
 
-
 We use the pre-implemented architecture from the \emph{LeJOS} where with the
 use of a \texttt{suppressed} flag in every behaviour. The \texttt{suppressed}
-variable is a flag that is set when \texttt{suppress} is called and the
+flag that is set when the \texttt{suppress} function is called and the
 \texttt{action} function will monitor said variable to be able to stop when it
-is suppressed. To be able to let the robot finish some critical actions (eg.\
-turning a specific number of degrees) we need some changes to the standard
-architecture. More concretely we change the standard \texttt{suppressed}
-boolean to a three state variable.
-
-When such a task is started and said behaviour does not ask for control the
-behaviour can still finish if it is not interrupted. For example when the left
-light sensor detects that the robot is driving of the planet a right turn of
-$90$ degrees may be initiated. This right turn will be completed even when the
-left light sensor is not detecting a dangerous value.  The suppressed flag can
-take three states. \texttt{IDLE}, \texttt{IN\_ACTION} and \texttt{SUPPRESSED}.
-By default all behaviours have the \texttt{IDLE} state. When a behaviour is
-started the state will change to \texttt{IN\_ACTION} and when a behaviour
-finished the state will be reset to \texttt{IDLE}. When a behaviour needs to be
-interrupted the state is set to \texttt{SUPPRESSED} and since the behaviour is
-always monitoring the state it will shutdown as soon as possible and reset the
-state.
+is suppressed. The entire action will always finish, however when the flag is
+set all loops are terminated immediately leaving only atomic actions which are
+executed almost always asychronously. Therefore the action will stop almost
+immediately after suppress is called.
 
 Since the task of the robot is to perform certain missions in sequence we also
 added a special kind of behaviour to the standard architecture. This behaviour,
@@ -164,6 +164,87 @@ mapping is described in \autoref{tab:mapping}.
 \end{table}
 
 \subsection{Domain Specific Language}
+A domain-specific language (DSL) is a programming language or executable
+specification language that offers, through appropriate notations and
+abstractions, expressive power focused on, and usually restricted to, a
+particular problem domain~\cite{van2002domain}. The DSL for the robot is
+designed so that the robot is able to perform multiple missions consisting of
+behaviours. A mission is basically a set of behaviours operating following the
+subsumption architecture assisted by a special purpose behaviour that shuts
+down the runtime to make room for a new mission. The complete grammar of the
+DSL can be found in \autoref{lst:grammar}.
+
+\autoref{fig:dsl} describes the hierarchy of the grammar. A \emph{Robot} is a
+set of constants combined with a list of \emph{Behaviour}s and a list of
+\emph{Mission}s. Missions consist of a \emph{StoppingCondition} and a list of
+references to \emph{Behaviour}. A \emph{Behaviour} is a literal representation
+of our interpretation of the subsumption architecture. Thus a control predicate
+and a sequence of \emph{Action}s.
+
+\emph{Action}s represent the real action the robot will perform. This can be an
+external action such as moving motors but also an internal action such as
+waiting or set something in memory.
+\emph{StopppingCondition}s are logical expression that can contain values from
+sensors or queries on the memory. To limit the complexity of the grammar a
+prefix notation is used for binary and unary operators.
+
+\begin{figure}[H]
+       \centering
+       $\xymatrix{
+               & *+[F]{Robot - Constants}\ar[dl]\ar[d]\ar[dr]\\
+               *+[F]{Mission_{\ldots}} & *+[F]{Mission_k}\ar[dl]\ar[d]\ar[dr]\ar[drr] & *+[F]{Mission_{\ldots}}\\
+               *+[F]{StoppingExpression} & *+[F]{Behaviour_{\ldots}} & *+[F]{Behaviour_k}\ar[dl]\ar[d] & *+[F]{Behaviour_{\ldots}}\\
+               & *+[F]{StoppingExpression} & *+[F]{Action_1}\ar[d]\\
+               && *+[F]{Action_2}\ar[d]\\
+               && *+[F]{Action_{\ldots}}\\
+       }$
+       \caption{Robot Domain Specific Language}\label{fig:dsl}
+\end{figure}
 
+To assist the user programming in the DSL small validation mechanism have been
+added. It could happen that the user specifies a mission that contains two
+behaviours both want control at all times. A behaviour that wants control at
+all times can be a valid design but when there are two with that property, only
+one will be used. To avoid such situations the IDE will warn the user when this
+is the case. The full code for the code validation can be found in
+\autoref{lst:val}. The full DSL instance used in the demonstration can be found
+in \autoref{lst:inst}.
 
 \subsection{Code Structure}
+The complete code generation code can be found in \autoref{lst:gen}. The
+following enumeration shows what is specifically generated per grammar object.
+All generated code can not function without the library. The library is an
+entire runtime that only needs a little amount of data plugged in to function
+as the program of a robot.
+
+The library provides an extension on the \texttt{Behavior} class from the
+\emph{LeJOS} library that offers several higher level operations such as
+turning and measuring. All the sensor data can be queried from the
+\texttt{SensorCollector} class that is available in every behaviour. This class
+provides up to date sensor data from both bricks. The special behaviour that
+terminates the current mission is an extension on the already extended
+\texttt{Behavior} class and is called \texttt{ShutdownBehaviour.java}.
+
+\begin{itemize}
+       \item\textbf{Robot}\\
+               A \texttt{Constants.java} file is created to hold the global speed and
+               acceleration values.
+       \item\textbf{Mission}
+               In the java code a mission is a list of defined behaviours plus the
+               special \emph{ShutdownBehaviour}. To make this simple all the missions
+               combined generate a single \texttt{Missions.java} file that has one
+               static function that will return a list of \emph{Mission}s that the
+               main program will use.
+       \item\textbf{Behaviour}
+               For every \emph{Behaviour} a file is created containing the specific
+               implementation. For example for a behaviour called \texttt{Wander} a
+               file called \texttt{WanderBehaviour.java} is created that contains the
+               class \texttt{WanderBehaviour} which implements the
+               \texttt{BasicBehaviour} class from the library.
+       \item\textbf{StoppingExpression}
+               Stopping expressions are used in missions and in behaviours and are
+               converted to java boolean expressions in-line.
+       \item\textbf{Action}
+               Actions are in-line converted to the respective calls to the library or
+               implemented directly.
+\end{itemize}