f409a9cf178e8042c5b7599f4de476f4162f2b60
[des2015.git] / marsrover / document / robot.tex
1 \section{Robot architecture}
2 \subsection{Tools}
3 \paragraph{\emph{LeJOS}~\cite{lejos_team_lejos_2015}} is an alternative
4 operating system for the \textsc{lego}$^{\small\textcopyright}$ \textsc{EV3}
5 bricks. By default the bricks come preinstalled with their own operating system
6 in which you can write programs. The programs have to be written in a
7 \textsc{C} dialect or in a graphical programming tool. \emph{LeJOS} on the
8 other hand is a constantly developed toolkit to write the programs in Java.
9 Because of this we can reuse Java standard libraries and program in a familiar
10 language that allows enough detail for the task.
11
12 \paragraph{\emph{XText}, \emph{XTend} and \emph{Antlr}} are a collection of
13 tools working together to provide the functionality for writing, parsing,
14 generating and validating the DSL. \emph{XText} is the format used to write the
15 concrete syntax in. The \emph{XText} syntax is converted to a so called
16 \emph{ecore} model which can be read by \emph{XTend} programs. \emph{XTend}
17 program can do code generation and validation. All the parsing is done by the
18 parser generator library \emph{Antlr}.
19
20 \paragraph{\emph{eclipse}} is the integrated development environment that ties
21 all the tools together. The DSL is developed in \emph{eclipse} and when the
22 language infrastructure is generated you can open a new instance of
23 \emph{eclipse} to write the actual DSL code in. In this way the user of the
24 child-instance of \emph{eclipse} has no view on the underlying mechanisms
25 generating the code and processing the data.
26
27 \subsection{Design patterns}
28 \subsubsection{Producer-Consumer}
29 Ultimately we want to only program one robot. The fact that the one robot
30 contains multiple control bricks is something that needs to be abstracted away
31 from following a low level paradigm called \emph{producer-consumer}. As will be
32 discussed in Section~\ref{sec:mapping} the first brick, from now on
33 \emph{Consumer}, has the direct control over all the motors. However the second
34 brick, from now on \emph{Producer}, controls no motors. Both the
35 \emph{Consumer} and the \emph{Producer} control $4$ sensors. Because of this
36 configuration the \emph{Producer} only needs to send its sensor data to the
37 \emph{Consumer}. There is no need to communicate anything back since the
38 \emph{Producer} can not respond in any physical way. An option could be to
39 distribute the processing power but due to the strength of the bricks and the
40 limitation of the Bluetooth this is very hard or even impossible to achieve.
41
42 The \emph{producer-consumer} paradigm always requires to certain parameters to
43 be set. It could very well be the case that the \emph{Producer} produces more
44 then the \emph{Consumer} can process. There are several strategies one could
45 use. For example we could send the sensor data from the \emph{Producer} all
46 the time, even when there are no updates. Since all communication happens via
47 Bluetooth it could be the case that the bandwidth is not high enough and
48 reading are queued and therefore reach the \emph{Consumer} too late. Another
49 approach would be to only send the differences in sensor values. In this way we
50 limit the needed bandwidth and still the \emph{Producer} can send its data
51 immediately without having to queue a lot. To keep the \emph{Producer} as dumb
52 as possible we do all the interpretation of the sensor values on the
53 \emph{Consumer}.
54
55 All of this combined leads to the visualization of the low level architecture
56 as seen in \autoref{fig:arch}
57
58 \begin{figure}[H]
59 \centering
60 $\xymatrix@C=.5pc@R=1pc{
61 *+[F]{\text{front-ultra}}\ar[ddr]
62 & *+[F]{\text{color}}\ar[dd]
63 & *+[F]{\text{left-touch}}\ar[ddl]
64 & *+[F]{\text{right-touch}}\ar[ddll]
65 & *+[F]{\text{left-light}}\ar[ddrr]
66 & *+[F]{\text{right-light}}\ar[ddr]
67 & *+[F]{\text{back-ultra}}\ar[dd]
68 & *+[F]{\text{gyro}}\ar[ddl]\\\\
69 & *+[F]{\text{producer}}\ar[rrrrr]^{\text{Bluetooth}}
70 & & & & & *+[F]{\text{consumer}}\ar[ddl]\ar[dd]\ar[ddr]\\\\
71 & & & & & *+[F]{\text{left-motor}}
72 & *+[F]{\text{right-motor}}
73 & *+[F]{\text{meas-motor}}
74 }$
75 \caption{Low level robot architecture visualizing data flow}\label{fig:arch}
76 \end{figure}
77
78 \subsubsection{Subsumption}
79 As the higher level architecture we use a slightly adapted version of the
80 subsumption architecture first described by Brooks~\cite{brooks_robust_1986}.
81
82 In the subsumption behaviour there is an arbitrator and several behaviours. A
83 behaviour is a class that contains three functionalities.
84 \begin{itemize}
85 \item \texttt{takeControl} is the function that is called every cycle
86 to determine which behaviour wants to be in control. The
87 execution of the function should always be very fast to make
88 sure behaviours can get control as soon as possible if they
89 need to.
90 \item \texttt{action} is the function that runs after the behaviour is
91 set to be active. The action function must be suppressible at
92 all times within a very short time period. In this way a
93 behaviour can be interrupted by a more important behaviour. When an
94 action stops it should always stop the robot in a \emph{safe} position.
95 \item \texttt{suppress} is the function that is called when a behaviour
96 becomes active while some other behaviour was active before.
97 When it is called the \texttt{action} function of the old
98 behaviour should terminate as soon as possible.
99 \end{itemize}
100
101 The basic architecture is visualized in \autoref{fig:sub} where the behaviour
102 with the lowest number has the highest priority and thus gets control over the
103 actuators when asked for.
104
105 \begin{figure}[H]
106 \centering
107 $\xymatrix{
108 & *+[F]{b_n}\ar[r] & \ar[d]\\
109 & *+[F]{b_{\ldots}}\ar[rr] & & \ar[d]\\
110 & *+[F]{b_2}\ar[rrr] & & & \ar[d]\\
111 *+[F]{\text{Sensors}}\ar[uuur]\ar[uur]\ar[ur]\ar[r]
112 & *+[F]{b_1}\ar[rrrr] & & & & *+[F]{\text{actuators}}
113 }$
114 \caption{Subsumption architecture}\label{fig:sub}
115 \end{figure}
116
117 We use the pre-implemented architecture from the \emph{LeJOS} where with the
118 use of a \texttt{suppressed} flag in every behaviour. The \texttt{suppressed}
119 flag that is set when the \texttt{suppress} function is called and the
120 \texttt{action} function will monitor said variable to be able to stop when it
121 is suppressed. The entire action will always finish, however when the flag is
122 set all loops are terminated immediatly leaving only atomic actions which are
123 executed almost always asychronously. Therefore the action will stop almost
124 immediatly after suppress is called.
125
126 Since the task of the robot is to perform certain missions in sequence we also
127 added a special kind of behaviour to the standard architecture. This behaviour,
128 from now on \emph{Shutdownbehaviour}, is a behaviour has a special
129 \texttt{takeControl} function. This function returns true when the end
130 condition of the mission occurs and the action will stop the arbitrator. The
131 main control loop of the program will then setup a new arbitrator to perform a
132 new mission.
133
134 \subsection{Mapping of the sensors and actuators}\label{sec:mapping}
135 For the first proposal we were required to opt for a mapping for the sensors.
136 After some discussion the initial proposed mapping also became the final
137 mapping of actuators and sensors because of the reasons explained below.
138
139 The actuators are all plugged into the \emph{Consumer} to achieve the maximum
140 safety in case the \emph{Bluetooth} connection fails between the
141 \emph{Consumer} and the \emph{Producer} and one of the actuators is moving.
142 All the safety-critical sensors for movement are placed on the \emph{Consumer}
143 brick too. All other sensors are placed on the \emph{Producer} brick. Any
144 increased latency on those sensors will not danger the safety. The final
145 mapping is described in \autoref{tab:mapping}.
146
147 \begin{table}[H]
148 \centering
149 \begin{tabular}{lll}
150 \toprule
151 & \emph{Consumer} & \emph{Producer}\\
152 \midrule
153 \multirow{2}{*}{Actuators} & Left motor\\
154 & Right motor & \\
155 & Measurement motor\\
156 \midrule
157 \multirow{4}{*}{Sensors} & Left light sensor & Color sensor\\
158 & Right light sensor & Front ultrasone sensor\\
159 & Back ultrasone sensor & Left touch sensor\\
160 & Gyro sensor & Right touch sensor\\
161 \bottomrule
162 \end{tabular}
163 \caption{Mapping of the sensors and actuators}\label{tab:mapping}
164 \end{table}
165
166 \subsection{Domain Specific Language}
167 A domain-specific language (DSL) is a programming language or executable
168 specification language that offers, through appropriate notations and
169 abstractions, expressive power focused on, and usually restricted to, a
170 particular problem domain~\cite{van2002domain}. The DSL is designed so that the
171 robot is able to perform multiple missions consisting of behaviours. An
172 implementation is a list of behaviour descriptions followed by a list of
173 missions referring to behaviours. This main structure of the DSL is
174 visualized in \autoref{fig:dsl}. The missions are performed in sequence.
175
176 \begin{figure}[H]
177 \centering
178 $\xymatrix{
179 & *+[F]{Mission_n} & *+[F]{Behaviour_n} \\
180 & *+[F]{Mission_{\ldots}} & *+[F]{Behaviour_3} \\
181 & *+[F]{Mission_2}\ar[r]\ar[ur]\ar[uur] & *+[F]{Behaviour_2}\\
182 *+[F]{\text{Robot}}\ar[uuur]\ar[uur]\ar[ur]\ar[r]
183 & *+[F]{Mission_1}\ar[r]\ar[ur] & *+[F]{Behaviour_1}
184 }$
185 \caption{Robot Domain Specific Language}\label{fig:dsl}
186 \end{figure}
187
188 As can be seen in \autoref{fig:beh}, a behaviour shall have a condition to make it take control and it shall contain one or more actions. Action is atomic and the use of the motor is defined in Action.
189
190 \begin{figure}[H]
191 \centering
192 $\xymatrix{
193 & & *+[F]{Action_n} \\
194 & & *+[F]{Action_2} \\
195 & *+[F]{Actions}\ar[r]\ar[ur]\ar[uur] & *+[F]{Action_1}\\
196 *+[F]{\text{Behaviour}}\ar[ur]\ar[r]
197 & *+[F]{Take Control}\ar[r] & *+[F]{Condition}
198 }$
199 \caption{Behaviour in Domain Specific Language}\label{fig:beh}
200 \end{figure}
201
202 We implemented the StoppingExpression in the DSL so that every mission have a condition to stop. Once the condition is reached then the mission is accomplished.
203 The combination of actions can be used to represent a robot movement, for example below code is used by the robot to turn left.
204
205 \begin{lstlisting}
206 right motor forward
207 left motor backward
208 \end{lstlisting}
209
210 \subsection{Code Structure}
211 The DSL is able to generate the source code for the missions and the behaviours. The generated source code contains one java class for the collection of missions and one java class for each different behaviours. Moreover, the default source code is categorized by two packages. First package contains a class implementing bluetooth communication protocol between Producer-Consumer (BTController.java), a class to collect the sensor data from Producer (SensorCollector.java), and a class to collect the sensor data from Consumer (RemoteSensors.java).
212 Second package mainly contains the arbitrator implementation of the robot (Marster.java), the default actions of the robot (BasicBehaviour.java), the implementation of the misssion (Mission.java), and a class to terminate the mission (ShutdownBehaviour.java).