Main Content

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

  1. Select the SimpleSubsystem subsystem block. Open the PLC Coder app.

  2. Click Settings and navigate to PLC Code Generation > Optimization. Set the Default Parameter Behavior to Inlined.

  3. Click Configure to open the Model Parameter Configuration dialog box. Set Source list to Referenced workspace variables. Select all the variables that you want to convert to tunable parameters and click Add to table.

  4. By default, thee Storage class is SimulinkGlobal. Change Storage class for K1 to Model default, and K2 and K3 to ExportedGlobal. Set the Storage type qualifier for K3 to const.

  5. 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:

  1. In the Apps tab, click PLC Coder.

  2. In the PLC Coder 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/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