Main Content

Generate Structured Text Code for Programmatically Created Tunable Parameters

This example shows how to programmaticaly create tunable parameters and generate structured text code. You create tunable parameters to:

  • Determine an optimal parameter value by tuning the parameter and monitoring the signal values during execution.

  • Set limits for process variables beyond which an alarm or safety shutdown triggers.

In Simulink®, create a Simulink.Parameter object to represent a tunable parameter. You use the Parameter object to set block parameter values, such as the Gain parameter of a Gain block. To see how to create tunable parameters interactively and generate structured text code, see Generate Structured Text Code for Interactively Created Tunable Parameters.

Configure Block Parameters by Using Parameter Object

Open the example model.

mdl ="plcdemo_tunable_params_slparamobj";

To programmatically create the parameter objects, run the setup_tunable_params file.

type setup_tunable_params;
% define tunable parameters in base workspace as Simulink.Parameter objects

%   Copyright 2011-2019 The MathWorks, Inc.

% tunable parameter mapped to local variable
K1 = Simulink.Parameter;
K1.Value = 0.1;
K1.CoderInfo.StorageClass = 'Model default';

% tunable parameter mapped to global variable
K2 = Simulink.Parameter;
K2.Value = 0.2;
K2.CoderInfo.StorageClass = 'ExportedGlobal';

% tunable parameter mapped to global const
K3 = Simulink.Parameter;
K3.Value = 0.3;
K3.CoderInfo.StorageClass = 'Custom'; 
K3.CoderInfo.CustomStorageClass = 'Const'; 

Generate and Inspect PLC Code

To generate structured text code:

  1. In the Apps tab, click PLC Coder.

  2. In the PLC Code tab, click Settings > PLC Code Generation Settings. Change the Target IDE to 3S CoDeSys 2.3. Click OK.

  3. In the top-level model, select the SimpleSubsystem block. In the PLC Code tab, click Generate PLC Code.

Alternatively, to generate structured text code from the MATLAB command line, use the plcgeneratecode function.

generatedFiles = plcgeneratecode('plcdemo_tunable_params_slparamobj/SimpleSubsystem');

After the code generation, the Diagnostic Viewer window displays hyperlinks to the generated code files. You can open the generated files by clicking on the links.

See Also

Related Examples

More About