Auto-Configuring Models for Code Generation

Overview

Traditionally, model parameters are configured manually prior to code generation. It is now possible to automate the configuration of all (or selected) model parameters at target selection time and at the beginning of the code generation process. Auto-configuration is performed initially when you use the Real-Time Workshop pane of the Configuration Parameters dialog box to select an auto-configuration target. Auto-configuration additionally is run at the 'entry' hook point of the STF_make_rtw_hook.m hook file. By automatically configuring a model in this way, you can avoid manually configuring models. This saves time and eliminates potential errors. Note that you can direct the automatic configuration process to save existing model settings before code generation and restore them afterwards, so that a user's manually chosen options are not disturbed.

Utilities for Accessing Model Configuration Properties

The Simulink® software provides two M-file utilities, set_param and get_param that you can use with the STF_make_rtw_hook.m hook file to automate the configuration of a model during the code generation process. These utilities let you configure all code-generation options relevant to the Simulink, Stateflow®, Real-Time Workshop®, and Real-Time Workshop® Embedded Coder™ products. You can assign values to model parameters, backup and restore model settings, and display information about model options.

Using set_param to Set Model Parameters

To assign an individual model parameter value with set_param, pass in the model name and a parameter name/parameter value pair, as in the following examples:

set_param('model_name', 'SolverMode', 'Auto')
set_param('model_name', 'GenerateSampleERTMain', 'on')

You can also assign multiple parameter name/parameter value pairs, as in the following example:

set_param('model_name', 'SolverMode', 'Auto', 'RTWInlineParameters', 'off')

Note that the parameter names used by the set_param function are not always the same as the model parameter labels seen on the Configuration Parameters dialog box. For a list of parameters that you can specify and their Configuration Parameters mapping, see Parameter Command-Line Information Summary in the Real-Time Workshop documentation.

Automatic Model Configuration Using ert_make_rtw_hook

As an example of automatic model configuration, consider the example hook file, ert_make_rtw_hook.m. This file invokes the function ert_auto_configuration, which in turn calls a lower level function that sets all parameters of the model using the set_param utility.

While reading this section, refer to the following files, (located in matlabroot\toolbox\rtw\targets\ecoder):

The ert_config_opt auto-configuration function is invoked first at target selection time and then again at the 'entry' stage of the build process. The following code excerpt from ert_make_rtw_hook.m shows how ert_auto_configuration is called from the 'entry' stage. At the 'exit' stage, the previous model settings are restored. Note that the ert_auto_configuration call is made within a try/catch block so that in the event of a build error, the model settings are also restored.

switch hookMethod
   case 'entry'
    % Called at start of code generation process (before anything happens.)
    % Valid arguments at this stage are hookMethod, modelName, and buildArgs.
    disp(sprintf(['\n### Starting Real-Time Workshop build procedure for ', ...
                  'model: %s'],modelName));
    
    option = LocalParseArgList(buildArgs);
    
    if ~strcmp(option,'none')
      try
        ert_unspecified_hardware(modelName);
        cs = getActiveConfigSet(modelName);
        cscopy = cs.copy;
        ert_auto_configuration(modelName,option);
        locReportDifference(cscopy, cs);
      catch
        % Error out if necessary hardware information is missing or
        % there is a problem with the configuration script.
        error(lasterr)
      end
    end
 ...
case 'exit'
    % Called at the end of the RTW build process.  All arguments are valid
    % at this stage.
    disp(['### Successful completion of Real-Time Workshop build ',...
          'procedure for model: ', modelName]);
  end

The ert_auto_configuration function takes variable input arguments, the first of which is interpreted according to the type of invocation.

ert_make_rtw_hook Limitation

The code that you specify to be executed during the build process using the ert_make_rtw_hook mechanism cannot include a cd (change directory) command. For example, you cannot use cd in 'entry' hook code to set the build directory.

Using the Auto-Configuration Utilities

To use the auto-configuration utilities during your target selection and make processes as described above:

  1. Set up the example ert_make_rtw_hook.m as your STF_make_rtw_hook file (see Customizing the Target Build Process with the STF_make_rtw Hook File).

  2. Reconfigure the set_param calls within ert_config_opt.m to suit your application needs.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS