big update
[msc-thesis1617.git] / methods.tex
1 \section{\acrlong{TOP}}
2 \gls{TOP} is a recent new programming paradigm implemented as
3 \gls{iTasks}~\cite{achten_introduction_2015} in
4 the pure lazy functional language \gls{Clean}
5
6 \todo{Main terms}
7 The lazy functional programming language based on graph rewriting
8 \gls{Clean}~\cite{brus_cleanlanguage_1987}
9
10 \section{\acrlong{EDSL}s}
11
12 \section{Devices}
13
14 \subsection{Specification}
15
16 \subsection{Communication}
17
18
19 \section{mTasks}
20 \subsection{\gls{EDSL}}
21
22 \subsection{Shares}
23 %\subsection{Serial port communication in Clean and iTasks}
24 %In the first exploration stage I added duplex serial port communication to
25 %iTasks in the same way as TCP is added. To make it work several changes had to
26 %be done to the iTasks core to allow backgroundtasks to be added at runtime. The
27 %function shown in Listing~\ref{lst:serialtask} results into a task that
28 %sends the data added to the output queue through the serial port and adds data
29 %received to the input queue for the user to process.
30 %
31 %\begin{lstlisting}[caption={Serial port communication in iTasks},
32 % language=Clean,label={lst:serialtask}]
33 %syncSerialChannel :: String TTYSettings (Shared ([String],[String],Bool)) -> Task ()
34 %
35 %:: ByteSize = BytesizeFive | BytesizeSix | BytesizeSeven | BytesizeEight
36 %:: Parity = ParityNone | ...ParityOdd | ParityEven | ParitySpace | ParityMark
37 %:: BaudRate = ... | B9600 | B19200 | ...
38 %:: TTYSettings = {
39 % baudrate :: BaudRate,
40 % bytesize :: ByteSize,
41 % parity :: Parity,
42 % stop2bits :: Bool,
43 % xonxoff :: Bool}
44 %\end{lstlisting}
45 %
46 %\subsection{mTasks}
47 %The core of the project revolves around the embedded domain specific language
48 %(EDSL) called mTask designed by Pieter Koopman. mTasks is used to use the task
49 %oriented programming on microcontrollers in a type-safe environment. Originally
50 %mTasks are compiled to c code that could be compiled for arduino compatible
51 %devices. Such generated code will be flashed to the program memory once. In
52 %short, the original mTask system is comparable to an entire iTasks sytem
53 %including the engine.
54 %
55 %For this project the imperative language constructs of the mTask DSL are used.
56 %Listing~\ref{lst:mtask} shows some of these class base DSL components for
57 %things like arithmetics, sequencing, conditionals, shared data sources and
58 %interaction with the user LEDs. Together with some helper functions code
59 %programmed in this DSL can be compiled to bytecode and sent to a device.
60 %
61 %\begin{lstlisting}[language=Clean,label={lst:mtask},
62 % caption={Parts of the mTask DSL}]
63 %:: Upd = Upd
64 %:: Expr = Expr
65 %:: Stmt = Stmt
66 %:: UserLED = LED1 | LED2 | LED3
67 %
68 %:: Main a = {main :: a}
69 %
70 %class arith v where
71 % lit :: t -> v t Expr | ...
72 % (+.) infixl 6 :: (v t p) (v t q) -> v t Expr | ...
73 %class IF v where
74 % IF :: (v Bool p) (v t q) (v s r) -> v () Stmt | isExpr p
75 %class sds v where
76 % sds :: ((v t Upd)->In t (Main (v c s))) -> (Main (v c s)) | ...
77 % pub :: (v t Upd) -> v t Expr | ...
78 %class seq v where
79 % (:.) infixr 0 :: (v t p) (v u q) -> v u Stmt | ...
80 %class assign v where
81 % (=.) infixr 2 :: (v t Upd) (v t p) -> v t Expr | ...
82 %class noOp v where noOp :: v t p
83 %class userLed v where
84 % ledOn :: UserLED -> (v () Stmt)
85 % ledOff :: UserLED -> (v () Stmt)
86 %\end{lstlisting}
87 %
88 %\subsection{iTasks}
89 %In iTasks several tasks have been devised to handle the communication. Moreover
90 %the tasks will synchronize the shared data sources in the mTask domain with
91 %real SDSs in the iTasks domain allowing for communication between mTasks and
92 %iTasks tasks. To not clutter the communication channels the SDSs are not
93 %synchronized on every change on the device. When a share changes on the server
94 %his new value will be pushed to the device immediately. When a share is updated
95 %on the client it must be explicitly published using. This approach has been
96 %taken because some shares might be only used internally and therefore would
97 %clutter the communication channels that could be low-bandwidth.
98 %
99 %
100 %\subsection{Devices}
101 %For the devices an engine has been built that can receive mTasks and SDSs and
102 %execute them accordingly. To add a new device to the list the programmer just
103 %has to implement a relatively small interface. All the other functionality is
104 %using standard C or the interface. This interface is listed in~%
105 %\ref{lst:interface} and includes reading and writing data, controlling the
106 %peripherals and some auxiliary functions.
107 %
108 %\begin{lstlisting}[language=c,caption={Device interface},label={lst:interface}]
109 %uint8_t read_byte(void);
110 %void write_byte(uint8_t b);
111 %
112 %void write_dpin(uint8_t i, bool b);
113 %bool read_dpin(uint8_t i);
114 %
115 %void write_apin(uint8_t i, uint8_t a);
116 %uint8_t read_apin(uint8_t i);
117 %
118 %long millis(void);
119 %bool input_available(void);
120 %void delay(long ms);
121 %
122 %void setup(void);
123 %void debug(char *fmt, ...);
124 %void pdie(char *s);
125 %void die(char *fmt, ...);
126 %\end{lstlisting}
127 %
128 %mTasks run either at a fixed interval or are one-shot which is added to the
129 %message. The entire protocol specification can be found in the code that is
130 %available. When an mTask is one-shot it will only be executed once and then
131 %removed. This can be useful for user related tasks such as shutting down blinds
132 %or turning on lights for an indefinite time. mTasks that run at a fixed
133 %interval time can be used to monitor sensors or to periodically communicate
134 %with peripherals like LCD screens.
135 %
136 %There are many things to be improved upon in future research. Most likely these
137 %points will be touched upon in my Master's thesis research.
138 %\begin{itemize}
139 % \item Support task combinators and functions.
140 %
141 % The mTask language already supports the step combinator and it might be
142 % fruitful to add for more expressively. Moreover functions would also
143 % add a lot. They can be used to share code between tasks to reduce the
144 % bytecode size.
145 % \item Seamless integration with iTasks.
146 %
147 % At the moment everything works but is hacked together. I would like to
148 % extend this with a more robust system that allows adding devices on the
149 % fly and adds functionality to monitor the mTask devices.
150 % \item Dynamic mTask/SDS allocation.
151 %
152 % Currently all client data for mTask and SDS storage is statically
153 % allocated. This means that when only a few tasks are used the memory
154 % needed is too high. This can be improved upon by only allocating
155 % resources for tasks when they are requested and this would allow the
156 % system to run on low memory devices like arduino's.
157 % \item Extend on SDSs.
158 %
159 % In the current system the shared data sources used in mTask programs
160 % live in a different domain and are synchronized with an iTask
161 % counterpart. Programming mTasks could be made more intuitive if you
162 % could use standard SDSs from iTasks. Moreover, at the moment only
163 % integer and boolean shares are allowed. This really should be extended
164 % to at least strings.
165 % \item Slicing tasks.
166 %
167 % Every mTask runs in its entirety and to not run into race and
168 % scheduling problems loops are not allowed in the mTasks. An improvement
169 % to this could be multithreading the tasks by giving them slices of
170 % computation and pausing them every slice. In this way loops could be
171 % allowed without blocking the entire system. It does require more memory
172 % however.
173 % \item Run tasks when an interrupt fires.
174 %
175 % Tasks can be scheduled either one-shot or at an interval. It might be
176 % useful to tie mTasks to hardware interrupts to increase responsiveness
177 % and possibly battery life. These interrupts do not need to be hardware
178 % based. A usecase might be to run a task when some other task is
179 % yielding a value (see task combinators) or to run a task when a shared
180 % data source is received from the server.
181 %\end{itemize}