BECKHOFF TwinCAT 3 Basics

Embed Size (px)

DESCRIPTION

TwinCAT Basic Tutorial

Citation preview

  • Training fr Umsteiger

    TR3020 | Training for new users | OverviewTwinCAT 3 New Automation Technology

  • TwinCAT Training: Programmer 29.10.2013 2

    Identifier

    Indentifier serves to assign individual names to variables, data types, functions, etc.

    The identifier starts with a letter or underscore followed by numbers, letters and underscore No distinction is made between upper and lower caseThe following are not permitted special characters (!, , , $, etc.) spaces consecutive underscores umlauts

  • TwinCAT Training: Programmer 29.10.2013 3

    Keywords

    Keywords are identifiers specified by IEC61131-3. They are thus fixed components of the syntax and therefore may not be used for other purposes.

    ExamplesStandard operators AND, OR, NOTStandard types BOOL, INT, REAL...Types TYPE, STRUCTBlock types FUNCTION, FUNCTION_BLOCK, PROGRAM

  • TwinCAT Training: Programmer 29.10.2013 4

    Keywords and comments

    The comments are delimited by character strings with (* or *) at the beginning and at the end. Comments may be placed wherever spaces are also permitted. Exception: within string literals.

    (*digitale Eingnge*)bStart AT %IX0.0 :BOOL;(*Anlagenstart*)(*analoge Eingnge*)TemK1 AT %IW10 (*Byte 10-11*) :WORD;

    Comments to the end of the linebStart AT %IX0.0 :BOOL; // Anlagenstart

  • TwinCAT Training: Programmer 29.10.2013 5

    Elementary data types

    Type Lower Upper Size PrefixBOOL FALSE TRUE x

    bBYTE 8 BIT by BitstringWORD 16 BIT w BitstringDWORD 32 BIT dw Bitstring

  • TwinCAT Training: Programmer 29.10.2013 6

    Elementary data types

    Typ Lower Upper Gre PrfixSINT -127 127 8 Bit siUSINT 0 255 8 BIT usiINT -32768 32767 16 BIT iUINT 0 65535 16 BIT uiDINT -134 217 728 134 217 727 32 BIT diUDINT 0 4294 967 295 32 BIT udiLINT 64 BIT li

    ULINT 0 64 BIT uli

    Detail slidesOverflowsExample: EL3102Example: KL2531

  • TwinCAT Training: Programmer 29.10.2013 7

    Data types 1: Elementary data types

    Type Lower Upper Size PrefixTIME_OF_DAY TOD#0:0:0 TOD#23:59:

    5932 Bit tod

    DATE D#1970:01:01 D#2106 ??? 32 Bit dateDATE_AND_TIME DT#1970:01:01:00:00:00 DT#2106

    ???32 Bit dt

    TIME T#0s T#49d17h2m47s295ms

    32BIT tim

    Detail slidesDT example - reading the system timeDT example - working with standard operators

  • TwinCAT Training: Programmer 29.10.2013 8

    Elementary data types

    Type Lower Upper Size PrefixREAL 4 Byte r

    LREAL 8 Byte lr

  • TwinCAT Training: Programmer 29.10.2013 9

    Data types: STRING

    Type Description Example Size PrefixSTRING String in ASCII

    code. Standard length 80 characters.Maximum length 255. Strings are zero-terminated

    1234ABCDE 80 +1 s

    ABCDE$R$LABCDE$0D$0A

    String length specificationsExample declaration

    Assignment Result SIZEOF Result LEN

    sVar : STRING; sVar:=ABC; 81 3sVar1 :STRING(1); sVar := X; 2 1sVar: STRING(255); sVar:=ABC; 256 3

  • TwinCAT Training: Programmer 29.10.2013 10

    Data types: STRING

    Constants $ ASCII Code$0D CR $R $r CR$L $l Line Feed$N $n New Line$T $t Tab

    Detail slidesExample: FINDExample: string functions LEN, REPLACEString conversion with Union

  • TwinCAT Training: Programmer 29.10.2013 11

    Data types: WSTRING

    Type Description Example Description Prefix

    WSTRING String in Unicode format

    Level 0,Block 0x0400-0x4FFFCyrillic

    ws

    Training, seminar Level 0 Block 0x0000-0x007FBasic Latin

  • TwinCAT Training: Programmer 29.10.2013 12

    Data types: Examples of literals

    Variable Type ExamplesBOOL TRUE 2#1 16#1 1

    FALSE 2#0 16#0 0WORD, DWORD

    2#1010111111111110 16#AFFE 45054

    INT 2#1000000000000001 16#8001 -32768TIME t#1h t#60m t#3600000msd dayh hours t#0.5d t#12h t#43200000msm min

    s sec t#30m18s90ms t#0.505025h t#1818090msms ms

    REAL 0.3333 3.333e-1

  • TwinCAT Training: Programmer 29.10.2013 13

    Data types 1: Variable declaration el. data types

    A variable possesses a name behind which a value (number, string, date, etc.) is concealed. The variable name is a type of description of the path to the declared data. Variables are characterised above all by the fact that their contents can be changed at runtime.

    bStellerUntenLinks:BOOL:=TRUE;

    Identifier Data type Initial valueThe physical-logical storage location of this variable is unknown to the user (unlocated)

    The degrees of freedom and restrictions in the assignment of the identifiers can be found on the slide entitled Identifiers and Prefixes

  • Identifier

    TwinCAT Training: Programmer 29.10.2013 14

    Data types 1: Located variables

    It is possible when declaring a variable to link the name with an address that must be explicitly specified. For the allocation of inputs and outputs of the hardware the incomplete location is to be carried out with I* and Q*

    Data type;%IAT

    %Q*

  • TwinCAT Training: Programmer 29.10.2013 15

    Identifier

    %M

    %Q

    %I

    Data types 1: Located variables

    Completely located variables. AT X

    B

    W

    D

    Byte

    Byte

    Bit

    These variables possess a clear address(located)In TwinCAT 3 incompletely located variables can be used for inputs and outputsApplications for %M variables can be solved simply with Unions and direct masking

    . Type

    Detail slidesDetail - Replace %MB by UNION

  • TwinCAT Training: Programmer 29.10.2013 16

    Data types 1: Address division

    Ain AT%IB0 : INT;IB1 IB0IW0

    Ain AT%IW0 : INT;

    Din0 AT%IX10.0 : BOOL;IX10.7 IX10.6 IX10.5 IX10.4 IX10.3 IX10.2 IX10.1 IX10.0IB10

    Posi AT%IB20 : UDINT;IB23 IB22

    Posi AT%ID20 : UDINT;

    IB21 IB20IW22 IW20

    ID20

    equivalent

    equivalent

    IX22.7 IX22.6 IX22.5 IX22.4 IX22.3 IX22.2 IX22.1 IX22.0

    Examples:

    BitVar AT%IX22.1 : BOOL;

  • TwinCAT Training: Programmer 29.10.2013 17

    Data types 1: Variable classes, scope

    Local variables are restricted to the block in which they were declared.

    KeywordsVAR ..

    END_VARVAR_INPUT .. END_VARVAR_IN_OUT ..END_VARVAR_OUTPUT .. END_VAR

    KeywordsVAR_GLOBAL .. END_VARVAR_CONFIG .. END_VAR

    Global variables are known in each block within a project.

  • TwinCAT Training: Programmer 29.10.2013 18

    Data types 1: I/O directly in an FB instance

    In an FB the inputs and outputs to the periphery are to be created directly as local variables

    Implementation

  • TwinCAT Training: Programmer 29.10.2013 19

    Data types 1: Access via the located variables

    The variable locVar locally declared in Program B can be directly accessed from Program A via address %MB2.

    PROGRAM AVAR

    END_VAR

    PROGRAM BVARlocVar AT%MB2:WORD;END_VAR

    LD %MB2

    Project machine

  • TwinCAT Training: Programmer 29.10.2013 20

    Data types 1: Overlaps in the scope

    As shown on the left, there is an overlap in the scope. In this case the locally declared variable Var1 is loaded into the accumulator. The global variable can also be accessed with Namespaces.

    PROGRAM AVAR

    Var1 :WORD;END_VAR

    LD Var1

    LD Gvl1.Var1

    Project machine

    VAR_GLOBALVar1:WORD;

    END_VAR

    Example name: Gvl1

  • TwinCAT Training: Programmer 29.10.2013 21

    Data types 1: PERSISTENT attribute

    Special properties of variables can be defined using attributes.

    Example:The variable(s) are saved when the PLC is shut down and loaded back on restarting.

  • TwinCAT Training: Programmer 29.10.2013 22

    Data types 1: Initialisation and CONSTANT

    Initial values, the variables are to be preset with a certain value when starting/resetting the PLC.

    VAR

    AccelerationTime : TIME := T#3s200ms;END_VAR

    Read-only:

    VAR_GLOBAL CONSTANTpi:REAL:=3.141592654;

    END_VARVAR CONSTANT

    pi:REAL:=3.141592654;END_VAR

    Global

    Also locally possible

  • TwinCAT Training: Programmer 29.10.2013 23

    Data Types 2: Derived data types

    The user can create his own data types on the basis of the elementary data types or data types that have already been created. The newly-created data types are visible in the entire project. The declaration starts with TYPE and ends with END_TYPE.

    Parent type Name Data type Initial value Area

    Derivation Name Data type Initial value Area

    InheritanceNew value

  • TwinCAT Training: Programmer 29.10.2013 24

    Data Types 2: References (alias types)

    The purpose of the self-defined data type Reference is to generate an alternative name for a variable, constant or function block. Create your references as objects in the object organiser on the Data Types tab. They start with the keyword TYPE and end with END_TYPE.

    Syntax:TYPE

    :;END_TYPE

    Type

    Declaration

  • TwinCAT Training: Programmer 29.10.2013 25

    Data Types 2: References (alias types)

    Example: A global string variable is transferred to various blocks. If changes are made to the Global Variables, the declarations must also be changed in every block

  • TwinCAT Training: Programmer 29.10.2013 26

    Data Types 2: References (alias types)

    If a type has been created for the string, further changes are made only to the type

  • TwinCAT Training: Programmer 29.10.2013 27

    Data Types 2: References (alias types)If a type has been created for the string, further changes are made only to the type

  • TwinCAT Training: Programmer 29.10.2013 28

    Data Types 2: References (alias types)

    If a type has been created for the string, further changes are made only to the type

  • TwinCAT Training: Programmer 29.10.2013 29

    Data Types 2: Enumerated type (Enum)An enumerated type is a self-defined data type consisting of a number of string constants. These constants are called enumeration values. The enumeration values are always known in the entire project. It is best to create your enumerated types as objects in the object organiser on the Data Types tab. They start with the keyword TYPEand end with END_TYPE.

    Syntax:TYPE :( ,, ...,);END_TYPE

    Example:TYPE Woche:(Mo, Di, Mi, Dn, Fr, Sa, So:=10);(*Mo = 0 Di = 1..

    .. Sa = 6 So = 10*)END_TYPE

    TYPE Richtung:(Up, Dn);(*Up = 0 Dn = 1*)END_TYPE

    The same enumeration value can be used twice via Namespace. Example: Woche.Dn Richtung.Dn

  • TwinCAT Training: Programmer 29.10.2013 30

    Data Types 2: Enumerated type (Enum)Example: signal light without Enum:

    Declaration Use

    Online

  • TwinCAT Training: Programmer 29.10.2013 31

    Data Types 2: Enumerated type (Enum)Example: signal light with Enum:

    Type

    Declaration

    Use

    Online

  • TwinCAT Training: Programmer 29.10.2013 32

    Data Types 2: Structure declaration

    Example: KL5101 Encoder Terminal Structures are self-defined data types. They are an important aid to better administration of the process data. In addition, the structures are suitable for encapsulated data transfer to function blocks. Structures can be used like individual element variables.

  • TwinCAT Training: Programmer 29.10.2013 33

    Data Types 2: Structures instancing

    ST_KL5101In_1 AT%I* : KL5101_INST_KL5101Out_1 AT%Q* : KL5101_OUT

  • TwinCAT Training: Programmer 29.10.2013 34

    Data Types 2: ArraysArrays represent lists or data fields. All elements in the arrays are of the same type. Naturally arrays can also consist of own data types (structures).One, two and three-dimensional arrays are possible.

    VARFeld_1 :ARRAY[0..9] OF BYTE; 1-dimensionalFeld_2 :ARRAY[0..9, 0..1] OF UINT; 2-dimensionalFeld_3 :ARRAY[0..9, 0..1,0..1] OF DINT; 3-dimensional

    END_VARThere is a possibility to place a data field in a directly addressed memory location

    VARFeld_1 AT%MB100:ARRAY[1..10] OF BYTE;

    END_VARAccess to the sub-elements of a data field

    Feld_1[2] := 120; (* Expliziter Zugriff*)Feld_2[i,j] := EXPT(i,j); (*Indizierter Zugriff*)

  • TwinCAT Training: Programmer 29.10.2013 35

    Data Types 2: Limit transgressions

    A dangerous state can arise if an area outside the data field is accessed in the PLC program.

    VARFeld_1 :ARRAY[1..10] OF BYTE;Feld_2 :ARRAY[1..10, 2..5] OF UINT;

    END_VAR

    i:= 9 9Feld_1[i+2] := 120;

    Feld_1[9]; 0Feld_2[1,2]; 120

  • TwinCAT Training: Programmer 29.10.2013 36

    Data Types 2: CheckBounds (FUN)

    The access can be monitored by the PLC at PLC runtimeThis function enables a limit transgression occurring in the program to be recognised and rectified.

    iMinMax

    Limited value

    FUNCTION CheckBounds :DINTVAR_INPUT

    I,L,U : DINT;END_VAR

    IF I< L THEN

    CheckBounds := L;ELSIF I > U THEN

    CheckBounds := U;ELSE

    CheckBounds := I;END_IF

    Error case

    Error case

    OK case

  • TwinCAT Training: Programmer 29.10.2013 37

    Data Types 2: Adding CheckBounds 1 (FUN)Adding Checkbounds:

  • TwinCAT Training: Programmer 29.10.2013 38

    Data Types 2: Adding CheckBounds 2 (FUN)

  • TwinCAT Training: Programmer 29.10.2013 39

    Data Types 2: Method of operation of CheckBounds (FUN)

    Example: user error

    10

    10

    source codeCheckbounds is compiled-in in XAR

    Do not write, it is called

    automatically

    (not visible in the code)

    Can be checked with call build:

  • TwinCAT Training: Programmer 29.10.2013 40

    Note about further checker functions

    The following further checker functions are possible from TwinCAT 2.8:

    Check for division by 0 CheckDivByte CheckDivWord CheckDivDWord CheckDivReal

    Check value ranges CheckRangeSigned CheckRangeUnsigned (see appendix)

  • TwinCAT Training: Programmer 29.10.2013 41

    Data Types 2: Combination: structures and arrays

    An array can consist of structures:Structure:TYPE DrillPos :STRUCT

    XPos: LREAL;FeedrateX: LREAL;AccelerationX: LREAL;DeccelerationX: LREAL;JerkX: LREAL;YPos: LREAL;FeedrateY: LREAL;AcceleartionY: LREAL;DeccelerationY: LREAL;JerkY: LREAL;FeedDrill: LREAL;Kuehlen: BOOL; (*Pumpe ?*)

    END_STRUCTEND_TYPEDeclaration of the array:

    Positions :ARRAY[0..100] OF DrillPos;

  • TwinCAT Training: Programmer 29.10.2013 42

    Data Types 2: Combination: structures and arrays

    Access to Drillpos 55:Access:MoveXAx (*FB Instanz*)

    (Execute:= TRUE,Position:= Positions[55].XPos ,Velocity:= Positions[55].FeedrateXAcceleration:= Positions[55].AccelerationX,Deceleration:= Positions[55].DeccelerationX,Jerk:= Positions[55].JerkX,Direction:= .........,Axis:= .............,);

  • TwinCAT Training: Programmer 29.10.2013 43

    Block types

    In IEC61131-3 there are three types of block covered by the generic term POU (PROGRAM ORGANISATION UNIT): Program Function Block Function

  • TwinCAT Training: Programmer 29.10.2013 44

    Block types: program PRG

    Program PRG Called by a task (one program can call another) Calls: FBs, functions, (programs) Local variable: static, i.e. the local data are available again in the next cycle. Inputs: usually 0, but VAR_INPUT possible Outputs: usually 0, but VAR_OUTPUT possible Transfer by reference VAR_IN_OUT likewise possible Monitoring: Local data are immediately visible in the online mode of the PLC

    control Use: Main programs, Main, Manual, Automatic, etc.

  • TwinCAT Training: Programmer 29.10.2013 45

    Block types: function block FB

    Function block FB Called by programs or other FBs Calls: FBs, functions, Local variable: static, i.e. the local data are available again in the next cycle.

    Can be instanced in case of multiple calls (multipliable). Each FB call can have its own local data.

    Inputs: 0,1,2,3VAR_INPUT Outputs 0,1,2,3.. VAR_OUTPUT Transfer by reference 0,1,2,3.. VAR_IN_OUT Monitoring: In the online mode of the PLC control the instance of the call

    concerned must first be specified. The local data are then visible for each call. Use: multiple-used modules, each of which requires its own data area. Step

    chains...

  • TwinCAT Training: Programmer 29.10.2013 46

    Block types: Function: FC

    Function: FC Called by programs, function blocks and other functions Calls: Functions Local variable: temporary, i.e. the local data are available only for the

    processing time of the function. Afterwards this data area is used by other functions.

    Inputs: 1,2,3........ VAR_INPUT Outputs: precisely 1!, but structure variable possible. The name of

    the output is at the same time the name of the function. Transfer by reference 1,2,3........ VAR_IN_OUT , Monitoring: In the online mode of the PLC control only ??? are visible

    for the local variables, since this data area is used by all functions in the cycle and monitoring (debug) takes place only at the cycle limits. Remedy: program development with breakpoints

    Use: algorithms where the result is available after a run. Scaling, comparison, etc.

  • TwinCAT Training: Programmer 29.10.2013 47

    ST Structured Text: OperatorsOperation Symbol Binding strengthput in parentheses (expression) Strongest binding

    Weakest binding

    Function call Function name (parameter list)Exponentiation EXPTNegate -Build. complements NOTMultiply *Divide /Modulo MODAdd +Subtract -Compare ,=Equal to =Not Equal to BOOL AND ANDBOOL XOR XORBOOL OR OR

    Same binding strength,processing from left to right(10/2*5 = 25 )

    Same binding strength

  • TwinCAT Training: Programmer 29.10.2013 48

    ST Structured Text: InstructionsInstruction ExampleAssignment:= PosWert := 10;Calling a Function Block Ton1(IN:=Start, PT:=T2s); Output:= Ton1.Q;RETURN RETURN;IF more precise explanations and examples on the

    following pagesCASEFORWHILE

    REPEAT

    EXIT

    Empty instruction ;

  • TwinCAT Training: Programmer 29.10.2013 49

    ST: IF instruction

    Is needed to branch in a program, depending on conditions. With the IF instructions its not possible to jump back in the PLC cycle.GOTO is also not available

    Keywords:

    IF THEN

    ELSIFELSEEND_IF

  • TwinCAT Training: Programmer 29.10.2013 50

    ST: IF instruction

    Instruction block

    Condition

    Yes

    No

    IF Condition THENInstruction block;

    END_IF

  • TwinCAT Training: Programmer 29.10.2013 51

    ST: IF instruction

    IF a>b THENInstruction block A;

    ELSEInstruction block B;

    END_IF

    Condition

    Yes

    No

    Instruction block A Instruction block B

  • TwinCAT Training: Programmer 29.10.2013 52

    IF Condition1 THENInstruction block A;

    ELSEIF Condition2 THEN

    Instruction block B;ELSE

    IF Condition3 THENInstruction block C;

    ELSEInstruction block D;

    END_IFEND_IF

    END_IF

    ST: IF instruction

    Instruction block A

    Condition 1

    YesNo

    Instruction block B

    Condition 2

    YesNo

    Condition 3

    YesNo

    Instruction block C

    Instruction block D

  • TwinCAT Training: Programmer 29.10.2013 53

    IF Condition1 THENInstruction block A;

    ELSIF Condition2 THENInstruction block B;

    ELSIF Condition3 THENInstruction block C;

    ELSEInstruction block D;

    END_IF

    ST: IF instruction

    Instruction block A

    Condition 1

    YesNo

    Instruction block B

    Condition 2

    YesNo

    Condition 3

    YesNo

    Instruction block C

    Instruction block D

  • TwinCAT Training: Programmer 29.10.2013 54

    ST: IF instruction

    What can the BOOLEAN EXPRESSION be?

    IF bVar THEN.

    IF a>b THEN.

    IF LEFT(STR:= strVar, SIZE:=7) = 'TwinCAT' THEN.

    IF Ton1.Q THEN.

    IF Ton1(IN:=bVar, PT:=T#1s ) THEN

    Conditions:

    BOOLEAN variable

    Comparison

    Function calls

    Querying of FB instances

    NO FB call!

  • TwinCAT Training: Programmer 29.10.2013 55

    ST CASE Instruction

    CASE Selection criterion OF1: Instruction 12, 4, 6: Instruction 27..10: Instruction 3..

    ELSE Default instructions

    END_CASE;Two identical values may not be available for selection in the list.

    Instruction 1

    Selection criterion = 1

    Yes

    No

    Instruction 2

    Selection criterion = 2 or 4 or 6

    Yes

    No

    YesNo

    Instruction 3 Default instructions

    Selection criterion = 7 or 8 or 9 or 10?

  • TwinCAT Training: Programmer 29.10.2013 56

    ST: CASE instruction: possibility for a step chain / state machine

    CASE State OF

    0: Q0:=TRUE;

    IF Transition THEN state := 1; END_IF

    1: Q1:=TRUE;

    IF Transition THEN state := 2; END_IF

    2: Q2:=TRUE;

    IF Transition THEN state := 3; END_IF

    3: Q3:=TRUE;

    IF Transition THEN state := 0; END_IF

    END_CASE

    Instructions for the step(Actions)

    Step-further condition(Transition)

  • TwinCAT Training: Programmer 29.10.2013 57

    CASE State OF

    0: Instructions;(*State=0*)IF THEN

    1: Instructions;(*State=1*)

    2: Instructions;(*State=2*)

    3: Instructions;(*State=3*)

    END_CASE

    ST: CASE instruction Integer Selector Value with constants

    Instructionsif state = 0

    Instructionsif state = 1

    Instructionsif state = 2

    Instructionsif state = 3

  • TwinCAT Training: Programmer 29.10.2013 58

    ST: CASE instruction Integer Selector Value with Enum types

    Enum-Typ:

    TYPE Schritte :

    (INIT:=0, START, AUTOMATIK, ENDE);END_TYPE

    CASE State OF

    INIT: Instructions;(*State=0*)START: Instructions;(*State=1*)AUTOMATIK: Instructions;(*State=2*)ENDE: Instructions;(*State=3*)

    END_CASE

  • TwinCAT Training: Programmer 29.10.2013 59

    ST: CASE instruction: suggestion for a step chain / state machine

    TYPE Schritte :( INIT:=0, START, AUTOMATIK, ENDE);

    END_TYPECASE State OF

    INIT: Q0:=TRUE;

    IF Transition THEN state := START; END_IF

    START: Q1:=TRUE;

    IF Transition THEN state := AUTOMATIK; END_IF

    AUTOMATIK: Q2:=TRUE;

    IF Transition THEN state := ENDE; END_IF

    ENDE: Q3:=TRUE;

    IF Transition THEN state := INIT; END_IF

    END_CASE

    Instructions for the step(Actions)

    Step-further condition(Transition)

    Step

  • TwinCAT Training: Programmer 29.10.2013 60

    ST: CASE instruction Integer Selector Value with constants

    VAR CONSTANTStep1 : INT:= 0;Step2 : INT:= 1;Step3 : INT:= 2;Step4 : INT:= 3;

    END_VAR

    VAR

    State:INT;END_VAR

    CASE State OF

    Step1: Instructions;(*State=0*)Step2: Instructions;(*State=1*)Step3..Step4: Instructions;(*State=2 oder 3*)

    END_CASE

  • TwinCAT Training: Programmer 29.10.2013 61

    ST: Repeat instructions

    The process sequence often requires the multiple processing of precisely the same program sequences, where their number is known only at runtime.Disadvantage of loops:In the case of wrong programming, an infinite number of repetitions takes place.If a continuous loop is executed, this does not impair the start of the time slices (real-time). Tasks with a higher priority will still be executed on time. Tasks with a lower priority will no longer be executed.

    1ms 2ms 3ms 4ms 5mse.g.:1 2 3 41 1 11 1

    Forced switchover to Win NT

    Begin of a new time slice

  • TwinCAT Training: Programmer 29.10.2013 62

    ST: Loops (overview)

    All loops can be terminated with the help of the EXIT instruction, regardless of the abort condition.

    Expression Processing n cycle fix

    FOR SINT/INT/DINT Instructions follow condition

    Yes

    WHILE BOOL Instructions follow condition

    No

    REPEAT BOOL Condition follows instructions

    No

  • TwinCAT Training: Programmer 29.10.2013 63

    ST: FOR loop

    At the beginning of the loop the control variable i is set to the starting value (see example). The control variable is decremented or incremented in each loop, depending on the step size (value after the keyword BY). If i exceeds the end value (after TO), the loop is no longer processed.

    FOR i:=1 TO 12 BY 2 DOFeld[i]:=i*2;(*Anweisung*)

    END_FOR

    Cycle n Start i:=StartValue

    Instruction block

    Cycle n

    i>EndValue

    Yes

    No

    i: = i + step size

  • TwinCAT Training: Programmer 29.10.2013 64

    ST: WHILE loop

    The instruction block of a WHILE loop is executed continuously until the Boolean expression returns TRUE. The abort condition can contain variables that can be changed in the instruction block. If the Boolean expression is FALSE at the beginning, then the instruction block of the WHILE loop is not processed.

    i:=0;WHILE i

  • TwinCAT Training: Programmer 29.10.2013 65

    ST: REPEAT loop

    The instruction block of a REPEATloop is executed as long as the Boolean expression is satisfied.The instruction block is processed at least once.

    i:=0;REPEAT

    Feld[i]:=i*2;(*Anweisung*)i:=i+1;

    UNTIL i>100END_REPEAT

    Boolean expression

    Instruction blocki:= i + step size

    Cycle n

    Cycle n

    No

    Yes

  • TwinCAT Training: Programmer 29.10.2013 66

    ST: FB calls in ST

    TON1 (IN:= NOT TON1.Q , PT:=T#1s );Q0:= TON1.Q;

    VAR

    TON1:TON;END_VAR

    TON1(IN:= NOT TON1.Q, PT:=T#1s , Q=>Q0 );

  • TwinCAT Training: Programmer 29.10.2013 67

    ST: FB calls in ST (alternative)

    TON1.IN:= NOT TON1.Q; TON1. PT:=T#1s;TON1();Q0:= TON1.Q;

    VAR

    TON1:TON;END_VAR

  • TwinCAT Training: Programmer 29.10.2013 68

    ST: FC calls in ST

    Result:=Scale (x:=Eingang, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);(* Gleichwertig:*)Result:=Scale (Eingang, 0.0, 32767.0, 0.0, 100.0);(* Gleichwertig:*)Result:=Scale (

    x:= Eingang,xug:= 0.0,

    xog:= 32767.0,yug:= 0.0,yog:= 100.0);

    In case of functions, all inputs must be occupied

  • TwinCAT Training: Programmer 29.10.2013 69

    ST: FC calls in ST

    Result := Scale (x:=Eingang, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);

    (* Gleichwertig:*)Result:=Scale (

    x:= Eingang,xug:= 0.0,xog:= 32767.0,yug:= 0.0,yog:= 100.0);

    Result CALL Input parameters