Skip to Main Content Skip to Search
Product Documentation

Configure Data Interface

About this Tutorial

Learning Objectives

Prerequisites

Required Files

Create Data Objects for Named Data in Base Workspace

Use the Data Object Wizard to find constructs in the base workspace for which you can create data objects.

  1. Open rtwdemo_throttlecntrl_dataplacement.mdl. Save a copy as throttlecntrl_dataplacement.mdl in a writable location on your MATLAB path.

  2. In the model window, select Tools > Data Object Wizard. The Data Object Wizard dialog box opens.

  3. In the Data Object Wizard dialog box, click Find to find candidate constructs. After a few minutes, constructs fbk_1 and pos_cmd_two appear in the dialog box.

  4. Click Check All to select both constructs.

  5. Click Apply Package to apply the default Simulink package for the data objects.

  6. Click Create to create the data objects. Constructs fbk_1 and pos_cmd_two are removed from the dialog box.

  7. Close the Data Object Wizard.

  8. Open the Model Explorer, click Base Workspace, On the Contents pane, find the newly created objects fbk_1 and pos_cmd_two.

Configure Data Objects

  1. In the Model Explorer, examine the contents of the base workspace.

  2. Configure the fbk_1 and pos_cmd_two signals with the following settings.

    SignalData TypeStorage Class
    fbk_1doubleImportedExtern
    pos_cmd_twodoubleExportedGlobal

  3. Close the Model Explorer.

  4. Save and close the model.

Control Placement of Parameter and Constant Data in Generated Code

You can control which generated files contain model data definitions and declarations, for example, to adhere to company standards, by specifying files on the Code Generation > Code Placement pane of the Configuration Parameters dialog box.

For this tutorial, define and declare parameters in eval_data.c and eval_data.h Separating the data declaration and assignment simplifies the integration of the code into the production environment.

  1. Open your copy of the throttle controller model, throttlecntrl_dataplacement.mdl.

  2. In the Configuration Parameters dialog box, open Code Generation > Code Placement.

  3. Set Data definition to Data defined in a single separate source file and Data declaration to Data declared in a single separate header file.

  4. Set Data definition filename and Data declaration filename to eval_data.c and eval_data.h, respectively.

  5. Generate code for the model. When code generation is complete, eval_data.c and eval_data.h are in the model build folder.

  6. Examine the data files.

    eval_data.c

    #include "rtwtypes.h"
    #include "throttlecntrl_dataplacement_types.h"
    
    /* Const memory section */
    /* Definition for custom storage class: Const */
    const real_T I_Gain = -0.03;
    const real_T I_InErrMap[9] = { -1.0, -0.5, -0.25, -0.05, 0.0, 0.05, 0.25, 0.5,
      1.0 } ;
    
    const real_T I_OutMap[9] = { 1.0, 0.75, 0.6, 0.0, 0.0, 0.0, 0.6, 0.75, 1.0 } ;
    
    const real_T P_Gain = 0.74;
    const real_T P_InErrMap[7] = { -1.0, -0.25, -0.01, 0.0, 0.01, 0.25, 1.0 } ;
    
    const real_T P_OutMap[7] = { 1.0, 0.25, 0.0, 0.0, 0.0, 0.25, 1.0 } ;

    eval_data.h

    #ifndef RTW_HEADER_eval_data_h_
    #define RTW_HEADER_eval_data_h_
    #include "rtwtypes.h"
    #include "throttlecntrl_dataplacement_types.h"
    
    /* Const memory section */
    /* Declaration for custom storage class: Const */
    extern const real_T I_Gain;
    extern const real_T I_InErrMap[9];
    extern const real_T I_OutMap[9];
    extern const real_T P_Gain;
    extern const real_T P_InErrMap[7];
    extern const real_T P_OutMap[7];
    
    #endif                                 /* RTW_HEADER_eval_data_h_ */
  7. Save and close the code files and model.

For more information, see Manage Placement of Data Definitions and Declarations.

Include Signal Data Objects in Generated Code

  1. Open your copy of the throttle controller model, throttlecntrl_dataplacement.mdl.

  2. In the Configuration Parameters dialog box, make sure that you select Optimization > Signals and Parameters > Inline parameters.

  3. Enable signal data object pos_cmd_one to appear in the generated code.

    1. In the model window, right-click the pos_cmd_one signal line and select Signal Properties. A Signal Properties dialog box opens.

    2. In the Signal Properties dialog box, make sure that you select Signal name must resolve to a Simulink signal object.

  4. Enable signal object resolution for all signals in the model simultaneously. In the MATLAB Command Window, enter:

    disableimplicitsignalresolution('throttlecntrl_dataplacement')

    Messaging in the MATLAB Command Window indicates that the following signal objects are resolved.

    Signal...Used By...
    pos_cmd_twoPI_ctrl_2/1
    error_resetDefine_Throt_Param/Constant4/1
    max_diffDefine_Throt_Param/Constant3/1
    fail_safe_posDefine_Throt_Param/Constant/1
    fbk_1fbk_1/1

  5. Save and close the model.

Get Familiar with Simulation and Data Typing

In the throttlecntrl_dataplacement model, all data types are set to double. Because Simulink software uses the double data type for simulation, do not expect changes in the model behavior when you run the generated code. You can verify this behavior by running the test harness.

Before you run your test harness, update it to include the throttlecntrl_dataplacement model.

  1. Open throttlecntrl_dataplacement.mdl.

  2. Open rtwdemo_throttlecntrl_testharnessert.mdl. Save a copy as throttlecntrl_testharnessert.mdl.

  3. In the test harness model, right-click the Unit_Under_Test Model block and select Model Reference Parameters.

  4. Set Model name to throttlecntrl_dataplacement and click OK.

  5. Update the test harness model diagram (Edit > Update Diagram).

  6. Simulate the test harness.

    The resulting plot shows that the difference between the golden and simulated versions of the model remains zero.

  7. Save and close the test harness model.

Viewe Data Objects in Generated Code

  1. Open your copy of the throttle controller model, throttlecntrl_dataplacement.

  2. Generate code for the model.

  3. Examine the code in the generated file, throttlecntrl_dataplacement.c.

    The following statement shows a sampling of generated variables for the model before you converted the data to data objects.

    rtb_Sum3 = my_throttlecntrl_dataplacement_U.pos_rqst...
    - my_throttlecntrl_dataplacement_U.fbk_1;

    After creating data objects for signals pos_rqst and fbk_1, the same line of generated code appears is:

    rtb_Sum3 = *pos_rqst - fbk_1;
  4. Close the model.

Save Base Workspace Data

In the base workspace, save the data that exists in the base workspace for future reference. In the MATLAB Command Window, enter save. Simulink places the data in the matlab.mat file in the model build folder.

Key Points

Learn More

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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