Generate Structured Text Code for Interactively Created Tunable Parameters
This example shows how to create tunable parameters by using the Simulink® canvas. Generate structured text code that contains the tunable parameters. 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 should be triggered.
To see how to generate structured text code for programmatically created tunable parameters, see Generate Structured Text Code for Programmatically Created Tunable Parameters.
Configure Block Parameters by Using Model Parameter Configuration Dialog Box
Open the example model.
mdl = "plcdemo_tunable_params";
open_system(mdl);
Select the
SimpleSubsystem
subsystem block. Open the PLC Coder app.Click Settings and navigate to PLC Code Generation > Optimization. Set the Default Parameter Behavior to
Inlined
.Click Configure to open the Model Parameter Configuration dialog box. Set Source list to R
eferenced workspace variables
. Select all the variables that you want to convert to tunable parameters and click Add to table.By default, thee Storage class is
SimulinkGlobal
. Change Storage class forK1
toModel default
, andK2
andK3
to ExportedGlobal
. Set the Storage type qualifier forK3
toconst
.Click OK to close the dialog box and save changes to the configuration settings.
This image shows the Model Parameter Configuration dialog box with the desired parameter settings.
Generate and Inspect PLC Code
To generate structured text code:
In the Apps tab, click PLC Coder.
In the PLC Coder tab, click Settings > PLC Code Generation Settings. Change the Target IDE to
3S CoDeSys 2.3
. Click OK.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/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.