george comments
[phd-thesis.git] / top / 4iot.tex
1 \documentclass[../thesis.tex]{subfiles}
2
3 \input{subfilepreamble}
4
5 \begin{document}
6 \input{subfileprefix}
7
8 \chapter{\texorpdfstring{\Glsxtrlong{TOP} for the \glsxtrlong{IOT}}{Task-oriented programming for the internet of things}}%
9 \label{chp:top4iot}
10 \begin{chapterabstract}
11 \noindent This chapter:
12 \begin{itemize}
13 \item introduces the problems with \gls{TOP} for the \gls{IOT}.
14 \item shows how to create the \emph{Hello World!} application for microcontrollers using \gls{ARDUINO};
15 \item extends this idea with multithreading, demonstrating the difficulty programming multi-tasking applications;
16 \item describes a comparative variant in \gls{MTASK} and shows that upgrading to a multi-tasking variant is straightforward
17 \item demonstrates that the complexity of running multiple tasks;
18 \item and concludes with the history of \gls{MTASK}'s development.
19 \end{itemize}
20 \end{chapterabstract}
21
22 The edge layer of \gls{IOT} systems predominantly contains of microcontrollers.
23 Microcontrollers are tiny computers designed specifically for embedded applications.
24 They therefore only have a soup\c{c}on of memory, have a slow processor, come with many energy efficient sleep modes and have a lot of peripheral support such as \gls{GPIO} pins.
25 Usually, programming microcontrollers requires an elaborate multi-step toolchain of compilation, linkage, binary image creation, and burning this image onto the flash memory of the microcontroller in order to compile and run a program.
26 The programs are usually cyclic executives instead of tasks running in an operating system, i.e.\ there is only a single task that continuously runs on the bare metal.
27 \Cref{tbl:mcu_laptop} compares the hardware properties of a typical laptop with two very popular microcontrollers.
28
29 \begin{table}
30 \caption{Hardware characteristics of typical microcontrollers compared to laptops.}%
31 \label{tbl:mcu_laptop}
32 \begin{tabular}{llll}
33 \toprule
34 & Laptop & Atmega328P & ESP8266\\
35 \midrule
36 CPU speed & \qtyrange{2}{4}{\giga\hertz} & \qty{16}{\mega\hertz} & \qty{80}{\mega\hertz} or \qty{160}{\mega\hertz}\\
37 \textnumero{} cores & \numrange{4}{8} & 1 & 1\\
38 Storage & \qty{1}{\tebi\byte} & \qty{32}{\kibi\byte} & \qtyrange{0.5}{4}{\mebi\byte}\\
39 \gls{RAM} & \qtyrange{4}{16}{\gibi\byte} & \qty{2}{\kibi\byte} & \qty{160}{\kibi\byte}\\
40 Power & \qtyrange{50}{100}{\watt} & \qtyrange{0.13}{250}{\milli\watt} & \qtyrange{0.1}{350}{\milli\watt}\\
41 Size & $\pm$\qty{1060}{\cubic\cm} & $\pm$\qty{7.5}{\cubic\cm} & $\pm$\qty{1.1}{\cubic\cm}\\
42 Price & \euro{1500} & \euro{3} & \euro{4}\\
43 \bottomrule
44 \end{tabular}
45 \end{table}
46
47 Different models of microcontrollers require their own vendor-provided drivers, hardware abstraction layer, compilers and \glspl{RTS}.
48 There are many platforms that abstract away from this such as \gls{MBED} and \gls{ARDUINO} of which \gls{ARDUINO} is specifically designed for education and prototyping and hence used here.
49 The popular \gls{ARDUINO} \gls{C}\slash\gls{CPP} dialect and accompanying libraries provide an abstraction layer for common microcontroller behaviour allowing the programmer to program multiple types of microcontrollers using a single language.
50 Originally it was designed for the in-house developed open-source hardware with the same name but the setup allows porting to many architectures.
51 It provides an \gls{IDE} and toolchain automation to perform all steps of the toolchain with a single command.
52
53 \subsection{\texorpdfstring{\Glsxtrshort{TOP} for the \glsxtrshort{IOT}}{TOP for the IoT}}
54 \Gls{TOP} is a programming paradigm that allows multi-tier systems to be generated from a single declarative source.
55 \Gls{ITASK} is a general-purpose \gls{TOP} system for programming distributed web applications.
56 These distributed web applications often form the core of \gls{IOT} applications as well but integrating these devices in \gls{ITASK} is not straightforward.
57 \Gls{ITASK} targets relatively fast but energy-hungry systems with large amounts of \gls{RAM} and a speedy connections.
58 Edge devices in \gls{IOT} systems are typically slow but energy efficient and do not have the memory to run the naturally heap-heavy functional programs that \gls{ITASK} results in.
59 \Gls{MTASK} bridges this gap by providing a \gls{TOP} \gls{DSL} for \gls{IOT} edge devices that can, because domain-specific knowledge is built in, run on hardware with much less memory and processor speed.
60 The following sections compare traditional microcontroller programming with programming the devices using \gls{MTASK}.
61
62 \section{Hello world!}
63 Traditionally, the first program that one writes when trying a new language is the so-called \emph{Hello World!} program.
64 This program has the single task of printing the text \emph{Hello World!} to the screen and exiting again, useful to become familiarised with the syntax and verify that the toolchain and runtime environment is working.
65 On microcontrollers, there usually is no screen for displaying text.
66 Nevertheless, almost always there is a built-in monochrome $1\times1$ pixel screen, namely \pgls{LED}.
67 The \emph{Hello World!} equivalent on microcontrollers blinks this \gls{LED}.
68
69 \Cref{lst:arduinoBlink} shows how the logic of a blink program might look when using \gls{ARDUINO}'s \gls{C}\slash\gls{CPP} dialect.
70 Every \gls{ARDUINO} program contains a \arduinoinline{setup} and a \arduinoinline{loop} function.
71 The \arduinoinline{setup} function is executed only once on boot, the \arduinoinline{loop} function is continuously called afterwards and contains the event loop.
72 After setting the \gls{GPIO} pin to the correct mode, blink's \arduinoinline{loop} function alternates the state of the pin representing the \gls{LED} between \arduinoinline{HIGH} and \arduinoinline{LOW}, turning the \gls{LED} off and on respectively.
73 In between it waits for \qty{500}{\ms} so that the blinking is actually visible for the human eye.
74
75 Translating the traditional blink program to \gls{MTASK} can almost be done by simply substituting some syntax as seen in \cref{lst:blinkImp}.
76 E.g.\ \arduinoinline{digitalWrite} becomes \cleaninline{writeD}, literals are prefixed with \cleaninline{lit} and the pin to blink is changed to represent the actual pin for the builtin \gls{LED} of the device used in the exercises.
77 In contrast to the imperative \gls{CPP} dialect, \gls{MTASK} is a \gls{TOP} language and therefore there is no such thing as a loop, only task combinators to combine tasks.
78 To simulate a loop, the \cleaninline{rpeat} task combinator can be used as this task combinator executes the argument task and, when stable, reinstates it.
79 The body of the \cleaninline{rpeat} contains similarly named tasks to write to the pins and to wait in between.
80 The tasks are connected using the sequential \cleaninline{>>|.} combinator that for all current intents and purposes executes the tasks after each other.
81
82 \begin{figure}[ht]
83 \begin{subfigure}[b]{.5\linewidth}
84 \begin{lstArduino}[caption={Blink program.},label={lst:arduinoBlink}]
85 void setup() {
86 pinMode(D2, OUTPUT);
87 }
88
89 void loop() {
90 digitalWrite(D2, HIGH);
91 delay(500);
92 digitalWrite(D2, LOW);
93 delay(500);
94 }\end{lstArduino}
95 \end{subfigure}%
96 \begin{subfigure}[b]{.5\linewidth}
97 \begin{lstClean}[caption={Blink program.},label={lst:blinkImp}]
98 blink :: Main (MTask v ()) | mtask v
99 blink =
100 declarePin D2 PMOutput \d2->
101 {main = rpeat (
102 writeD d2 true
103 >>|. delay (lit 500)
104 >>|. writeD d2 false
105 >>|. delay (lit 500)
106 )
107 }\end{lstClean}
108 \end{subfigure}
109 \end{figure}
110
111 \section{Multi tasking}
112 Now say that we want to blink multiple blinking patterns on different \glspl{LED} concurrently.
113 For example, blink three \glspl{LED} connected to \gls{GPIO} pins $1,2$ and $3$ at intervals of \qtylist{500;300;800}{\ms}.
114 Intuitively you want to lift the blinking behaviour to a function and call this function three times with different parameters as done in \cref{lst:blinkthreadno}
115
116 \begin{lstArduino}[caption={Naive approach to multiple blinking patterns.},label={lst:blinkthreadno}]
117 void setup () { ... }
118
119 void blink (int pin, int wait) {
120 digitalWrite(pin, HIGH);
121 delay(wait);
122 digitalWrite(pin, LOW);
123 delay(wait);
124 }
125
126 void loop() {
127 blink (D1, 500);
128 blink (D2, 300);
129 blink (D3, 800);
130 }\end{lstArduino}
131
132 Unfortunately, this does not work because the \arduinoinline{delay} function blocks all further execution.
133 The resulting program will blink the \glspl{LED} after each other instead of at the same time.
134 To overcome this, it is necessary to slice up the blinking behaviour in very small fragments so it can be manually interleaved \citep{feijs_multi-tasking_2013}.
135 Listing~\ref{lst:blinkthread} shows how three different blinking patterns might be achieved in \gls{ARDUINO} using the slicing method.
136 If we want the blink function to be a separate parametrizable function we need to explicitly provide all references to the required state.
137 Furthermore, the \arduinoinline{delay} function can not be used and polling \arduinoinline{millis} is required.
138 The \arduinoinline{millis} function returns the number of milliseconds that have passed since the boot of the microcontroller.
139 Some devices use very little energy when in \arduinoinline{delay} or sleep state.
140 Resulting in \arduinoinline{millis} potentially affects power consumption since the processor is basically busy looping all the time.
141 In the simple case of blinking three \glspl{LED} on fixed intervals, it might be possible to calculate the delays in advance using static analysis and generate the appropriate \arduinoinline{delay} code.
142 Unfortunately, this is very hard when for example the blinking patterns are determined at runtime.
143
144 \begin{lstArduino}[label={lst:blinkthread},caption={Threading three blinking patterns.}]
145 long led1 = 0, led2 = 0, led3 = 0;
146 bool st1 = false, st2 = false, st3 = false;
147
148 void blink(int pin, int interval, long *lastrun, bool *st) {
149 if (millis() - *lastrun > interval) {
150 digitalWrite(pin, *st = !*st);
151 *lastrun += interval;
152 }
153 }
154
155 void loop() {
156 blink(D1, 500, &led1, &st1);
157 blink(D2, 300, &led2, &st1);
158 blink(D3, 800, &led3, &st1);
159 }\end{lstArduino}
160
161 This method is very error prone, requires a lot of pointer juggling and generally results into spaghetti code.
162 Furthermore, it is very difficult to represent dependencies between threads, often state machines have to be explicitly programmed by hand to achieve this.
163
164 \subsection{Multi tasking in \texorpdfstring{\gls{MTASK}}{mTask}}
165 The \cleaninline{delay} \emph{task} does not block the execution but \emph{just} emits no value when the target waiting time has not yet passed and emits a stable value when the time is met.
166 In contrast, the \arduinoinline{delay()} \emph{function} on the \gls{ARDUINO} is blocking which prohibits interleaving.
167 To make code reuse possible and make the implementation more intuitive, the blinking behaviour is lifted to a recursive function instead of using the imperative \cleaninline{rpeat} construct.
168 The function is parametrized with the current state, the pin to blink and the waiting time.
169 Creating recursive functions like this is not possible in the \gls{ARDUINO} language because the program would run out of stack in an instant and nothing can be interleaved.
170 With a parallel combinator, tasks can be executed in an interleaved fashion.
171 Therefore, blinking three different blinking patterns is as simple as combining the three calls to the \cleaninline{blink} function with their arguments as seen in \cref{lst:blinkthreadmtask}.
172
173 % VimTeX: SynIgnore on
174 \begin{lstClean}[label={lst:blinkthreadmtask},caption={Threaded blinking.}]
175 blinktask :: MTask v () | mtask v
176 blinktask =
177 declarePin D1 PMOutput \d1->
178 declarePin D2 PMOutput \d2->
179 declarePin D3 PMOutput \d3->
180 fun \blink=(\(st, pin, wait)->
181 delay wait
182 >>|. writeD d13 st
183 >>|. blink (Not st, pin, wait))
184 In {main =
185 blink (true, d1, lit 500)
186 .||. blink (true, d2, lit 300)
187 .||. blink (true, d3, lit 800)
188 }
189 \end{lstClean}
190 % VimTeX: SynIgnore off
191
192 \section{Conclusion}
193 The edge layer of \gls{IOT} systems is powered by microcontrollers.
194 Programming them happens through compiled firmwares using low-level imperative programming languages.
195 Due to the lack of an \gls{OS}, writing applications that perform multiple tasks at the same time is error prone, and complex; and requires a lot of boilerplate and manual scheduling code.
196 With the \gls{MTASK} system, a \gls{TOP} programming language for \gls{IOT} edge devices, this limitation can be overcome.
197 \todo{uit\-breiden}
198
199 \begin{subappendices}
200 \section{History of \texorpdfstring{\gls{MTASK}}{mTask}}
201 The development of \gls{MTASK} or its predecessors has been going on for almost seven years now though it really set off during my master's thesis.
202 This section provides an exhaustive overview of the work on \gls{MTASK} and its predecessors.
203
204 \subsection*{Generating \texorpdfstring{\gls{C}/\gls{CPP}}{C/C++} code}
205 A first throw at a class-based shallowly \gls{EDSL} for microcontrollers was made by \citet{plasmeijer_shallow_2016}.
206 The language was called \gls{ARDSL} and offered a type safe interface to \gls{ARDUINO} \gls{CPP} dialect.
207 A \gls{CPP} code generation backend was available together with an \gls{ITASK} simulation backend.
208 There was no support for tasks nor even functions.
209 Some time later in the 2015 \gls{CEFP} summer school, an extended version was created that allowed the creation of imperative tasks, local \glspl{SDS} and the usage of functions \citep{koopman_type-safe_2019}.
210 The name then changed from \gls{ARDSL} to \gls{MTASK}.
211
212 \subsection*{Integration with \texorpdfstring{\gls{ITASK}}{iTask}}
213 \Citet{lubbers_task_2017} extended this in his Master's Thesis by adding integration with \gls{ITASK} and a bytecode compiler to the language.
214 \Gls{SDS} in \gls{MTASK} could be accessed on the \gls{ITASK} server.
215 In this way, entire \gls{IOT} systems could be programmed from a single source.
216 However, this version used a simplified version of \gls{MTASK} without functions.
217 This was later improved upon by creating a simplified interface where \glspl{SDS} from \gls{ITASK} could be used in \gls{MTASK} and the other way around \citep{lubbers_task_2018}.
218 It was shown by \citet{amazonas_cabral_de_andrade_developing_2018} that it was possible to build real-life \gls{IOT} systems with this integration.
219 Moreover, a course on the \gls{MTASK} simulator was provided at the 2018 \gls{CEFP}/\gls{3COWS} winter school in Ko\v{s}ice, Slovakia \citep{koopman_simulation_2018}.
220
221 \subsection*{Transition to \texorpdfstring{\gls{TOP}}{TOP}}
222 The \gls{MTASK} language as it is now was introduced in 2018 \citep{koopman_task-based_2018}.
223 This paper updated the language to support functions, simple tasks, and \glspl{SDS} but still compiled to \gls{ARDUINO} \gls{CPP} code.
224 Later the byte code compiler and \gls{ITASK} integration was added to the language \citep{lubbers_interpreting_2019}.
225 Moreover, it was shown that it is very intuitive to write microcontroller applications in a \gls{TOP} language \citep{lubbers_multitasking_2019}.
226 One reason for this is that a lot of design patterns that are difficult using standard means are for free in \gls{TOP} (e.g.\ multithreading).
227 In 2019, the \gls{CEFP} summer school in Budapest, Hungary hosted a course on developing \gls{IOT} applications with \gls{MTASK} as well \citep{lubbers_writing_2019}.
228
229 \subsection*{\texorpdfstring{\Glsxtrshort{TOP}}{TOP}}
230 In 2022, the SusTrainable summer school in Rijeka, Croatia hosted a course on developing greener \gls{IOT} applications using \gls{MTASK} as well \citep{lubbers_green_2022}.
231 Several students worked on extending \gls{MTASK} with many useful features:
232 \Citet{van_der_veen_mutable_2020} did preliminary work on a green computing analysis, built a simulator, and explored the possibilities for adding bounded datatypes; \citet{de_boer_secure_2020} investigated the possibilities for secure communication channels; \citeauthor{crooijmans_reducing_2021} \citeyearpar{crooijmans_reducing_2021,crooijmans_reducing_2022} added abstractions for low-power operation to \gls{MTASK} such as hardware interrupts and power efficient scheduling; and \citet{antonova_mtask_2022} defined a preliminary formal semantics for a subset of \gls{MTASK}.
233
234 \subsection*{\texorpdfstring{\gls{MTASK}}{mTask} in practise}
235 Funded by the Radboud-Glasgow Collaboration Fund, collaborative work was executed with Phil Trinder, Jeremy Singer, and Adrian Ravi Kishore Ramsingh.
236 An existing smart campus application was developed using \gls{MTASK} and quantitively and qualitatively compared to the original application that was developed using a traditional \gls{IOT} stack \citep{lubbers_tiered_2020}.
237 This research was later extended to include a four-way comparison: \gls{PYTHON}, \gls{MICROPYTHON}, \gls{ITASK}, and \gls{MTASK} \citep{lubbers_could_2022}.
238 Currently, power efficiency behaviour of traditional versus \gls{TOP} \gls{IOT} stacks is being compared as well adding a \gls{FREERTOS} implementation to the mix as well.
239
240 \subsection*{Future work}
241 Plans for extensions and improvements include exploring integrating \gls{TINYML} into \gls{MTASK}; adding intermittent computing support to \gls{MTASK}; and extending the formal semantics to cover the entirety of the language.
242 In 2023, the SusTrainable summer school in Coimbra, Portugal will host a course on \gls{MTASK} as well.
243
244 \end{subappendices}
245
246 \input{subfilepostamble}
247 \end{document}