Bundling of several models in one TwinCAT driver
Automatically generated code from Simulink® models and MATLAB® functions can be bundled into a single C++ project. After the build process, all bundled objects are then available in one driver.
Sample code in MATLAB®
![]() | Open the appropriate sample with: |
Advantages of bundling
When a PLC library is created, all created objects are then listed as a function block (FB) in this library. Although only one driver and one tmc file are created, all modules can still be instantiated individually at System > TcCOM, i.e. from the user's point of view in TwinCAT XAE nothing changes with regard to the use of the TcCOM objects. Using the PLC library increases the clarity.

Advantages of bundling in one driver:
- The number of files in the repository directory is significantly reduced. This also means that fewer files have to be copied to other engineering systems in order to make a large number of modules available on engineering systems.
- The management of different versions is simplified, as interacting modules can be exchanged in a bundle, so that no version conflicts arise.
Notice | |
Simulink® Coder™ does not support namespaces If data types or functions with the same naming are defined in multiple models, the build process fails because the definitions are in the same namespace. |
Procedure
- 1. Disable “Run the publish step after project generation” in Simulink®. This will abort after the code generation and the created C++ project will not be compiled.
- 2. When bundling multiple modules, simply use the generated folders <modelname>_tcgrt, which are placed in the current MATLAB® path.
- 3. Load, bundle and configure export configurations (<modelname>_tcgrt) in a project using ModuleGenerator.
- 4. Create an export project.
In the following, this is exemplified by the bundling of 2 export configurations:
% find the code artifacts in the existing code generation directories
controllerBuildDir = fullfile(pwd,'TempCtrl_tcgrt');
ctrlsystemBuildDir = fullfile(pwd,'TempCtrlSysPT2_tcgrt');
% load existing export configurations
controllerCfg = TwinCAT.ModuleGenerator.ProjectExportConfig.Load(controllerBuildDir);
ctrlsystemCfg = TwinCAT.ModuleGenerator.ProjectExportConfig.Load(ctrlsystemBuildDir);
% create a new project export configuration
combinedCfg = TwinCAT.ModuleGenerator.ProjectExportConfig('FullPath',fullfile(pwd,'TempCtrlLib','TempCtrlLib.vcxproj'));
% add the loaded class export configurations to the new project configuration
combinedCfg.AddClassExportConfig(controllerCfg.ClassExportCfg{1});
combinedCfg.AddClassExportConfig(ctrlsystemCfg.ClassExportCfg{1});
% ...
% additinal class export configurations can be added here, loaded from
% - Simulink code generation directories (as described)
% - MATLAB code generation directories (from MATLAB Coder with TE1401) in the same way
% turn on generation and installation of the PLC library
combinedCfg.Project.GeneratePlcLibrary = true; % generate a PLC Lib true/false
combinedCfg.Project.InstallPlcLibrary = true; % install the PLC lib on local system true/false
% instantiate and run the project exporter
TwinCAT.ModuleGenerator.ProjectExporter(combinedCfg);