Configure C++ Class Interface for Rate-Based Models - MATLAB & Simulink (original) (raw)

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, CppClassRateBased, 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.

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:

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.

4. Configure the other entry-point methods.

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 CppClassRateBased model, use this workflow:

Open the example model

model ='CppClassRateBased'; 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

Code Mappings – C++ Editor | coder.mapping.api.CodeMappingCPP

Topics