Parameterization of the code generation via an m-file
There are two ways to parameterize the code generation via an m-file (or mlx-file):
- via model-specific Simulink® parameters with
set_param
- via an instance of the module generator
TwinCAT.ModuleGenerator
Configuration via Simulink® parameters
With set_param
you can assign specific parameters to an object in Simulink®. If you configure the module generator with set_param
, it will be stored accordingly in the Simulink® model.
To structure the configuration parameters no namespace can be used here, therefore a prefix is set to the configuration parameters. The parameter name (“Name” column in the configuration parameter table) is preceded by Project_, TcCom_
or TcPlcFb_
depending on the level.
Project_
prefix: For all parameters that are grouped in the presentation of the configuration parameters in the tabs TC General, TC Build, TC PLC Library and TC License.TcCom_
prefix: For all parameters grouped in the presentation of the configuration parameters in the tabs TC TcCom.TcPlcFb_
prefix: For all parameters grouped in the presentation of the configuration parameters in the tabs TC PlcFb.
You can also find out the exact parameter name by using the search function in the Simulink® model.
Sample
% load Simulink model
controller = load_system('TempCtrl.mdl');
% configure TwinCatGrt
set_param(controller,'SystemTargetFile','TwinCatGrt.tlc');
% set project specific parameters
set_param(controller,'TcProject_VendorName','CompanyName');
set_param(controller,'TcProject_GeneratePlcLibrary','on');
set_param(controller,'TcCom_OnlineChange','on');
set_param(controller,'TcPlcFb_MonitorExecutionTime','on');
% build the model
slbuild(controller);
% save and close the model
close_system(controller,1);
Configuration via the module generator
Parameter settings like in the sample above can also be made via the module generator. Thus, the settings do not remain in the Simulink® model, but in the instance of the module generator.
For this purpose, only the SystemTargetFile is defined for the Simulink® model and the parameter TcProject_Generate is switched off. This will only generate Code Artifacts, but no TwinCAT C++ project will be derived and accordingly not compiled. The code artifacts are stored in the current MATLAB® path in the folder <ModelName>_tcgrt.
Export of the block diagram At the level of the Simulink® model, you should also decide whether you want to export the block diagram. In the following steps you will work on the Code Artifacts and no longer on the Simulink® model. |
You can specify the Code Artifacts folder afterwards to load a ProjectExport configuration to the TwinCAT.Modulgenerator
. Here you can then make your settings and compile the project.
Sample
% load Simulink model
controller = load_system('TempCtrl.mdl');
% configure TwinCatGrt
set_param(controller,'SystemTargetFile','TwinCatGrt.tlc');
% disable generation of C++ project files for each model (suppresses build)
set_param(controller,'TcProject_Generate','off');
% create code artifacts
slbuild(controller);
% save and close the model
close_system(controller,1);
% find the code artifacts in the existing code generation directories
controllerBuildDir = fullfile(pwd,'TempCtrl_tcgrt');
% load existing export configurations
controllerCfg = TwinCAT.ModuleGenerator.ProjectExportConfig.Load(controllerBuildDir);
% show complete configuration in MATALB Command Window
controllerCfg
% set project specific parameters
controllerCfg.Project.VendorName = "Company";
controllerCfg.Project.GeneratePlcLibrary = true;
controllerCfg.ClassExportCfg{1}.TcCom.OnlineChange = true;
controllerCfg.ClassExportCfg{1}.PlcFb.MonitorExecutionTime = true;
% set generate to true
controllerCfg.Project.Generate = true;
% instantiate and run the project exporter
TwinCAT.ModuleGenerator.ProjectExporter(controllerCfg);
Application examples
The separation of the code generation process into the steps (1) creation of the Code Artifacts and (2) configuration of the module generator and creation of the TwinCAT objects, can be helpful in different scenarios:
- You do not want to save the module generator settings in each Simulink® model.
- You want to merge several Simulink® models into one project. This combines all models in one driver and in one PLC library. Sample:
TwinCAT.ModuleGenerator.Samples.Start('Combine Multiple TwinCAT Classes In Libraries')
- You are working with a Build Server or with a CI/CD system. Sample:
TwinCAT.ModuleGenerator.Samples.Start("Use Continuous Integration Principles with Code Generation")