4b84324f1dfe4853fb66a86c93050efe57b97a2b
[bachelor-thesis/written-stuff.git] / Ausarbeitung / wiselib.tex
1 \section{Wiselib}
2 The \definition{Wiselib}\cite{wiselib} is a C++ algorithm library for
3 sensor networks, containing for example algorithms for routing, localization and
4 time synchronization, and is strongly focused on portability and cross-platform
5 development. In particular, it allows the user to develop applications that run
6 on different hardware platforms without the need to change the code, and it
7 strongly uses C++ templates to achieve that feature. Amongst the supported
8 platforms are diverse sensor node platforms, like iSense, Contiki and TinyOS,
9 but there are as well implementations for the diverse x86-compatible Personal
10 Computer platforms, and the Shawn sensor network simulator.
11
12 \subsection{Architecture}
13 \paragraph{Concepts and Models}
14 Wiselib makes strong uses of \definition{concepts} and \definition{models} as
15 central design objects. Concepts serve as an informal description of interfaces,
16 only existent in documentation, defining expected parameters and types. Models
17 however implement these interfaces in C++ code while fulfilling their
18 specification. The Wiselib algorithms can in turn rely on the concepts as a
19 generic specification, and take models as template parameters to use their
20 functionality, so a function call will be immediately resolved to a specific
21 model at compile time without the need for an additional function call as it is
22 the case with virtual inheritance. Furthermore, this also allows usage on
23 platforms which do not have support for C++, as the bytecode generated by the
24 compiler does not include C++ specific extensions (no virtual function tables,
25 and templates are resolved at compile time) and can be linked against any
26 Standard C Library.
27
28 This makes cross-platform development easily possible. For example, to implement
29 a routing algorithm, one can rely on the concept of a Radio to send and receive
30 data packets, without needing to implement code specific to the used radio
31 hardware. The users of that routing algorithm can now choose which radio model
32 they want to use, according to their needs and the underlying hardware, provided
33 that their radio model also implements the same Radio concept that the routing
34 algorithm uses.
35
36 \begin{figure}
37 \centering
38 \includegraphics[width=.8\textwidth]{images/Wiselib-Arch.pdf}
39 \caption{Wiselib architecture\label{fig:wiselib-arch}}
40 \end{figure}
41 Besides algorithms, and basic concepts and models, the Wiselib also consists of
42 two other main parts: the internal interface and the external interface (see
43 Figure~\ref{fig:wiselib-arch}).
44
45 \paragraph{External Interface}
46 The \definition{External Interface} provides access to the underlying \ac{OS},
47 like iSense, Contiki, Shawn\ldots and defines concepts like a Radio or a Timer.
48 Thus, the concepts are as generic as possible to match all supported operating
49 systems and provide a light-weight abstraction to the underlying \ac{OS}. These
50 concepts sometimes extend the generic concepts, for example there is a TxRadio
51 which has the ability to set the transmission power on the radio. The models
52 (for example an iSenseRadioModel or a ShawnTimerModel) implement these concepts
53 on the specific operating system, and can be passed around as template
54 parameters.
55
56 \paragraph{Internal Interface}
57 The \definition{Internal Interface} defines concepts and models for data
58 structures that can be used in algorithms. This allows to specialize for
59 restricted platforms; for instance a wireless sensor node without dynamic memory
60 management can use a static implementation of lists and other containers,
61 whereas a full-grown desktop can use the dynamic implementation provided by the
62 C++ \ac{STL}. For this purpose, the Wiselib also contains the
63 \acused{pSTL}\definition{pico Standard Template Library} (\acs{pSTL}) which
64 implements a subset of the \ac{STL} without the use of dynamic memory
65 allocation.
66
67 \paragraph{Stackability}
68 Another central design principle used in the Wiselib is
69 \definition{stackability}, which describes the possibility to stack
70 implementations of the same concept, thereby building a layered structure. For
71 example, one could stack a cryptography algorithm on top of a radio model, which
72 both implement the Radio concept. In this case, the cryptography layer doesn't
73 have to know anything at all about the underlying implementation, as long as it
74 can use the Radio concept of the underlying layer. And it can even provide the
75 same interface to a possibly higher layer in order to provide transparent packet
76 de- and encryption over the radio.
77
78 \subsection{Roomba Control}
79 Even more interesting is the fact that the Wiselib includes code to control an
80 iRobot Roomba\index{Roomba} over a serial interface, and getting access to its
81 internal sensor data, using the \ac{ROI}\index{Roomba Open Interface} mentioned
82 earlier. For this purpose, it defines two concepts for Robot Motion:
83
84 \paragraph{TurnWalkMotion concept}\index{TurnWalkMotion (concept)}
85 This concept represents a simple robot that can turn on the spot and walk
86 straight, without automatic stopping.
87 \begin{description}
88 \item Types:
89 \begin{description}
90 \item[\code{velocity\_t}] Type for velocity measurement
91 \item[\code{angular\_velocity\_t}] Type for angular velocity measurement
92 \end{description}
93 \item \todo{clearpage?} Methods:
94 \begin{description}
95 \item[\code{int turn(angular\_velocity\_t)}] turn the robot with a
96 constant angular velocity
97 \item[\code{int move (velocity\_t)}] move the robot straight with a
98 constant velocity
99 \item[\code{int stop()}] stop the robot
100 \item[\code{int wait\_for\_stop()}] hold the execution until the robot has
101 stopped
102 \end{description}
103 \end{description}
104
105 \paragraph{Odometer concept}\index{Odometer (concept)}
106 This concept represents an Odometer which tracks motions over time.
107 Whenever the object turns or moves, internal counters will adjust their
108 guessing of the object's traveled distance and current orientation.
109 \begin{description}
110 \item Types:
111 \begin{description}
112 \item[\code{angle\_t}] Type for angle measurement
113 \item[\code{distance\_t}] Type for distance measurement
114 \end{description}
115 \item Methods:
116 \begin{description}
117 \item[\code{angle\_t angle()}] return the current angle
118 \item[\code{int reset\_angle()}] reset the angle of the object
119 \item[\code{distance\_t distance()}] return the current distance
120 \item[\code{int reset\_distance()}] reset the distance of the object
121 \item[\code{int register\_state\_callback (T *obj)}] register a callback
122 that gets called when the state changes
123 \item[\code{int unregister\_state\_callback (int)}] unregister a
124 previously registered callback
125 \item[\code{int state()}] return the current state
126 \end{description}
127 \end{description}
128
129 \paragraph{ControlledMotion class}\index{ControlledMotion (class)}
130 On top of the TurnWalkMotion and Odometer concepts builds the
131 \textit{ControlledMotion}\index{ControlledMotion (class)} model. It takes
132 implementations of each of these concepts as template parameters and extends the
133 simple turn-and-walk paradigm by a temporal dimension, which let the robot stop
134 after a specific time interval. In particular, it provides the following
135 methods:
136 \begin{description}
137 \item[\code{int move\_distance(distance\_t, velocity\_t)}] move the robot
138 straight by a given distance with a given velocity
139 \item[\code{int turn\_about(angle\_t, angular\_velocity\_t)}] turn the robot
140 about a given angle with a given angular distance
141 \item[\code{int turn\_to(angle\_t, angular\_velocity\_t)}] turn the robot
142 to a given orientation with a given angular distance
143 \end{description}
144
145 The class first registers a callback function at the given Odometer instance,
146 and then uses its distance and angle values to control the robot over the
147 TurnWalkMotion instance. Everytime the state of the robot changes (i.~e. new
148 data from its sensors are received), it compares the new actual values with the
149 target values given by the user through the functions above, and if the actual
150 values exceed the target values, the robot is stopped.
151
152 \paragraph{Underlying Roomba Implementation}
153 The actual communication with the Roomba is done in the
154 \textit{RoombaModel}\index{RoombaModel (class)} class. It implements the
155 aforementioned TurnWalkMotion\index{TurnWalkMotion (concept)} and
156 Odometer\index{Odometer (concept)} concepts and therewith allows the interaction
157 with a ControlledMotion\index{ControlledMotion (class)} instance. In particular,
158 it manages the serial communication with the Roomba and translates the function
159 calls \code{turn()}, \code{move()} and \code{stop()} of the TurnWalkMotion
160 concept to the according parameters for the \ac{ROI} \cmd{Drive} command, reads
161 a subset of the Roomba's sensors and presents the sensor data to the user, and,
162 while implementing the Odometer concept, calculates the covered distance and
163 angle from the Roomba's right and left wheel rotations.
164
165 The sensor data is read from the Roomba using the \cmd{Stream} command on the
166 \ac{ROI}, which results in a sensor data packet (see Section
167 \ref{sec:roi-stream-packet}) every 15~ms, and $66.67$ packets per second. At a
168 speed of 19,200 baud in mode \ac{8N1}, the maximum size of a data packet is
169 $19,200 \div (66.67 \times 9) = 32$ byte, so at the moment only the sensor
170 packets \emph{encoder counts left/right} (IDs~\magicvalue{0x2b},
171 \magicvalue{0x2c}, 2+2 bytes), \emph{battery voltage/current/charge/capacity}
172 (IDs~\magicvalue{0x16}, \magicvalue{0x17}, \magicvalue{0x19},
173 \magicvalue{0x1a}, 2+2+2+2 bytes) are streamed, which add up to 18 data bytes +
174 3 header/checksum bytes. There has currently been no success yet in
175 communicating at the higher speed of 115,200 baud.
176
177 Also there has been research to use the distance and angle values that the
178 Roomba itself provides (sensor packet~IDs \magicvalue{0x13} and
179 \magicvalue{0x14}). However, according to the \ac{ROI} Specification these
180 values are integer values, and the value is reset to zero every time it is read.
181 By using the \cmd{Stream} command on the \ac{ROI} and therefore reading these
182 values every 15~ms, the Roomba cannot move fast enough to increment these values
183 to \magicvalue{1}, so everytime \magicvalue{0} is read. It is obvious that
184 these sensor values are not suited for such rapid evaluations, and can only be
185 used for larger distances and angles. Nevertheless, since the Wiselib Roomba
186 control needs to keep track of the Roomba's current position and orientation as
187 fast as possible to maintain a certain accuracy in movement, the distance and
188 angle values as provided by the Roomba itself cannot be used. On the other hand,
189 working with the Roomba's wheel encoder counts (sensor packet~IDs
190 \magicvalue{0x2b} and \magicvalue{0x2c}) has proven itself quite acceptable.
191 After a few test runs, the number of encoder counts per~mm for straight
192 walks turned out as $2.27$, and for turning on the spot, $2.27 \times 115 =
193 261.05$ encoder counts per radian, which is the number of encoder counts per~mm
194 multiplicated with half the Roomba's wheelbase. So when new sensor data is read
195 each 15~ms, the RoombaModel implementation calculates the Odometer
196 \index{Odometer (concept)} distance and angle from these values.
197
198 \todo{cite Wisebed book chapter on Roomba code}
This page took 0.062203 seconds and 3 git commands to generate.