TC3 Preparations

Article Index

This is the first post for my TwinCAT 3.1 tutorial. These posts are intended to supplement my video series on YouTube and provide further information on the topics covered there. This post is about the preparations of the simulation system used throughout the tutorial, mainly answering the questions: what do I need? How do I install it? What do I have to configure or adapt?

And, of course, the post also tells you which steps to take in order to use the system simulation so that we have something to automate in the tutorial.

System Requirements

TwinCAT's system requirements are quite comprehensible. The IDE as such runs on nearly every PC built in the last few years. For the runtime (the part of TwinCAT which executes our control software later on), it is harder to tell, which system is required in order to achieve the desired performance. For our purposes, above explanations should also be true for the runtime since we do not have highest performance needs or complex automation code.

The 3D simulation is based on Unity, a widely spread game engine. It heavily utilizes Unity's physics simulation which is quite demanding in terms of processing power. Therefore, your PC should have a decent graphics card (e.g. a GeForce GTX680, GTX660, Radeon 6970 or better) and a modern processor like Intel's i3, i5 or i7 or an AMD FX. In the end, the simulation is more realistic the faster it can run.


Installation

Basically, we have to install three things to be able to follow the tutorial:

  1. TwinCAT, consisting of an IDE and a Runtime
  2. TwinCAT TCP/IP, since we need it later in the tutorial
  3. the simulation of our to-be-automated production plant

But one step at a time.

TwinCAT

The installation of TwinCAT 3.1 is straightforward. You just download the latest release of the TwinCAT 3.1 XAE from the Beckhoff homepage. You find it in the download-section:

TwinCAT 3 Download

You have to register using your e-mail address and a passwort in order to be able to download files from Beckhoff. After that, get yourself a coffee while the download completes.

The software comes in a *.zip file you unzip (surprise) before you execute the included setup file. Then, you just follow the instructions.

It is important to know that TwinCAT's IDE is based on Microsoft's VisualStudio. If you have some release of VisualStudio installed on your system, you can tell the setup to integrate TwinCAT into your VS installation.

If you do not have VS yet, you can decide to install the VisualStudio shell together with TwinCAT. You do not need to buy a VS license in order to use TwinCAT.


TwinCAT TCP/IP

The TwinCAT TCP/IP package contains a TCP/IP server we can use in our control software. Why do we need that? Well, our virtual production system uses RFID readers which communicate via TCP/IP. So, the server is required by the simulation to feign you such a RFID reader. And you will need the included libraries lateron to exchange data with these virtual devices.

Installation again is easy. Download the package from Beckhoff's download section under TwinCAT functions, also shown in the following screenshot:

TwinCAT TCP/IP Download

Afterwards, execute the setup. That's it.


Production system simulation

The simulation of the production line consists of three parts:

  1. A TwinCAT simulation project
  2. A 3D simulation
  3. A panel simulator

First we get this stuff onto your hard drive. Afterwards I will describe what the single parts do and how you use them. 

You can download the complete package as a *.zip file here:

After downloading, simply unzip and that's it. You will then find the following subdirectories wherever you extracted the file:

3D-Simulation: Contains the 3D simulation of the system that will serve you as an automation playground.

Panel Simulator: Contains the panel simulator which will simulate the hardware buttons and indicator lights which can normally be found on the cabinet doors.

Template Project: An empty TwinCAT project with integrated simulation you can use as a staring point.

You can put these directories wherever you want since they do not reference each other in any way. But you should have the simulation in a path that is shorter than 255 characters, e.g. directly under C:\Sim. It will be launched automatically and also in 2016 some system functions have that DOSish 255-char-limit.

Ablegen könnt ihr diese Verzeichnisse wo immer ihr wollt, sie hängen nicht voneinander ab und benötigen auch keinen speziellen Pfad, um zu funktionieren.


Further system settings

As TwinCAT is a real-time system running beneath your windows OS, there are some other things required for it to run as it should. As long as you don't want to reserve single CPU cores exclusively for TwinCAT, the system will utilize the hardware virtualization features of your CPU to use the cores for real-time.

Therefore, you should activate these features in your BIOS (for Intel CPUs these are calles VT-x or VT-d). Furthermore you should check your antivirus software. At least with AVAST you need to set some parameters that otherwise prevent the runtime from, well, running.

Open the AVAST UI and navigate to the settings menu. Under "Error Handling" deactivate the following features: 

  1. Activate hardware virtualization
  2. Activate root-kit detection on startup

In addition, I also deactivated following options, even though they should not cause trouble:

  1. Activate Avast self-protection
  2. Enable raw disk access

If you are running Windows 8, 8.1 or 10 you have to use a batch-file provided by Beckhoff to allow TwinCAT to activate the system timer functionality. You find the file unter:

C:\TwinCAT\3.1\System\win8settick.bat

Execute it with admin privileges and reboot. Done.


Simulation architecture

The simulation consists of three components. These components are linked via several interfaces and exchange data to provide you with the simulation and allow user interaction. The following graphic shows the architecture:

Simulation architecture

Real-time simulation model

The first component is the real-time simulation software that is execution in the TwinCAT runtime. It models all the components to simulate, for example pneumatic cylinders, electrical drives or RFID readers.

Because this part of the simulation is execution within the TwinCAT runtime, the simulation of these devices is deterministic. The connection to the I/Os of the control software (you write) is also done in this part. We will cover this later, but in principle, it works just the way it would in a real system.

3D-Simulation

The second component is the 3D simulation. It's purpose is the visualization of the simulated plant in the first place, so you get an idea, what the plant is doing. But the 3D simulation also uses a physics engine to simulate parts of the system directly. This is true for the material flow as well as for all physical sensors i.e. tactile, magnetic, optical, etc. Example: The signal of a proximity switch is generated because some object moves into the detection area of that particular sensor.

This has two advantages; First off, the simulation reacts to errors in the system in a realistic manner. For example, collisions of system components can be simulated. Also, the development effort for the simulation of material flow is very low and makes the simulation easily extensible. In other words: I can easily and quickly provice new content for you to work with.

Panel simulator

The last component is a small program which simulates the typical, electromechanical control panels of machinery. It provides you with a neat, graphical user interface that shows buttons and indicator lights, so you do not have to make them on your own in the control software. This also means you have some realistic way of mapping your software to that "hardware". Exactly the same way it would be in a real system.


Simulation setup

After extracting the simulation files, place them somewhere on your file system. There is no need for more installation procedurs. It just works. The only thing you have to do is entering the path to the 3D simulation's executable.

As shown in the architecture overview, the real-time simulation and your code will run in the same TwinCAT runtime, but in different tasks. All of this is already prepared in the template project. Therefore, it's a good idea to use that as a starting point for your programming.

When you open the template project, you will see two sub-projects, as shown below:

Template project structure

The first one is the real-time simulation. It consists of only one line of code that calls the actual simulation code that resides in a precompiled library.

The other one is a (nearly) empty PLC project, where you can go crazy with your coding. It only has the default MAIN program in it. This contains one string variable where the path to the 3D simulation executable is stored:

 

Path selection

Enter the path that is correct for your system and rebuild the complete solution using Build → Rebuild.


Using the simulation

To start your code and the simulation, activate the configuration via TwinCAT → Activate Configuration. Subsequently, a message box will ask you, if TwinCAT shall go into run mode. Confirm this.

Starting TwinCAT

Now all the real-time stuff is working. Afterwards, you have to load and start the two PLC projects. To do so, choose one of them (1) as shown in the picture and click Login (2).

PLC project selection

It is possible that you are asked if the according application shall be created, if it is not existing yet. Confirm this. As soon as the code has been compiled and loaded, start it's execution with Run (3).

Run command

Repeat these steps for the remaining project. When you have completed these steps for the simulation project, the 3D simulation will start automatically. Now you can start the panel simulator from the appropriate directory and start testing your code.