upadtes
[phd-thesis.git] / top / green.tex
index 79843f2..20d6ee9 100644 (file)
@@ -2,60 +2,70 @@
 
 \input{subfilepreamble}
 
+\setcounter{chapter}{6}
+
 \begin{document}
 \input{subfileprefix}
-
 \chapter{Green computing with \texorpdfstring{\gls{MTASK}}{mTask}}%
 \label{chp:green_computing_mtask}
 \begin{chapterabstract}
-       This chapter demonstrate the energy saving features of \gls{MTASK}.
-       First it gives an overview of general green computing measures for edge devices.
-       Then \gls{MTASK}'s task scheduling is explained and it is shown how to customise it so suit the applications and energy needs.
-       Finally it shows how to use interrupts in \gls{MTASK} to reduce the need for polling.
+       This chapter demonstrate the energy-saving features of \gls{MTASK} by:
+       \begin{itemize}
+               \item giving an overview of general green computing measures for edge devices;
+               \item explaining task scheduling in \gls{MTASK}, and how to tweak it so suit the applications and energy needs;
+               \item showing how to use interrupts in \gls{MTASK} to reduce the need for polling.
+       \end{itemize}
 \end{chapterabstract}
 
-The edge layer of the \gls{IOT} contains small devices that sense and interact with the world and it is crucial to lower their energy consumption.
+The edge layer of the \gls{IOT} contains small devices that sense and interact with the world.
 While individual devices consume little energy, the sheer number of devices in total amounts to a lot.
 Furthermore, many \gls{IOT} devices operate on batteries and higher energy consumption increases the amount of e-waste as \gls{IOT} edge devices are often hard to reach and consequently hard to replace \citep{nizetic_internet_2020}.
-
-To reduce the power consumption of an \gls{IOT} device, the specialized low-power sleep modes of the microprocessors can be leveraged.
-Different sleep modes achieve different power reductions because of their different run time characteristics.
-These specifics range from disabling or suspending WiFi; stopping powering (parts) of the \gls{RAM}; disabling peripherals; or even turning off the processor completely, requiring an external signal to wake up again.
-Determining when exactly and for how long it is possible to sleep is expensive in the general case and often requires annotations in the source code, a real-time operating system or a handcrafted scheduler.
+It is therefore crucial to lower their energy consumption.
+
+To reduce the power consumption of an \gls{IOT} edge device, the specialized low-power sleep modes of the microprocessors can be leveraged.
+Various sleep mode achieve different power reductions because of their run time characteristics.
+These specifics range from disabling or suspending \gls{WIFI}; stop powering (parts) of the \gls{RAM}; disabling peripherals; or even turning off the processor completely, requiring an external signal to wake up again.
+Determining exactly when, and for how long it is possible to sleep is expensive in the general case.
+In practise it means that either annotations in the source code, a \gls{RTOS}, or a handcrafted or general-purpose scheduler is required.
+
+\Cref{tbl:top_sleep} shows the properties and current consumption of two commonly used microcontrollers in their various sleep modes.
+It uncovers that switching the \gls{WIFI} radio off yields the biggest energy savings.
+In most \gls{IOT} applications, we need \gls{WIFI} for communications.
+It is fine to switch it off, but after switching it on, the \gls{WIFI} protocol needs to transmit a number of messages to re-establish the connection.
+This implies that it is only worthwhile to switch the radio off when this can be done for some time.
+The details vary per system and situation.
+As a rule of thumb, derived from experimentation, it is only worthwhile to switch the \gls{WIFI} off when it is not needed for at least some tens of seconds.
 
 \begin{table}
        \centering
-       \caption{Current use in \unit{\milli\ampere} of two microprocessor boards in various sleep modes.}%
+       \caption{Current use (\unit{\milli\ampere}) of two microprocessor boards in various sleep modes.}%
        \label{tbl:top_sleep}
        \small
+       \begingroup
+       % default is 6pt but this gives an overflow of 4.25816pt
+       \setlength\tabcolsep{5.5pt}
        \begin{tabular}{ccccccccc}
                \toprule
-               & \multicolumn{4}{c}{Wemos D1 mini} & \multicolumn{4}{c}{Adafruit Feather M0 Wifi} \\
+                                 & \multicolumn{4}{c}{\Gls{WEMOS} D1 mini}
+                                 & \multicolumn{4}{c}{Adafruit Feather M0 Wifi} \\
                \midrule
-                               & active & modem & light & deep & active & modem & light & deep \\
-                               & & sleep & sleep & sleep & & sleep & sleep & sleep \\
+                         & active   & modem & light & deep  & active & modem & light & deep\\
+                         &          & sleep & sleep & sleep &        & sleep & sleep & sleep\\
                \midrule
-               WiFi      & on & off & off & off  & on & off & off & off \\
-               CPU       & on & on & pending & off & on & on & idle & idle \\
-               \gls{RAM} & on & on & on & off & on & on & on & on\\%low power \\
+               \gls{WIFI}& on       & off   & off   & off   & on     & off   & off   & off\\
+               CPU       & on       & on    & pend. & off   & on     & on    & idle  & idle\\
+               \gls{RAM} & on       & on    & on    & off   & on     & on    & on    & on\\
                \midrule
-               current   & 100--240 & 15 & 0.5 & 0.002 & 90--300 & 5 & 2 &  0.005\\
+               current   & \numrange{100}{240} & \num{15} & \num{0.5} & \num{0.002} & \numrange{90}{300} & \num{5} & \num{2} &  \num{0.005}\\
                \bottomrule
        \end{tabular}
+       \endgroup
 \end{table}
 
-\Cref{tbl:top_sleep} shows the properties and current consumption of two commonly used microcontrollers.
-It shows that switching the WiFi radio off yields the biggest energy savings.
-In most \gls{IOT} applications, we need WiFi for communications.
-It is fine to switch it off, but after switching it on, the WiFi protocol needs to transmit a number of messages to re-establish the connection.
-This implies that it is only worthwhile to switch the radio off when this can be done for some time.
-The details vary per system and situation.
-As a rule of thumb, it is only worthwhile to switch the WiFi off when it is not needed for at least some tens of seconds.
-
 \section{Green \texorpdfstring{\glsxtrshort{IOT}}{IoT} computing}
 The data in \cref{tbl:top_sleep} shows that it is worthwhile to put the system in some sleep mode when there is temporarily no work to be done.
 A deeper sleep mode saves more energy, but also requires more work to restore the software to its working state.
-A processor like the ESP8266 driving the Wemos D1 mini loses the content of its \gls{RAM} in deep sleep mode.
+A processor like the ESP8266 driving the \gls{WEMOS} D1 mini loses the content of its \gls{RAM} in deep sleep mode.
 As a result, after waking up, the program itself is preserved, since it is stored in flash memory, but the program state is lost.
 When there is a program state to be preserved, we must either store it elsewhere, limit us to light sleep, or use a microcontroller that keeps the \gls{RAM} intact during deep sleep.
 
@@ -66,41 +76,35 @@ This is especially annoying when the other tasks are executing time critical par
 Such protocols control the communication with sensors and actuators.
 Without the help of an \gls{OS}, the programmer is forced to combine all subtasks into one big system that decides if it is safe to sleep for all subtasks.
 
-\Gls{MTASK} offers abstractions for edge layer-specific details such as the heterogeneity of architectures, platforms and frameworks; peripheral access; and multitasking but also for energy consumption and scheduling.
+The \gls{MTASK} language offers abstractions for edge layer-specific details such as the heterogeneity of architectures, platforms and frameworks; peripheral access; and multitasking but also for energy consumption and scheduling.
 In \gls{MTASK}, tasks are implemented as a rewrite system, where the work is automatically segmented in small atomic bits and stored as a task tree.
 Each cycle, a single rewrite step is performed on all task trees, during rewriting, tasks do a bit of their work and progress steadily, allowing interleaved and seemingly parallel operation.
 After a loop, the \gls{RTS} knows which task is waiting on which triggers and is thus able to determine the next execution time for each task automatically.
 Utilising this information, the \gls{RTS} can determine when it is possible and safe to sleep and choose the optimal sleep mode according to the sleeping time.
 For example, the \gls{RTS} never attempts to sleep during an \gls{I2C} communication because \gls{IO} is always contained \emph{within} a rewrite step.
-
-An \gls{MTASK} program is dynamically transformed to byte code.
-This byte code and the initial \gls{MTASK} expression are shipped to \gls{MTASK} \gls{IOT} node.
-The \gls{MTASK} rewrite engine rewrites the current expression just a single rewrite step at a time.
-When subtasks are composed in parallel, all subtasks are rewritten unless the result of the first rewrite step makes the result of the other tasks superfluous.
-The task design ensures such that all time critical communication with peripherals is within a single rewrite step.
 This is very convenient, since the system can inspect the current state of all \gls{MTASK} expressions after a rewrite and decide if sleeping and how long is possible.
 %As a consequence, we cannot have fair multitasking.
 %When a single rewrite step would take forever due to an infinite sequence of function calls, this would block the entire IoT node.
-Even infinite sequences rewrite steps, as in the \cleaninline{blink} example, are perfectly fine.
+Even infinite sequences rewrite steps are perfectly fine.
 The \gls{MTASK} system does proper tail-call optimizations to facilitate this.
 
-\section{Task scheduling in the \gls{MTASK} language}
+\section{Rewrite interval}
 Some \gls{MTASK} examples contain one or more explicit \cleaninline{delay} primitives, offering a natural place for the node executing it to pause.
 However, there are many \gls{MTASK} programs that just specify a repeated set of primitives.
 A typical example is the program that reads the temperature for a sensor and sets the system \gls{LED} if the reading is below some given \cleaninline{goal}.
 
 \begin{lstClean}[caption={A basic thermostat task.},label={lst:thermostat}]
-thermostat :: Main (MTask v Bool) | mtask v
-thermostat = DHT I2Caddr \dht->
-       {main = rpeat ( temperature dht >>~. \temp.
-                       writeD builtInLED (goal <. temp))}
+thermostat :: Main (MTask v Bool) | mtask, dht v
+thermostat = declarePin D8 PMOutput \ledPin->
+       DHT I2Caddr \dht->
+       {main = rpeat (temperature dht >>~. \temp->
+                      writeD ledPin (goal <. temp))}
 \end{lstClean}
 
 This program repeatedly reads the \gls{DHT} sensor and sets the on-board \gls{LED} based on the comparison with the \cleaninline{goal} as fast as possible on the \gls{MTASK} node.
-This is a perfect solution as long as we ignore the power consumption.
 The \gls{MTASK} machinery ensures that if there are other tasks running on the node, they will make progress.
 However, this solution is far from perfect when we take power consumption into account.
-In most applications, it is very unlikely that the temperature will change significantly within one minute, let alone within some milliseconds.
+In most applications, it is very unlikely that the temperature changes significantly within one minute, let alone within some milliseconds.
 Hence, it is sufficient to repeat the measurement with an appropriate interval.
 
 There are various ways to improve this program.
@@ -111,101 +115,112 @@ Both solutions rely on an explicit action of the programmer.
 
 Fortunately, \gls{MTASK} also contains machinery to do this automatically.
 The key of this solution is to associate dynamically an evaluation interval with each task.
-The interval $\refreshrate{low}{high}$ indicates that the evaluation can be safely delayed by any number of milliseconds in that range.
+The interval $\rewriterate{low}{high}$ indicates that the evaluation can be safely delayed by any number of milliseconds within that range.
 Such an interval is just a hint for the \gls{RTS}.
 It is not a guarantee that the evaluation takes place in the given interval.
 Other parts of the task expression can force an earlier evaluation of this part of the task.
 When the system is very busy with other work, the task might even be executed after the upper bound of the interval.
-The system calculates the refresh rates from the current task expression.
+The system calculates the rewrite rates from the current task expression.
 This has the advantage that the programmer does not have to deal with them and that they are available in each and every \gls{MTASK} program.
 
 \subsection{Basic tasks}
 
-We start by assigning default refresh rates to basic tasks.
-These refresh rates reflect the expected change rates of sensors and other inputs.
-Writing to basic \gls{GPIO} pins and actuators has refresh rate $\refreshrate{0}{0}$, this is never delayed.
+We start by assigning default rewrite rates to basic tasks.
+These rewrite rates reflect the expected change rates of sensors and other inputs.
+Basic tasks to one-shot set a value of a sensor or actuator usually have a rate of $\rewriterate{0}{0}$, this is never delayed, e.g.\ writing to a \gls{GPIO} pin.
+Basic tasks that continuously read a value or otherwise interact with a peripheral have default rewrite rates that fit standard usage of the sensor.
+\Cref{tbl:rewrite} shows the default values for the basic tasks.
+I.e.\ reading \glspl{SDS} and fast sensors such as sound or light aim for a rewrite every \qty{100}{\ms}, medium slow sensors such as gesture sensors every \qty{1000}{\ms} and slow sensors such as temperature or air quality every \qty{2000}{\ms}.
 
 \begin{table}
        \centering
-       \caption{Default refresh rates of basic tasks.}%
-       \label{tbl:refresh}
+       \caption{Default rewrite rates of basic tasks.}%
+       \label{tbl:rewrite}
        \begin{tabular}{ll}
                \toprule
-               task & default interval \\
+               task & default interval\\
                \midrule
-               reading \pgls{SDS} & $\refreshrate{0}{2000}$ \\
-               slow sensor, like temperature & $\refreshrate{0}{2000}$ \\
-               gesture sensor & $\refreshrate{0}{1000}$ \\
-               fast sensor, like sound or light & $\refreshrate{0}{100}$ \\
-               reading GPIO pins & $\refreshrate{0}{100}$ \\
+               reading \pgls{SDS} & $\rewriterate{0}{2000}$\\
+               slow sensor & $\rewriterate{0}{2000}$\\
+               medium sensor & $\rewriterate{0}{1000}$\\
+               fast sensor & $\rewriterate{0}{100}$\\
                \bottomrule
        \end{tabular}
 \end{table}
 
-\subsection{Deriving refresh rates}\label{sec:deriving_refresh_rates}
-Based on these refresh rates, the system can automatically derive refresh rates for composed \gls{MTASK} expressions using $\mathcal{R}$.
-We use the operator $\cap_{\textit{safe}}$ to compose refresh ranges.
-When the ranges overlap the result is the overlapping range.
-Otherwise, the result is the range with the lowest numbers.
-The rationale is that subtasks should not be delayed longer than their refresh range.
-Evaluating a task earlier should not change its result but can consume more energy.
-
-\begin{align}
-       \cap_{\textit{safe}} :: \refreshrate{\mathit{Int}}{\mathit{Int}} \; \refreshrate{\mathit{Int}}{\mathit{Int}} & \shortrightarrow \refreshrate{\mathit{Int}}{\mathit{Int}} & \notag \\
-       R_1 \cap_{\textit{safe}} R_2 & = R_1 \cap R_2 & \text{if } R_1 \cap R_2 \neq \emptyset \\
-       \refreshrate{l_1}{h_1} \cap_{\textit{safe}} \refreshrate{l_2}{h_2} & = \refreshrate{l_2}{h_2} & \text{if } h_2 < l_1 \\
-       R_1 \cap_{\textit{safe}} R_2 & = R_1  & \text{otherwise}
-\end{align}
-
-\begin{align}
-       \mathcal{R} :: (\mathit{MTask}~v~a)    & \shortrightarrow \refreshrate{\mathit{Int}}{\mathit{Int}} \notag \\
-       \mathcal{R} (t_1~{.||.}~t_2)  & = \mathcal{R}(t_1) \cap_{\textit{safe}} \mathcal{R}(t_2) \label{R:or} \\
-       \mathcal{R}(t_1~{.\&\&.}~t_2) & = \mathcal{R}(t_1) \cap_{\textit{safe}} \mathcal{R}(t_2) \label{R:and}\\
-       \mathcal{R}(t_1~{>\!\!>\!|.}~t_2)   & = \mathcal{R}(t_1) \label{R:seq} \\
-       \mathcal{R}(t~{>\!\!>\!=.}~f)       & = \mathcal{R}(t) \label{R:bind} \\
-       \mathcal{R}(t~{>\!\!>\!\!*.}~[a_1 \ldots a_n]) & = \mathcal{R}(t) \label{R:step} \\
-       \mathcal{R}(\mathit{rpeat}~t)             & = \refreshrate{0}{0} \label{R:rpeat} \\
-       \mathcal{R}(\mathit{rpeatEvery}~d~t)   & = \refreshrate{0}{0} \label{R:rpeatevery} \\
-       \mathcal{R}(delay~d) & = \refreshrate{d}{d} \label{R:delay} \\
-       \mathcal{R}(t) & =
-               \left\{%
+\subsection{Deriving rewrite rates}\label{sec:deriving_rewrite_rates}
+Based on these default rewrite rates, the system automatically derives rewrite rates for composed \gls{MTASK} expressions using the function $\mathcal{R}$ as shown in \cref{equ:r}.
+
+\begin{equ}
+       \begin{align}
+               \mathcal{R} :: (\mathit{MTask}~v~a)    & \shortrightarrow \rewriterate{\mathit{Int}}{\mathit{Int}} \notag \\
+               \mathcal{R} (t_1~{.||.}~t_2)  & = \mathcal{R}(t_1) \cap_{\textit{safe}} \mathcal{R}(t_2) \label{R:or} \\
+               \mathcal{R}(t_1~{.\&\&.}~t_2) & = \mathcal{R}(t_1) \cap_{\textit{safe}} \mathcal{R}(t_2) \label{R:and}\\
+               \mathcal{R}(t~{>\!\!>\!\!*.}~[a_1 \ldots a_n]) & = \mathcal{R}(t) \label{R:step} \\
+               \mathcal{R}(\mathit{rpeat}~t~\mathit{start})   & =
+                       \left\{\begin{array}{ll}
+                               \mathcal{R}(t) & \text{if $t$ is unstable}\\
+                               \rewriterate{r_1-\mathit{start}}{r_2-\mathit{start}} & \text{otherwise}\\
+                       \end{array}\right.\\
+               \mathcal{R} (\mathit{waitUntil}~d) & = \rewriterate{e-\mathit{time}}{e-\mathit{time}}\label{R:delay}\\
+               \mathcal{R} (t) & =
+                       \left\{%
+                               \begin{array}{ll}
+                                       \rewriterate{\infty}{\infty}~& \text{if}~t~\text{is Stable} \\
+                                       \rewriterate{r_l}{r_u} & \text{otherwise}
+                               \end{array}
+                       \right.\label{R:other}
+       \end{align}
+       \caption{Function $\mathcal{R}$ for deriving refresh rates.}%
+       \label{equ:r}
+\end{equ}
+
+\subsubsection{Parallel combinators}
+For parallel combinators, the \emph{or}-combinator (\cleaninline{.\|\|.}) in \cref{R:or} and the \emph{and}-combinator (\cleaninline{.&&.}) in \cref{R:and}, the safe intersection (see \cref{equ:safe_intersect}) of the rewrite rates is taken to determine the rewrite rate of the complete task.
+The conventional intersection does not suffice here because it yields an empty intersection when the intervals do not overlap.
+In that case, the safe intersection returns the range with the lowest numbers.
+The rationale is that subtasks should not be delayed longer than their rewrite range.
+Evaluating a task earlier should not change its result but just consumes more energy.
+
+\begin{equ}
+       \[
+               X \cap_{\textit{safe}} Y = \left\{%
                        \begin{array}{ll}
-                               \refreshrate{\infty}{\infty}~& \text{if}~t~\text{is Stable} \\
-                               \refreshrate{r_l}{r_u} & \text{otherwise}
+                               X\cap Y & X\cap Y \neq \emptyset\\
+                               Y & Y_2 < X_1\\
+                               X & \text{otherwise}\\
                        \end{array}
-               \right.\label{R:other}
-\end{align}
-
-We will briefly discuss the various cases of deriving refresh rates together with the task semantics of the different combinators
-
-\subsubsection{Parallel combinators} For the parallel composition of tasks we compute the intersection of the refresh intervals of the components as outlined in the definition of $\cap_{\textit{safe}}$.
-The operator \cleaninline{.\|\|.} in \cref{R:or} is the \emph{or}-combinator; the first subtask that produces a stable value determines the result of the composition.
-The operator \cleaninline{.&&.} in \cref{R:and} is the \emph{and}-operator. The result is the tuple containing both results when both subtasks have a stable value.
-The refresh rates of the parallel combinators have no direct relation with their task result.
+                       \right.
+       \]
+       \caption{Safe intersection operator}\label{equ:safe_intersect}
+\end{equ}
 
 \subsubsection{Sequential combinators}
-For the sequential composition of tasks we only have to look at the refresh rate of the current task on the left.
-The sequential composition operator \cleaninline{>>\|.} in \cref{R:seq} is similar to the monadic sequence operator \cleaninline{>>\|}.
-The operator \cleaninline{>>=.} in \cref{R:bind} provides the stable task result to the function on the right-hand side, similar to the monadic bind.
-The operator \cleaninline{>>~.} steps on an unstable value and is otherwise equal to \cleaninline{>>=.}.
-The step combinator \cleaninline{>>*.} in \cref{R:step} has a list of conditional actions that specify a new task.
-
-\subsubsection{Repeat combinators}
-The repeat combinators repeats their argument indefinitely.
-The combinator \cleaninline{rpeatEvery} guarantees the given delay between repetitions.
-The refresh rate is equal to the refresh rate of the current argument task.
-Only when \cleaninline{rpeatEvery} waits between the iterations of the argument the refresh interval is equal to the remaining delay time.
-
-\subsubsection{Other combinators}
-The refresh rate of the \cleaninline{delay} in \cref{R:delay} is equal to the remaining delay.
-Refreshing stable tasks can be delayed indefinitely, their value never changes.
-For other basic tasks, the values from \cref{tbl:refresh} apply.
-The values $r_l$ and $r_u$ in \cref{R:other} are the lower and upper bound of the rate.
-
-The refresh intervals associated with various steps of the thermostat program from \cref{lst:thermostat} are given in \cref{tbl:intervals}.
+For the step combinator (\cref{R:step})---and all other derived sequential combinators\nobreak---\nobreak\hskip0ptthe refresh rate of the left-hand side task is taken since that is the only task that is rewritten.
+Only after stepping, the combinator rewrites to the right-hand side.
+
+\subsubsection{Repeating combinators}
+The repeat combinators repeats its argument indefinitely.
+As the \cleaninline{rpeat} task tree node already includes a rewrite rate (set to $\rewriterate{0}{0}$ for a default \cleaninline{rpeat}), both \cleaninline{rpeat} and \cleaninline{rpeatEvery} use the same task tree node and thus only one entry is required here.
+The derived refresh rate of the repeat combinator is the refresh rate of the child if it is unstable.
+Otherwise, the refresh rate is the embedded rate time minus the start time.
+In case of the \cleaninline{rpeat} task, the default refresh rate is $\rewriterate{0}{0}$ so the task immediately refreshes and starts the task again.
+\todo{netter opschrijven}
+
+\subsubsection{Delay combinators}
+Upon installation, a \cleaninline{delay} task is stored as a \cleaninline{waitUntil} task tree containing the time of installation added to the specified time to wait.
+Execution wise, it waits until the current time exceeds the time is greater than the argument time.
+
+\subsubsection{Other tasks}
+All other tasks are captured by \cref{R:other}.
+If the task is stable, rewriting can be delayed indefinitely since the value will not change anyway.
+In all other cases, the values from \cref{tbl:rewrite} apply where $r_l$ and $r_u$ represent the lower and upper bound of this rate.
+
+The rewrite intervals associated with various steps of the thermostat program from \cref{lst:thermostat} are given in \cref{tbl:intervals}.
 Those rewrite steps and intervals are circular, after step 2 we continue with step 0 again.
 Only the actual reading of the sensor with \cleaninline{temperature dht} offers the possibility for a non-zero delay.
 
+\subsection{Example}
 %%\begin{table}[tb]
 \begin{table}
        \centering
@@ -221,7 +236,7 @@ rpeat ( temperature dht >>~. \temp.
         writeD builtInLED (goal <. temp)
 )\end{lstClean}
                   &
-                  $\refreshrate{0}{0}$ \\
+                  $\rewriterate{0}{0}$ \\
                   %\hline
                1  &
                \begin{lstClean}[aboveskip=-2ex,belowskip=-2ex,frame=]
@@ -230,7 +245,7 @@ writeD builtInLED (goal <. temp) >>|.
 rpeat ( temperature dht >>~. \temp.
         writeD builtInLED (goal <. temp)
 )\end{lstClean}
-                  & $\refreshrate{0}{2000}$ \\
+                  & $\rewriterate{0}{2000}$ \\
                   %\hline
                2  &
                \begin{lstClean}[aboveskip=-2ex,belowskip=-2ex,frame=]
@@ -238,12 +253,12 @@ writeD builtInLED false >>|.
 rpeat ( temperature dht >>~. \temp.
         writeD builtInLED (goal <. temp)
 )\end{lstClean}
-               & $\refreshrate{0}{0}$ \\
+               & $\rewriterate{0}{0}$ \\
                \bottomrule
        \end{tabular}
 \end{table}
 
-\subsection{Tweaking refresh rates}
+\subsection{Tweaking rewrite rates}
 A tailor-made \gls{ADT} (see \cref{lst:interval}) determines the timing intervals for which the value is determined at runtime but the constructor is known at compile time.
 During compilation, the constructor of the \gls{ADT} is checked and code is generated accordingly.
 If it is \cleaninline{Default}, no extra code is generated.
@@ -252,19 +267,19 @@ In the case that there is a lower bound, i.e.\ the task must not be executed bef
 
 \begin{lstClean}[caption={The \gls{ADT} for timing intervals in \gls{MTASK}.},label={lst:interval}]
 :: TimingInterval v = Default
-                    | BeforeMs (v Int)         // yields [+$\refreshrate{0}{x}$+]
-                    | BeforeS  (v Int)         // yields [+$\refreshrate{0}{x \times 1000}$+]
-                    | ExactMs  (v Int)         // yields [+$\refreshrate{x}{x}$+]
-                    | ExactS   (v Int)         // yields [+$\refreshrate{0}{x \times 1000}$+]
-                    | RangeMs  (v Int) (v Int) // yields [+$\refreshrate{x}{y}$+]
-                    | RangeS   (v Int) (v Int) // yields [+$\refreshrate{x \times 1000}{y \times 1000}$+]
+                    | BeforeMs (v Int)         // yields [+$\rewriterate{0}{x}$+]
+                    | BeforeS  (v Int)         // yields [+$\rewriterate{0}{x \times 1000}$+]
+                    | ExactMs  (v Int)         // yields [+$\rewriterate{x}{x}$+]
+                    | ExactS   (v Int)         // yields [+$\rewriterate{0}{x \times 1000}$+]
+                    | RangeMs  (v Int) (v Int) // yields [+$\rewriterate{x}{y}$+]
+                    | RangeS   (v Int) (v Int) // yields [+$\rewriterate{x \times 1000}{y \times 1000}$+]
 \end{lstClean}
 
 \subsubsection{Sensors and \texorpdfstring{\glspl{SDS}}{shared data sources}}
-In some applications, it is necessary to read sensors or \glspl{SDS} at a different rate than the default rate given in \cref{tbl:refresh}, i.e.\ to customise the refresh rate.
-This is achieved by calling the access functions with a custom refresh rate as an additional argument (suffixed with the backtick (\cleaninline{`}))
+In some applications, it is necessary to read sensors or \glspl{SDS} at a different rate than the default rate given in \cref{tbl:rewrite}, i.e.\ to customise the rewrite rate.
+This is achieved by calling the access functions with a custom rewrite rate as an additional argument (suffixed with the backtick (\cleaninline{`}))
 The adaptions to other classes are similar and omitted for brevity.
-\Cref{lst:dht_ext} shows the extended \cleaninline{dht} and \cleaninline{dio} class definition with functions for custom refresh rates.
+\Cref{lst:dht_ext} shows the extended \cleaninline{dht} and \cleaninline{dio} class definition with functions for custom rewrite rates.
 
 \begin{lstClean}[caption={Auxiliary definitions to \cref{lst:gpio,lst:dht} for \gls{DHT} sensors and digital \gls{GPIO} with custom timing intervals.},label={lst:dht_ext}]
 class dht v where
@@ -299,8 +314,8 @@ devTask =
 \subsubsection{Repeating tasks}
 The task combinator \cleaninline{rpeat} restarts the child task in the evaluation if the previous produced a stable result.
 However, in some cases it is desirable to postpone the restart of the child.
-For this, the \cleaninline{rpeatEvery} task is introduced which receives an extra argument, the refresh rate, as shown in \cref{lst:rpeatevery}.
-Instead of immediately restarting the child once it yields a stable value, it checks whether the lower bound of the provided timing interval has passed since the start of the task.\footnotemark{}
+For this, the \cleaninline{rpeatEvery} task is introduced which receives an extra argument, the rewrite rate, as shown in \cref{lst:rpeatevery}.
+Instead of immediately restarting the child once it yields a stable value, it checks whether the lower bound of the provided timing interval has passed since the start of the task\footnotemark.
 \footnotetext{In reality, it also compensates for time drift by taking into account the upper bound of the timing interval.
 If the task takes longer to stabilise than the upper bound of the timing interval, this upper bound is taken as the start of the task instead of the actual start.}
 
@@ -336,15 +351,15 @@ timedPulseNaive = declarePin D0 PMOutput \d0->
        }
 \end{lstClean}
 
-\section{Task scheduling in the \texorpdfstring{\gls{MTASK}}{mTask} engine}
-The refresh rates from the previous section only tell us how much the next evaluation of the task can be delayed.
-An \gls{IOT} edge devices executes multiple tasks may run interleaved.
+\section{Task scheduling in the \texorpdfstring{\gls{MTASK}}{mTask} engine}\label{sec:scheduling}
+The rewrite rates from the previous section only tell us how much the next evaluation of the task can be delayed.
+In the \gls{MTASK} system, an \gls{IOT} edge devices can run multiple tasks.
 In addition, it has to communicate with a server to collect new tasks and updates of \glspl{SDS}.
-Hence, the refresh intervals cannot be used directly to let the microcontroller sleep.
+Hence, the rewrite intervals cannot be used directly to let the microcontroller sleep.
 Our scheduler has the following objectives.
 \begin{itemize}
        \item
-               Meet the deadline whenever possible, i.e.\ the system tries to execute every task before the end of its refresh interval.
+               Meet the deadline whenever possible, i.e.\ the system tries to execute every task before the end of its rewrite interval.
                Only too much work on the device might cause an overflow of the deadline.
        \item
                Achieve long sleep times. Waking up from sleep consumes some energy and takes some time.
@@ -352,17 +367,17 @@ Our scheduler has the following objectives.
        \item
                The scheduler tries to avoid unnecessary evaluations of tasks as much as possible.
                A task should not be evaluated now when its execution can also be delayed until the next time that the device is active.
-               That is, a task should preferably not be executed before the start of its refresh interval.
-               Whenever possible, task execution should even be delayed when we are inside the refresh interval as long as we can execute the task before the end of the interval.
+               That is, a task should preferably not be executed before the start of its rewrite interval.
+               Whenever possible, task execution should even be delayed when we are inside the rewrite interval as long as we can execute the task before the end of the interval.
        \item
                The optimal power state should be selected.
                Although a system uses less power in a deep sleep mode, it also takes more time and energy to wake up from deep sleep.
                When the system knows that it can sleep only a short time it is better to go to light sleep mode since waking up from light sleep is faster and consumes less energy.
 \end{itemize}
 
-The algorithm $\mathcal{R}$ from \cref{sec:deriving_refresh_rates} computes the evaluation rate of the current tasks.
+The algorithm $\mathcal{R}$ from \cref{sec:deriving_rewrite_rates} computes the evaluation rate of the current tasks.
 For the scheduler, we transform this interval to an absolute evaluation interval; the lower and upper bound of the start time of that task measured in the time of the \gls{IOT} edge device.
-We obtain those bounds by adding the current system time to the bounds of the computed refresh interval by algorithm $\mathcal{R}$.
+We obtain those bounds by adding the current system time to the bounds of the computed rewrite interval by algorithm $\mathcal{R}$.
 
 For the implementation, it is important to note that the evaluation of a task takes time.
 Some tasks are extremely fast, but other tasks require long computations and time-consuming communication with peripherals as well as with the server.
@@ -370,7 +385,7 @@ These execution times can yield a considerable and noticeable time drift in \gls
 For instance, a task like \cleaninline{rpeatEvery (ExactMs 1) t} should repeat \cleaninline{t} every millisecond.
 The programmer might expect that \cleaninline{t} will be executed for the ${(N+1)}^{th}$ time after $N$ milliseconds.
 Uncompensated time drift might make this considerably later.
-\Gls{MTASK} does not pretend to be a hard real-time \gls{OS}, and cannot give firm guarantees with respect to evaluation time.
+The \gls{MTASK} \gls{RTS} does not pretend to be a hard real-time \gls{OS}, and cannot give firm guarantees with respect to evaluation time.
 Nevertheless, we try to make time handling as reliable as possible.
 This is achieved by adding the start time of this round of task evaluations rather than the current time to compute absolute execution intervals.
 
@@ -380,7 +395,7 @@ When the microcontroller is active, it checks the connection and updates from th
 Next, the microcontroller goes to light sleep for the minimum of a predefined interval and the task delay.
 
 In general, the microcontroller node executes multiple \gls{MTASK} tasks at the same time.
-\Gls{MTASK} nodes repeatedly check for inputs from servers and execute all tasks that cannot be delayed to the next evaluation round one step.
+The \gls{MTASK} node repeatedly check for inputs from servers and execute all tasks that cannot be delayed to the next evaluation round one step.
 The tasks are stored in a priority queue to check efficiently which of them need to be stepped.
 The \gls{MTASK} tasks are ordered at their absolute latest start time in this queue; the earliest deadline first.
 We use the earliest deadline to order tasks with equal latest deadline.
@@ -423,7 +438,7 @@ A task that produces a stable value is completed and is not queued again.
 
 The \cleaninline{sleep} function determines the maximum sleep time based on the top of the queue.
 The computed sleep time and the characteristics of the microprocessor determine the length and depth of the sleep.
-For very short sleep times it might not be worthwhile to sleep.
+For very short sleep times it is not be worthwhile to put the processor in sleep mode.
 In the current \gls{MTASK} \gls{RTS}, the thresholds are determined by experimentation but can be tuned by the programmer.
 On systems that lose the content of their \gls{RAM} it is not possible to go to deep sleep mode.
 
@@ -433,9 +448,9 @@ These interrupts are hard-wired signals that can interrupt the normal flow of th
 While the \glspl{ISR} look like regular functions, they do come with some limitations.
 For example, they must be very short, in order not to miss future interrupts; can only do very limited \gls{IO}; cannot reliably check the clock; and they operate in their own stack, and thus communication must happen via global variables.
 After the execution of the \gls{ISR}, the normal program flow is resumed.
-Interrupts are heavily used internally in the \gls{RTS} of the microcontrollers to perform timing critical operations such as WiFi, \gls{I2C}, or \gls{SPI} communication; completed \gls{ADC} conversions, software timers; exception handling; \etc.
+Interrupts are heavily used internally in the \gls{RTS} of the microcontrollers to perform timing critical operations such as \gls{WIFI}, \gls{I2C}, or \gls{SPI} communication; completed \gls{ADC} conversions; software timers; exception handling; \etc.
 
-Interrupts offer two substantial benefits: fewer missed events and better energy usage.
+Using interrupts in \gls{MTASK} task offer two substantial benefits: fewer missed events and better energy usage.
 Sometimes an external event such as a button press only occurs for a very small duration, making it possible to miss it due to it happening right between two polls.
 Using interrupts is not a fool-proof way of never missing an event.
 Events may still be missed if they occur during the execution of an \gls{ISR} or while the microcontroller is still in the process of waking up from a triggered interrupt.
@@ -484,7 +499,7 @@ Only when an interrupt triggers, the program continues, writes the state to the
 #define INTERRUPTPIN 11
 #define DEBOUNCE 30[+\label{lst:arduino_interrupt:defs_to}+]
 
-volatile byte state = LOW;[+\label{lst:arduino_interrupt:state}+]
+volatile int state = LOW;[+\label{lst:arduino_interrupt:state}+]
 volatile bool cooldown = true;[+\label{lst:arduino_interrupt:cooldown}+]
 
 void setup() {[+\label{lst:arduino_interrupt:setup_fro}+]
@@ -507,7 +522,7 @@ void buttonPressed() {[+\label{lst:arduino_interrupt:isr_fro}+]
 }[+\label{lst:arduino_interrupt:isr_to}+]
 \end{lstArduino}
 
-\subsection{\texorpdfstring{\Gls{MTASK}}{MTask} language}
+\subsection{The \texorpdfstring{\gls{MTASK}}{mTask} language}
 \Cref{lst:mtask_interrupts} shows the interrupt interface in \gls{MTASK}.
 The \cleaninline{interrupt} class contains a single function that, given an interrupt mode and a \gls{GPIO} pin, produces a task that represents this interrupt.
 Lowercase variants of the various interrupt modes such as \cleaninline{change :== lit Change} are available as convenience macros (see \cref{sec:expressions}).
@@ -519,8 +534,8 @@ class interrupt v where
 :: InterruptMode = Change | Rising | Falling | Low | High
 \end{lstClean}
 
-When the \gls{MTASK} device executes this task, it installs an \gls{ISR} and sets the refresh rate of the task to infinity, $\refreshrate{\infty}{\infty}$.
-The interrupt handler is set up in such a way that the refresh rate is changed to $\refreshrate{0}{0}$ once the interrupt triggers.
+When the \gls{MTASK} device executes this task, it installs an \gls{ISR} and sets the rewrite rate of the task to infinity, $\rewriterate{\infty}{\infty}$.
+The interrupt handler is set up in such a way that the rewrite rate is changed to $\rewriterate{0}{0}$ once the interrupt triggers.
 As a consequence, the task is executed on the next execution cycle.
 
 The \cleaninline{pirSwitch} function in \cref{lst:pirSwitch} creates, given an interval in \unit{\ms}, a task that reacts to motion detection by a \gls{PIR} sensor (connected to \gls{GPIO} pin 0) by lighting the \gls{LED} connected to \gls{GPIO} pin 13 for the given interval.
@@ -532,13 +547,13 @@ pirSwitch :: Int -> Main (MTask v Bool) | mtask v
 pirSwitch =
        declarePin D13 PMOutput \ledpin->
        declarePin D0 PMInput \pirpin->
-       {main = rpeat (      interrupt high pirpin
-                       >>|. writeD ledpin false
-                       >>|. delay (lit interval)
-                       >>|. writeD ledpin true) }
+       {main = rpeat (     interrupt high pirpin
+                      >>|. writeD ledpin false
+                      >>|. delay (lit interval)
+                      >>|. writeD ledpin true) }
 \end{lstClean}
 
-\subsection{\texorpdfstring{\Gls{MTASK}}{MTask} engine}
+\subsection{The \texorpdfstring{\gls{MTASK}}{mTask} engine}
 
 While interrupt tasks have their own node type in the task tree, they differ slightly from other node types because they require a more elaborate setup and teardown.
 Enabling and disabling interrupts is done in a general way in which tasks register themselves after creation and deregister after deletion.
@@ -564,7 +579,7 @@ Furthermore, as the \gls{ISR} is supposed to be be very short, just a flag in th
 Interrupt event flags are processed at the beginning of the event loop, before tasks are executed.
 For each subscribed task, the task tree is searched for nodes listening for the particular interrupt.
 When found, the node is flagged and the pin status is written.
-Afterwards, the evaluation interval of the task is set to $\refreshrate{0}{0}$ and the task is reinsterted at the front of the scheduling queue to ensure rapid evaluation of the task.
+Afterwards, the evaluation interval of the task is set to $\rewriterate{0}{0}$ and the task is reinsterted at the front of the scheduling queue to ensure rapid evaluation of the task.
 Finally, the event is removed from the registration and the interrupt is disabled.
 The interrupt can be disabled as all tasks waiting for the interrupt become stable after firing.
 More occurrences of the interrupts do not change the value of the task as stable tasks keep the same value forever.
@@ -573,5 +588,8 @@ Evaluating interrupt task node in the task tree is trivial because all of the wo
 The task emits the status of the pin as a stable value if the information in the task shows that it was triggered.
 Otherwise, no value is emitted.
 
+\section{Conclusion}
+\todo[inline]{Conclusion}
+
 \input{subfilepostamble}
 \end{document}