====== Function Blocks ====== A function block, unlike a function, may remember some values from the previous call(s), for example status information. These remembered values may be used in computations, and thus influence the result. The main difference between a function and a function block is, thus, the function block's capability to allocate memory for storage of values of some variables. Functions do not have this feature and their results are determined by the input parameters at the time a function is called. A function block may also return more than one result. (A function has one result only). Function blocks can be compared to "black boxes" which perform a dedicated control functionality. They contain algorithms and data, so they can store information between the calls (this is where they differ from functions). They have a defined interface and inner variables hidden to the outside world. A typical example of a function block is a PID controller. ==== Function block declaration ==== FUNCTION_BLOCK name ... END_FUNCTION_BLOCK ==== Variables declaration ==== The declaration part of a function block contains variable definitions necessary for the block's activity. The variables are used for information storage and processing. Each variable is defined by a variable name, and data type. The data type is specifying the size of the occupied place in the memory, and determines the way the variable is processed (to a certain extent). For variables definition, standard data types are available (BOOL, BYTE, INT, …). Usage of these types depends on what information is going to be stored in the variable (e.g. BOOL for yes/no information, INT for signed integer numbers, etc.). User is allowed to define his/her own data types. The location of variables in memory is managed by the IDE automatically. If necessary, the location in memory may be user-defined, too. Input variables VAR_INPUT ... END_VAR Output variables VAR_OUTPUT ... END_VAR Input/output variables VAR_IN_OUT ... END_VAR Local variables VAR ... END_VAR External variables VAR_EXTERNAL ... END_VAR Assignment of initial values to the variables VAR I : INT := 1; END_VAR; ==== Executive part ==== The executive part of a function block follows the declaration, and contains commands and instructions. ==== Function block call ==== Formal, complete MY_FB(First_Index := 1, Last_Index := 5); Informal MY_FB(1, 5); ---- Function block example (DEBOUNCE): FUNCTION_BLOCK DEBOUNCE VAR_INPUT //input variables IN : BOOL; DB_TIME : TIME := t#10ms; //default value of 10 ms END_VAR VAR_OUTPUT //output variables OUT : BOOL; ET_OFF : TIME; END_VAR VAR //local variables DB_ON, DB_OFF : TON; DB_FF : SR; END_VAR (* Function block body *) DB_ON(IN := IN, PT := DB_TIME); DB_OFF(IN := NOT IN, PT := DB_TIME); DB_FF(S1 := DB_ON.Q, R := DB_OFF.Q); OUT := DB_FF.Q1; ET_OFF := DB_OFF.ET; END_FUNCTION_BLOCK {{:en:mervis-ide:35-help:funb1.png}} {{:en:mervis-ide:35-help:funb2.png}} A user-specified function block inserted in a FUPLA program. If the block used in program is linked to source code, which is in executable od library project, editor of this block can be opened by doubleclick on its instance. Editor of funkction block can be opened even during the debugging. Editor is then opened with automatically filled instance name. Blocks created in FBD does not need any additional editing. However blocks defined in ST needs to contain correct definition of namespaces in ST code. Definition of namespaces needs to match resulting declaration of function block. This declaration can be found in properties of function block. {{:en:mervis-ide:35-help:namespaces.png}} ==== Manual mode ==== The variables en and eno are used to switch the function block to manual mode. By setting the input variable en to the value "False", the function block stops executing the computational algorithm and values can be written to the output variables regardless of the input variables. The variable "eno" copies the state of the input variable "en" and serves as an indication of automatic and manual mode.