Create Custom Code Generation Objectives - MATLAB & Simulink (original) (raw)

Main Content

The Code Generation Advisor reviews your model based on objectives that you specify. If the predefined efficiency, traceability, Safety precaution, and debugging objectives do not meet your requirements, you can create custom objectives.

To create custom objectives:

Specify Parameters in Custom Objectives

When you create a custom objective, you specify the values of configuration parameters that the Code Generation Advisor reviews. You can use the following methods:

Specify Checks in Custom Objectives

Objectives include the Check model configuration settings against code generation objectives check by default. When you create a custom objective, you specify the list of additional checks that are associated with the custom objective. You can use the following methods:

Determine Checks and Parameters in Existing Objectives

When you base a new objective on an existing objective, you can determine what checks and parameters the existing objective contains. The Code Generation Advisor contains the list of checks in each objective.

For example, the Efficiency objective includes checks that you can see in the Code Generation Advisor.

  1. Open the EmbeddedCoderIntro model.
    openExample('EmbeddedCoderIntro')
  2. Specify an ERT-based target.
  3. On C Code tab, clickC/C++ Code Advisor.
  4. In the System Selector window, select the model or subsystem that you want to review, and then click OK.
  5. In the Code Generation Advisor, on theCode Generation Objectives pane, select the code generation objectives. As you select objectives, on the left pane, the Code Generation Advisor updates the list of checks it runs on your model. For this example, selectExecution efficiency. In Available objectives, double-click Execution efficiency. Execution efficiency is added toSelected objectives - prioritized.

In the left pane, the Code Generation Advisor lists the checks for theExecution efficiency objective. The first check, Check model configuration settings against code generation objectives, lists parameters and values specified by the objective. For example, the Code Generation Advisor displays the list of parameters and the recommended values in the Execution efficiency objective. To see the list of parameters and values:

  1. Run Check model configuration settings against code generation objectives.
  2. Click Modify Parameters.
  3. Rerun the check.

In the check results, the Code Generation Advisor displays the list of parameters and recommended values for theExecution efficiency objective.

Steps to Create Custom Objectives

To create a custom objective:

  1. Create ansl_customization.m file.
    • Specify custom objectives in a singlesl_customization.m file only or the software generates an error. This issue is true even if you have more than onesl_customization.m file on your MATLAB® path.
    • Except for the_`matlabroot`_/work folder, do not place ansl_customization.m file in your root MATLAB folder or its subfolders. Otherwise, the software ignores the customizations that the file specifies.
  2. Create an sl_customization function that takes a single argument. When the software invokes the function, the value of this argument is the Simulink® customization manager. In the function:
    • To create a handle to the code generation objective, use the ObjectiveCustomizer constructor.
    • To register a callback function for the custom objectives, use the ObjectiveCustomizer.addCallbackObjFcn method.
    • To add a call to execute the callback function, use the ObjectiveCustomizer.callbackFcn method.
      For example:
      function sl_customization(cm)
      %SL_CUSTOMIZATION objective customization callback
      objCustomizer = cm.ObjectiveCustomizer;
      index = objCustomizer.addCallbackObjFcn(@addObjectives);
      objCustomizer.callbackFcn{index}();
      end
  3. Create a MATLAB callback function that:
    • Creates code generation objective objects by using the rtw.codegenObjectives.Objective constructor.
    • Adds, modifies, and removes configuration parameters for each objective by using the addParam, modifyInheritedParam, and removeInheritedParam methods.
    • Includes and excludes checks for each objective by using the addCheck, excludeCheck, and removeInheritedCheck methods.
    • Registers objectives by using the register method.
      The following example shows how to create an objective Reduce RAM Example.Reduce RAM Example includes five parameters and three checks that the Code Generation Advisor reviews.
      function addObjectives
      % Create the custom objective
      obj = rtw.codegenObjectives.Objective('ex_ram_1');
      setObjectiveName(obj, 'Reduce RAM Example');
      % Add parameters to the objective
      addParam(obj, 'DefaultParameterBehavior', 'Inlined');
      addParam(obj, 'BooleanDataType', 'on');
      addParam(obj, 'OptimizeBlockIOStorage', 'on');
      addParam(obj, 'EnhancedBackFolding', 'on');
      addParam(obj, 'BooleansAsBitfields', 'on');
      % Add additional checks to the objective
      % The Code Generation Advisor automatically includes 'Check model
      % configuration settings against code generation objectives' in every
      % objective.
      addCheck(obj, 'mathworks.design.UnconnectedLinesPorts');
      addCheck(obj, 'mathworks.design.Update');
      %Register the objective
      register(obj);
      end
      The following example shows you how to create an objective My Traceability Example based on the existing Traceability objective. The custom objective modifies, removes, and adds parameters that the Code Generation Advisor reviews. It also adds and removes checks from the Code Generation Advisor.
      function addObjectives
      % Create the custom objective from an existing objective
      obj = rtw.codegenObjectives.Objective('ex_my_trace_1', 'Traceability');
      setObjectiveName(obj, 'My Traceability Example');
      % Modify parameters in the objective
      modifyInheritedParam(obj, 'GenerateTraceReportSf', 'Off');
      removeInheritedParam(obj, 'ConditionallyExecuteInputs');
      addParam(obj, 'MatFileLogging', 'On');
      % Modify checks in the objective
      addCheck(obj, 'mathworks.codegen.SWEnvironmentSpec');
      removeInheritedCheck(obj, 'mathworks.codegen.CodeInstrumentation');
      %Register the objective
      register(obj);
      end
  4. If you previously opened the Code Generation Advisor, close the model from which you opened the Code Generation Advisor.
  5. Refresh the customization manager. At the MATLAB command line, entersl_refresh_customizations.
  6. Refresh the advisor check information cache. At the MATLAB command line, enterAdvisor.Manager.refresh_customizations().
  7. Open your model and review the new objectives.

See Also

Topics