f7b173b5b8a1518d41898dd6cba8606caece6e60
[bachelor-thesis/written-stuff.git] / Ausarbeitung / implementation.tex
1 \chapter{Implementation}
2 \hyphenation{im-ple-men-ta-tion im-ple-men-ta-tion-spe-ci-fic}
3 \todo{more?}
4 This chapter describes the implementation that was used for the aforementioned
5 experiments. It consists of the measuring programs themselves, which use the
6 Wiselib Roomba Control, and are written in C++. Additionally, there are several
7 Bash and Perl scripts to help with the analysis of the measured data.
8
9 All code used for this thesis resides in the Wiselib source tree\footnote{see
10 \url{https://github.com/ibr-alg/wiselib/}, or the \acs{CDROM} attached at the
11 end of this book} in the directory \filepath{apps/pc\_apps/roomba\_tests}.
12
13 \section{Measuring}
14 \label{sec:impl:measuring}
15
16 The measuring implementation is a set of programs running on a x86-compatible
17 Linux system which uses the Wiselib to control a Roomba attached to the system
18 over a serial interface. It features a graphical user interface using the
19 Qt application framework. Basically, it carries out a movement with specified
20 parameters (velocity, and destination or angle, depending on the experiment),
21 then prompts the user to measure and input the actual destination or angle the
22 Roomba has moved after it has finished the movement. In this context, the user
23 can choose between two operation modes. In the manual mode, the user can control
24 the target values for the Roomba movement himself, in automatic mode, the
25 application uses a list of pre-programmed values for the target values, so the
26 user only has to input the measured data.
27
28 \paragraph{Logging}
29 All measured data is written to a log file in the current directory whose name
30 is based on the type of experiment performed. Every line in that log file
31 describes one measurement made by the user, and consists of pairs of the form
32 \code{key=value}, separated of by whitespace. In particular, the data on each
33 line are (with key name, in the order of appearance on the line):
34 \begin{enumerate}[noitemsep]
35 \item\texttt{svn:} the \ac{SVN} or Git revision the program was compiled from
36 (statically compiled into the program)
37 \item\texttt{roomba\_id:} the ID of the Roomba the measurement was performed
38 with (given by the user)
39 \item\texttt{ground\_type:} the ground type used for measurement:
40 \magicvalue{iz250flur} for laminated floor, \magicvalue{seminarraum}
41 for carpet floor (given by the user)
42 \item\texttt{diff\_ticks\_left:} the difference of encoder counts between the
43 beginning and the end of the movement, on the Roomba's left wheel
44 (packet~ID \magicvalue{0x2b} in the \ac{ROI} Specification)
45 \item\texttt{diff\_ticks\_right:} the difference of encoder counts between the
46 beginning and the end of the movement, on the Roomba's right wheel
47 (packet~ID \magicvalue{0x2c} in the \ac{ROI} Specification)
48 \item\texttt{raw\_ticks\_left:} the absolute value of the encoder count of the
49 Roomba's left wheel (packet~ID \magicvalue{0x2b})
50 \item\texttt{raw\_ticks\_right:} the absolute value of the encoder count of
51 the Roomba's right wheel (packet~ID \magicvalue{0x2c})
52 \item\texttt{batt\_charge:} the charge of the Roomba's battery in mAh
53 (packet~ID~\magicvalue{0x19})
54 \item\texttt{batt\_capacity:} the capacity of the Roomba's battery in mAh
55 (packet~ID~\magicvalue{0x1a})
56 \item\texttt{batt\_voltage:} the voltage of the Roomba's battery in mV
57 (packet~ID~\magicvalue{0x16})
58 \item\texttt{batt\_voltage:} the current of the Roomba's battery in mA
59 (packet~ID~\magicvalue{0x17})
60 \item\texttt{move:} movement type; \magicvalue{straight} for straight moves,
61 \magicvalue{turn} for turn moves (given by the user while selecting the
62 experiment type)
63 \newcounter{logitems}\setcounter{logitems}{\value{enumi}+1}
64 \end{enumerate}
65 For straight moves (\magicvalue{move=straight}) follow:
66 \begin{enumerate}[noitemsep,start=\value{logitems}]
67 \item\texttt{input\_distance}: the target distance in mm sent to the Roomba
68 via the \ac{ROI} \cmd{Drive} command (given by the user in manual mode,
69 or determined by the program in automatic mode)
70 \item\texttt{velocity:} (the velocity in mm/s sent to the Roomba via the
71 \ac{ROI} \cmd{Drive} command (given by the user in manual mode, or
72 determined by the program in automatic mode)
73 \item\texttt{measured\_x:} the actual covered distance in mm in the Roomba's
74 original viewing direction (measured by the user)
75 \item\texttt{measured\_y:} originally, the distance shift perpendicular to the
76 viewing direction of the Roomba. Not used anymore.
77 \item\texttt{deviation\_orientation:} originally, the shift in orientation of
78 the Roomba. Not used anymore.
79 \end{enumerate}
80 For turn moves (\magicvalue{move=turn}) follow:
81 \begin{enumerate}[noitemsep,start=\value{logitems}]
82 \item\texttt{turn\_angle:} the turn angle in degree sent to the Roomba via the
83 \ac{ROI} \cmd{Drive} command (given by the user in manual mode, or
84 determined by the program in automatic mode)
85 \item\texttt{measured\_angle:} the actual turned angle of the Roomba in degree
86 (measured by the user)
87 \item\texttt{velocity:} (the velocity in mm/s sent to the Roomba via the
88 \ac{ROI} \cmd{Drive} command (given by the user in manual mode, or
89 determined by the program in automatic mode)
90 \end{enumerate}
91
92 \begin{figure}[htbp]
93 \centering
94 \includegraphics[width=0.9\textwidth]{images/Implementation-Diagram.pdf}
95 \caption[File structure of the measuring implementation]{Simplified file
96 structure of the measuring implementation\label{fig:impl:struct}\\
97 yellow: common files used for all three experiments; green: files for
98 Experiment~1; blue: files for Experiment~2; red: files for Experiment~3.\\
99 An arrow from node $x$ to node $y$ means ``$y$ depends from $x$''.}
100 \end{figure}
101
102 Figure~\ref{fig:impl:struct} shows the file layout of the measuring
103 implementation. It reuses components whereever possible, and therefore
104 consists of three parts (green, blue, red) specific for the three performed
105 experiments (see Sections~\ref{sec:exp1}, \ref{sec:exp2} and~\ref{sec:exp3}), as
106 well as a common part (yellow), which contains the elements of the
107 implementation shared by the other three parts.
108
109 \subsection{Common part}
110 % no \file{} here please, we don't want "stuff.{cc,h}" in index...
111 The files \filefont{stuff.\{cc,h\}} and
112 \filefont{target\_value\_input\_dialog.\{cc,h\}} build up the general part of
113 the implementation (shown with a yellow background in
114 Figure~\ref{fig:impl:struct}). These are included in all other three parts of
115 the implementation.
116
117 \paragraph{Global declarations}
118 These files \file{stuff.cc} and \file{stuff.h} contain global helper functions
119 and variables, data structures specific to the implementation, and all kinds of
120 convenient typedefs. In particular, there are functions for formatting and
121 logging messages to a log file, according to the format described above. While
122 \file{stuff.h} contains the declarations and typedefs, \file{stuff.cc} contains
123 the appropriate definitions. Besides, since \file{stuff.h} is included
124 indirectly in most other source files, it also includes the Wiselib headers
125 needed for Roomba control.
126
127 \paragraph{Input dialog for measured orientation values}
128 \hyphenation{Tar-get-Va-lue-In-put-Dia-log}
129 The class \class{TargetValueInputDialog}, which is an implementation of a simple
130 dialog box using the Qt widget framework, resides in the files
131 \file{target\_value\_input\_dialog.cc} and
132 \file{target\_value\_input\_dialog.h}. This dialog allows the user to input a
133 measured angle (although the name mentions a ``target value'', which is probably
134 named this way only for the sake of confusion\ldots). It contains an input box
135 with adjacent spin box for entering the measured value, OK and Cancel buttons,
136 and also information about the target orientation and the current real
137 orientation as well as the current capacity of the Roomba's battery.
138
139 When the dialog is displayed, it obtains the orientation as it was before the
140 last movement, and the target angle, and sets the input box to the specified
141 target value. When the input box is changed, the dialog calculates the new
142 orientation from the last orientation and the value in the input box, and
143 displays this new orientation. This way, the user can simply enter the measured
144 angle by increasing or decreasing the value by operating the spin box, and
145 adjusting the value until the displayed orientation reflects the real
146 orientation of the Roomba.
147
148 To achieve this, the class uses the Qt signal-slot
149 mechanism~\cite{qt-signalslots}, and connects the \fnfont{intValueChanged(int)}
150 signal of the input box to the method
151 \fnfont{turn\_dialog\_value\_changed(int)},
152 which performs the calculation and display of the new value.
153
154 \subsection{Implementation for Experiment~1: Application \prog{roomba\_test}}
155 The implementation for Experiment~1 is used to build the application
156 \prog{roomba\_test}. Its main part is constituted by the file \file{main.cc},
157 which uses the global definitions of \file{stuff.h} and the dialog box from
158 \file{target\_value\_input\_dialog.h}.
159
160 \paragraph{Initial setup}
161 The program first checks for command line arguments, where it expects the type
162 of test performed and if automatic or manual mode, the ground type, and the
163 Roomba's ID, or the \code{-{}-help} switch to display a usage message.
164
165 After that, it instanciates and initializes a \class{RoombaModel} instance,
166 while also providing for the needed instances of the \concept{Timer} concept
167 and the \concept{SerialCommunication} concept, which is needed for the serial
168 communication with the Roomba. Furthermore, an instance of
169 \class{ControlledMotion} is initialized by passing the \class{RoombaModel}
170 instance. This instance is later used to control the Roomba's movements.
171
172 Due to an implementation detail inside the \class{RoombaModel} class, the sensor
173 data returned by the class itself may be corrupt if it is read while new sensor
174 data packets arriving from the Roomba are processed. To prevent this corruption,
175 we register a callback function which gets called everytime new sensor data is
176 available (i.~e. the sensor data packets have been fully processed, which is
177 every 15~ms as we are using the \cmd{Stream} command), and let the callback
178 function perform a deep copy of the sensor values we are interested in.
179
180 If the user has specified automated measurement on the command line, an array of
181 velocities and target values is set up, as described in
182 Section~\ref{sec:exp1:setup}, and slightly scrambled to ensure independence of
183 the measurements. Furthermore, the file name for the log file to record the
184 measured values is determined from the operation mode, the type of experiment
185 performed, and the Unix timestamp\footnote{The Unix timestamp is a signed
186 integer which is defined as the seconds elapsed since January 1st, 1970, 0:00
187 \ac{UTC}}. To ease measurement of angles, if the type of experiments are turn
188 movements, the user is prompted once to input the current orientation of the
189 Roomba.
190
191 \paragraph{Input/measurement loop}
192 The rest of the program consists of a loop which does the following things:
193 \begin{enumerate}[noitemsep]
194 \item \textbf{In manual mode:} Prompt the user for a new pair of velocity and
195 input value \\
196 \textbf{In automatic mode:} For each pair of velocity and input value
197 specified in the previously definied arrays, do the following:
198 \item Carry out the specified movement using the \class{ControlledMotion}
199 instance
200 \item Prompt the user for the measured value using the
201 \class{TargetValueInputDialog} for angles, or a simple Qt
202 \class{QInputDialog} for distances
203 \item Write the values to the log file, as specified at the beginning of this
204 section, using the log function from \file{stuff.h}
205 \item Repeat until the user cancels.
206 \end{enumerate}
207
208 \subsection{Implementation for Experiment~2:
209 Application \prog{mean\_correction\_test}}
210 Additionally, the program \prog{mean\_correction\_test} for Experiment~2 uses
211 the class \class{CorrectedMeanMotion} from \file{corrected\_mean\_motion.h}.
212 This class adapts the input value according to the fit function determined from
213 the data in Experiment~1 (see Chapter~\ref{sec:exp2}), and exposes the exact
214 interface as \class{ControlledMotion} does. Thus, both classes are easily
215 interchangeable, and \class{CorrectedMeanMotion} serves as an example of the
216 stackability principle mentioned in Section~\ref{sec:wiselib:arch}.
217
218 The essential parts of \class{CorrectedMeanMotion} are the functions
219 \fnfont{set\_profile} to specify the fit function (i.~e. the ground type),
220 and \fnfont{move\_distance} and \fnfont{turn\_to} which use that specified fit
221 function to adapt the input value (angle or distance) and then move the Roomba
222 around accordingly. As in the implementation of \class{ControlledMotion}, the
223 encoding counts of the Roomba's wheels are monitored to determine when the
224 Roomba has to be stopped.
225
226 The main function in \file{mean\_correction.cc} is mostly the same as in
227 \file{main.cc}, except that it does not use a \class{ControlledMotion}
228 instance but a \class{CorrectedMeanMotion} instance to control the Roomba. The
229 profile that \class{CorrectedMeanMotion} uses is determined by the floor type
230 given on the command line.
231
232 \subsection{Implementation for Experiment~3: Application
233 \prog{soft\_start\_test}}
234 As the implementation for Experiment~2, the application \prog{soft\_start\_test}
235 which is built from the file \file{soft\_start.cc} has the same basic layout as
236 \file{main.cc} from Experiment~1. As previously, it defines a custom movement in
237 the class \class{SoftStartMotion} (file \file{soft\_start\_motion.h}), which
238 is also compatible with the interface defined by \class{ControlledMotion}. This
239 class causes the Roomba to constantly accelerate at the start of the movement,
240 and also constantly decelerate at the end of its movement, until the target
241 distance or angle is reached.
242
243 Internally, the class defines a time interval for the duration of the start and
244 stop processes, and breaks down the process into multiple acceleration steps
245 which each increase the velocity by a constant value, so for sufficient steps,
246 the acceleration remains nearly constant. Therefore, a timer is used, which
247 increases resp. decreases the velocity over time (depending if starting or
248 stopping), until the target velocity or zero is reached, and sends \cmd{Drive}
249 commands to the Roomba over the \ac{ROI}. The methods \fnfont{move\_distance}
250 and \fnfont{turn\_to} on the other hand calculate the distance at which
251 the deceleration process begins, and fire the initial timer to increase the
252 velocity.
253
254 \section{Evaluation}
255 \label{sec:impl:eval}
256 For evaluation of the log files, a few Bash and Perl scripts were used. They
257 use Gnuplot to plot graphs and \ac{GNU} R for statistical analysis, and reside
258 in the Wiselib source tree under \filepath{apps/pc\_apps/roomba\_tests/logs}.
259
260 \paragraph{\prog{graph.sh}}
261 This wrapper script uses Gnuplot to create interactive 3-dimensional plots of
262 the original behaviour from Experiment~1, and also includes the fit function as
263 determined by \ac{GNU} through linear regression. For each pair of ground type
264 (carpet floor, laminate floor) and experiment type (straight movement, turn on
265 spot), it opens a Gnuplot window containing a graph with the target value on the
266 $x$ axis, the velocity on the $y$ axis and the measured value on the $z$ axis,
267 and the fit function. The user can rotate and tilt the
268 plot with the mouse.
269
270 \paragraph{\prog{graph-mean.sh}} This script does the same as \prog{graph.sh},
271 except it visualizes the measured data from Experiment~2 (Mean Correction), and
272 a fit function for that data.
273
274 \paragraph{\prog{graph-soft.sh}} Also, like both scripts mentioned above, this
275 script uses the measured data from Experiment~3 (Constant Start/Stop
276 Acceleration), and fit function for that data.
277
278 \paragraph{\prog{graph-mean-soft.sh}} This wrapper script plots both results
279 from Experiment~2 and 3 into one coordinate system, for better comparison. It
280 does not include the fit functions.
281
282 \paragraph{\prog{graph-errorlines.sh}} In contrast to the other scripts,
283 \prog{graph-errorlines.sh} plots the 2D graphs seen in
284 Chapters~\ref{sec:exp1},~\ref{sec:exp2} and~\ref{sec:exp3}, with target value on
285 the $x$ axis, measured value on the $y$ axis, and velocities as single
286 functions each.
This page took 0.058543 seconds and 3 git commands to generate.