LUA_SCRIPT (224)


LUA_SCRIPT (224)

ID                          224

                                  
 

The appearance of the LUA_SCRIPT block depends on its user-defined inputs and outputs.

Application:

PLC

SIM

 

Group: I/O

 

Short description:

The LUA_SCRIPT function block allows you to very flexibly create your own function blocks that have a user-defined number and names of inputs and outputs, and the functionality of the block is determined by the implementation of the associated LUA script (LUA language version: 5.4.2.)

Loops, arithmetic operations, function definitions i many other functions... - LUA scripts will greatly increase SIMULTUS functionality.

We decided to implement the LUA scripting language due to its high functionality, popularity and availability of many training materials, including textbooks.

Below are some useful links for those of you who would like to learn about the possibilities of this language today.

- LUA.org home page - home page of the LUA language development organization

- Manual - full version of the manual

- DEMO of the LUA scripting language online - a useful tool (with ready-made examples) for learning and quickly testing programs

- Abridged YouTube course - abridged (1h) LUA course in English in the form of a video

Inputs - number and types depend on the user settings.

Outputs - number and types depend on the user settings.

Settings:


basic:



Settings for the number and types of inputs and outputs:



Function operation:




In order to use the LUA script in Simultus, insert a block named LUA_SCRIP from the I / O tab on the function block diagram and execute it by activating its input, for example:



In order to run a script at each program cycle, you can permanently activate the 'exe' input using e.g. the BOOLCONST function block set to the value 1:



After inserting the LUA_SCRIPT block and double-clicking on this block, a new tab with scripts will appear. The first one will be a global script (always executed only once at program start) and the second one will correspond to the name of our LUA_SCRIPT block (in the example below 'my script').
Activate the execution of the script 'my script' by activating its 'exe' input.




Additional tabs will appear after inserting subsequent LUA_SCRIPT blocks on the diagram.



In the above example, the global script will be executed at program startup, and the scripts 'my script' and 'my script 2' will be executed by pressing the appropriate buttons on the panel.

Each script block can have a configurable number (maximum 30 for each type) and the type of inputs and outputs. Configuration can be done in the properties of each block, e.g .:

          

You can also configure the names of inputs and outputs:



The script has access to the following user program variables:

- LUA_SCRIPT block inputs (R, I and B inputs) with which it is associated - read only
- LUA_SCRIPT block outputs (R, I and B type inputs) with which it is associated - write only
- VRn simulation registers (when the LUA_SCRIPT function block is included in the SIM diagram) - read and write
- Rn driver registers (when the LUA_SCRIPT function block is included in STARTUP, CONTROL or PLC diagrams) - read and write 
- VMn simulation markers (when the LUA_SCRIPT function block is included in the SIM diagram) - read and write 
- Mn controller markers (when the LUA_SCRIPT function block is included in STARTUP, CONTROL or PLC diagrams) - read and write 
- Xn controller physical inputs - read only
- Yn controller physical outputs - write only

When writing markers Mn, inputs Xn and outputs Yn, use integers: FALSE = 0, TRUE = 1 (any non-zero value = TRUE). Using "TRUE" or "FALSE" does not work.

A detailed description of how to access each variable is provided below.

Access to them is done through special objects that need to be created (examples below). Access is done using the array access syntax (Indexes from 1).

By using the sample code presented below, the user will have access to appropriate program variables by using the created objects and using them as from arrays. For example, access to the controller VR5 register (both read and write) will be possible by referring to the object SIM_Registers_VR [5].

Access to LUA_SCRIPT block inputs (R, I and B inputs) with which it is associated - read only:

Create an object with a variable of any name (we suggest: Function_Block_Inputs). The object is best created at the beginning of the '(SIM) global' or '(Dev) global' script:
Function_Block_Inputs = FBInTblHelper.new ()

and we reference its elements as an array:
stan_wejscia_5 = Function_Block_Inputs [5]

This way we saved the value of the 5th input defined by us (not counting the 'exe' input) to the variable input_state_5.


We suggest that you include the following code snippet at the beginning of each script related to LUA_SCRIPT function blocks:

-- creating an object that provides access to the I / O of the LUA_SCRIPT function block:
Function_Block_Inputs = FBInTblHelper.new()
Function_Block_Outputs = FBOutTblHelper.new()


Access of LUA_SCRIPT block outputs (R, I and B type inputs) with which it is associated - write only

Create an object with any name (we suggest: Function_Block_Outputs). The object is best created at the beginning of the '(SIM) global' or '(Dev) global' script:

Function_Block_Outputs = FBOutTblHelper.new()

and we reference its elements as an array:
Function_Block_Outputs[8] = 1

This way we set the value of the 8th output defined by us (not counting the 'error' input) to 1.

We suggest that you include the following code snippet at the beginning of each script related to LUA_SCRIPT function blocks:

-- creating an object that provides access to the I / O of the LUA_SCRIPT function block:
Function_Block_Inputs = FBInTblHelper.new()
Function_Block_Outputs = FBOutTblHelper.new()


Access to VRn simulation registers (when the LUA_SCRIPT function block is included in the SIM diagram) - read and write
and Rn driver registers (when the LUA_SCRIPT function block is included in STARTUP, CONTROL or PLC diagrams) - read and write)
:

Create an object with any name (we suggest: SIM_Registers_VR for '(SIM) global'). The object is best created at the beginning of the '(SIM) global' or '(Dev) global' script:

SIM_Registers_VR = RnTblHelper.new()

and we reference its elements as an array:
SIM_Registers_VR[5] = 3

This way, we created an object named 'SIM_Registers_VR', which is an image of the values ​​stored in the simulation program in VR registers. We can write or read a value to this object (as to an array).

Example of reading and writing to registers:

SIM_Registers_VR[5] = 3
SIM_Registers_VR[6] = SIM_Registers_VR[5] + 2

We suggest that you include the following code snippet at the beginning of the '(SIM) global' script:

-- creating an object providing access to simulation variables:
SIM_Registers_VR = RnTblHelper.new()
SIM_Markers_VM = MnTblHelper.new()


Access to VMn simulation markers (when the LUA_SCRIPT function block is included in the SIM diagram) - read and write 
and Mn controller markers (when the LUA_SCRIPT function block is included in STARTUP, CONTROL or PLC diagrams) - read and write):

Create an object with any name (we suggest: SIM_Markers_VM for '(SIM) global'). The object is best created at the beginning of the '(SIM) global' or '(Dev) global' script:
SIM_Markers_VM = MnTblHelper.new()

and we reference its elements as an array:
SIM_Markers_VM[5] = 1

This way, we created an object named 'SIM_Markers_VM', which is an image of the values ​​stored in the simulation program in the VM registers or in the M driver registers. We can write or write a value into this array.

Example of reading and writing to markers:

SIM_Markers_VM[6] = 1
stan_markera_6 =
SIM_Markers_VM[6]

We suggest that you include the following code snippet at the beginning of the '(SIM) global' script:

-- creating an object providing access to simulation variables:
SIM_Registers_VR = RnTblHelper.new()
SIM_Markers_VM = MnTblHelper.new()


Access to physical inputs of the Xn controller - read only

Create an object with any name (we suggest: Device_Inputs_X). The object is best created at the beginning of the '(Dev) global' script:
Device_Inputs_X = XnTblHelper.new()

and we reference its elements as an array:
Device_Inputs_X[5] = 1

In this way, we created an object named 'Device_Inputs_X', which is an image of the physical input values ​​of the controller. From this object (as from an array) we can only read the value.

An example of reading the physical inputs of the controller:

stan_wejscia_5 = Device_Inputs_X[5]

We suggest that you include the following code snippet at the beginning of the '(Dev) global' script:

-- creating an object that provides access to the variables and I / O of the controller:
Device_Registers_R = RnTblHelper.new()
Device_Markers_M = MnTblHelper.new()
Device_Inputs_X = XnTblHelper.new()
Device_Outputs_Y = YnTblHelper.new()


Access to physical outputs of the Yn controller - write only

Create an object with any name (we suggest: Device_Outputs_Y). The object is best created at the beginning of the '(Dev) global' script:
Device_Outputs_Y = XnTblHelper.new()

and we reference its elements as an array:
Device_Outputs_Y[3] = 1

This way, we created an object named 'Device_Outputs_Y', which is an image of the physical output values ​​of the driver. From this object (as from an array) we can read or write a value.

Example of writing and writing physical inputs of the controller:

stan_wyjscia_3 = Device_Outputs_Y[3]
Device_Outputs_Y[4] = Device_Outputs_Y[3]


We suggest that you include the following code snippet at the beginning of the '(Dev) global' script:

-- creating an object that provides access to the variables and I / O of the controller:
Device_Registers_R = RnTblHelper.new()
Device_Markers_M = MnTblHelper.new()
Device_Inputs_X = XnTblHelper.new()
Device_Outputs_Y = YnTblHelper.new()


An example of a script that uses the above functions:





-- this is an example of a script associated with the LUA_SCRIPT function block

-- reating an object that provides access to the I / O of the LUA_SCRIPT function block:
Function_Block_Inputs = FBInTblHelper.new()
Function_Block_Outputs = FBOutTblHelper.new()

-- creating an object providing access to simulation variables:
SIM_Registers_VR = RnTblHelper.new()
SIM_Markers_VM = MnTblHelper.new()

-- performing an operation on LUA variables (dynamically created variables):
a = 3
b = 4
c = math.sqrt(a^2 + b^2)

-- copying the result of the operation to the VR registers of the simulation:
SIM_Registers_VR[1] = c

-- getting values ​​from inputs 1 and 2 of the LUA_SCRIPT function block
zezwolenie_1 = Function_Block_Inputs[1]
zezwolenie_2 = Function_Block_Inputs[2]

-- executing operations on logical variables:
zezwolenie = zezwolenie_1 & zezwolenie_2

-- copying the result of the logical operation to the simulation VM markers:
SIM_Markers_VM[1] = zezwolenie

-- copying the result of the logical operation to the outputs of the function block:
Function_Block_Outputs[1] = SIM_Markers_VM[1]





The LUA_SCRIPT function block I / O names declared in the block properties are not associated with the variable names in the script.


Each type also allows you to get the size (number of elements), e.g.

rn[1] = MnTblHelper.size(mn)
rn[2] = RnTblHelper.size(rn)
rn[3] = FBInTblHelper.size(fb_in)
rn[4] = FBOutTblHelper.size(fb_out)


Attention!
Some functions of the LUA scripting language can slow down script execution (eg the "print" function).
Please be careful!