-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpreview_example2.lua
More file actions
65 lines (51 loc) · 1.92 KB
/
preview_example2.lua
File metadata and controls
65 lines (51 loc) · 1.92 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--- Simple preview coordination example.
-- Variant that shows how the prepare function can be setup to only be
-- called once.
local rfsm = require("rfsm")
local rfsm_timeevent = require("rfsm.timeevent")
local ac=require("ansicolors")
local state, trans = rfsm.state, rfsm.trans
rfsm_timeevent.set_gettime_hook(function () return os.time(), 0 end)
function prnt(...) print(ac.green(ac.bright(table.concat({...}, '\t')))) end
--- Generate a one shot function.
-- Generate a wrapper function that, when called, will call the function
-- supplied as an argument exactly once. After that it must be reset
-- using the reload function.
-- @param func function to be called once
-- @return one shot function
-- @return reload function
function gen_call_it_once(func)
local loaded=true
local function reload() loaded=true end
local function caller(...)
if loaded then loaded=false; return func(...) end
end
return caller, reload
end
-- here we generate the one-shot prepare function and the
-- reloader. These are then used in the FSM below:
pre_pos_arm, reload_pre_pos_arm = gen_call_it_once(
function () prnt("pre-positioning arm for grasp") end
)
--- FSM
return state {
idle_hook=function() uml(); os.execute("sleep 0.5") end,
approach_grasp_pos = state {
entry=function () prnt("approaching grasp position") end
},
grasping = state {
prepare=pre_pos_arm,
entry=function() prnt("executing grasp") end
},
drop_off = state{
entry=function() prnt("dropped off object") end,
},
trans{ src='initial', tgt='approach_grasp_pos' },
trans{ src='approach_grasp_pos', tgt='grasping',
events={ 'e_after(5)' },
likely=function() return true end, -- static high probability of transitioning
effect=reload_pre_pos_arm,
},
trans{ src='grasping', tgt='drop_off', events={ 'e_done' } },
trans{ src='drop_off', tgt='approach_grasp_pos', events={ 'e_after(5)' } },
}