Download presentation
1
Arrays in structured text
2
Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural language similar to C The variables defined in ST can be used in other languages ST is used to do complex data manipulation and write blocks Caution: writing programs in structured text can breach the real-time rules !
3
Data Types Function are typed: the types of connection, input and output must match. Elementary Types are defined either in Structured Text or in the FB configuration Derived Types are user-defined and must be declared in Structured Text subrange, enumerated, arrays, structured types (e.g. AntivalentBoolean2) Variables can receive initial values and be declared as non-volatile (RETAIN), so after restart they contain the last value before power-down or reset. binary types: analog types: BOOL BYTE WORD DWORD REAL (Real32) LREAL (Real64)
4
Structured text supports class and struct declarations
TYPE ANALOG_CHANNEL_CONFIGURATION STRUCT RANGE: ANALOG_SIGNAL_RANGE; MIN_SCALE : ANALOG_DATA ; MAX_SCALE : ANALOG_DATA ; END_STRUCT; ANALOG_16_INPUT_CONFIGURATION : SIGNAL_TYPE : ANALOG_SIGNAL_TYPE; FILTER_CHARACTERISTIC : SINT (0.99) CHANNEL: ARRAY [1..16] OF ANALOG_CHANNEL_CONFIGURATION; END_STRUCT ; END_TYPE
5
Structured text examples
IF tank.temp > 200 THEN pump.fast :=1; pump.slow :=0; pump.off :=0; ELSIF tank.temp > 100 THEN pump.fast :=0; pump.slow :=1; ELSE pump.slow :=0; pump.off :=1; END_IF; IF( Switch_0 AND Switch_1 ) THEN Start_Motor := 1; Start_Count := Start_Count + 1; END_IF; pos := 0; WHILE((pos < 100) & s_arr[pos].value <> target)) DO pos := pos + 2; String_tag.DATA[pos] := SINT_array[pos]; END_WHILE; Predefined functions, e.g.: SIZE(SINT_array, 0, SINT_array_size); Count the number of elements in SINT_array (array that contains inputs) and store result in SINT_array_size (DINT tag).
6
Structured text example
Move ASCII characters from a SINT array into a string tag. (In a SINT array, each element holds one character.) Stop when you reach the carriage return. element_num := 0; SIZE(SINT_array, 0, SINT_array_size); WHILE SINT_array[element_num] <> 13 DO String_tag.DATA[element_num] := SINT_array[element_num]; element_num := element_num + 1; String_tag.LEN := element_num; IF element_num = SINT_array_size then exit; END_IF; END_WHILE; Explanations: Initialize element_num to 0. Count the number of elements in SINT_array (array that contains the ASCII characters) and store the result in SINT_array_size (DINT tag). If the character at SINT_array[element_num] = 13 (carriage return), then stop. Set String_tag[element_num] = the character at SINT_array[element_num]. Add 1 to element_num. This lets the controller check the next character in SINT_array. Set the Length member of String_tag = element_num. (This records the number of characters in String_tag so far.) If element_num = SINT_array_size, then stop. (You are at the end of the array and it does not contain a carriage return.) Go to 3.
7
Structured text exercise
A user-defined data type (structure) stores information about an item in an Inventory array: • Inventory[i].ID: Barcode ID of the item (string data type) • Inventory[i].Qty: Quantity in stock of the item (DINT data type) An array of the above structure contains an element for each different item in your inventory. You want to search the array for a specific product (by its barcode) and determine the quantity in stock. Pseudocode: Get size (number of items) of Inventory array and store result in Inventory_Items (DINT tag). Loop over positions in array. If Barcode matches the ID of an item in the array, then: a. Set the Quantity tag = Inventory[position].Qty b. Stop.
8
Structured text solution
A user-defined data type (structure) stores information about an item in an Inventory array: • Inventory[i].ID: Barcode ID of the item (string data type) • Inventory[i].Qty: Quantity in stock of the item (DINT data type) An array of the above structure contains an element for each different item in your inventory. You want to search the array for a specific product (by its barcode) and determine the quantity in stock. Pseudocode: Get size (number of items) of Inventory array and store result in Inventory_Items (DINT tag). Loop over positions in array. If Barcode matches the ID of an item in the array, then: a. Set the Quantity tag = Inventory[position].Qty b. Stop. Solution: SIZE(Inventory,0,Inventory_Items); FOR position:=0 to Inventory_Items - 1 DO IF Barcode = Inventory[position].ID THEN Quantity := Inventory[position].Qty; EXIT; END_IF; END_FOR;
9
Sequential function charts
START STEP ACTION D1 N D1_READY D ACTION D2 D2_READY T1 T2 STEP B STEP A Describes sequences of operations and interactions between parallel processes. Derived from Grafcet and SDL (Specification and Description Language, used for communication protocols), mathematical foundation lies in Petri Nets.
10
SFC Elements Ec = ((varX & varY) | varZ) token Sa Sb "1" Ea Sc Eb transitions states event condition ("1" = always true) example transition condition S0 The sequential program consists of states connected by transitions. A state is activated by the presence of a token (the corresponding variable becomes TRUE). The token leaves the state when the transition condition (event) on the state output is true. Only one transition takes place at a time, the execution period is a configuration parameter (task to which this program is attached) Rule: there is always a transition between two states, there is always a state between two transitions
11
SFC: Initial state State which come into existence with a token are called initial states. All initial states receive exactly one token, the other states receive none. Initialization takes place explicitly at start-up. In some systems, initialization may be triggered in a user program (initialization pin in a function block).
12
SFC: switch and parallel execution
"1" E0 token switch : the token crosses the first active Sa transition (at random if both Ea and Eb are true) Note: transitions are after the alternance Ea Eb Sc Ec Sb Sd Ed token forking : when the transition Ee is true, the token Se is replicated to all connected states Note: transition is before the fork Ee Ef Sg token join : when all connected states have tokens and transition Eg is true, one single token is forwarded. Note: transition is after the join Sf Eg
13
SFC: P1, N and P0 actions State1 P1 State1_P1: do at enter N State1_N: do while P0 State1_P0: do at leaving P1 (pulse raise) action is executed once when the state is entered P0 (pulse fall) action is executed once when the state is left N (non-stored) action is executed continuously while the token is in the state P1 and P0 actions could be replaced by additional states. The actions are described by a code block written e.g. in Structured Text.
14
Implementation of timers as a transition variable
15
SFC Graphic Rules The input and output flow of a state are always in the same vertical line (simplifies structure) Alternative paths are drawn such that no path is placed in the vertical flow (otherwise would mean this is a preferential path) intentional displacement to avoid optical preference of a path. Priority: • The alternative path most to the left has the highest priority, priority decreases towards the right. • Loop: exit has a higher priority than loopback.
16
SFC: exercise Speed = {+20: +1 m/s; +1: +5 cm/s; 0: 0m/s} negative values: opposite direction Variables Input: I0, I1, I2, I3 (boolean); Output: Trap = {0: closed; 1: open} Register = {0: closed; 1: open} I2 I3 Inputs generate “1” as long as the tag of the vehicle (1cm) is over the sensor. Register = {0: closed; 1: open} I0 I1 trap +speed Speed = 5 cm/s from I1 to I0 and from I2 to I3, faster otherwise. Initially: move vehicle at reduced speed until it touches I0 and open the trap for 5s (empty the vehicle). 1 - Let the vehicle move from I0 to I3 4 - Go back to I0 2 - Stop the vehicle when it reaches I3. 5 - Open the trap and wait 5s. 3 - Open the tank during 5s. repeat above steps indefinitely
17
Exercise SFC solution
18
SFC: structuring Every flow chart without a token generator may be redrawn as a structured flow chart (by possibly duplicating program parts) Not structured structured A a A B a b d B d C b c C c B' b d A' a
19
SFC: Complex structures
These general rules serve to build networks, termed by DIN and IEC as flow charts Consider the network shown- What potential faults does this SFC contain? Problems with general networks: Solution: Deadlocks, uncontrolled assistance through the flow chart editor. token multiplication
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.