arrays in structured text. structured text structured text is an imperative language similar to...

19
Arrays in structured text

Upload: eleanor-matthews

Post on 04-Jan-2016

341 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Arrays in structured text

Page 2: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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 !

Page 3: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Data TypesFunction 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 Textsubrange,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:

BOOLBYTEWORDDWORD

181632

REAL (Real32)LREAL (Real64)

Page 4: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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 : STRUCT SIGNAL_TYPE : ANALOG_SIGNAL_TYPE; FILTER_CHARACTERISTIC : SINT (0.99) CHANNEL: ARRAY [1..16] OF ANALOG_CHANNEL_CONFIGURATION; END_STRUCT ;END_TYPE

Page 5: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Structured text examplesIF tank.temp > 200 THEN

pump.fast :=1;pump.slow :=0; pump.off :=0;

ELSIF tank.temp > 100 THENpump.fast :=0; pump.slow :=1; pump.off :=0;

ELSE pump.fast :=0; pump.slow :=0; pump.off :=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).

IF( Switch_0 AND Switch_1 ) THEN Start_Motor := 1; Start_Count := Start_Count + 1;

END_IF;

Page 6: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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:1. Initialize element_num to 0. 2. Count the number of elements in SINT_array (array that contains the ASCII characters) and store the result in SINT_array_size (DINT tag).3. If the character at SINT_array[element_num] = 13 (carriage return), then stop. 4. Set String_tag[element_num] = the character at SINT_array[element_num]. 5. Add 1 to element_num. This lets the controller check the next character in SINT_array. 6. Set the Length member of String_tag = element_num. (This records the number of characters in String_tag so far.) 7. If element_num = SINT_array_size, then stop. (You are at the end of the array and it does not contain a carriage return.) 8. Go to 3.

Page 7: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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:1. Get size (number of items) of Inventory array and store result in Inventory_Items (DINT tag). 2. Loop over positions in array. 3. If Barcode matches the ID of an item in the array, then:

a. Set the Quantity tag = Inventory[position].Qtyb. Stop.

Page 8: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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:1. Get size (number of items) of Inventory array and store result in Inventory_Items (DINT tag). 2. Loop over positions in array. 3. If Barcode matches the ID of an item in the array, then:

a. Set the Quantity tag = Inventory[position].Qtyb. Stop.

Solution:SIZE(Inventory,0,Inventory_Items);FOR position:=0 to Inventory_Items - 1 DO

IF Barcode = Inventory[position].ID THENQuantity := Inventory[position].Qty; EXIT;

END_IF; END_FOR;

Page 9: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Sequential function charts

• 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.

START STEP

ACTION D1N D1_READY

D ACTION D2 D2_READY

T1

T2

STEP BSTEP A

Page 10: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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

Page 11: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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).

Page 12: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

SFC: switch and parallel execution

Sa

Sb

"1"

Se

token switch : the token crosses the first active

transition (at random if both Ea and Eb are true)Note: transitions are after the alternance

token forking : when the transition Ee is true, the token

is replicated to all connected statesNote: transition is before the fork

Ed

token join : when all connected states have tokensand transition Eg is true, one single token is forwarded.Note: transition is after the join

Ee

Sc

Sd

SfSg

Eg

E0

Ea Eb

Ec

Ef

Page 13: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

SFC: P1, N and P0 actions

P1 State1_P1: do at enter

N State1_N: do while

P0 State1_P0: do at leaving

State1

P1 (pulse raise) action is executed once when the state is enteredP0 (pulse fall) action is executed once when the state is leftN (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.

Page 14: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Implementation of timers as a transition variable

Page 15: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

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.

Page 16: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

SFC: exercise

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 I32 - Stop the vehicle when it reaches I3.3 - Open the tank during 5s.

4 - Go back to I05 - Open the trap and wait 5s.

repeat above steps indefinitely

I2 I3Inputs 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 = {+20: +1 m/s; +1: +5 cm/s; 0: 0m/s}

negative values: opposite direction

VariablesInput: I0, I1, I2, I3 (boolean);Output:Trap = {0: closed; 1: open}

Register = {0: closed; 1: open}

Page 17: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

Exercise SFC solution

Page 18: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

SFC: structuringEvery flow chart without a token generator may be redrawn as astructured flow chart (by possibly duplicating program parts)

A

B

C

a

b

d

c

Not structured

A

B

C

a

b

a

bB'

A'

d

c

d

structured

Page 19: Arrays in structured text. Structured text Structured Text is an imperative language similar to Pascal (If, While, etc..) Structed Text is also a procedural

SFC: Complex structures

Problems with general networks:Deadlocks, uncontrolled token multiplication

These general rules serve to build networks, termed by DIN and IEC as flow charts

Solution:assistance through the flow chart editor.

Consider the network shown-What potential faults does thisSFC contain?