Skip to content
JamesKingWork edited this page Oct 16, 2019 · 30 revisions

I've added the EPICS Lua support module to our build system. Lua is a scripting language designed to be embedded, it has a small footprint and is reasonably powerful. It would provide an alternative to jumping through hoops in st.cmd syntax but also provides a few other options. All iocsh commands are imported into lua and so you can do things like:

for index=1,10,1
do
    print(string.format("Loading instance: %d", index))
    iocsh.dbLoadRecords("test.db", string.format("P=xxx:,Q=%d", index))
end

You execute files from st.cmd using:

epicsEnvSet("LUA_PATH", "${UTILITIES}/lua")
epicsEnvSet("LUA_SCRIPT_PATH","${TOP}/iocBoot/${IOC}")
luash("file.lua")

or just typing luash puts you into an interactive lua shell.

The lua script record is like a calcout record but can execute lua script. It might be an alternative to e.g. aSub records for parsing stream device strings when writing C is a bit overkill.

As well as being able to read/write PVs there is also some asyn integration into lua, so you can read/write/set asyn parameters from lua command line or script record, or even talk to a device by creating an asyn IP port and sending strings. See the documentation directory in lua support module and the example scripts directory in iocBoot

To use

add    LUA=$(SUPPORT)/lua/master     to configure/RELEASE
add    luaSupport.dbd                to the IOC Makefile DBD list
add    lua   and   asyn              to the IOC Makefile   _LIBS    list

There are examples of a lua script used in the DETADC, Attocube and OERCONE iocs.

There is a powerpoint about lua here: https://indico.cern.ch/event/766611/contributions/3438291/attachments/1856812/3050126/Lang-Lua_Integrating_Scripting_Language.pdf

See also the documentation on our epics-lua module or the actual epics module for more information on using lua in EPICS.

Lua utility functions

We have a few lua utility functions available in our utilities submodule. For usage and how to add to them see this page.

Style Guide

We are using the style guide from LuaRocks as documented in https://github.com/luarocks/lua-style-guide#conditional-expressions

LuaCheck

For installation, usage and troubleshooting see the luacheck page

Issues with Epics Lua

Whilst during the conversion of the oercone ioc to lua we came across a few issues with the epics lua library.

The process for resolving names in epics lua starts with looking for a variable with that name, if it does not find one it looks in the running EPICS environment and then if it cannot find anything there it looks for a matching function name.

This is great so that we can access macros easily by instead of doing $(MYMACRO) from cmd we can just do MYMACRO. However, we cannot stipulate a default. There is a lua utility function we have written, getMacroValue(options), which expects to be called like this getMacroValue{macro="MYMACRO", default="VAL"} or getMacroValue{macro="MYMACRO"}.

There is a strange issue due to the way epics lua runs lua scripts that sometimes a local variable will drop out of scope, seemingly for no reason. We expect variables to be local and are thus kept to the namespace of the lua script itself, we do not want to use global variables for no reason.

You may see that a variable was once the value you expect and then changes to something like func_meta: 008.... This is because the variable name has dropped out of scope and epics lua attempts to look for a macro and fails, so then looks for a function and returns the func_meta string.

We have a couple of options for a solution:

  • Don't use variables but set macros using iocsh.epicsEnvSet("MACRONAME", "MACROVALUE") and then get this by calling getMacroValue{macro="MACRONAME", default="DEFAULTVAL"}.
  • Only use global variables.

Both of these solutions are not nice. The other things is that epics lua sets your macros as global variables automatically. I would hope that this global scope is contained within the lua shell itself. If it is, setting global variables ourselves is not a big issue. However, if it is not, both the epics lua setting global variables and us setting global variables are security and concurrency issues.

Clone this wiki locally