forked from kmarkus/rfsm-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheatsheet.tex
348 lines (270 loc) · 10 KB
/
cheatsheet.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
\documentclass[10pt,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage{ifthen}
\usepackage[landscape]{geometry}
\usepackage{amsmath,amsthm,amsfonts,amssymb}
\usepackage{color,graphicx,overpic}
\usepackage{hyperref}
\usepackage{fancyvrb}
\usepackage{url}
\include{pygmentize}
\pdfinfo{
/Title (rfsm-cheatsheat.pdf)
/Creator (TeX)
/Producer (pdfTeX 1.40.0)
/Author (Markus Klotzbuecher)
/Subject (rFSM cheatsheet)
/Keywords (rfsm, statecharts, cheatsheet, lua)}
% This sets page margins to .5 inch if using letter paper, and to 1cm
% if using A4 paper. (This probably isn't strictly necessary.)
% If using another size paper, use default 1cm margins.
\ifthenelse{\lengthtest { \paperwidth = 11in}}
{ \geometry{top=.5in,left=.5in,right=.5in,bottom=.5in} }
{\ifthenelse{ \lengthtest{ \paperwidth = 297mm}}
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
{\geometry{top=1cm,left=1cm,right=1cm,bottom=1cm} }
}
% Turn off header and footer
\pagestyle{empty}
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\small\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\footnotesize\bfseries}}
\makeatother
% Define BibTeX command
\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
% Don't print section numbers
\setcounter{secnumdepth}{0}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
%My Environments
\newtheorem{example}[section]{Example}
% -----------------------------------------------------------------------
\begin{document}
\raggedright
\scriptsize
\begin{multicols}{3}
% multicol parameters
% These lengths are set only within the two main columns
%\setlength{\columnseprule}{0.25pt}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}
\begin{center}
\Large{\underline{rFSM Cheatsheet}}\\
\small{(beta5)} \\
\end{center}
\section{Specifying rFSM Models}
\subsubsection{Synopsis}
\begin{verbatim}
return rfsm.state {
...
}
\end{verbatim}
\subsubsection{Decription}
A rFSM model is contained in a file that returns the toplevel state.
\subsection{Toplevel rFSM Configuration}
The following fields may defined in the toplevel state:
\begin{itemize}
\item \texttt{\texttt{getevents()}}: function that will be executed
during step/run and must return a table of new events (index part
only).
\item \texttt{dbg}, \texttt{info}, \texttt{err}: log functions. Must
accept multiple parameters to log.
\end{itemize}
%%% States
\subsection{States}
States are the principle building block of rFSM Statecharts.
\subsubsection{Synopsis}
\begin{verbatim}
rfsm.state {
entry=function() do_this() end,
exit=function() do_that() end,
substate1=rfsm.state{...}, ...
transition{...} ...
}
\end{verbatim}
\subsubsection{Fields}
\begin{itemize}
\item \texttt{entry(fsm, state, 'entry'}: entry function
\item \texttt{exit(fsm, state, 'exit'}: entry function
\item \texttt{doo(fsm, state, 'doo'}: doo function/coroutine (only permited for leaf states)
\end{itemize}
\subsubsection{Description}
States model distinguishable conditions and encapsulate behavior. If a
state has substates it is called a \textbf{composite state}. Otherwise
it is called a \textbf{leaf state}. If a transition ends on a
composite state, this state must define an initial
transition. \textbf{Common error:} forgetting the commas.
\subsection{Transitions}
Transition connect states.
\subsubsection{Synopsis}
\begin{verbatim}
rfsm.transition{ src=''stateX'', tgt=''stateY'',
events={"e_1", "e_2"} }
rfsm.trans{ ... } -- short form
\end{verbatim}
\subsubsection{Fields}
\begin{itemize}
\item \texttt{src=<state-ref>}: source state reference
\item \texttt{tgt=<state-ref>}: target state reference
\item \texttt{events=\{event1, event2, ...\} }: list of events of
which each may trigger the transition (\texttt{or}).
\item \texttt{guard}: side-effect free function that returns
\texttt{true} or \texttt{false}. If false will inhibit transition.
\item \texttt{effect}: function that is execute when transition is taken.
\item \texttt{pn=<number>}: Priority number. Transitions with larger
numbers take priority in case of conflicting transitions.
\end{itemize}
\subsubsection{Description}
Transitions define how the FSM changes state when events are received.
\texttt{<state-ref>} can be \textit{absolute} ``root.s1.s2'',
\textit{local} ``s1'' or \textit{relative} (``.subst1.subst2'').
Transitions may cross state boundaries or be layered on top of deeper
nested states by parent states using the relative notation.
\subsection{Connectors}
\label{sec:connectors}
\subsubsection{Synopsis}
\begin{verbatim}
rfsm.connector{}
rfsm.conn{} -- short form
\end{verbatim}
\subsubsection{Description}
Connectors can be used to build composite transitions by chaining
multiple connectors, or for defining different entry and exit points
of composite states. \textit{Note:} connectors only permit to build
more sophisticated transitions, the connector is \textit{never}
active!
\section{Loading, Instantiating and Advancing}
\subsection{Loading and Instantiating}
\subsubsection{Synopsis}
\begin{itemize}
\item \texttt{model=rfsm.load(filename)} Load a rFSM Model from a file.
\item \texttt{rfsm.init(model)} instantiate and validate a rFSM model
\end{itemize}
\subsection{Advancing rFSM instances}
\subsubsection{Synopsis}
\begin{itemize}
\item \texttt{rfsm.step(fsm)}: \texttt{step} a rFSM instance. This will execute at most one transition or one doo cycle.
\item \texttt{rfsm.step(fsm, N)}: \texttt{step} as above, but will perform at most \texttt{N} transitions or doo cycles.
\item \texttt{rfsm.run(fsm)} will run step until there are no new events and no active \texttt{doo} function.
\item \texttt{rfsm.send\_events(fsm, event1, event2, \dots)}: send events to the internal fsm queue.
\end{itemize}
\subsubsection{Description}
The basic step consists of the following: \textbf{1.} retrieve new
events using \texttt{getevents} hook. \textbf{2.} Find enabled
transitions starting from \texttt{root} state to active
leaf. \textbf{3.} If enabled transition found $\rightarrow$
execute. \textbf{4.} elseif active \texttt{doo} $\rightarrow$ run
it. \textbf{5.} discard current events.
After each \texttt{step} exactly \textbf{one} leaf-state will be
active.
\section{Miscellaneous}
\label{sec:miscellaneous}
\subsection{Tracing}
\label{sec:debugging}
Enable state entry and exit debug messages:
\begin{verbatim}
require "rfsmpp"
fsm.dbg=rfsmpp.gen_dbgcolor("fsmX",
{ STATE_ENTER=true,
STATE_EXIT=true }, false)
\end{verbatim}
\medskip
Most important debug IDs are:
\begin{verbatim}
STATE_ENTER, STATE_EXIT, EFFECT, DOO, EXEC_PATH,
ERROR, HIBERNATING, RAISED, TIMEEVENT
\end{verbatim}
\subsection{Time Events}
\begin{verbatim}
require "rfsm_timeevent
rfsm_timeevent.set_gettime_hook(gettime_func)
return rfsm.state {
trans{ src="sA", tgt="sB", events={ "e_after(3)"} },
...
}
\end{verbatim}
\subsubsection{Description}
The \texttt{rfsm\_timeevent} module enables relative time
events. After loading, a suitable \texttt{gettime} function must be
set. This function returns two values: current seconds and current
nanoseconds. Available functions: \texttt{rtp.clock\_gettime} (from
the rtp module, see section \textit{Links}), \texttt{rtt.getTime} from RTT-Lua, or for
second resolution events the Lua built-in:
\begin{verbatim}
function gettime() return os.time(), 0 end
\end{verbatim}
\textit{Note:} These timeevents only work for periodically triggered
components.
\subsection{Sequential AND state}
\subsubsection{Synopsis}
\begin{verbatim}
seqAND = rfsm.seqand {
subfsm1=rfsm.init(rfsm.load("subfsm1.lua")),
subfsm2=...
}
\end{verbatim}
\subsubsection{Fields}
\begin{itemize}
\item \texttt{order}: table of substate names that indicate the desired execution order. Not mentioned states are executed last.
\item \texttt{andseqdbg}: if true print debug info
\item \texttt{step}: number of steps to advance each time.
\item \texttt{run}: if true, don't step but \texttt{run}.
\item \texttt{idle\_doo}: doo flag to be returned by seqand yield.
\end{itemize}
\subsubsection{Description}
Specialized state that \texttt{step}'s or \texttt{run}'s the
initialized substates in a serialized manner inside the \texttt{doo}
function of the seqand state.
\subsection{Complete Example}
\begin{verbatim}
local state, trans, conn = rfsm.state, rfsm.conn, rfsm.trans
return rfsm.state {
dbg=rfsmpp.gen_dbgcolor("sample-fsm")
entry=function() enable_robot() end,
entry=function() disable_robot() end,
stopped = state{},
moving = state {
entry=function() start_motor() end,
exit=function() stop_motor() end,
doo = function()
while true do
compute_next_pos()
rfsm.yield(true)
end,
}
trans{ src="initial", tgt="stopped" },
trans{ src="stopped", tgt="moving", events={"e_start"} },
trans{ src="moving", tgt="stopped", events={"e_stop"} },
}
\end{verbatim}
\subsection{Links}
\label{sec:links}
\url{www.orocos.org/rfsm}
\url{http://www.orocos.org/wiki/orocos/toolchain/LuaCookbook}
\url{https://github.com/kmarkus/rtp}
% You can even have references
\rule{0.3\linewidth}{0.25pt}
\scriptsize
\bibliographystyle{abstract}
\bibliography{refFile}
\end{multicols}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End: