LUA, LUAQUAD, LUAOCT, LUAHEXD – User programmable blocks in Lua
Block SymbolsLicensing group: REXLANG
Function Description
The standard function blocks of the REXYGEN system cover the most typical needs in control
applications. But there still exist situations where it is necessary (or more convenient) to
implement an user-defined function. The REXLANG block covers this case
for application where real-time behavior is strictly demanded. In the rest of the cases the LUA
block can be used.
The LUA block implements an user-defined algorithm written in a Lua scripting language and in comparison to the REXLANG block it provides a better user experience in the stage of development of the algorithm and can extend the feature set of REXYGEN system through various 3rd party libraries that are available in the Lua environment.
There are four variants of the LUA block with different number of inputs and outputs. The inputs and outputs of the block are indexed from 0 to 15. The inputs and outputs of the block can be of any data type supported by the REXYGEN system.
Scripting language
The scripting language is a standard Lua v5.4 language (see [15]). Every block references a
script stored in a *.lua source file or it directly contains the script in a parameter of the block.
The script can optionally contain functions with a reserved name that are then executed by
REXYGEN.
The main() function is executed periodically during runtime. Alongside the main() function the init() function is executed once at startup and after reset of the block, the exit() function is executed once when the control algorithm is stopped and before reset of the block and the parchange() function is executed on parameters change in REXYGEN.
Additionally, the compile() function is executed once during the compilation process of the REXYGEN configuration. This function can be used to modify the table REX.ctx that then after the compilation gets serialized and stored in the configuration and is later deserialized during the initialization of the block on the target device. This way the script can create a shared context between the compilation environment and the target device. Since the script is to some extend executed both on the host and target device, the items REX.host and REX.target are available to the script to distinguish between the two environments.
Scripts on the target device
Lua interpreter can load a script from specified file or directly from a string. If the parameter
embedded is set to on the script referenced by the block in the parameter src gets embedded in
the REXYGEN configuration during compilation process and will be loaded during initialization
of the block once the configuration is downloaded and executed on the target device.
Otherwise the script is loaded from a file on the target device that is again specified by the
parameter src.
Data exchange API
For the purpose of data exchange between a Lua interpreter and REXYGEN system an object
REX containing the data exchange API was developed and is injected into every instance of a
LUA block interpreter.
I/O objects
The range of input and output objects depends on the used block. The most compact LUA
block contains 2 inputs and 2 outputs, LUAQUAD 4 inputs and 4 outputs, LUAOCT 8 inputs and 8
outputs and the largest LUAHEXD 16 inputs and 16 outputs.
-
REX.u0 - REX.u15
-
– objects representing block inputs in Lua environment
-
REX.p0 - REX.p15
-
– objects representing block parameters in Lua environment
-
REX.y0 - REX.y15
-
– objects representing block outputs in Lua environment
Access to values
The I/O objects must have its data type specified during compilation of the REXYGEN
configuration. Default data type is double but it can be changed using the function
type().
Example of setting the data type of the block IOs:
REX.y0.type("string")
All I/O objects contain a function v(). That performs a conversion from REXYGEN data types to Lua data types. The value then can be stored in variables and used in the block algorithm.
Example of reading a value of the block input:
local myU0 = REX.u(0)
local x = myU0.v()
Setting the REXYGEN value from the Lua script can be achieved with function setV() that performs a conversion from Lua data types to REXYGEN data types and exports the value to the corresponding block output/parameter.
Example of writing a value to the block output:
local myY0 = REX.y(0)
myY0.setV(42)
Arrays
Arrays can be manipulated through function v() but direct conversions between REXYGEN
arrays and Lua tables are not very memory efficient. In addition input objects support direct
access to the values of the REXYGEN array connected to the block input using functions arr()
and setArr().
Example of reading and writing a value of the block input for matrix:
REX.u0.setArr(0, 0, 42)
External items
The object REX contains a function Item() that returns a handle to an external REXYGEN
item based on a connection string specified in a parameter of the function.
Example of creating a handle to an external item and setting its value:
cns.setV("abc")
Tracing
The object REX contains functions Trace, TraceError, TraceWarning, TraceVerbose and
TraceInfo that can be used to write messages into REXYGEN system log.
Example of logging a message:
Additional features
REX.RexDataPath() – RexDataPath() is a function that returns a path to a data folder of
the REXYGEN system on the given platform. That can come handy for writing a
platform independent code that requires access to the file system using absolute
paths.
This block does not propagate the signal quality. More information can be found in the 1.4 section.
Input
HLD | Hold | Bool |
RESET | Block reset | Bool |
u0..u15 | Input signal | Any |
Parameter
src | Source file name or source program.lua | String |
embedded | Embedding of the script in the REXYGEN executive program on | Bool |
limit | Limit of the Lua interpreter execution per period 0 | Long (I32) |
p0..p15 | Parameter | Any |
Output
iE | Error code | Error |
|
|
|
iRes | Execution result code | Long (I32) |
y0..y15 | Output signal | Any |
[Back to top] [Up] [Next]
2024 © REX Controls s.r.o., www.rexygen.com