You are on page 1of 176

TwinCAT 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 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

06.11.2012

The IEC 61131-3


IEC 61131-3

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

-3 Rules for using and implementation of PLC programming languages

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

-6 Communication via fieldbus.

-7 Fuzzy systems in the PLC

06.11.2012

Standard guide

The PLCopen contains 3 devaluation compatible compliance classes:


Base level Contains IL, ST, SFC, CFC (in preparation) a few data types, standard operators, functions, function blocks as well as local variables Data exchange format (8 bit ASCII). Data types with 32 bit strings, Arrays and all functions and operators based on this data type. Here the supreme compatibility degree must exist.

Portability level

Full compliance level

06.11.2012

Functional structure of a PLC


Power supply system Other systems Operator Programmer

MMI functions Communication function Check functions

Power supply

Signal executing function

Operating system Operating program Data

Executing function

Interface function to sensors and actuators

process

06.11.2012

Communication functions

Ethernet, RS232, Modem Other systems

Server

ADS OCX, ADS DLL, TwinCAT OPC

MMI functions Communication function Check functions ADS/AMS router

Operator Programmer

Forcen, Breakpoints, single step, System Manager, Scope View

06.11.2012

Signal executing function

Compiled PLC project, selfdefined server

Win NT, 2000, XP

Signal executing function

Operating system Operating program

Operating function

Data

PLC Server 1, PLC Server 2, PLC Server 3, PLC Server 4, NC Server, Cam Server

ADS-, I/O process image

06.11.2012

Interface function between sensors and actuators

Interface function between sensors and actuators Copy rule DP RAM <-> PA PLC

Actuators and sensors


DP/PA

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

06.11.2012

Software model

Configuration Resource Task1 Program Task2 Resource Task1 Program FB FB Task2 Program FB

Main

Program Motion FB FB

Global and direct addressed variable

access paths

10

06.11.2012

Software model Example


Example PC PLC with 1 run time und zwei Task 1 BC900 (Ethernet Controller) Configuration

Resource PC Task1 Program Task2

Resource BC9000 Task1 Program FB FB

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

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 and comments

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

16

06.11.2012

Data types

Data types describe memory locations resp. appoint their features.

Value type Data width Initial value Value range


Type (Part) STRING SINT USINT Memory value

(WORD) (2 Byte) (0) (0..65535)

-75 181 1
17

1
06.11.2012

Elementary data types

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

Key word BOOL BYTE WORD DWORD

Data width (Bit) 1 8 16 32 8 16 32 8 16 32

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

SINT INT DINT USINT UINT UDINT

18

06.11.2012

Elementary data types

Type

ANY-Type

Key word

Data width (Bit) 32 64 32 32 32 32


(80+1)*8

Initial

Value range

Slide point Long slide point Date Time of day Date time of day time Sequential characters

ANY_Real

REAL LREAL

0.0 0.0 D#1970-01-01 TOD#00:00 DT#1970-0101-00:00 T#0ms

-1.18*10-38.. 3.4*1038 -2.22*10-308.. 1.798*10308 TOD#00:00.. TOD#23:59

ANY_Date

DATE (D) TIME_OF_DAY (TOD) DATE_AND_TIME (DT)

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.

VAR strVar :STRING(3); lenVar: INT; sizeVar: INT; END_VAR

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

ASCII <-> CHR

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.

(Component of the Comlib)

22

06.11.2012

Variables declaration el. data types

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

Variables declaration el. data types


At the declaration of the variables its possible to link the name with an explicit specified address. For the mapping of the inputs and outputs to the symbolic variables, the locating of variables is essential.

Identifier
Identifier

AT
AT

Address
%I %Q %M X B W D Byte Byte Bit

Data type ;
Data type

These variables own a unique address (located)

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

25

06.11.2012

Validity range

Local variables are limited on the block, in which they were declared.

Global variables are known in each block inside a project.

Key words VAR .. END_VAR VAR_INPUT .. END_VAR VAR_IN_OUT .. END_VAR VAR_OUTPUT .. END_VAR

Key words VAR_GLOBAL .. END_VAR VAR_CONFIG .. END_VAR

26

06.11.2012

Access via the located variables

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

Overlapping in the validity range

Project Machine
VAR_GLOBAL Var1:WORD; END_VAR

PROGRAM A VAR Var1 :WORD; END_VAR LD Var1

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

31

06.11.2012

POU program organisation units

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.

The data POUs are replaced by multi-dimensional fields (ARRAYs).

32

06.11.2012

POU program organisation units

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

PROGRAM PRG Program PRG


Call by a task (TwinCAT: One programm calls another) calls : FBs, Functions, (Programs) Local variable : static, i.e. the local data are available at the next cycle. Inputs: mostly 0, but VAR_INPUT possible Outputs: mostlys 0, but VAR_OUTPUT possible Transfer by reference: VAR_IN_OUT also possible Debug: The local data are directly visible in the online mode of the PLC Control Using: Main programms, main, hand, automatic....

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

Instanz_1 Var_In :WORD; Var_Out :BYTE; Var_1:WORD; Instanz_1 X :REAL; Y :REAL;

FB
FUNCTION_BLOCK B VAR_INPUT X :REAL; END_VAR VAR _OUTPUT Y :REAL; END_VAR

Instanz_2 Var_In :WORD; Var_Out :BYTE; Var_1:WORD; Instanz_1 X :REAL; Y :REAL;

LD Var1 CAL Instanz_1

Instanz_3 X :REAL; Y :REAL;

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

Return value The name of the output is scale.

Inputs Local variables are only valid for the operating time of the function

Scale can be used as local variable inside the function(Write/Read)

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

39

06.11.2012

TwinCAT System Service

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

TwinCAT System Service

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

Assigning the computing power


Real time operation of PLC software in the classical PLC. Read inputs Write outputs Operate program Win NT & HMI Interface

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

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

Real time operation

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

Real time operation


Cyclic PLC task e.g. 10ms

10ms 0ms

20ms

30ms 40ms

Refinement: Behavior under TwinCAT base cycle 1 ms

80% TwinCAT 0ms PLC W 1ms PLC TC W

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

Real time operation

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 PLC cycle

NC cycle

SPS cycle
49

(e.g. 2ms)

Real time operation

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

Task + POUs -> Create a new project

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)

Hard SPS (BCXXX0)


51 06.11.2012

Create a new project

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

Create a new project


The task name is an identifier. Respectively the rules for the identifier obtain.

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 interval time is always a multiple of the time slice.


53 06.11.2012

PLC Control Symbol bar

File new open safe

Project Start Stop Single step Breakpoint Log in Log off

Element Cut Copy Insert Find Find next

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

Derivated data types (Variable II)

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

References (Alias Types) (Variable II)

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.

Syntax: TYPE <Identifier>:<Assignment term>; END_TYPE

Example: Ads_Net_ID TYPE Net_ID:STRING(23); END_TYPE

59

06.11.2012

Enumeration (Variable II)

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

Enumeration (Variable II)

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.

VAR WochenTag:Woche; END_VAR WochenTag:=3;

61

06.11.2012

Structure declaration (Variable II)

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

Structures Instances (Variable II)


P1 P1 K2

Name:=Mller Firstname:=Peter Age:=32 Address:=Postweg 34


P3

P3

Name:=Koschnik Firstname:=Heinz Age:=37 Address:=Domplatz 10

VAR P1, P3 : Pers_Data; END_VAR VAR_OUTPUT K2 : Pers_Data; END_VAR



63

VAR_INPUT Employees: Pers_Data;


END_VAR Name_total:=CONCAT(P3.Firstname, P3.Name) (*Heinz Koschnik*)

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 (Variable II)

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

one-dimensional two-dimensional Feld_3 three-dimensional

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

Array one-dimensional example with initialisation (Variable II)


One-dimensional

Identifier

Field

Data type

Initial value

DBZeiten :ARRAY [0..6] OF TIME:= T#1s, T#2s, T#1s, 4(T#0s);


Faktor Wert

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

Array two-dimensional example with initialisation (Variable II)

To assign for example support points, an array is well qualified.

Identifier

Field

Data type

Initial value

Supportpoint:ARRAY [0..1, 0..6] OF REAL:= 0, 1.7, 2, 4(3.33), 6, 6(1.2);


Factor Value

0 0 1 0 6

1 1.7 1.2

2 2 1.2

Access:
VAR WertAusArray : REAL; END_VAR

3.33 3.33 3.33 3.33 1.2 1.2 1.2 1.2

WertAusArray := Supportpoint[1 ,0];

67

06.11.2012

Array initialisation more clearly with comments (Variable II)

Example: Drive jobs for an axis

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

Array three-dimensional example with initialisation (Variable II)

Identifier

Array

Datatype

Initial value

Supportpoint :ARRAY [0..2, 0..1, 0..2] OF UINT:= 0,1,2,3,4,5, 10,11,12,13,14,15,

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

Exceed bounds (Variable II)

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

Check Bounds (FUN)

If you define a function in your project with the name CheckBounds, you can automatically check for outof-range errors in arrays.

FUNCTION CheckBounds :INT VAR_INPUT I,L,U : INT; END_VAR

IF I< L THEN
Error case

CheckBounds := L;

ELSIF I > U THEN


Error case i Min Max Limited value

CheckBounds := U;

ELSE
OK case

CheckBounds := I;

END_IF

72

06.11.2012

Inserting Check Bounds 1(FUN)

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

Inserting Check Bounds 2(FUN)


So that CheckBounds is recognised by translating, the following may NOT be changed: -Name and type of the inputs I,L and U -Name (CheckBounds) and return value (INT). -In the function can be edited freely. At application of own local variables (e. g. error counter, instances of FBs) is to be considered that these are only temporary (at functions). Such a variable has to be declared (in this case) under the global variables.

74

06.11.2012

Check Bounds (FUN) mode of operation


FUNCTION CheckBounds :INT VAR_INPUT I,L,U : INT; END_VAR Automatical call IF I< L THEN CheckBounds := L; ELSIF I > U THEN CheckBounds := U; Access at 2 ->ok ELSE

CheckBounds := I;
END_IF FUNCTION CheckBounds :INT VAR_INPUT I,L,U : INT; END_VAR IF I< L THEN CheckBounds := L;

Checkbounds returns 3, the access is limited to the maximum index


75

ELSIF I > U THEN

CheckBounds := U;
ELSE CheckBounds := I; END_IF 06.11.2012

Note: Further Checker functions

From TwinCAT 2.8 the following further Checker functions are possible:

Check division by 0 CheckDivByte CheckDivWord CheckDivDWord CheckDivReal

Check value range CheckRangeSigned CheckRangeUnsigned (see Appendix)

76

06.11.2012

Combination Structures and Arrays (1)


An array can consist of structures: Structure: TYPE DrillPos : STRUCT XPos: FeedrateX: AccelerationX: DeccelerationX: JerkX: YPos: FeedrateY: AcceleartionY: DeccelerationY: JerkY: FeedDrill: Kuehlen: END_STRUCT END_TYPE Declaration of the arrays :

LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; LREAL; BOOL; (*Pump ?*)

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


77 06.11.2012

Combination Structures and Arrays (1)

Access to Drillpos 55: Access: MoveXAx (*FB Instance*) ( Execute:= Position:= Velocity:= Acceleration:= Deceleration:= Jerk:= Direction:= Axis:= );

TRUE, Positions[55].XPos , Positions[55].FeedrateX Positions[55].AccelerationX, Positions[55].DeccelerationX, Positions[55].JerkX, ........., .............,

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

ST Structured Text operators in the order of their binding strength:

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

Binding strength Strongest binding

Weakest binding

80

06.11.2012

ST Structured text: Overview about Instructions

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

Is needed to branch in a program depending on conditions.

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)

Condition IF Condition THEN Instruction block; END_IF Yes

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

Condition 1 No Yes Condition 2 No Yes Condition 3 No Yes


Instruction block A Instruction block B Instruction block C

Instruction block D

85

06.11.2012

IF Instruction (4)

Condition 1 No Yes Condition 2 No Yes Condition 3 No Yes


Instruction block A Instruction block B Instruction block C

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

IF Instruction (5) 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 Call FB Instances NO FB call!

87

06.11.2012

CASE Instruction

CASE Selection criterion OF 1: 2,4,6: 7..10 : .. ELSE Default Instructions END_CASE;


Yes

Instruction 1 Instruction 2 Instruction 3

Selection criterion = 1
No Yes

Selection criterion = 2 Or 4 or 6 Selection criterion = 7 Or 8 or 9 or 10?


No Yes

No

Instruction 1

Instruction 2

Instruction 3

Default Instructions

Two same values mustnt be available at the listing.


88 06.11.2012

CASE Instruction Integer Selector Value with Enum types

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 State:Schritte; (* State:INT also possible*) END_VAR


89 06.11.2012

CASE Instruction Integer Selector Value with constants

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

CASE Instruction proposal for a Statemachine


TYPE Steps : ( INIT:=0, START, AUTOMATIC, END); END_TYPE CASE State OF INIT: Q0:=TRUE; IF Transition THEN state := START; END_IF START:
Step step enabling condition (Transition) Instruction for the step (Actions)

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.

Forced switch to Win NT

Begin of a new time slice

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.

Expression FOR WHILE REPEAT SINT/ INT / DINT BOOL BOOL

Work flow Pre repel Pre repel Post repel

n cycle fix Yes No No

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.

Start i:=Start value

i >End value No Instruction block

Yes

I:= i+ Step width

FOR i:=1 TO 12 BY 2 DO Field[i]:=i*2;(*instruction*) END_FOR


94

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

i:=0; WHILE i<100 DO Field[i]:=i*2;(*instruction*) i:=i+1; END_WHILE


95

Instruction block I:= i+ Step width

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.

Instruction block I:= i+ Step width

i:=0; REPEAT Field[i]:=i*2;(*Instruction*) i:=i+1; UNTIL i>100 END_REPEAT


96

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 :

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

97

06.11.2012

FB calls in ST explanation:
Create instance of FB

VAR TON1:TON; END_VAR

Call with instance name

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


input parameters Scan output Not possible: FB can have several outputs:

Q0:= TON1.Q

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


98 06.11.2012

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

FB calls in ST (alternative) explanation

Declaration

VAR
TRANSFER ONLY INPUT PARAMETER . This is

TON1:TON; END_VAR

NO FB CALL!!!!!

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


scan output
100

FB CALL
06.11.2012

Q0:= TON1.Q;

FC calls in ST

Result:=Scale

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

(* 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 := Scale (x:=input, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);

Result

CALL

Input parameters

(* equal:*) Result:=Scale ( x:= xug:= xog:= yug:= yog:= );


102

input, 0.0, 32767.0, 0.0, 100.0

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

SFC Sequential Function Chart

Step N Action

Transition Following step

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

Initial step active at the start Init N Action

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

Actions, several allowed per step

Transition

Step1

N P N R

bOutput STAction LDAction FBDAction

107

06.11.2012

Steps /alternative branches

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

Steps / alternative branches

Init N Action

The branches neednt be symmetrical.

Transition Stepa N Action

Transition Stepb N Action

Transition Stepc Transition N Action

Transition

109

06.11.2012

Steps /alternative branches

Branches can be skipped.


Init N Action

Transition

Stepa N Action Transition

Transition

110

06.11.2012

Steps /simultaneous branches

Init N Action Transition at the beginning of the simultaneous branch

Two branches are processed simultaneous.

Transition

Double line, symbolises the simultaneous branch N Action

Step_a N Action

Step_b

Transition

Transition at the end

111

06.11.2012

Steps /simultaneous branches

Transition

Simultaneous branches neednt be symmetrical.

Step_b N Action

Step_a N Action Step_c

Transition

Action

Transition
112 06.11.2012

Transitions

A Transition must be of type BOOL. Possibilities:


Init N Action

BOOLEAN Variable ST Instruction programmed Transition

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

The result must be of type BOOL and is the transition GT AND

A B Hides behind Possible: FBD, LD, IL, ST.

INPUT0

Points to programmed transition


114

Limitations: one network, one Instruction sequence, no FB calls.


06.11.2012

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

N: Non Stored Combination in FBD


001

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

ATTENTION: A SECOND FLOW PROCESSES!

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

Sequential Function Chart step diagnosis

VAR SFCEnableLimit: BOOL;

(*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

Sequential Function Chart step diagnosis

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

Sequential Function Chart step diagnosis

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

Sequential Function Chart step diagnosis

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

Sequential Function Chart step diagnosis

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

Sequential Function Chart step diagnosis (from 2.8)

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

Sequential Function Chart process diagnosis

Implicit variable

131

06.11.2012

Sequential Function Chart process diagnosis

set step attributes for the step to be observed

132

06.11.2012

Online (and per ADS) can be requested

133

06.11.2012

Sequential Function Chart Tipmode

insert implicit variable:

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

TRUE TRUE TRUE FALSE FALSE


134

FALSE TRUE TRUE TRUE FALSE

TRUE TRUE FALSE FALSE TRUE

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

Remanent Flags Variables Attributes

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

Retain Persistent Retain


To store

Persistent

To delete Possible for

Rebuild all, Reset unlocated, located (%M)

Reset all Unlocated, located (%I, %Q, %M)

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!

Loading the boot project into the Run-Time

Start PLC

139

06.11.2012

1&2

1 2
TwinCAT Auto boot Auto logon with Win NT

140

06.11.2012

Create a boot project

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

Timing for Sourcecode download

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

Timing/Extent for Sourcecode download

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

Plc project open from the controller

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

Were retain and persitent data loaded successfully?

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

Meaning of the flags


Bit number 0 1 Description RETAIN variables: LOADED (without error) RETAIN variables: INVALID (the back-up copy was loaded, since no valid data was present) RETAIN variables: REQUESTED (RETAIN variables should be loaded, a setting in TwinCAT System Control) Reserved PERSISTENT variables: LOADED (without error) PERSISTENT variables: INVALID (the back-up copy was loaded, since no valid data was present) Reserved Reserved

2 3 4 5 6 7

148

06.11.2012

How can an access to the bootdata flags take place?


Because the variable exists in the PLC, (implicit) it can be prompted directly. TcPlcSystem.Lib

IF

GETBIT32(inVal32:=SystemInfo.BootDataFlags , bitNo:=4) THEN errLoadBootData:=FALSE; strBootDataState:= 'PersistentData OK';

ELSIF GETBIT32(inVal32:=SystemInfo.BootDataFlags , bitNo:=5) THEN errLoadBootData:=TRUE; strBootDataState:= 'Error Load PersistentData '; END_IF
149 06.11.2012

Write Persistent Data on demand

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

Write Persitent Data on demand, Example:

IF ReqWritePersistent THEN

fbWritePersistent(NETID:='' , PORT:=801 , START:=TRUE , TMOUT:=T#500ms );


IF fbWritePersistent.ERR THEN

fbWritePersistent(START:=FALSE );
ReqWritePersistent:=FALSE; ELSIF NOT fbWritePersistent.BUSY THEN

PLC Runtime

fbWritePersistent(START:=FALSE ); ReqWritePersistent:=FALSE;
END_IF END_IF

Further start after edge

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.

Create and test project

Delete unnecessary elements of a library

Save as internal *.Lib

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

CheckRangexxx can be done with TYPES

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

Paint station (PRG)

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

Creating the variables list

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

Initial value False False

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

AT %QX0.0 AT %QX0.1 AT %QX0.2 AT %QX0.1

False False False False 400 800 1200 1600 0

Switch on / off the plant

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

Simulate encoder & check bounds

The encoder value is incremented each cycle around 1. This is implemented with the operator ADD. Thus a running encoder develop.

LD Inc ADD 1 ST Inc

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

INC Marke_1 (INC Marke_2) bDuese_1(

IL
165 06.11.2012

Safe state when plant off

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

FALSE bDuese_1 bDuese_2 bDuese_3 bMotor 0 Inc

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

Modular Structure of the Slaves modules

Beckhoff DP-Slave

General DP-Slave

xN

170

06.11.2012

Paint station (PRG)

Load example

Graphical elements in LD

Cursor in KOP is used for inserting new elements

contact

coil

171

06.11.2012

context menue

172

06.11.2012

Insert new elements

Identifier

The following elements can be inserted at the marked cursor positions


173 06.11.2012

Paint station (PRG)

Load example

Graphical elementes in FBD

Cursor in FBD is used for inserting new elements

Operator

174

06.11.2012

Context menue

175

06.11.2012

Insert new elements

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

You might also like