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:
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.
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'.
Open Project:
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:
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.
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:
In the upper blue beam the project name Maschine.pro is located.
Among them there´s the command menu
and the toolbar.
The lowest grey beam contains the status line.
The dialog window divides into three individual windows, it contains the object list, the variable declaration and the program representation.
Object list:
TwinCAT distinguishes three kinds of basic objects in a project:
· (Program) Blocks
· Data Types
· Resources
Variable declaration:
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:
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.
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.
If the PLC runtime is not on the local computer, you must first establish a connection to the desired target system (see TwinCAT documentation).
Logging in:
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:
Starting PLC program:
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).
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.
Ending the program:
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:
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.
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.
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.
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:
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:
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.