PYTHON – User programmable block in Python

Block SymbolLicensing group: REXLANG
Qt SVG Document Exported by REXYGEN Studio HLD RESET u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 u11 u12 u13 u14 u15 iE iRes y0 y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 y13 y14 y15 PYTHON

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 PYTHON block can be used.

The PYTHON block implements an user-defined algorithm written in a Python 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 Python environment.

Warning: the PYTHON block is intended for prototyping and experiments so please consider using the block in your application very carefully. It is an experimental block and always will be. There are many corner cases that may lead to unexpected behavior or even block the runtime. Packages may be poorly written or provide incorrect finalization and reinitialization which may even lead to a crash. Only a very limited support is provided for this block.

Scripting language
The scripting language is a standard Python v.3 language (see [16]). Every block references a script written in a *.py source file. The file 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.

Scripts on the target device
Standard python interpreter can load modules/scripts from various locations on the target device. The PYTHON block can reference any python script available for the standard interpreter and in addition the block can access scripts located in a directory /rex/scripts/python. User scripts can be directly uploaded to this directory or if the parameter embedded is set to on the script referenced by the block gets embedded in the REXYGEN configuration during compilation process and will be temporarily stored in the directory /rex/scripts/python/embedded during initialization of the block once the configuration is downloaded and executed on the target device.

Data exchange API
For the purpose of data exchange between a Python interpreter and REXYGEN system a module PyRexExt was developed as a native extension to the interpreter. The module contains an object REX that handles the data exchange operations. Use the following snippet at the start of the script to setup the data exchange API.

from PyRexExt import REX

I/O objects

REX.u0 - REX.u15

– objects representing block inputs in Python environment

REX.p0 - REX.p15

– objects representing block parameters in Python environment

REX.y0 - REX.y15

– objects representing block outputs in Python environment

Access to values
All I/O objects contain a property v. Reading of the property v performs a conversion from REXYGEN data types to Python data types. The value then can be stored in variables and used in the block algorithm. A REXYGEN array type converts into a list of values in case of one-dimensional array or into a list of lists in case of multidimensional array.

Example of reading a value of the block input:

x = REX.u0.v

Writing to the property v, on the other hand, performs a conversion from Python 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:

REX.y0.v = 5

Arrays
Input and output objects have a readonly property size. It is a tuple with number of rows and columns. Arrays can be manipulated through property v but direct conversions between REXYGEN arrays and Python lists are not very memory efficient. However, input and output objects support indexing operator [] that restricts the conversion overhead only on the specified item.

Example of reading a value of the block input for one-dimensional array:

x = REX.u0[0]

Example of writing a value to the block output for multidimensional array:

REX.u0[1, 3] = 5

External items
The object REX contains a method Item that returns a handle to an external REXYGEN item based on a connection string specified in a parameter of the method.

Example of creating a handle to an external item and setting its value:

cns = REX.Item("myproject_task.CNS:scv")  
cns.v = "abc"

Tracing
The object REXYGEN contains methods Trace, TraceError, TraceWarning, TraceVerbose and TraceInfo can be used to write messages into REXYGEN system log. Every message has a stacktrace attached.

Example of logging a message:

REX.Trace("abc")

Additional features
REX.RexDataPath RexDataPath is a string constant that contains 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

srcname

Source file name  program.py

String

embedded

Embedding of the script in the REXYGEN executive program  on

Bool

limit

Limit of the Python interpreter execution per period   1 10000

Long (I32)

limitMode

Python interpreter limit mode

Long (I32)

0 ....

no limit

1 ....

instructions per period, continue after interrupt

2 ....

instructions per period, start from scratch after interrupt

3 ....

milliseconds per period, continue after interrupt

4 ....

milliseconds per period, start from scratch after interrupt

p0..p15

Parameter

Any

Output

iE

Error code

Error

i ....

REXYGEN error code

iRes

Execution result code

Long (I32)

DONE

Script execution finished

Bool

y0..y15

Output signal

Any

2024 © REX Controls s.r.o., www.rexygen.com