Quick start
Starting with a simple MATLAB® function
- Feel free to use our built-in samples for first steps with the TwinCAT Target for MATLAB®. The MATLAB® Command Window provides a list of available samples via
TwinCAT.ModuleGenerator.Samples.List
- 1. First select simple samples, e.g. BaseStatistics - can also be called directly with
TwinCAT.ModuleGenerator.Samples.Start('BaseStatistics')
.
Beginner video The following video (only available in English) can also be used as an introduction: TwinCAT Target for MATLAB®. |
Getting started with the Base Statistics Sample
- Opening the Base Statistics Sample opens a MATLAB Live Script, which contains documentation parts as well as sections with code for execution.
- 1. Execute the Code Sections by clicking on the respective Run buttons.
- 2. Work your way through the sample step by step.
- The sample shows how you can use the Target for MATLAB® to convert two MATLAB® functions into two TcCOM objects and two function blocks, bundling the function blocks in a common PLC library.
If you do not have a valid TE1401 license, you can activate the TrialLicense checkbox in the sample. This converts only one MATLAB® function into a TcCOM and a function block. The sample is then compliant with the demo terms of the product.
Selection of components and paths
The General Preparation section of the sample states:
What should be the name of the created TwinCAT driver (tmx-file)?
Here Tc3_BaseStatistics is selected.
This name is then used in the following places:
- File path in the Engineering Repository:
%TwinCATInstallDir% \3.1\Repository\<TE140x Module Vendor>\Tc3_BaseStatistics\<Version>\ - Name of the created files *.tmx, *.tml, *.tmc and *.library
- Name of the created PLC library in TwinCAT, which then contains the two function blocks
Where should all source files be stored?
buildDir
specifies where the MATLAB Coder™ and also the TwinCAT Target for MATLAB® should store all source files, log files and other meta information files. This folder contains all the information required to create the TwinCAT objects from here. In this case, a new folder _ buildDir
is created in the current MATLAB® path.
Which MATLAB® functions should be made available in TwinCAT?
The MATLAB® functions are named here with the variables module1
and module2
; the two MATLAB® functions BaseStatistics and BaseStatisticsIteravtive (stored in subfolder M) are to be transferred to TwinCAT objects accordingly.
Each of these modules gets its own subfolder in buildDir
which is named cppDir1
and cppDir2
. The C++ code is later generated into these subfolders by the MATLAB CoderTM.
Creating a MATLAB Coder™ configuration
In the further course of the MATLAB® Live Script, a MATLAB CoderTM configuration is created. This section does not contain any TwinCAT-specific components, i.e. only the MATLAB Coder™ is used. For detailed MATLAB Coder™ documentation, see the MATLAB® documentation.
When creating the Coder configuration cfg
, please note:
- The Embedded Coder is not supported.
- Only the generated code is needed.
The codegen
command then receives the Coder configuration and the corresponding MATLAB® function to be translated. The argument "-d", cppDir1 instructs the MATLAB Coder™ to place the C++ code in the cppDir1 path.
Accordingly, after this step the generated C++ code of the function BaseStatistic.m and BaseStatisticIterarive.m are located in the folders _BuildDir/ BaseStatistic and _BuildDir\ BaseStatisticIterative.
Creating a Target for MATLAB® project export configuration
The following code segments in the MATLAB® Live Script apply only to the Target for MATLAB® and are independent of the MATLAB Coder™ in that only C++ code already created by the MATLAB Coder™ will be used.
Optionally, you can extract the MATLAB® code description from the m-file and display it later in TwinCAT XAE, see MATLAB code representation.
TwinCAT.ModuleGenerator.Matlab.ExportMCodeRepresentation('MFile',module1,'BuildDir',cppDir1);
The m-file with the module1 function, i.e. BaseStatistics, must be located in the MATLAB® workspace. The information is then extracted from the BaseStatistics.m file and stored in the cppDir1 folder.
In the next step, a project export configuration is created by the TwinCAT module generator with
TwinCAT.ModuleGenerator.ProjectExportConfig('FullPath',FullPathToVcxproj);
The fullpath to the new Visual Studio project to be created is specified as the argument. In this case ...\_BuildDir\Tc3_BaseStatistics.vcxproj. After the build, the naming of the Visual Studio project also defines the naming of the created files *.tmx, *.tmc, *.tml and *.library. See Quick start.
For each MATLAB® function, an export configuration must be created with TwinCAT.ModuleGenerator.Matlab.FunctionExportConfig()
. AddClassExportConfig adds this export configuration to the project export configuration as a module.
exportConfig.AddClassExportConfig(TwinCAT.ModuleGenerator.Matlab.FunctionExportConfig('MFile',module1,'BuildDir',cppDir1));
The path to the C++ code created by the MATLAB Coder™ and the name of the m-file with the corresponding MATLAB® function are passed as arguments to FunctionExportConfig(). For example, module1 is then used to set that the TcCOM object to be created and the function block in the PLC library are called BaseStatistics or FB_BaseStatistics.
The project export configuration will be further adapted in the following. This defines the platforms for which a driver is to be built (here for Windows 32-bit, Windows 64-bit and TwinCAT/BSD® 64-bit). It is also configured that a PLC library is to be created and also installed on the local TwinCAT XAE.
For each module added to the project export configuration, properties can be set individually. Here it is explicitly set that for the first added module both a PLC function block and a TcCOM are to be generated.
You can use disp(exportConfiguration)
to display an overview of the entire configuration.
This gives you an overview of the values set (Value), the data type used (DataType), suggested values (Options) and a short description (Displayname).
With TwinCAT.ModuleGenerator.ProjectExporter(exportConfig)
the build process of the configured platforms is triggered. This creates a folder on the local file system in the repository and stores the created drivers and description files.
The path description is:
%TwinCATInstallDir% \3.1\Repository\< VendorName >\<ProjectName>\<Version>\
You can copy the folder to any number of TwinCAT XAE systems, so that the modules are available there. Only the *.library must be installed in TwinCAT via the PLC library repository. Please note that the folder structure is not changed during copying.
Use PLC library in TwinCAT 3
- Starting from a new TwinCAT solution, create a PLC project:
- 1. Perform the following menu steps.
- 2. Then add the newly created (and already installed) PLC library:
- 3. Get an overview of the data types and function blocks:
- 4. Insert instances of the function blocks into your PLC and use them in your application:
Using TcCOM objects in TwinCAT 3
- 1. Insert a new TcCOM object.
- 2. Select the corresponding TcCOM object:
- 3. Create a new cyclic task of type TwinCAT Task.
- 4. Assign the newly created task to the newly created TcCOM object. To do this, go to the instance of the TcCOM object and select the Context tab.
- You can now activate the configuration. In order to connect the TcCOM object with other modules in your TwinCAT solution beforehand, you can use the process image to create mappings.
You can view the MATLAB® code on the Block Diagram tab and observe and scope values on the fly. See MATLAB code representation.