TwinCAT & virtualization

Most people who have developed software have at some point or another used virtualization technology. Software development for PLCs in a virtual environment is often overlooked, since PLC development is so close to the hardware. Nevertheless, there are still advantages. Working for several projects with various requirements, but where a Beckhoff PLC/TwinCAT was the common delimiter, made me ask myself “How much use of virtualization can I do for TwinCAT software development?”

There can be many reasons for wanting to use a virtual machine while doing TwinCAT development, a few of them being:

  • Doing a temporary test of a certain functionality without messing up the main PC
  • Have different toolchains for development
  • Developing software for different operating systems
  • General prototyping

During a project a requirement came up in where I needed to develop some software that was supposed to run on a Linux machine, but communicating to a TwinCAT runtime.  What I needed was to run a client on a Linux machine, talking to a TwinCAT3 PLC through the ADS protocol. At this point I had no access to any physical PLC, which meant I needed to run the TwinCAT code on my Windows 10 developer machine. I used VirtualBox, which is a free open-source hypervisor to virtualize x86 computers. For Linux, I had a virtual instance of it in VirtualBox. The problem is, if you try to run a virtual machine and at the same time try to put the TwinCAT3 runtime in “RUN”-mode, you get a warning:

TwinCAT & VirtualBox warning

TwinCAT might fail if I press Continue? Let’s try anyway…

…and my computer crashed. Something needed to be done. The above suggests two solutions:

  • Don’t run any VirtualBox machines when TwinCAT is in run mode at the host machine
  • Active a solution using only isolated cores

I didn’t like the first suggestion, which is just another way of saying “just give up”. The other suggestion was however good. This warning message suggests that the problem occurs when you’re trying to run a TwinCAT task on a CPU-core that is also used on the virtual machine. If we could make sure to remove one (or more) CPU-cores from the host operating system, thus allowing only TwinCAT to have access to it. If the host OS did not have access to the CPU core(s), then neither would any virtual machines running in the host have it and all should be fine. Theory is one thing, time to do some experiments.

My development machine is running a Intel Core i7-8550U CPU, and according to the datasheet it has four physical CPU cores (eight running with Hyper-Threading). Doing a “Read from Target” in TwinCAT3 on my machine confirmed this:

“Read from Target” on developer machine

Now the trick here is that with the current configuration, all these logical CPUs are accessible for both TwinCAT and Windows. Because they are accesible to both Windows and TwinCAT they need to be shared (through some magic done in TwinCAT). What we need to make sure is that TwinCAT has 100% access to at least one CPU-core. Now I should mention that TwinCAT generally is not too fond of having hyperthreading enabled, so I would suggest to disable it (possible in the BIOS/UEFI of most computers), though I for this example will not do so. If hyperthreading would be disabled the above would show just four rows, one for each physical core. Change the above settings so that we have 1 isolated core and 7 cores for Windows like this:

Isolated core in TwinCAT

After doing this, TwinCAT is going to ask you to reboot the machine. After rebooting you might need to run the batch-file C:\TwinCAT\3.1\System\win8settick.bat in a command prompt (as administrator), and reboot again. After the reboot is done, you can see that Windows only “sees” seven of the eight logical CPUs:

Seven cores (sorry for the Swedish language)

Now, enabling configuration in TwinCAT3 and starting a Linux machine (specifically CentOS 7.5), it works!

TwinCAT & Virtual Linux

So apparently it is possible to run TwinCAT on a hyperthreading CPU. This is great stuff! Even better, thanks to the bridged networking functionality in VirtualBox, I can have the Linux machine do network communications with the TwinCAT runtime on the Windows host. For instance, it’s possible to compile libraries for Linux by using Beckhoffs ADS protocol. Now TwinCAT (The WINdows Control and Automation Technology) got a little bit flavoured with the sweet taste of Linux.

All of this is amazing, but I want more. Is it possible to run a virtual Windows machine running TwinCAT3? Probably not, as TwinCAT most likely needs direct access to the hardware and not through any abstraction layers provided by a hypervisor as VirtualBox. But for experiment, I created a Windows 10 x64 Pro machine and installed the latest TwinCAT 3.1.4022.20 XAE (development environment + runtime). I allocated 4 logical CPUs to the Win10-machine in VirtualBox, and verified that this was what TwinCAT was seeing as well.

Now I tried to start the program with TwinCAT using one of the CPUs that are shared with Windows:

TwinCAT3 not working in VirtualBox

This was expected. But TwinCAT says there is a solution. TwinCAT suggests to activate it on an isolated core. We could again isolate a core as I showed you above, but because we are software engineers we want to have at least two ways to do one thing, just in case we forget one of them. Another way to remove a logical CPU from Windows so that TwinCAT can have it all for itself is by using a built-in way in Windows. Press WindowsKey+R, enter msconfig and click OK. This will give you the System configuration tool. Go to Boot, and click Advanced options. Tick Number of processors and select one less than the virtual machine has. So in this example the virtual machine has four, select three.

System configuration in Windows 10

Do a reboot. Again, verify both in the task manager and doing a read from target in TwinCAT3. This is exactly how I want it to look. Just as with the previous example, we have an isolated core just for the TwinCAT3 program in the virtual machine.

Three windows cores + one isolated core

And finally, write a quick little structured-text program that only increments a number, activate configuration and put the system in RUN:

TwinCAT running in VirtualBox

it works!

Some final thoughts. Even though this worked, I’m still expecting some issues. I haven’t done any deep analysis, but the real-time properties (determinism/jitter) were far from optimal. The jitter for instance topped at a whopping 2000µs+ (i.e. more than 2ms). Even though my program only consisted of a simple counter running all by itself without any fieldbuses or external communication,  making it useless for critical tasks (especially involving fast fieldbuses as EtherCAT). Though this is of course less than optimal, for most of my purposes it doesn’t matter! If I want high-determinstic/low jitter performance I’d run this on a dedicated PLC optimized for this very purpose. But being able to virtualize a TwinCAT runtime gives me a possibility to do certain tests that would require a large amount of physical PLCs. This is a huge benefit with running PC-based control in generic and TwinCAT in specific. The possibilites to run many instances of a real-time controller (even only for doing certain tests), mixed with standard user-space programs are huge, and is definitely something I will elaborate further on. Beckhoff should without any doubt emphasize this possibility more!

Also, I have not tested what combinations of hardware and versions of TwinCAT/Windows this works with. This might or might not work with your specific configuration of the hardware and software. For reference I used:

  • Lenovo ThinkPad X1 carbon (gen6) with Intel Core i7-8550U CPU, BIOS 1.22
  • TwinCAT 3.1.4022.20
  • VirtualBox 5.2.12
  • Windows 10 Pro version 1803 (in both host and virtual machine)
  • CentOS 7.5.1804

As a grand finale I started a TwinCAT3 runtime/program on my (real) Windows host, and simultaneously I started another TwinCAT3 runtime/program as a virtual machine, and simultaneously with this I started the CentOS linux machine. Now I had two TwinCAT machines (on the same physical machine, one real and one virtual) each with their own separate AMS NetID. On the CentOS linux machine I downloaded the ADS-client sources from Beckhoff GitHub, compiled them and wrote a small little C++ test-program that retrieved a PLC-variable from the virtual ADS-host, and the result being:

Windows and Linux VMs communicating through ADS

This is PC-based control at its best!

  • Share on:

8 Comments, RSS

  1. Yannick Asselin 2018-06-18 @ 13:38

    Very interesting. As a side not, here’s an email I have received from Beckhoff regarding an issue with version 4022.20:

    Dear TwinCAT 3 users,

    as we have seen, you have downloaded TwinCAT 3.1 Build 4022.20. We need to inform you, that we have recognized problems
    (affecting even other software installations), which could occur during the uninstalling of this build.

    This concerns all TwinCAT installations (Full and XAR). We are currently building a new version.
    The current problem will be solved by simply installing this new version.

    Please do not hesitate to contact our worldwide support in case of any questions.

    We apologize for the inconvenience.

    TwinCAT Product Management Team

  2. sagfak 2018-06-22 @ 20:37

    thank you .thank you so much .

  3. Steven 2018-07-02 @ 20:07

    first, thank’s for your great articles about TwinCAT… !

    one question, i planed to buy a new notebook (actual T470s, reason is the
    ethernet Port)
    i read you use an X1 Carbon 6th. gen. with no real eth port,
    (i read with an Adapter) Is this port working well with EtherCAT ?

    thanks a lot, steven

    • Jakob Sagatowski 2018-07-02 @ 21:42

      Steven,
      thanks for your feedback!

      Actually, the 6th gen X1 carbon DOES have an Ethernet adapter NIC (Intel I219-V) integrated with an Ethernet port. However, it does not have an RJ45-port but a proprietary one, so you need to connect a dongle to the computer that converts the proprietary connector on the X1 to an RJ45. This dongle only converts the physical port of the X1, the NIC is however integrated in the computer.

      To know whether your computer/NIC is capable of running EtherCAT you can find it out in two ways.
      Alternative 1: Find out EXACTLY which NIC the T470s is using. This can be found in the device manager, right click on the NIC, select details and “Hardware Ids”. The information that you need is this string that includes VEN/DEV/SUBSYS (it’s not enought to just say Intel I219-V as there are variants of this). Send this information to your local Beckhoff support and ask if the EtherCAT-driver supports that NIC.

      Alternative 2: If you have a T470s available, install TwinCAT XAE on it, start visual studio and go to the toolbar -> “TwinCAT” -> “Show Realtime Ethernet Compatible Devices…”. If you NIC is listed under “Compatible devices” it will work with EtherCAT, otherwise not. If it’s compatible, you can install the EtherCAT-driver for it. Remember that you however most likely will not get the same real-time performance as from a Beckhoff PLC, thought I honestly have never compared the EtherCAT-performance between a say CX5140 PLC with my laptop.

      Good luck!

  4. Alex Wallace 2018-07-02 @ 23:08

    This is really great!

    Did you try running an ethercat simulator in either the VM or the host TC? I wonder if you could do that or if ethercat would complain too much.

    • Jakob Sagatowski 2018-07-03 @ 19:49

      Hi Alex!

      I’ve personally not tried to run an EtherCAT simulator inside a virtual machine, but there are people that on the Linkedin TwinCAT-group have reported that it works (although with poor realtime performance). As a quick test I created a virtual machine, installed latest TwinCAT (3.1.4022.22) on it, selected the NIC on the VM to be “Intel Pro/1000 MT Desktop (82540EM)”, and TwinCAT says it is capable of running the EtherCAT-driver! So it’s definitely worth a try!

  5. Wolfgang 2018-07-16 @ 08:01

    Very nice article!

    I had a problem with virtualization from TwinCAT 4020. Back in TwinCAT 4018 we did it with VMWare without any problems. We had a 32 Bit Windows 7 as PlC and even the corresponding Server was an VMWare. In this way we could simulate our real machine. The only sideeffect was that the PLC sometimes crashed when we started the process where many simulated axis were moving, because there were timing violations.

    Thank You for this great article!

  6. Kevin van der Maarel 2018-07-30 @ 12:38

    Thank you for this post. I always have the need to develop and test offline without a physical PLC but always ran into the roadblock of running the runtime in a virtual environment (VMWare). This solution worked well and will definitely save me some time.

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.