TwinCAT 3

Startup

TwinCAT makes the development environment areas available together with Microsoft Visual Studio: after startup, the project folder explorer appears on the left in the general window area (cf. “TwinCAT System Manager” of TwinCAT 2) for communication with the electromechanical components.

After successful installation of the TwinCAT system on the PC to be used for development, TwinCAT 3 (shell) displays the following user interface after startup:

TwinCAT 3 1:
Initial TwinCAT 3 user interface

First create a new project via TwinCAT 3 2: (or under “File”→“New”→ “Project…”). In the following dialog make the corresponding entries as required (as shown in the diagram):

TwinCAT 3 3:
Create new TwinCAT project

The new project is then available in the project folder explorer:

TwinCAT 3 4:
New TwinCAT3 project in the project folder explorer

Generally, TwinCAT can be used in local or remote mode. Once the TwinCAT system including the user interface (standard) is installed on the respective PLC, TwinCAT can be used in local mode and thereby the next step is “Insert Device”.

If the intention is to address the TwinCAT runtime environment installed on a PLC as development environment remotely from another system, the target system must be made known first. Via the symbol in the menu bar:

TwinCAT 3 5:

expand the pull-down menu:

TwinCAT 3 6:

and open the following window:

TwinCAT 3 7:
Selection dialog: Choose the target system

Use “Search (Ethernet)...” to enter the target system. Thus a next dialog opens to either:

  • enter the known computer name after “Enter Host Name / IP:” (as shown in red)
  • perform a “Broadcast Search” (if the exact computer name is not known)
  • enter the known computer IP or AmsNetID.
TwinCAT 3 8:
Specify the PLC for access by the TwinCAT System Manager: selection of the target system

Once the target system has been entered, it is available for selection as follows (a password may have to be entered):

TwinCAT 3 9:

After confirmation with “OK” the target system can be accessed via the Visual Studio shell.

Adding devices

In the project folder explorer of the Visual Studio shell user interface on the left, select “Devices” within element “I/O”, then right-click to open a context menu and select “Scan” or start the action via TwinCAT 3 10: in the menu bar. The TwinCAT System Manager may first have to be set to “Config mode” via TwinCAT 3 11: or via the menu “TwinCAT” → “Restart TwinCAT (Config mode)”.

TwinCAT 3 12:
Select “Scan”

Confirm the warning message, which follows, and select “EtherCAT” in the dialog:

TwinCAT 3 13:
Automatic detection of I/O devices: selection the devices to be integrated

Confirm the message “Find new boxes”, in order to determine the terminals connected to the devices. “Free Run” enables manipulation of input and output values in “Config mode” and should also be acknowledged.

Based on the sample configuration described at the beginning of this section, the result is as follows:

TwinCAT 3 14:
Mapping of the configuration in VS shell of the TwinCAT3 environment

The whole process consists of two stages, which may be performed separately (first determine the devices, then determine the connected elements such as boxes, terminals, etc.). A scan can also be initiated by selecting “Device ...” from the context menu, which then reads the elements present in the configuration below:

TwinCAT 3 15:
Reading of individual terminals connected to a device

This functionality is useful if the actual configuration is modified at short notice.

Programming the PLC

TwinCAT PLC Control is the development environment for the creation of the controller in different program environments: TwinCAT PLC Control supports all languages described in IEC 61131-3. There are two text-based languages and three graphical languages.

  • Text-based languages
    • Instruction List (IL)
    • Structured Text (ST)
  • Graphical languages
    • Function Block Diagram (FBD)
    • Ladder Diagram (LD)
    • The Continuous Function Chart Editor (CFC)
    • Sequential Function Chart (SFC)

The following section refers to Structured Text (ST).

In order to create a programming environment, a PLC subproject is added to the project sample via the context menu of “PLC” in the project folder explorer by selecting “Add New Item….”:

TwinCAT 3 16:
Adding the programming environment in “PLC”

In the dialog that opens select “Standard PLC project” and enter “PLC_example” as project name, for example, and select a corresponding directory:

TwinCAT 3 17:
Specifying the name and directory for the PLC programming environment

The “Main” program, which already exists by selecting “Standard PLC project”, can be opened by double-clicking on “PLC_example_project” in “POUs”. The following user interface is shown for an initial project:

TwinCAT 3 18:
Initial “Main” program of the standard PLC project

To continue, sample variables and a sample program have now been created:

TwinCAT 3 19:
Sample program with variables after a compile process (without variable integration)

The control program is now created as a project folder, followed by the compile process:

TwinCAT 3 20:
Start program compilation

The following variables, identified in the ST/ PLC program with “AT%”, are then available in under “Assignments” in the project folder explorer:

TwinCAT 3 21:

Assigning variables

Via the menu of an instance - variables in the “PLC” context, use the “Modify Link…” option to open a window for selecting a suitable process object (PDO) for linking:

TwinCAT 3 22:
Creating the links between PLC variables and process objects

In the window that opens, the process object for the variable “bEL1004_Ch4” of type BOOL can be selected from the PLC configuration tree:

TwinCAT 3 23:
Selecting PDO of type BOOL

According to the default setting, certain PDO objects are now available for selection. In this sample the input of channel 4 of the EL1004 terminal is selected for linking. In contrast, the checkbox “All types” must be ticked for creating the link for the output variables, in order to allocate a set of eight separate output bits to a byte variable. The following diagram shows the whole process:

TwinCAT 3 24:
Selecting several PDOs simultaneously: activate “Continuous” and “All types”

Note that the “Continuous” checkbox was also activated. This is designed to allocate the bits contained in the byte of the variable “nEL2008_value” sequentially to all eight selected output bits of the EL2008 terminal. In this way it is possible to subsequently address all eight outputs of the terminal in the program with a byte corresponding to bit 0 for channel 1 to bit 7 for channel 8 of the PLC. A special symbol (TwinCAT 3 25:) at the yellow or red object of the variable indicates that a link exists. The links can also be checked by selecting a “Goto Link Variable” from the context menu of a variable. The object opposite, in this case the PDO, is automatically selected:

TwinCAT 3 26:
Application of a “Goto Link” variable, using “MAIN.bEL1004_Ch4” as a sample

The process of creating links can also take place in the opposite direction, i.e. starting with individual PDOs to variable. However, in this example it would then not be possible to select all output bits for the EL2008, since the terminal only makes individual digital outputs available. If a terminal has a byte, word, integer or similar PDO, it is possible to allocate this a set of bit-standardized variables (type “BOOL”). Here, too, a “Goto Link Variable” from the context menu of a PDO can be executed in the other direction, so that the respective PLC instance can then be selected.

Note on the type of variable assignment

The following type of variable assignment can only be used from TwinCAT version V3.1.4024.4 onwards and is only available for terminals with a microcontroller.

In TwinCAT it is possible to create a structure from the mapped process data of a terminal. An instance of this structure can then be created in the PLC, so it is possible to access the process data directly from the PLC without having to declare own variables.

The procedure for the EL3001 1-channel analog input terminal -10...+10 V is shown as an example.

  1. First the required process data must be selected in the “Process data” tab in TwinCAT.
  2. After that, the PLC data type must be generated in the tab “PLC” via the check box.
  3. The data type in the “Data Type” field can then be copied using the “Copy” button.
  4. TwinCAT 3 27:
    Creating a PLC data type
  5. An instance of the data structure of the copied data type must then be created in the PLC.
  6. TwinCAT 3 28:
    Instance_of_struct
  7. Then the project folder must be created. This can be done either via the key combination “CTRL + Shift + B” or via the “Build” tab in TwinCAT.
  8. The structure in the “PLC” tab of the terminal must then be linked to the created instance.
  9. TwinCAT 3 29:
    Linking the structure
  10. In the PLC the process data can then be read or written via the structure in the program code.
TwinCAT 3 30:
Reading a variable from the structure of the process data

Activation of the configuration

The allocation of PDO to PLC variables has now established the connection from the controller to the inputs and outputs of the terminals. The configuration can now be activated with TwinCAT 3 31: or via the menu under “TwinCAT” in order to transfer settings of the development environment to the runtime system. Confirm the messages “Old configurations are overwritten!” and “Restart TwinCAT system in Run mode” with “OK”. The corresponding assignments can be seen in the project folder explorer:

TwinCAT 3 32:

A few seconds later the corresponding status of the Run mode is displayed in the form of a rotating symbol TwinCAT 3 33: at the bottom right of the VS shell development environment. The PLC system can then be started as described below.

Starting the controller

Select the menu option “PLC” → “Login” or click on TwinCAT 3 34: to link the PLC with the real-time system and load the control program for execution. This results in the message No program on the controller! Should the new program be loaded?, which should be acknowledged with “Yes”. The runtime environment is ready for program start by click on symbol TwinCAT 3 35:, the “F5” key or via “PLC” in the menu selecting “Start”. The started programming environment shows the runtime values of individual variables:

TwinCAT 3 36:
TwinCAT development environment (VS shell): logged-in, after program startup

The two operator control elements for stopping TwinCAT 3 37: and logout TwinCAT 3 38: result in the required action (accordingly also for stop “Shift + F5”, or both actions can be selected via the PLC menu).