Main Content

Configure C++ Class Interface for Rate-Based Models

This example shows how to configure a C++ class interface for a rate-based model. In Simulink you can create and generate code for a rate-based modeling system that enables you to control the scheduling of model components. This example shows you how to customize the generated class name, namespace, class members, and class methods for an example model. This example, rtwdemo_cppclass, simulates whether a vehicle engine is on or off depending if the ignition has been turned on.

Interface Goals and Requirements

A primary goal for configuring a C++ class interface for a rate-based model is to customize the generated code so that it easily integrates with your task scheduler. For this example, the goal is to update the name and namespace of the class to more meaningful values, distinguish the varied sample rates, and to simplify and encapsulate the remaining data and functions in the interface.

  • To better reflect that this model is an engine status model, update the class name to engine_status.

  • To prevent clashes with other symbols in a project and indicate that the generated code is from Simulink, update the class namespace to sl.

  • To enable the configuration of the name and arguments of the base-rate periodic function, configure the inports and outports of the model as public class members without a get/set method.

  • To enable the calibrate of shared values across multiple instances of the generated model class, configure the model parameters as private class members with a get/set method.

  • To simplify the interface, configure parameter arguments and internal data as private without a get/set method.

  • To make the other entry-point names more meaningful, update the names of the initialize and terminate entry-rate methods.

Interactive Example

Configure Model Class Name and Namespace

1. On the C++ Code tab, click Code Interface and select Class Name & Namespace.

2. To configure the class name, in the C++ Class Name field, update the name to engine_status.

3. To configure the class namespace, in the C++ Class Namespace field, update the name to sl.

4. Click OK.

Configure Model Data Elements as Class Members

1. Open the Code Mappings editor. On the C++ Code tab, click Code Interface and select Code Mappings.

2. Configure the data visibility. For the model element categories:

  • Set the Inports and Outports to public and None.

  • Set the Model parameters to private and Method.

  • Set the Model parameter arguments and Signals, states, and internal data to private and None.

Configure Model Functions as Class Methods

1. Open the Functions pane. In the Code Mappings editor, click the Functions tab.

2. To view a complete list of entry-point functions for your model, click the Update Diagram button.

3. Configure the periodic functions to distinguish the sample rates.

  • To configure the base-rate periodic function, in the Method Preview column, click its method preview hyperlink.

  • Configure the function name. In the Step Function Interface configuration dialog box C++ Step Function Name field, update the function name to EngineEntrypoint.

  • Open the arguments viewer. In the dialog box, select Configure arguments for Step function prototype and click Get default.

  • Configure the argument names. In the C++ Identifier Name column, click and edit the names to keyState arg_keyState, engineState arg_engineState, cycleTime arg_cycleTime.

  • Configure the argument identifiers. In the C++ Type Qualifier column, select the appropriate identifier from the drop-down options. Configure keyState arg_keyState as value, engineState arg_engineState as Pointer, and cycleTime arg_cycleTime as Pointer.

  • Change the order of the arguments. Click and drag the argument rows in the viewer to the following order: keyState arg_keyState, engineState arg_engineState, and cycleTime arg_cycleTime.

  • Verify the arguments selections. Click Validate.

  • To apply changes and exit the dialog box, click OK.

4. Configure the other entry-point methods.

  • Configure the Initialize function name. In the Method Name column, click and edit the spreadsheet to change the name to initIntegrator.

  • Configure the Terminate function name. In the Method Name column, click and edit the spreadsheet to change the name to terminateReadIntegrator.

5. Verify the method prototypes for all entry-point functions in the Method Preview column.

Generate the Interface

1. Generate code. To generate a C++ class interface, on the C++ Code tab, click Build.

2. View code. To view the generated code, on the tab click View Code. The generated code appears beside the model in the model workspace.

Programmatic Example

To programmatically configure a C++ class interface for the rtwdemo_cppclass model, use this workflow:

Open the example model

model ='rtwdemo_cppclass';
open_system(model);

% Get the C++ mapping object
cm = coder.mapping.api.get(model);

Configure Model Class Name and Namespace

% Configure the C++ class name
setClassName(cm,'engine_status');
% Configure the enclosing namespace
setClassNamespace(cm,'sl');

Configure Model Data Elements as Class Members

% Configure the inports and outport to pass external data directly to
% the base-rate periodic function in the modeled application. This
% configuration enables you to later configure the name and arguments for
% the base-rate periodic function
setData(cm, 'Inports', 'DataVisibility', 'public');
setData(cm, 'Outports', 'DataVisibility', 'public');
setData(cm, 'Inports', 'MemberAccessMethod', 'None');
setData(cm, 'Outports', 'MemberAccessMethod', 'None');

% Configure model parameters so that you can adjust calibration values
setData(cm, 'ModelParameters','DataVisibility','private');
setData(cm,'ModelParameters', 'MemberAccessMethod','Method');

% Configure Model Parameter Arguments and Iternal data as encapsulated and simple
setData(cm, 'ModelParameterArguments', 'DataVisibility', 'private');
setData(cm, 'ModelParameterArguments', 'MemberAccessMethod', 'None');
setData(cm, 'InternalData', 'DataVisibility', 'private');
setData(cm, 'InternalData', 'MemberAccessMethod', 'None');

Configure Model Functions as Class Methods

% To configure the periodic functions, use the find function to
% retrieve the periodic functions for the model
periodic_functions = find(cm, 'PeriodicFunctions');

% Configure the base-rate periodic function method name
setFunction(cm, periodic_functions, 'MethodName', 'EngineEntrypoint');

% Configure the base-rate periodic function arguments
setFunction(cm, periodic_functions,'Arguments',...
    '(const & keyState arg_keyState, * engineState arg_engineState, * cycleTime arg_cycleTime)');

% Configure the initialize and terminate function names
setFunction(cm,'Initialize','MethodName', 'initIntegrator');
setFunction(cm,'Terminate', 'MethodName', 'terminateReadIntegrator');

Generate The Interface

Build the application model to generate the C++ class interface.

See Also

|

Related Topics