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.

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.

Bundling of several models in one TwinCAT driver 1:

Advantages of bundling in one driver:

Notice

Simulink CoderTM 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);

Sample code in MATLAB®

Bundling of several models in one TwinCAT driver 2:

Open the appropriate sample with: TwinCAT.ModuleGenerator.Samples.Start('Combine_Modules')