forked from kmarkus/rFSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfsm_rtt.lua
291 lines (248 loc) · 9.61 KB
/
rfsm_rtt.lua
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
-- rFSM RTT
--
-- (C) 2010-2013 Markus Klotzbuecher <markus.klotzbuecher@mech.kuleuven.be>
-- (C) 2014-2020 Markus Klotzbuecher <mk@mkio.de>
--
-- SPDX-License-Identifier: BSD-3-Clause
--
-- This module contains some useful functions for using the rfsm
-- statecharts together with OROCOS RTT.
--
require "rttlib"
require "utils"
require "rfsm"
local rtt = rtt
local rfsm = rfsm
local rttlib = rttlib
local string = string
local utils = utils
local print = print
local assert, ipairs, pairs, type, error, tostring = assert, ipairs, pairs, type, error, tostring
module("rfsm_rtt")
--- Generate an event reader function.
--
-- When called this function will read all new events from the given
-- dataports and return them in a table.
--
-- @param ... list of ports to read events from
-- @return getevent function
function gen_read_events(...)
local str_ev = rtt.Variable("string")
local function read_events(tgttab, port)
local fs,ev
while true do
fs, ev = port:read()
if fs == 'NewData' then
tgttab[#tgttab+1] = ev
else
break -- OldData or NoData
end
end
end
local ports = {...}
assert(#ports > 0, "no ports given")
-- check its all ports
return function ()
local res = {}
for _,port in ipairs(ports) do
read_events(res, port)
end
return res
end
end
--- Generate an event reader function optimized for string events.
--
-- When called this function will read all new events from the given
-- dataports and return them in a table.
--
-- @param ... list of ports to read events from
-- @return getevent function
function gen_read_str_events(...)
local str_ev = rtt.Variable("string")
local function read_events(tgttab, port)
local fs
while true do
fs = port:read(str_ev)
if fs == 'NewData' then tgttab[#tgttab+1] = str_ev:tolua()
else break end -- OldData or NoData
end
end
local ports = {...}
assert(#ports > 0, "no ports given")
-- check its all ports
return function ()
local res = {}
for _,port in ipairs(ports) do read_events(res, port) end
return res
end
end
--- Generate an event raising function.
--
-- The generated function accepts zero to many arguments and writes
-- them to the given port (and if the fsm argument is provided) to the
-- internal queue of fsm.
-- @param port outport to write events to
-- @param fsm events are sent to this fsm's internal queue (optional)
-- @return function to send events to the port
function gen_raise_event(port, fsm)
assert(port, "No port specified")
return function (...) for
_,e in ipairs{...} do port:write(e) end
if fsm then rfsm.send_events(fsm, ...) end
end
end
--- Generate a function which writes the fsm fqn to a port.
--
-- This function returns a function which takes a rfsm instance as the
-- single parameter and write the fully qualifed state name of the
-- active leaf to the given string rtt.OutputPort. To be added to the
-- fsm post_step_hook.
-- @param port rtt OutputPort to which the fqn shall be written
function gen_write_fqn(port)
assert(port:info().type==rtt.Variable('string'):getType(), "gen_write_fqn: port must be of type string")
local act_fqn = ""
local out_dsb = rtt.Variable.new('string')
port:write(out_dsb, "<none>") -- initial val
return function (fsm)
local actl = fsm._act_leaf
if not actl or act_fqn == actl._fqn then return end
act_fqn = actl._fqn
out_dsb:assign(act_fqn)
port:write(out_dsb, act_fqn)
end
end
--- Lauch an rFSM statemachine in a RTT Lua Service.
--
-- This function launches an rfsm statemachine in the given file
-- (specified with return rfsm.state{}) into a service, and optionally
-- install a eehook so that it will be periodically triggerred. It
-- also create a port "fqn" in the TC's interface where it writes the
-- active. Todo: this could be done much nicer with cosmo, if we chose
-- to add that dependency.
-- @param file file containing the rfsm model
-- @param execstr_f exec_string function of the service. retrieve with compX:provides("Lua"):getOperation("exec_str")
-- @param eehook boolean flag, if true eehook for periodic triggering is setup
-- @param env table with a environment of key value pairs which will be defined in the service before anything else
function service_launch_rfsm(file, execstr_f, eehook, env)
local s = {}
s[#s+1] = "require 'rttlib'"
s[#s+1] = "require 'rfsm'"
s[#s+1] = "require 'rfsm_rtt'"
s[#s+1] = "require 'utils'"
if env and type(env) == 'table' then
for k,v in pairs(env) do s[#s+1] = k .. '=' .. '"' .. v .. '"' end
end
s[#s+1] = [[
fqn = rtt.OutputPort("string", "fqn", "rFSM currently active fully qualified state name")
rtt.getTC():addPort(fqn)
setfqn = rfsm_rtt.gen_write_fqn(fqn)
]]
s[#s+1] = '_fsm = rfsm.load("' .. file .. '")'
s[#s+1] = "fsm = rfsm.init(_fsm)"
s[#s+1] = "rfsm.post_step_hook_add(fsm, setfqn)"
s[#s+1] = [[ function trigger()
rfsm.step(fsm)
return true
end ]]
if eehook then
s[#s+1] = 'eehook = rtt.EEHook("trigger")'
s[#s+1] = "eehook:enable()"
end
for _,str in ipairs(s) do
assert(execstr_f(str), "Error launching rfsm: executing " .. str .. " failed")
end
end
--- Launch a rFSM in a component.
--
-- Will first create a Lua rFSM Component.
-- Next the following is done: require "rttlib" and "rFSM",
-- set environment variables, execute prefile, setup outport for FSM
-- status, load rFSM, define updateHook and finally execute postfile.
-- @param argtab table with the some or more of the following fields:
-- - fsmfile rFSM file (required)
-- - name of component to be create (required)
-- - deployer deployer to use for creating LuaComponent (required)
-- - luatype type of lua component to create. (default: OCL::LuaComponent)
-- - sync boolean flag. If true rfsm.step() will be called in updateHook, otherwise rfsm.run(). default=false.
-- - ev_inport. If not false or nil will create an inport and connect 'getevents' to it.
-- If a string it will be used as the Port name.
-- - ev_outport. If not false or nil will create an outport and function emit_events that writes to it.
-- If a string it will be used as the Port name.
-- - prefile Lua script file executed before loading rFSM for preparing the environment.
-- - prestr Lua script string executed before loading rFSM for preparing the environment.
-- - postfile Lua script file executed after loading rFSM.
-- - poststr Lua script string executed after loading rFSM.
-- - env environment table of key-value pairs which are initalized in the new component. Used for parametrization.
--
-- regarding getevents, if this function finds a table extra_in_ports
-- (that must contain input ports!), it will add those as parameters
-- to the getevents call
--
function component_launch_rfsm(argtab)
assert(argtab and type(argtab) == 'table', "No argument table given")
assert(type(argtab.name) == 'string', "No 'name' specified")
assert(type(argtab.fsmfile) == 'string', "No 'fsmfile' specified")
assert(type(argtab.deployer) == 'userdata', "No 'deployer' provided")
if not argtab.luatype then
argtab.luatype = "OCL::LuaComponent"
end
local depl=argtab.deployer
local name=argtab.name
local fsmfile=argtab.fsmfile
if not depl:loadComponent(name, argtab.luatype) then
error("Failed to create lua component (" .. argtab.luatype .. ")")
end
comp=depl:getPeer(name)
comp:addPeer(depl)
exec_str = comp:provides():getOperation("exec_str")
exec_file = comp:provides():getOperation("exec_file")
local s = {}
s[#s+1] = "require 'rttlib'"
s[#s+1] = "require 'rfsm'"
s[#s+1] = "require 'rfsm_rtt'"
s[#s+1] = "require 'utils'"
if argtab.env and type(argtab.env) == 'table' then
for k,v in pairs(argtab.env) do s[#s+1] = k .. '=' .. '"' .. tostring(v) .. '"' end
end
for _,str in ipairs(s) do
assert(exec_str(str), "Error launching rfsm: executing " .. str .. " failed")
end
s={}
if argtab.prefile then exec_file(argtab.prefile) end
if argtab.prestr then exec_str(argtab.prestr) end
s[#s+1] = [[fqn = rtt.OutputPort("string", "fqn", "rFSM currently active fully qualified state name")
rtt.getTC():addPort(fqn)
setfqn = rfsm_rtt.gen_write_fqn(fqn)
]]
if argtab.ev_outport then
if type(argtab.ev_outport) ~= 'string' then argtab.ev_outport='events_out' end
s[#s+1] = "ev_outport = rtt.OutputPort('string', '" .. argtab.ev_outport .. "', 'Autogenerated event-out port')"
s[#s+1] = "rtt.getTC():addPort(ev_outport)"
s[#s+1] = "function emit_event(e) ev_outport:write(e) end"
end
s[#s+1] = ([[_fsm = rfsm.load('%s')
fsm = rfsm.init(_fsm)
rfsm.post_step_hook_add(fsm, setfqn)
]]):format(fsmfile)
if argtab.sync then
s[#s+1] = "function updateHook() rfsm.step(fsm) end"
else
s[#s+1] = "function updateHook() rfsm.run(fsm) end"
end
-- todo: properly delete this port again.
if argtab.ev_inport then
if type(argtab.ev_inport) ~= 'string' then argtab.ev_inport='events_in' end
s[#s+1] = "ev_inport = rtt.InputPort('string', '" .. argtab.ev_inport .. "', 'Autogenerated event-in port')"
s[#s+1] = "rtt.getTC():addEventPort(ev_inport)"
s[#s+1] = "extra_in_ports = extra_in_ports or {}"
s[#s+1] = "extra_in_ports[#extra_in_ports+1] = ev_inport"
s[#s+1] = "fsm.getevents = rfsm_rtt.gen_read_events(unpack(extra_in_ports))"
end
for _,str in ipairs(s) do
assert(exec_str(str), "Error launching rfsm: executing " .. str .. " failed")
end
s={}
if argtab.postfile then exec_file(argtab.postfile) end
if argtab.poststr then exec_file(argtab.poststr) end
return comp
end