TwinCAT Quick Start

Sample Maschine.pro

Creation of applications using TwinCAT will be explained with reference to a sample program. This program represents a machine tool for any chosen workpieces. After you have installed TwinCAT, you will find it in the '\TwinCAT\Samples\First Steps' directory. It has the name 'Maschine.pro'.

Sketch:

Sample Maschine.pro 1:

Description

1) The conveyor belt is moved by 25 steps.

2.) The drill is moved down for 2 sec.

3.) The drill is moved up for 2 sec.

4.) Begin at step 1 again.

Start TwinCAT:

Before you are able to execute the program, you must activate the TwinCAT real time server.

Sample Maschine.pro 2:

To do this, click on the TwinCAT real time server icon and activate the "Start" command from the 'System' menu. The color of the icon changes through yellow to green, which means that the TwinCAT real time kernel is active.

Starting TwinCAT PLC Control :

Start now the programming surface from the TwinCAT PLC. Then click with the mouse 'start' -> 'programs' -> 'TwinCAT system' -> 'TwinCAT PLC control'.

Sample Maschine.pro 3:

Open Projekt:

Sample Maschine.pro 4:

A PLC project is stored in a file on the hard disk or on a diskette that bears the name of the project. To open a project, select the 'File' menu item and then the 'Open' command.

Select directory:

Sample Maschine.pro 5:

Switch to the directory specified above by clicking the symbol shown on the left in the dialog box. Double click the 'Samples' entry. Then proceed in exactly the same way with the 'First steps' entry.

Select project:

Select the 'Maschine.tpy' project by clicking the entry in the dialog box with the mouse and by then executing the 'Open' command.

Sample Maschine.pro 6:

Items of the PLC Control:

After opening Maschine.pro select the POU (programm object) MAIN and make a double click with the left mouse button. The following dialog box opens:

Sample Maschine.pro 7:

In the upper blue beam the project name Maschine.pro is located. Sample Maschine.pro 8:

Among them there´s the command menu Sample Maschine.pro 9:

and the toolbar.

Sample Maschine.pro 10:

The lowest grey beam contains the status line. Sample Maschine.pro 11:

The dialog window divides into three individual windows, it contains the object list, the variable declaration and the program representation.

Object list:

Sample Maschine.pro 12:

TwinCAT distinguishes three kinds of basic objects in a project:

· (Program) Blocks

· Data Types

· Resources

Variable declaration:

Sample Maschine.pro 13:

A PLC program stores its data in variables. Variables are comparable to flag words or data words. Before a variable can be used, it must be declared, i.e. its affiliation to a specific data type (e.g. BYTE or REAL) must be made known. Declaration also involves defining specific attributes such as battery buffering, initial values or affiliation to physical addresses. If a variable is not needed in the input or output image, i.e. only within the PLC program, the PLC programmer need not worry about the storage location of the data. This is taken care of by TwinCAT. This avoids unintentional overlapping of flag words/data words as was possible in previous systems (side effects). As in the case of variables, function blocks also have to be declared (instance). In the example, one instance each (drive, down and up) of the three functions 'DriveType', 'DownType' and 'UpType' are created. After instancing, the instances can be used and can be activated.

Program representation:

Sample Maschine.pro 14:

The actual PLC program is entered and represented in this area of TwinCAT PLC control.

Select destination system

TwinCAT provides up to 4 run time systems. Each of these run time systems is capable of executing PLC programs in IEC 61131-3 independently of the other run time systems. To use the 'Choose run time system...' command in the 'Online' menu to define which run time system you wish to run your programs on.

Sample Maschine.pro 15:

After installation of TwinCAT, only one run time system is released and so only the first run time system (Run Time 1) is displayed in this dialog. Select OK to confirm the selection.

Logging in:

Sample Maschine.pro 16:

You have now loaded the PLC program in TwinCAT PLC control and you are able to execute it. Make sure the TwinCAT real time server is active. This is recognizable by the fact that the TwinCAT real time server icon is displayed in green on the bottom right of the screen. Before you start a PLC program, you must link TwinCAT PLC control to the run time system, i.e. you must 'log in' with the control system. Execute the 'Log in' command in the 'Online' menu. As there is still no PLC program in the run time system, you get the message: No program on the controller! Rebuild All? Acknowledge the question with ' OK'.

The current status of the connection is displayed in the status line:

Sample Maschine.pro 17:

Starting PLC program:

Sample Maschine.pro 18:

You start the PLC program in the TwinCAT real time server by selecting the 'Start' command from the 'Online' menu. The word "RUN" displayed in the status line darkens. You should also see that individual steps within the sequential function chart are temporarily displayed in blue.

A step shown in blue is currently being executed, i.e. it is the active step.

Tracing the PLC program sequence:

When you activate the 'Global variables' window by clicking the Resources Tab at the bottom of the 'Object List' window, and then double clicking the 'Global Variables' object, you will see all globally declared variables. Global variables can be used jointly by all program objects (POU).

Sample Maschine.pro 19:

Besides the variables, the 'timerUp', 'timerDown' and 'devTimer' function blocks are also displayed there. A lozenge is visible before the function names. When you double click the lozenge, a tree-like display opens in which all variables of the function are displayed.

Changing the numeric representation

You can display the contents of the variables in various numeric systems. You have a choice between decimal, hexadecimal and binary. If you wish to change the display, you must select the 'Options' command in the 'Project' menu. The current setting is indicated by a tick mark before the corresponding entry.

Sample Maschine.pro 20:

Ending the program:

Sample Maschine.pro 21:

You have now loaded a PLC program into TwinCAT PLC control (IEC 61131-3 programming environment) and you have executed it on the TwinCAT PLC server (run time system). Now end the PLC program. To do this, select the 'Stop' command in the 'Online' menu.

Logging out:

Sample Maschine.pro 22:

In the next sections, we will add to the PLC program. To do this, you must log out of the TwinCAT PLC server. To do this, execute the 'Log out' command in the 'Online' menu.

Viewing program text:

This example was programmed in the various IEC 61131-3 programming languages. The main part of the program was created in the sequential function chart (SFC). It contains the steps

· Init

· Engine

· Drill

And the transitions:

· TRUE

· End

· TimerUp.Q

Viewing transitions:

The 'TRUE' transition is constantly fulfilled because the 'TRUE' key word is a system-wide constant and is permanently fulfilled. The 'Engine' step is executed unconditionally after the 'INIT' step. TimerUp.Q means that the variable Q in the Up function must be TRUE (or also 1) for this transition to be fulfilled. 'End' is a transition that contains further program text. When you double click the transition, a further window opens in which the corresponding program text is displayed.

Sample Maschine.pro 23:

In the 'End' transition, a comparison is made as to whether the 25 steps of the motor have already been reached. If this is the case, the program changes in the next cycle from the 'engine' step to the 'drill' step.

If a step or a transition contains further program text, this is indicated by a small black triangle.

Sample Maschine.pro 24:

Modifying the PLC program

Switch back to the 'MAIN' window. At this point, you should modify the PLC program in such a way that the motor's cycle speed can be modified in two stages (fast/slow) by way of a variable. Open the 'Drive Type' function block in the object list by double clicking it. Move the input cursor to the first line and enter the following text:

IF switch = TRUE THEN

When the Enter (Return) key is pressed, a dialog box appears which you must fill out as shown below.

Sample Maschine.pro 25:

When you select 'OK', the 'switch' variable is added to the variable list of MAIN. If this dialog box not opens, your auto declare is deactivated. (You can activate it by selecting autodeclaration under 'Options' 'Editor' in the menu 'Project'. ) Open 'Auto declare' in the menu 'Edit'.

Then also enter the following program lines:

devSpeed := T#10ms;
   ELSE 
devSpeed := T#25ms;
  END_IF

The window must then have the following contents:

Sample Maschine.pro 26:

If the 'switch' variable is set, the 'devSpeed' variable is set to 25 ms, or otherwise to 10 ms. The result of this is that the pulse and pause duration of the clock pulse generator in the following program lines either amount to 25 ms or 10 ms.

Saving the program:

Sample Maschine.pro 27:

Save the program by selecting the 'Save ' command in the 'File' menu.

Compiling the program:

Before a program can be transferred to the TwinCAT PLC server, it must be compiled, i.e. it must be converted from its textual or graphical representations to a form that is understandable to the control system. To do this, select the 'Rebuild all' command from the 'Project' menu item.

Starting the program:

Log in with the control system and start the PLC program. You see that the 'switch' variable is set to 'FALSE' when starting.

Modifying variable values:

You have the possibility of modifying values of variables while the PLC program is running. Open the 'Global variables' window and double click the 'switch' entry. The display changes from FALSE to TRUE and the lettering turns red. Up to this time, however, the value in the TwinCAT PLC server has not yet changed. To do this, you must execute the 'Write values' command in the 'Online' menu item. The lettering turns black again and the 'devSpeed' variable changes to 10 ms.

Tracing the program sequence:

Follow the program flow with TwinCAT Scope View.

You will find more information about TwinCAT PLC Control in the documentation on TwinCAT PLC Control in the Beckhoff Information System.