-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathintroductory.lua
More file actions
49 lines (40 loc) · 1.62 KB
/
introductory.lua
File metadata and controls
49 lines (40 loc) · 1.62 KB
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
local rfsm = require("rfsm")
-- any rFSM is always contained in a composite_state
local state, conn, trans = rfsm.state, rfsm.conn, rfsm.trans
return state {
dbg = true, -- enable debugging
on = state {
entry = function () print("disabling brakes") end,
exit = function () print("enabling brakes") end,
moving = state {
entry=function () print("starting to move") end,
exit=function () print("stopping") end,
},
waiting = state {},
-- define some transitions
trans{ src='initial', tgt='waiting' },
trans{ src='waiting', tgt='moving', events={ 'e_start' } },
trans{ src='moving', tgt='waiting', events={ 'e_stop' } },
},
in_error = state {
doo = function (fsm)
print ("Error detected - trying to fix")
rfsm.yield()
math.randomseed( os.time() )
rfsm.yield()
if math.random(0,100) < 40 then
print("unable to fix, raising e_fatal_error")
rfsm.send_events(fsm, "e_fatal_error")
else
print("repair succeeded!")
rfsm.send_events(fsm, "e_error_fixed")
end
end,
},
fatal_error = state {},
trans{ src='initial', tgt='on', effect=function () print("initalizing system") end },
trans{ src='on', tgt='in_error', events={ 'e_error' } },
trans{ src='in_error', tgt='on', events={ 'e_error_fixed' } },
trans{ src='in_error', tgt='fatal_error', events={ 'e_fatal_error' } },
trans{ src='fatal_error', tgt='initial', events={ 'e_reset' } },
}