BECKHOFF TwinCAT 3 Basics
BECKHOFF TwinCAT 3 Basics
Training fr Umsteiger
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 case The following are not permitted special characters (!, , , $, etc.) spaces consecutive underscores umlauts
29.10.2013
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.
Examples Standard operators AND, OR, NOT Standard types BOOL, INT, REAL... Types TYPE, STRUCT Block types FUNCTION, FUNCTION_BLOCK, PROGRAM
29.10.2013
(*digitale Eingnge*) bStart AT %IX0.0 :BOOL;(*Anlagenstart*) (*analoge Eingnge*) TemK1 AT %IW10 (*Byte 10-11*) :WORD;
29.10.2013
29.10.2013
T#0s
Detail slides DT example - reading the system time DT example - working with standard operators
29.10.2013
29.10.2013
Result LEN 3 1 3
29.10.2013
Detail slides Example: FIND Example: string functions LEN, REPLACE String conversion with Union
29.10.2013
10
29.10.2013
11
Examples TRUE FALSE 2#1 2#0 16#1 16#0 16#AFFE 16#8001 t#60m t#12h t#0.505025h 3.333e-1 1 0 45054 -32768 t#3600000ms t#43200000ms t#1818090ms
REAL
29.10.2013
12
Identifier
Data type
Initial value The physical-logical storage location of this variable is unknown to the user (unlocated)
bStellerUntenLinks:BOOL:=TRUE;
The degrees of freedom and restrictions in the assignment of the identifiers can be found on the slide entitled Identifiers and Prefixes
TwinCAT Training: Programmer 29.10.2013 13
29.10.2013
14
These variables possess a clear address (located) In TwinCAT 3 incompletely located variables can be used for inputs and outputs Applications for %M variables can be solved simply with Unions and direct masking
IB10
IB1 IW0
IB0
IX22.7
IX22.6
IX22.5
IX22.4
IX22.3
IX22.2
IX22.1
IX22.0
IB23 IW22
IB22
IB20
29.10.2013
16
29.10.2013
17
Implementation
TwinCAT Training: Programmer 29.10.2013 18
Project machine
PROGRAM A VAR END_VAR PROGRAM B VAR locVar AT%MB2:WORD; END_VAR
LD %MB2
29.10.2013
19
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.
29.10.2013
20
29.10.2013
21
Global
Parent type
Name
Data type
Initial value
Area
New value
Inherit ance
Derivation
Name
Data type
Initial value
Area
29.10.2013
23
Declaration
29.10.2013
24
29.10.2013
25
29.10.2013
26
29.10.2013
27
29.10.2013
28
The same enumeration value can be used twice via Namespace. Example: Woche.Dn Richtung.Dn
TwinCAT Training: Programmer 29.10.2013 29
Online
29.10.2013
30
Declaration
Online
29.10.2013
31
29.10.2013
32
29.10.2013
33
There is a possibility to place a data field in a directly addressed memory location VAR Feld_1 AT%MB100:ARRAY[1..10] OF BYTE; END_VAR Access to the sub-elements of a data field Feld_1[2] := 120; (* Expliziter Zugriff*) Feld_2[i,j] := EXPT(i,j); (*Indizierter Zugriff*)
29.10.2013
34
Feld_1[9]; Feld_2[1,2];
0 120
29.10.2013
35
IF I< L THEN
Error case
CheckBounds := L;
CheckBounds := U;
ELSE
OK case
CheckBounds := I;
END_IF
29.10.2013
36
29.10.2013
37
29.10.2013
38
Data Types 2: Method of operation of CheckBounds (FUN) Example: user error source code Checkbounds is compiled-in in XAR
10
10
Can be checked with call build:
TwinCAT Training: Programmer 29.10.2013 39
29.10.2013
40
Block types
In IEC61131-3 there are three types of block covered by the generic term POU (PROGRAM ORGANISATION UNIT): Program Function Block Function
29.10.2013
43
29.10.2013
44
29.10.2013
45
29.10.2013
46
Weakest binding
TwinCAT Training: Programmer 29.10.2013 47
29.10.2013
48
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:
THEN
29.10.2013
49
ST: IF instruction
No Condition Yes
Instruction block
29.10.2013
50
ST: IF instruction
Condition Yes
No
Instruction block A
Instruction block B
29.10.2013
51
ST: IF instruction
IF Condition1 THEN Instruction block A; ELSE IF Condition2 THEN Instruction block B; Condition 1 ELSE No Yes IF Condition3 THEN Condition 2 Instruction block C; ELSE Yes Instruction block D; END_IF END_IF END_IF Instruction Instruction
block A block B
29.10.2013
52
ST: IF instruction
IF Condition1 THEN Instruction block A; ELSIF Condition2 THEN Instruction block B; ELSIF Condition3 THEN Instruction block C; ELSE Instruction block D; END_IF
Condition 1 No Yes Condition 2 No Yes Condition 3 No Yes Instruction block A Instruction block B Instruction block C Instruction block D
29.10.2013
53
ST: IF instruction
What can the BOOLEAN EXPRESSION be? Conditions:
BOOLEAN variable Comparison Function calls 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
29.10.2013
54
ST CASE Instruction
CASE Selection criterion OF 1: Instruction 1
Selection criterion = 1
Yes No No
2, 4, 6: Instruction 2 7..10: Instruction 3 .. ELSE Default instructions END_CASE; Two identical values may not be available for selection in the list.
Yes No Yes
Instruction 1
Instruction 2
Instruction 3
Default instructions
29.10.2013
55
29.10.2013
56
2:
Instructions;(*State=2*)
3:
Instructions;(*State=3*)
END_CASE
29.10.2013
57
CASE State OF INIT: START: AUTOMATIK: ENDE: END_CASE Instructions;(*State=0*) Instructions;(*State=1*) Instructions;(*State=2*) Instructions;(*State=3*)
29.10.2013
58
29.10.2013
60
1
e.g.: 1ms
2
2ms
3
3ms
4
4ms
1
5ms
29.10.2013
61
Processing
n cycle fix
Yes Instructions follow condition Instructions No follow condition Condition follows instructions No
REPEAT
BOOL
29.10.2013
62
Start i:=StartValue
Yes
i: = i + step size
Yes
Boolean expression
No
Cycle n
TwinCAT Training: Programmer 29.10.2013 64
No
ST: FB calls in ST
VAR TON1:TON; END_VAR TON1 (IN:= NOT TON1.Q , PT:=T#1s ); Q0:= TON1.Q;
29.10.2013
66
29.10.2013
67
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:= xug:= xog:= 32767.0, yug:= yog:= ); In case of functions, all inputs must be occupied
TwinCAT Training: Programmer 29.10.2013 68
Eingang, 0.0,
0.0, 100.0
ST: FC calls in ST
Result := Scale (x:=Eingang, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);
Result
CALL
Input parameters
(* Gleichwertig:*)
Result:=Scale ( x:= xug:= xog:= yug:= yog:= ); Eingang, 0.0, 32767.0, 0.0, 100.0
29.10.2013
69