Customer question: How do I initialise variables?

0
 How do I initialise variables for the IEC 61131-3 language Structured Text?
asked
2 answers
2

Hi Matthias,

 

thank you for your question. Here there's a collection of examples for some useful initializations. 

 

1. Easy use cases

Let´s start with some easy ones like elementary types, arrays, structured types and references.

1.1 Initialization of elementary types

 

VAR_GLOBAL
    // Initializing of elementary data types (some examples) with:
    r0 : LREAL := 5.0;  // a constant 
    r1 : LREAL := (5.0 + 2.0) * -1.0; // result of a calculation
    w0 : WORD := WORD#16#ABCD; // hexadecimal value
    w1 : WORD := WORD#65535; // a decimal value
    w2 : WORD := WORD#16#AABB AND MASK; // with a result of a boolean AND operation, where MASK is a global constant
END_VAR

 

1.2 Initialization of arrays

 

VAR_GLOBAL

    // Initializing of arrays (some examples):

    arr1 : ARRAY[1..3] OF INT := [1, 2, 4]; // set each element indivudually

    arr2 : ARRAY[1..3] OF INT := [3(99)]; // set all elements to value 99

    arr3 : ARRAY[1..3] OF INT := [2(99), 1(11)]; // set first two elemenst to 99, third element to 11

    arr4 : ARRAY[0..2, 0..2] OF INT := [1, 2, 3, 4, 5, 6, 7, 8, 9]; // multi dimensional arrray

END_VAR

 

2. More complex initialization

 

2.1 Initialization of structured types

 

Having two structs `Limits` and `Parameter` while `Parameter` contains also a nested structure `Limits`

 

TYPE

    Limits : STRUCT

        Min : LREAL;

        Max : LREAL := 9999.9;

    END_STRUCT;

    Parameter : STRUCT

        Velocity : LREAL;

        Limit : Limits;

    END_STRUCT;

END_TYPE

 

Instantiate and initialize the structures `Limits` and `Parameter`

VAR_GLOBAL

    // Initialize a structured type

    limit : Limits := (Min := 1.0, Max := 100.0);

    // or an nested type

    parameter : Parameter := ( Limit := ( Min := 1.0 ) ) ;



    // Initialize a reference of type with a reference of an type INT

    xRef : REF_TO INT := REF(i1);

    i1  : INT;

END_VAR

 

2.2 Initializers

 

Have you ever heard about initializers in ST? No!

 

Initializers are a convenient feature to initialize variables of Function Blocks and Classes when they're are public accessible.

 

Initialize function blocks / classes

 

VAR_GLOBAL

    m1 : Motor := (Name := 'M1');

    m2 : Motor := (Name := 'M2', Parameter := (Limit := (Max := 100.0)));

END_VAR

 

Use case

 

The concept of initializers and references allows you, to modularize your software, so that you can implement and instantiate `Motor` and `Conveyor` independent. and you can `aggregate` them very flexible. We call this `aggregation`

 

> Assigning references and interfaces as `Initializers` is just possible in the `VAR_GLOBAL` section or `VAR` section of a `PROGRAM`

 

VAR_GLOBAL

    // aggregate motor and conveyor via a reference

    c1 : Conveyor := (Name := 'Conveyor1', Motor := REF(m1));

END_VAR

 

Some interesting use case of initializers you'll find on our SIMATIC AX GitHub community! Feel free to have a look and to contribute!

answered
0

Hi Jürgen,

 

thank you for the extensive and quick answer! Great to see examples in the GitHub Community!

answered