Sample: Tc3AutomationInterface

Overview

The sample code consists of two m-files:

  • Tc3AutomationInterface.m: MATLAB® class that implements several frequently used methods.
  • Tc3AutomationInterfaceGuide.mlx: MATLAB live script that calls the MATLAB® class as an example.

Call sample with MATLAB®

The TwinCAT Tool for MATLAB® and Simulink® Setup installs the sample on your system. Call the sample with the MATLAB® Command Window: TwinCAT.ModuleGenerator.Samples.Start('AutomationInterface').

The MATLAB® script

The MATLAB® script provides a sample of how you can generate a TwinCAT solution, scan the EtherCAT master for I/Os, instantiate two TcCOM modules, link them and activate the project on a target.

In order to be able to run the script, the two TcCOMs used must be present in your publish directory %TwinCATDir%\\CustomConfig\Modules\. For this, download the Temperature Controller sample from the TE1400 | Target for MATLAB®/Simulink®. Then copy the file folder from the directory .\TE1400Sample_TemperatureController\_PrecompiledTcComModules\Actual TwinCAT versions\ into the publish directory.

Run the m-file Tc3AutomationInterface_Testbench.m. The latest Visual Studio instance available on your system is opened in the background and the TwinCAT solution is configured, saved and activated.

The MATLAB® class

The properties

All variables and interfaces belonging to the instance of the class are contained in the properties of the Tc3AutomationInterface class. Hence, several TwinCAT solutions can be built up in a MATLAB® script by generating an instance of the class for each solution. There are then no overlaps.

The constructor

function this = Tc3AutomationInterface

The constructor loads all necessary assemblies and, if successful, sets the AssembliesLoaded property to TRUE. The loaded assemblies are:

  • EnvDTE and EnvDTE80: libraries for the Visual Studio Core Automation. Necessary for the configuration of Visual Studio.
  • TCatSysManagerLib: TwinCAT Automation Interface library for the configuration of a TwinCAT solution in Visual Studio.
  • TwinCAT.Ads: ADS library, e.g. for reading and changing the XAR state.
  • System.Xml: library for parsing XML files.

Selected methods of the class

function TcComObject = CreateTcCOM(this, Modelname)

Use the MATLAB® help functions in order to view the function and the parameters of the method.

Sample: Tc3AutomationInterface 1:

A link to the Beckhoff Infosys is also offered with some methods. These refer to documentation examples from the TwinCAT Automation Interface documentation, so that you can directly view a comparison of the implementation in MATLAB®, C# and PowerShell. You can also find a link to the Beckhoff Infosys in the comment in some sections, allowing you to view the source of the information.

The CreateTcCOM method initially begins with the parsing of the <modelname>.tmc file, from which the ClassID, the task cycle time and the task priority are extracted with System.Xml. A corresponding TcCOM is then instantiated and one (or more) associated tasks generated with the Automation Interface. Finally, the task is/tasks are assigned to the TcCOM.

function ActivateOnDevice(this, AmsNetId)

TwinCAT ADS is used in order to query or change the current status of a TwinCAT runtime, e.g. config or run. In the ActivateOnDevice method the XAR is initially switched to the config mode with the specified AmsNetId and the current TwinCAT configuration is then activated and the system started. Pauses are entered between the individual steps, as this procedure may need a little time.

Static methods

Static methods are also available even without an instance of the class.

function vsVersions = GetInstalledVisualStudios

A function that detects and lists the Visual Studio installations available on the system via the Register Key entries is prepared here. The implementation is limited to VS 2010 to VS 2017.