Professional Documents
Culture Documents
Beckhoff IEC61131 3
Beckhoff IEC61131 3
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
06.11.2012
-1 General definitions and typical function (cyclic processing, process image input and output)
-2 Environmental conditions and conditioning classes of the control and the programming devices. (temperature, air humidity)
-4 Guide line for the system analysis of the user, the system selection, the realisation of the application, as well as maintenance and servicing
-5 Definition of the communication via funcion blocks and communication via access paths (additionally to 3)
06.11.2012
Standard guide
Portability level
06.11.2012
Power supply
Executing function
process
06.11.2012
Communication functions
Server
Operator Programmer
06.11.2012
Operating function
Data
PLC Server 1, PLC Server 2, PLC Server 3, PLC Server 4, NC Server, Cam Server
06.11.2012
Interface function between sensors and actuators Copy rule DP RAM <-> PA PLC
F-Sensor 8
F-FieldDevice
F-Actuator 06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
06.11.2012
Software model
Configuration Resource Task1 Program Task2 Resource Task1 Program FB FB Task2 Program FB
Main
Program Motion FB FB
access paths
10
06.11.2012
Main
Program Motion FB FB
Global and direct addressed variable Mapping in the TwinCAT System Manager
access paths
11
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
12
06.11.2012
Identifier
Identifier serves to the individual name assignment for variables, data types, functions...
The identifier begins with a letter or a underscore Followed by numbers, letters and underscore No difference between capital letters and small letters Not allowed Special characters (!,,,$..) Blank character Sequential underscores mutated vowel
13
06.11.2012
Prefix
Prefixes are not specificated, but they make the handling of the identifier easier. Here some suggestions:
Hungarian notation: Write part words together. The first letter of a part word must be a capital letter.
b Boolean r Real s - String ST_ - Declaration of structures st - Initialisation of structures FB_ - Declaration of function blocks fb Initialisation of function blocks M_ - Declaration of methods
bEndschalterLinks rSollPosition sRxDaten ST_MotorDaten stM1Parameter FB_Ueberlast fbM1Ueberlast (declaration) (instance) (declaration) (instance)
14
06.11.2012
Key words are preset indentifer by the IEC61131-3. They are fixed components of the syntax and must not be used for other purposes. TRUE, FALSE, AND, FUNCTION,...
The comments are limited with the characters (* at the beginning and *) at the end. Comments can be placed there, where blank characters are allowed. Exception: inside character string literals.
(*digital inputs*) bStart AT%IX0.0:BOOL;(*Machine start*) (*analog inputs*) TemK1 AT%IW10(*Byte 10-11*):WORD;
Using the option Auto format, the keywords are written in capital letters.
15
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
16
06.11.2012
Data types
-75 181 1
17
1
06.11.2012
Type Boolean Bit string(8) Bit string(16) Bit string(32) Short integer Integer Double integer Unsigned short integer Unsigned integer Unsigned double integer
ANY-Type ANY_Bit
Initial FALSE 0 0 0 0 0 0 0 0 0
Value range TRUE/FALSE 0..16#FF 0..16#FFFF 0..16#FFFF_F FFF -27...27-1 -215...215-1 -231...231-1 0...28-1 0...216-1 0...232-1
ANY_Num
18
06.11.2012
Type
ANY-Type
Key word
Initial
Value range
Slide point Long slide point Date Time of day Date time of day time Sequential characters
ANY_Real
REAL LREAL
ANY_Date
ANY_Time ANY_String
TIME STRING
19
06.11.2012
String
A STRING type variable can contain any string of characters. The size entry in the declaration determines how much memory space should be reserved for the variable. It refers to the number of characters in the string and can be placed in parentheses or square brackets. If no size specification is given, the default size of 80 characters will be used.
Strings are zero terminated, that means the last character of a string is always zero. Each character inside a string needs one byte.
20
06.11.2012
Special characters
If you want to add a special character into a string, you have to begin with a $character.
Special Characters character description dollar signs Single quotation mark Line feed New line Page feed Line break Tab
A CR 1 0 0 (*Str. Abschluss*)
$$ $ $L or $l $N or $n $P or $p $R or $r $T or $t
21
06.11.2012
If a character in a program ought to be converted to an ASCII character, two procedures are allowed: 1. 2. Indirectly, by interpreting the data memory different. Directly via the provided function block. ASC and CHR are both included in the library ChrAsc.lib.
22
06.11.2012
A variable owns a name, behind which a value (number, string, date) hides. The name of the variable is a way description to the declared data. Variables distinguish themselves thereby, that their content can be changed to the run time.
Identifier
Data type
Initial value
bStellerUntenLinks:BOOL:=TRUE;
The physical logical storage location of the variable is not known by the operator (unlocated)
The degrees of freedom and the restrictions at the assignment of the identifiers can be seen on the slides identifier and prefixes.
23 06.11.2012
Identifier
Identifier
AT
AT
Address
%I %Q %M X B W D Byte Byte Bit
Data type ;
Data type
bStellerUntenLinks AT%IX0.0:BOOL:=TRUE; From TwinCAT 2.8 the addressing can be done automatically. Then the program works with not completely located variables. bStellerUntenLinks AT%I*:BOOL:=TRUE;
24 06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
25
06.11.2012
Validity range
Local variables are limited on the block, in which they were declared.
Key words VAR .. END_VAR VAR_INPUT .. END_VAR VAR_IN_OUT .. END_VAR VAR_OUTPUT .. END_VAR
26
06.11.2012
From program A is a direct access by address %MB2 to the local declared variable locVar in program B possible.
Project Machine
PROGRAM A VAR END_VAR PROGRAM B VAR locVar AT%MB2:WORD; END_VAR
LD %MB2
27
06.11.2012
Project Machine
VAR_GLOBAL Var1:WORD; END_VAR
As shown in the example on the left, there is an overlapping in the validity range. In this case, the local declared variable Var1 is loaded into the accumulator. The compiler generates no warning for this overlapping.
28
06.11.2012
Attributes
Attributes can be used to define special features of variables. Examples: The variable(s) should be stored at the shutdown of the PLC, to be reloaded at the new start. VAR RETAIN Zaehler:UINT; END_VAR VAR PERSISTENT Zaehler:UINT; END_VAR Initial values, the variables should be allocated with a special value at the PLC start or reset. VAR AccelerationTime : TIME := T#3s200ms; END_VAR
29 06.11.2012
Attributes (constants)
Projekt Maschine
VAR_GLOBAL CONSTANT END_VAR PROGRAM A VAR CONSTANT END_VAR
If you want to use a mathematic, construction, or machine constant, you have to complete the regular key words VAR_GLOBAL .. END_VAR with the key word CONSTANT. This completement can also be used with local keywords. The state of these identifier is read. VAR_GLOBAL CONSTANT pi:REAL:=3.141592654; END_VAR
30
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
31
06.11.2012
In the IEC61131-3 exists under the main generic term three POUs: Programs Function blocks Functions The organisation POU is replaced by the task configurator.
32
06.11.2012
Each POU consists of a declaration part and a body. The declaration part is the same in each IEC programming language. The local variables of the block are declared there.
The body is written in one of the IEC programming languages which include IL, ST, SFC, FBD, LD or CFC.
33
06.11.2012
34
06.11.2012
Function block FB
Function block FB
Called by programs or other FBs calls : FBs, functions, Locale variable : static, i.e. the local data are again available at the next cycle. At multiple call multiple instances (mulitply). Each FB call can have own local data. Inputs: 0,1,2,3 VAR_INPUT Outputs: 0,1,2,3 VAR_OUTPUT Transfer by reference 0,1,2,3 VAR_IN_OUT Debug: In the online mode of PLC Control, the instance of the according call has to declared. After this, the local data are visible for each call. Using: multiple used function blocks, which need an own data range each. Multiple sequences....
35
06.11.2012
Create an instance
FB
FUNCTION_BLOCK A VAR _INPUT Var_IN :WORD; END_VAR VAR _OUTPUT Var_Out :BYTE; END_VAR VAR Var1 :WORD; Instanz_1: B; END_VAR
PROGRAM MAIN VAR Instanz_1 :A; Instanz_2 :A; Instanz_3 :B; END_VAR CAL Instanz_1 CAL Instanz_3
PRG
FB
FUNCTION_BLOCK B VAR_INPUT X :REAL; END_VAR VAR _OUTPUT Y :REAL; END_VAR
36
06.11.2012
Function FC
Function FC
called by: programs, function blocks and other functions calls: functions Local variable : temporary, i.e. the local data are only available for the operating time of the function. Afterwards this data range is used by other functions. Inputs: 1,2,3........ VAR_INPUT Outputs: exactly 1!, but structure varaibale possible. The output name is at the same time the name of the function. Except for TwinCAT: VAR_IN_OUT possible, Debug: The local variables are visible with ??? in the online mode of PLC Control, because these variables are multiple used by all functions in the cycle, and the monitoring (debug) takes place at the cycle bounds. Hepl: program development with breakpoints Breakpoints Using: algorithms, at which the result is available after a pass. Scaling, compare......
37
06.11.2012
FC Specials
From TwinCAT 2.8: The return value can be defined directly if a new function is created.
Function name
Inputs Local variables are only valid for the operating time of the function
38
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
39
06.11.2012
The TwinCAT System Service operates as Windows NT service in the local system account. In this way, the TwinCAT System Service is started by Windows NT before a user has logged on. As an activity symbol, the TwinCAT System Service incorporates its icon into the task bar of the desktop. In addition, the colour of the icon indicates the state of the TwinCAT system. TwinCAT stopped TwinCAT starting. TwinCAT running. TwinCAT Config Mode
The TwinCAT System Service is primarily responsible for starting and stopping the TwinCAT run time system. It loads all configured servers and initialises them during the TwinCAT system start. .
40 06.11.2012
The event display is a programm to moniotor the events in the system. The event logging service starts automatically, if you execute Windows NT.
The TwinCAT I/O subsystem can be reset via the TwinCAT System Service. For this, the corresponding function must be selected in the context menu. The reset applies to all connected field bus systems.
41 06.11.2012
Multitasking
TwinCAT possesses more than 62 different tasks. The default settings can use preset profiles or change the priority individually.
42
06.11.2012
PLC cycle PLC cycle Real time operation of PLC software (1 task) on a PC with windows NT.
PLC cycle
43
PLC cycle
t
06.11.2012
Overview
Contents
Part 1
IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
44
06.11.2012
Real time
Many industrial applications demand a guarantee, that, clearly predictable and reproduceable, the system load reacts sufficient fast to the process event in a defined time. The real time is very important for the digital control. The sampling of an analog signal (actual position) with a PC should have absolute constant distances between two measurements. Each part process requires different reaction times. Because of this, several part processes with different features and different reaction times can be created in one automation task. If several tasks want to access the CPU simultaneously, the IEC 61131-3 defines two procedures: 1. Preemptive (interruptible execution) multi tasking (TwinCAT) 2. Non preemptive (not interruptible execution) multi tasking
45
06.11.2012
The real time operation will be achieved with deterministic time slices. The width of the time slices can be chosen in steps: (1000s ... 50s). The default setting is 1ms.
The time slices will be kept with an accuracy of 15s (Jitter). Device with the lowest priority goes to the waiting loop and waits until the CPU is free.
With the begin of a new time slice, the software devices (PLC, NC) will be executed with priority control.
- 15s +15s
46
06.11.2012
Real time
The TwinCAT real-time system can be configured via the context menu of the TwinCAT System Service. Length of the time slice
Processor time can be assigned to the TwinCAT real-time system via the linear regulator in the figure above. On a time basis of 1 ms, this means that TwinCAT has a maximum of 800s available each millisecond. When the TwinCAT real-time system switches to its idle task, the processor is returned to Windows NT. The bar in the linear regulator dis-plays the current utilisation level of the real-time system. The display is averaged over 256 cycles (ms).
In this case, the current and maximum latency times in the real-time sys-tem are shown. The time by which the central system tick arrives too late is measured.
47 06.11.2012
10ms 0ms
20ms
30ms 40ms
80% W 2ms
80% Time slice for Windows 3ms Time slice for TwinCAT PLC program cyclic task
If TwinCAT does not need the (full) reserved time slice, the scheduler provides this computing power to windows.
48 06.11.2012
PLC tasks and drive control will be executed deterministically with multiple tasking.
Real time operation of a PLC program and NC control with a PC NC Win NT & HMI Interface PLC program
1
e.g.: 1ms
2
2ms (e.g. 1ms)
3
3ms
4
4ms
2
5ms
3 t t
06.11.2012
NC cycle
NC cycle
NC cycle
SPS cycle
49
(e.g. 2ms)
The smaller the time slice, the shorter the reaction time of the highest priority task. This has the consequence that the software devices must be fairly often interrupted. If a device is interrupted, the program stack has to be safed. This has the consequence that the recopy expense rises. TwinCAT and the operating system are equal. For the operating system, calculating capacity is given regularly. The switch to the operating system takes place at the earliest, as soon as all TwinCAT devices complete the processing, and at the latest at the CPU limit.
50
06.11.2012
Before a new project starts, the following questions have to be checked: 1. What is the target platform, i.e. the device the user wants his program to run. TwinCAT offers three different platforms. Soft SPS (IPC)
2. In which distances and under which circumstances shall the PLC program be processed? The IEC 61131-3 defines the task as a element of the execution control, which is able to call several programs to execute. At the configuration of the task one of the variants cyclic and event can be chosen. TwinCAT only supports the cyclic variant.
Its possible to create an event driven task from a cyclic task. For this, the program call must depend on an event.
52
06.11.2012
Priority of the task. The value range (0-3) is an offset on the default value 25 for the first run time. Each run time possess maximum to four different tasks.
The field for the variables depends on the selected IEC language.
PLC Control
54 06.11.2012
Main program
The task is a trigger mechansim. A program is required to execute logical operations. The task calls one program. If an instance of a function block is needed, it can only be called from a program.
The features of an POU can be defined with this dialog. The name is an identifier. Respectively the rules for the identifier obtain. The type of the POU depends on the problem. The language of the POU should be used according to the problem.
55
06.11.2012
Program
A program is a POU which returns several values during operation. Programs are recognized globally throughout the project. All values are retained from the last time the program was run until the next.
Programs can be called by programs and function blocks. A program call in a function is not allowed. If a POU calls a program, and if thereby values of the program are changed, then these changes are retained the next time the program is called, even if the program has been called from within another POU.
56
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 2
Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
57
06.11.2012
The user can create own data types on the base of elementary data types or already created data types. The new created data types are visible in the whole project. They begin with the keyword TYPE and end with END_TYPE.
Parent type
Name
Data type h e i r
Initial value
Range
New value
Derivation
Name
Data type
Initial value
Range
06.11.2012
58
You can use the user-defined reference data type to create an alternative name for a variable, constant or function block. Create your references as objects in the Object Organizer under the register card Data types. They begin with the keyword TYPE and end with END_TYPE.
59
06.11.2012
Enumeration is a user-defined data type that is made up of a number of string constants. These constants are referred to as enumeration values. Enumeration values are recognized in all areas of the project even if they were locally declared within aPOU. It is best to create your enumerations as objects in the Object Organizer under the register card Data types. They begin with the keyword TYPE and end with END_TYPE. Syntax: TYPE <Bezeichner>:(<Enum_0> ,<Enum_1>, ...,<Enum_n>); END_TYPE Beispiel: 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
You may not use the same enumeration value more than once.
06.11.2012
60
The <Identifier> can take on one of the enumeration values and will be initialized with the first one. These values are compatible with whole numbers which means that you can perform operations with them just as you would with INT. You can assign a number x to the <Identifier>. If the enumeration values are not initialized, counting will begin with 0. When initializing, make certain the initial values are increasing. The validity of the number will be reviewed at the time it is run.
61
06.11.2012
form
Pers_Data
Name: Age: Firstname: Address:
Structures are self defined data types. They are important aids for managing the process data. Furthermore the structures are suited for capsulated data transfer to function blocks. Structures can be used like single element variables.
TYPE Pers_Data : STRUCT Name: STRING(25); Firstname: STRING(25); Age:USINT; Address: STRING(55); END_STRUCT END_TYPE
62
Identifier for the new data type Identifier : parents data type
06.11.2012
P3
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 2
Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
64
06.11.2012
Arrays describe lists resp. data arrays. All elements in the arrays are from the same type. Arrays can also exist of own data types (structures). One- , two-, and three-dimensional arrays are possible. VAR Feld_1 :ARRAY[1..10] OF BYTE; Feld_2 :ARRAY[1..10, 2..5] OF UINT; :ARRAY[1..10] OF DINT; END_VAR
Its possible to put a data array to a direct addressed memory position VAR Feld_1 AT%MB100:ARRAY[1..10] OF BYTE; END_VAR
Access to the sub-elements of a data array Feld_1[2] := 120; (* explicit access*) Feld_2[i,j] := EXPT(i,j); (*indicated access*)
65 06.11.2012
Identifier
Field
Data type
Initial value
0 T#1s
1 T#2s
2 T#1s
3 T#0s
4 T#0s
5 T#0s
6 T#0s
The field length can be done explicit or with the aid of constants. A dynamic change of the field size is not possible.
Access:
VAR WertAusArray : TIME; END_VAR
WertAusArray := DBZeiten[1];
66 06.11.2012
Identifier
Field
Data type
Initial value
0 0 1 0 6
1 1.7 1.2
2 2 1.2
Access:
VAR WertAusArray : REAL; END_VAR
67
06.11.2012
Drivejob:ARRAY [0..3, 0..1] OF LREAL:= (* target position, (*Job 0*) (*Job 1*) (*Job 2*) (*Job 3*) 20.0, 33.75, 45.0, 70.75, velocity *) 30.0, 30.0, 30.0, 30.0;
68
06.11.2012
Identifier
Array
Datatype
Initial value
2 1 0 1 0 1 1 0 0
20,21,22,23,24,25
23 24 25 20 21 22
0 1 2
Access:
VAR ValfromArray : UINT; END_VAR ValfromArray
13 14 15 10 11 12
0 1 2
3 0
0
4 1
1
5 2
2
69
:=
Supportpoint[ 2,0,1 ];
06.11.2012
A dangerous state can arise in the PLC program, if an access to a range outside the data field takes place.
VAR Feld_1 :ARRAY[1..10] OF BYTE; Feld_2 :ARRAY[1..10, 2..5] OF UINT; Feld_3 :ARRAY[1..10] OF DINT; END_VAR i:= 9 Feld_1[i+2] := 120; 9
Feld_1[9]; Feld_2[1,2];
70
0 120
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 2
Variables II Structs, Enums Variables II Arrays Check bounds Structured text
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
71
06.11.2012
If you define a function in your project with the name CheckBounds, you can automatically check for outof-range errors in arrays.
IF I< L THEN
Error case
CheckBounds := L;
CheckBounds := U;
ELSE
OK case
CheckBounds := I;
END_IF
72
06.11.2012
CheckBounds can be copied with Copy Project from another PLC project to the current project ( e.g. training project). Checkbounds can also be created or written directley.
73
06.11.2012
74
06.11.2012
CheckBounds := I;
END_IF FUNCTION CheckBounds :INT VAR_INPUT I,L,U : INT; END_VAR IF I< L THEN CheckBounds := L;
CheckBounds := U;
ELSE CheckBounds := I; END_IF 06.11.2012
From TwinCAT 2.8 the following further Checker functions are possible:
76
06.11.2012
LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; BOOL; (*Pump ?*)
Access to Drillpos 55: Access: MoveXAx (*FB Instance*) ( Execute:= Position:= Velocity:= Acceleration:= Deceleration:= Jerk:= Direction:= Axis:= );
78
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing
Part 2
Variables II Structs, Enums Variables II Arrays Check bounds Structured text
Part 3 Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
79
06.11.2012
Operation
Put in parentheses Function call Exponentiation Negate Build. complements Multiply Divide Modulo Add Substract Compare Equal to Not Equal to Bool AND Bool XOR Bool OR
Symbol
(expression) Function name (parameter list) EXPT NOT * / MOD + <,>,<=,>= = <> AND XOR OR
Weakest binding
80
06.11.2012
Instruction
Assignment := Callin a function block RETURN IF CASE FOR WHILE REPEAT EXIT Empty instruction
Example
PosWert := 10; Ton1(IN:=Start, PT:=T2s); Output:= Ton1.Q RETURN; See the following pages
81
06.11.2012
IF Instruction
Keywords:
IF With the IF instructions its not possible to jump back in the PLC cycle. ELSIF ELSE END_IF GOTO is not available
THEN
e.g.:
82
06.11.2012
IF Instruction (1)
No
Instruction block
83
06.11.2012
IF Instruction (2)
Condition IF a>b THEN Instruction block A; ELSE Instruction block B; END_IF Yes
No
Instruction block A
Instruction block B
84
06.11.2012
IF Instruction (3)
IF Condition1 THEN Instruction block A; ELSE IF Condition2 THEN Instruction block B; ELSE IF Condition3 THEN Instruction block C; ELSE Instruction block D; END_IF END_IF END_IF
Instruction block D
85
06.11.2012
IF Instruction (4)
IF Condition1 THEN Instruction block A; ELSIF Condition2 THEN Instruction block B; ELSIF Condition3 THEN Instruction block C; ELSE Instruction block D; END_IF
Instruction block D
86
06.11.2012
87
06.11.2012
CASE Instruction
Selection criterion = 1
No Yes
No
Instruction 1
Instruction 2
Instruction 3
Default Instructions
CASE State OF Enum Typ: TYPE Schritte : ( INIT:=0, START, AUTOMATIK, ENDE); END_TYPE INIT: START: AUTOMATIK: ENDE: END_CASE instructions;(*State=0*) instructions;(*State=1*) instructions;(*State=2*) instructions;(*State=3*)
If the integer selector variable state is declared as enum, the value of the variable is visible in the online mode.
VAR CONSTANT Step1 : INT:= 0; Step2 : INT:= 1; Step3 : INT:= 2; Step4 : INT:= 3; END_VAR CASE State OF Step1: Step2: Step3..Step4: END_CASE VAR State:INT; END_VAR instructions;(*State=0*) instructions;(*State=1*) instructions;(*State=2 oder 3*)
90
06.11.2012
Q1:=TRUE;
IF Transition THEN state := AUTOMATIC; END_IF AUTOMATIC: Q2:=TRUE; IF Transition THEN state := END; END_IF END: Q3:=TRUE; IF Transition THEN state := INIT; END_IF END_CASE
91 06.11.2012
Repeat Instructions
The process flow requires the multiple handling of exactly the same program sequences, whose quantitiy is known at the run time. Disadvantage of loops: During faulty programming, many repetitions take place infinitely.
If a continuous loop is executed this does not impair the start of the time slice (real-time). Tasks that will have a higher priority are still executed on time. Tasks that will have a lower priority are not longer executed.
1
e.g.: 1ms
2
2ms
3
3ms
4
4ms
1
5ms
92
06.11.2012
Loops (Overview)
All loops can be ended with the EXIT instruction, regardless of the break-off condition.
93
06.11.2012
FOR loop
cycle n At the beginning of the loop, the variable i is defined as start value (see example). The variable in incremented or decremented in each cycle depending on the step width (value after the keyword BY) If i exceeds the end value (after TO), the loop is not longer processed.
Yes
cycle n
06.11.2012
WHILE loop
cycle n The instruction block of a WHILE loop is executed as long as the boolean expression supplies TRUE . The exit condition contains variables which can be changed in the instruction block. If the boolean expression is FALSE at the beginning, the instruction block of the WHILE loop is not processed.
Yes
Boolean Expression
No
cycle n
06.11.2012
REPEAT loop
Cycle n The instruction block of a REPEAT loop is processed as long as (UNTIL) the boolean expression is no longer fullfilled. The instruction block is executed at least once.
Yes
Boolean expression
No
cycle n
06.11.2012
FB calls in ST
VAR TON1:TON; END_VAR TON1 (IN:= NOT TON1.Q , PT:=T#1s ); Q0:= TON1.Q
from TwinCAT 2.8 :
97
06.11.2012
FB calls in ST explanation:
Create instance of FB
Q0:= TON1.Q
FB calls in ST (alternative)
VAR TON1:TON; END_VAR TON1.IN:= NOT TON1.Q , TON1. PT:=T#1s; TON1(); Q0:= TON1.Q
99
06.11.2012
Declaration
VAR
TRANSFER ONLY INPUT PARAMETER . This is
TON1:TON; END_VAR
NO FB CALL!!!!!
FB CALL
06.11.2012
Q0:= TON1.Q;
FC calls in ST
Result:=Scale
(* equal:*)
Result:=Scale (input, 0.0, 32767.0, 0.0, 100.0);
(* equal :*)
Result:=Scale ( x:= xug:= xog:= yug:= yog:= ); input, 0.0, 32767.0, 0.0, 100.0
101
06.11.2012
FC calls in ST explanation:
Result
CALL
Input parameters
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing Part 2 Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3
Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
103
06.11.2012
Step N Action
Qualifier
Only one step is active at a time The condition to change from one step to another is the transition. In the action must be programmed what should be executed during the active step.
Action
Transition Action, Can be written in Structured Text, Instruction list, Ladder Diagram, Function Block Diagram and in Sequential Function Chart .
Qualifier
104
06.11.2012
Steps
The activity of a step can be requested with Stepname.X. The duration of the activity of a step can be requested with Stepname.T . Both are components of a structure, which are created automatically from PLC Control. At the programming only the stepname has to be defined. Stepname.X and Stepname.T are local variable and can only be read.
Transition Step1 N
Normal Step
Action
Transition
105
06.11.2012
Actions
Transition Transition Step1 N Action Step1 N Transition Transition bOutput
Action, can be programmed in -> Structured Text, -> Instruction List, -> Ladder Diagram, -> Function block diagram, CFC/FBD -> Sequential Function Chart Action, can be a variable of type BOOL. The variable is TRUE by activating the the step and FALSE by leaving the step.
106
06.11.2012
Transition
Step1
N P N R
107
06.11.2012
Init N Action
Only one branch can be active. Because only the left or the right branch is important, two transitions are necessary before the combination.
Transition
Transition
Stepa N Action
Stepb N Action
Transition
Transition
108
06.11.2012
Init N Action
Transition
109
06.11.2012
Transition
Transition
110
06.11.2012
Transition
Step_a N Action
Step_b
Transition
111
06.11.2012
Transition
Step_b N Action
Transition
Action
Transition
112 06.11.2012
Transitions
bVariable Step1 N
BOOLEAN VARIABLE
Action ST instruction.
A>B
The result be must be of type BOOL. Note: If the instruction is too long, the display will be shorten automatically.
113
06.11.2012
Transitions
Programmed Transitions
Init N Action With this mark its only a comment. NOTHING CONNECT N Action
001 Comment
A>B Step1
INPUT0
Final Scan
If a step is left, the processing takes exactly one more cycle. This behaviour can be used for cleaningin the action. Example: Reset outputs.
Step.X 1
Step1
Action
t
Action processing
1 0 t
1 Cycle
Go on Following step
001
Action
release
AND
Output Step1.X
Transition At the last pass the step.X = FALSE. Thus the variable Output is FALSE .
115 06.11.2012
Final Scan
At a certain action the final scan leads to an unwanted behaviour.
Step1
Action
Step.X 1
TRUE
0 t
Action processing
Behaviour: Counter := Counter +1; (*Counter increases at 2*) Remedy: The step flag is only for one cycle 1: IF Schritt.X THEN Counter := Counter +1; END_IF (*Counter increases at 1*)
116
0 t
1 cycle 1 cycle
06.11.2012
Qualifier
Controls the action processing after activating a step. N: Non Stored
Step
N
TRUE
Action
Step.X 1 0 t
Action processing
1 0 t
1 cycle
Step.X
Action processing
117
06.11.2012
Qualifier
Controls the action processing after activating a step S: SET
Step
S
TRUE
Action
Step.X 1 0 t
Action processing
1 0 t
1 cycle
Combination in FBD
001 Step.X
S R
SR
Action processing
118
06.11.2012
Qualifier
Controls the action processing after activating a step R: RESET
Step
R
TRUE
Aktion
Step.X 1 0 t
Action processing
1 0 t
Combination in FBD
001
S
Step.X
SR
Action processing
119
06.11.2012
Qualifier
Controls the action processing after activating a step D: DELAY
Step
D T#1s
Action
Step.X 1
TRUE
0 t
Action processing
1 0 Delay t
Combination in FBD
001
Step.X T#1s
TON IN Q PT ET
Action processing
120
06.11.2012
Qualifier
Controls the action processing after activating a step L: LIMITED
Step
L T#1s
Action
Step.X 1 0 t
Action processing
TRUE
1 0
LIMITED
Combination in FBD
001
Step.X 1
AND
Step.X T#1s
Action processing
0 t
Action processing
TON IN Q PT ET
1 0
Limit
121
06.11.2012
Qualifier
Controls the action processing after activating a step P: PULSE
Step
Action
Step.X 1 0 t
Action processing
TRUE
1 0
1 cycle 1 cycle
Combination in FBD
001 R_TRIG Step.X Clk Q Action processing
122
06.11.2012
Qualifier, Combinations
SD: Stored and delayed DS: Delayed and stored SL: Stored and time limeted
123
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing Part 2 Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3
Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
124
06.11.2012
(*When it has the value TRUE, the timeouts of the steps will be registered in SFCError. Other timeouts will be ignored.*)
SFCInit:
BOOL;
(*When this boolean variable has the value TRUE the sequential function chart is set back to the Init step. The other SFC flags are reset too (initialization).
The Init step remains active, but is not executed, for as long as the variable has the value TRUE. It is only when SFCInit is again set to FALSE that the block can be processed normally. *)
125
06.11.2012
SFCReset:
BOOL;
(*This variable, of type BOOL, behaves similarly to SFCInit. Unlike the latter, however, further processing takes place after the initialization of the Init step. Thus for example the SFCReset flag could be re-set to FALSE in the Init step.*)
126
06.11.2012
SFCQuitError:
BOOL;
(*Execution of the SFC diagram is stopped for as long as this boolean variable has the value TRUE whereby a possible timeout in the variable SFCError is reset. All previous times in the active steps are reset when the variable again assumes the value FALSE.*)
SFCPause:
BOOL;
(*Execution of the SFC diagram is stopped for as long as this boolean variable has the value TRUE.*)
SFCTrans:
BOOL;
(*This boolean variable takes on the value TRUE when a transition is actuated. .*)
127
06.11.2012
SFCError:
BOOL;
(*This Boolean variable is TRUE when a timeout has occurred in a SFC diagram. If another timeout occurs in a program after the first one, it will not be registered unless the variable SFCError is reset first. *)
SFCErrorStep:
STRING;
(*This variable is of the type STRING. If SFCError registers a timeout, in this variable is stored the name of the step which has caused the timeout. *)
SFCErrorPOU:
STRING;
(*This variable of the type STRING contains the name of the block in which a timeout has occurred. *)
128
06.11.2012
SFCCurrentStep: :
STRING;
(*This variable is of the type STRING. The name of the step is stored in this variable which is active, independently of the time monitoring. In the case of simultaneous sequences the step is stored in the branch on the outer right. No further timeout will be registered if a timeout occurs and the variable SFCError is not reset again.*)
129
06.11.2012
SFCErrorAnalyzation:
STRING;
(*This variable, of type STRING, provides the transition expression as well as every variable in an assembled expression which gives a FALSE result for the transition and thus produces a timeout in the preceding step. A requirement for this is declaration of the SFCError flag, which registers the timeout. SFCErrorAnalyzation refers back to a function called AppedErrorString in the TcSystem.Lib library. The output string separates multiple components with the symbol |. *)
SFCTip: SFCTipMode:
BOOL; BOOL;
(*This variables of type BOOL allow inching mode of the SFC. When this is switched on by SFCTipMode=TRUE, it is only possible to skip to the next step if SFCTip is set to TRUE. As long as SFCTipMode is set to FALSE, it is possible to skip even over transitions.*) END_VAR
130
06.11.2012
Implicit variable
131
06.11.2012
132
06.11.2012
133
06.11.2012
effect:
SFCTipMode SFCTip Transition effect
Process stays in the current step Change to next step Change to next step Process stays in the current step Change to next step
06.11.2012
Actions also in other IEC languages possible! (POU type : PRG, FB) Mainprogram
Action step1
Action step2
135 06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing Part 2 Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3
Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
136
06.11.2012
These variables maintain their value, even after a power failure. When the program is run again, the stored values will be processed further. A practical example would be an operations timer that recommences timing after a power failure. A practical example would be an operations timer that recommences timing after a power failure. All other variables are newly initialized, either with their initialized values or with the standard initializations. TwinCAT supports two kind of remanent flags:
RETAIN
PERSISTENT
137
06.11.2012
Persistent
138
06.11.2012
Boot project
Power ON
Requirement :
It should be possible to automate the loading and the starting of the PLC project after switching on the computer.
Start NT
Start TwinCAT
Log on
The PLC can start independent from the user log on!
Start PLC
139
06.11.2012
1&2
1 2
TwinCAT Auto boot Auto logon with Win NT
140
06.11.2012
Requirement:
1. 2.
The machine should work properly. The hardware, software and the mappings are correct. The PLC Control in the status online.
3.
141
06.11.2012
3&4
Number of run time systems
Enabling for loading the boot project for the run time system 1.
Enabling for loading and saving the RETAIN data for the run time 1.
06.11.2012
142
Sourcecode download
1.Goto Project/Options and press the left mouse button. 2.A Window will open 3.Choose the Point Sourcedownload
143
06.11.2012
1.Implicit at load Every time when you open the PLC Project the Sourcecode will be written down to the controller.
2.Notice at load If the PLC Project changed, you get a message box, when you open the project. 3.Implicit at create boot project. Everytime you create a bootproject, the sourcecode will be transfered to the controller
144 06.11.2012
1.On demand The Sourcecode will be written down to the controller on demand. Online/Sourcecode download
Extent Sourcecode only The plc project will be written in the controller All files The plc project with all libaries will be written tin the controller
145 06.11.2012
1. You can open the actuell plc projekt direct from the controller 2. Under File/Open you can open the project direct from the plc.
146
06.11.2012
In order to be able to view this data structure, the "PlcSystem.lib" library must be linked in. (Global variable)
The structure component shows if the persistent/retain data were loaded successfully.
147
06.11.2012
2 3 4 5 6 7
148
06.11.2012
IF
ELSIF GETBIT32(inVal32:=SystemInfo.BootDataFlags , bitNo:=5) THEN errLoadBootData:=TRUE; strBootDataState:= 'Error Load PersistentData '; END_IF
149 06.11.2012
With the fuction block WritePersistentData (TcPlcUtilities.Lib) it is possible to initiate the writing of the Persistent Data. The writing takes place at the Shut Down of the PLC (standard). While the function block is busy, the access to the Persistent Variable is not allowed!
TcPlcUtilities.Lib
150
06.11.2012
IF ReqWritePersistent THEN
fbWritePersistent(START:=FALSE );
ReqWritePersistent:=FALSE; ELSIF NOT fbWritePersistent.BUSY THEN
PLC Runtime
fbWritePersistent(START:=FALSE ); ReqWritePersistent:=FALSE;
END_IF END_IF
151
06.11.2012
Internal Libraries
Unnecessary elements: All tasks will be deleted in the task configuration. All POUs, which are not to belong to thecontents of a library, are removed. All global variables will be deleted. Valid: global constants, self defined data types.
152
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing Part 2 Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3
Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
153
06.11.2012
CheckDivByte
154
06.11.2012
CheckDivWord
155
06.11.2012
CheckDivDWord
156
06.11.2012
CheckDivReal
157
06.11.2012
CheckRangeSigned
Variable to be checked
Checker function
158
06.11.2012
CheckRangeUnsigned
Variable to be checked
Checker function
159
06.11.2012
160
06.11.2012
Overview
Contents Part 1 IEC 61131-3 Overview Software model Identifier Elementary data types Variables classes Block types TwinCAT System Service Timing Part 2 Variables II Structs, Enums Variables II Arrays Checkbounds Structured text
Part 3
Sequential Function Chart Step diagnosis Appendix Bootprojects, Data remanence Checker functions Example Step by Step
161
06.11.2012
nozzle 1-3
Start Stop
workpiece
Motor
Encoder
Part exercise: Switch on and off the plant simulate encoder Request marks & switch on the nozzles Safe state when plant off.
162
06.11.2012
Identifier bStart bStop bMotor bDuese_1 bDuese_2 bDuese_3 Marke_1 Marke_2 Marke_3 Marke_End Inc
163
%I AT%IX0.0 AT%IX0.1
%Q
Type BOOL BOOL BOOL BOOL BOOL BOOL WORD WORD WORD WORD WORD Global Global Global Global Global Global Local Local Local Local Local
06.11.2012
The switches bStart and bStop should be used as push-button. To safe the status, its necessary to add a hold element (RS). There are two bistable memorys under the standard function blocks. The dominant input is marked with xxx1.
Set
Set1
Reset1
Reset
Q1
164
Q1
06.11.2012
The encoder value is incremented each cycle around 1. This is implemented with the operator ADD. Thus a running encoder develop.
It is checked, if the encoder value stands between Marke_1 und Marke_2 . Marke_1 GT Inc LT Marke_2 In this case, the Duese_1 is switched ST
LD GT AND LT ST
IL
165 06.11.2012
If the plant is switched off, all nozzles and the conveyer should be stopped. If the conveyor is stopped, the encoder should became the value 0.
LD ST ST ST ST LD ST
Load example
166
06.11.2012
System Manager
By adding a correct project (i.e. the project has been compiled in TwinCAT PLC Control without errors, and afterwards stored), the PLC configuration will be integrated into the current system configuration. The address located I/O variables will be read. By selecting the added PLC project in the tree, the appropriate dialog IEC61131-3 appears on the right side. At I/O configuration, you can configure the fieldbus cards (master) and the boxes (slave) for the given configuration.
167
06.11.2012
Master cards
Different fieldbus systems. Each fieldbus has several master cards from different manufacturers (Beckhoff FC, Siemens CP, Hilscher CIF). Several different fieldbus master cards can be used parallel.
DP Master
The FC 310X supports the PROFIBUS protocols: PROFIBUS-DP (as Master, Slave and Multi-Slave), PROFIBUS-DPV1 (as Master) PROFIBUS-MC (as Master)
168
06.11.2012
Slave modules
With the dialog Insert I/O device its possible to insert the Beckhoff slave modules. If the Profibus Slave doesnt exist in the list its possible to select Generic Profibus Box and search the profibus box in the gse file.
Each GSE file which was read by TwinCAT, can bee seen in the list ( with the name of the producer).
169
06.11.2012
Beckhoff DP-Slave
General DP-Slave
xN
170
06.11.2012
Load example
Graphical elements in LD
contact
coil
171
06.11.2012
context menue
172
06.11.2012
Identifier
Load example
Operator
174
06.11.2012
Context menue
175
06.11.2012
List of the operators (AND, OR, GE, ADD, ..) implemented by the system.
Dependent by the the cursor position the following language elements can be inserted.
The program can be expand to the left and the right side.
176
06.11.2012