Main Content

Use Protected Models from Previous Releases to Perform SIL Testing and Generate Code

This example shows how you can use a protected model from a previous release (R2018b onwards) to:

  • Test numerical equivalence between the protected model and its previously generated code.

  • Generate new code in the current release.

The workflow in this example supports only protected models that specify these system target file types:

  • ERT

  • ERT-based

  • AUTOSAR Classic

  • GRT (Embedded Coder required)

  • GRT-based (Embedded Coder required)

Protected Model

Use the pregenerated protected model rtwdemo_crossrelease_protected_counter.slxp, which was created in R2018b.

The protected model allows the user to:

  • Open a read-only view of the model.

  • Run simulations.

  • Use generated code.

The protected model supports the ERT system target file, and the code that it contains is "Readable source code" and supports the "Model reference" code interface.

If you want to recreate the protected model with different settings, you can export the model CrossReleaseUnprotectedCounter from the current release into a format that can be used in MATLAB R2018b. In the current release, run these commands:

load_system('CrossReleaseUnprotectedCounter');
Simulink.exportToVersion('CrossReleaseUnprotectedCounter', ...
    'rtwdemo_crossrelease_protected_counter.slx', ...
    'R2018b');
close_system('CrossReleaseUnprotectedCounter', 0);

The protected model was created in MATLAB R2018b by running this command:

Simulink.ModelReference.protect('rtwdemo_crossrelease_protected_counter', ...
    'Mode', 'CodeGeneration', ...
    'OutputFormat', 'MinimalCode', ...
    'ObfuscateCode', false, ...
    'WebView', true, ...
    'Report', true)

Open Top Model

Open CrossReleaseProtectedModel, the top model that references the protected model.

model = 'CrossReleaseProtectedModel';
open_system(model);

Open Protected Model Report

To open the protected model report, on one of the Model blocks, right- click the protected-model badge icon and then click "Display report". Or, open the report from the command line.

protectedModel = 'rtwdemo_crossrelease_protected_counter';
Simulink.ModelReference.ProtectedModel.displayReport(protectedModel);

The report shows that the protected model was generated by using Simulink Coder 9.0 (R2018b).

Open Read-Only View

To open a read-only view of the protected model, right-click the protected-model badge icon and then click "Show web view". Or, open the read-only view from the command line.

Simulink.ModelReference.ProtectedModel.displayWebview(protectedModel);

Merge Shared Utility Code

When you integrate code from multiple releases, you can specify the shared utility code that the final build uses.

Specify the shared code folder for the top model.

mkdir('sharedCode');
set_param(model, 'ExistingSharedCode', fullfile(pwd, 'sharedCode'));

Use sharedCodeUpdate to merge protected model shared utility files with the shared code folder content.

Simulink.ModelReference.ProtectedModel.CurrentTarget.clear(protectedModel);
sharedCodeUpdate('rtwdemo_crossrelease_protected_counter.slxp', 'sharedCode', 'Interactive', false);
The following files will be copied from rtwdemo_crossrelease_protected_counter.slxp:slprj/ert/_sharedutils to sharedCode/R2018b:

    rtwtypes.h

Files copied from rtwdemo_crossrelease_protected_counter.slxp:slprj/ert/_sharedutils to sharedCode/R2018b.

Test the Code in the Protected model

The top model references two instances of the protected model. The Simulation mode Model block parameter for "CounterA" and "CounterB" are Software-in-the-loop (SIL) and Accelerator respectively. When you run a top-model simulation, "CounterA" runs generated code from the protected model while "CounterB" runs the protected model.

simOut = sim(model);
Simulink.sdi.view;
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Starting SIL simulation for component: rtwdemo_crossrelease_protected_counter
### Application stopped
### Stopping SIL simulation for component: rtwdemo_crossrelease_protected_counter

Compare Outputs from Both Model Blocks

To view the simulation output and compare the values of the signals from the Model blocks, use the Simulation Data Inspector.

simulationRunObj =  Simulink.sdi.getCurrentSimulationRun(model);
counterASignal = getSignalsByName(simulationRunObj, 'counterA');
counterBSignal = getSignalsByName(simulationRunObj, 'counterB');
Simulink.sdi.compareSignals(counterASignal.ID, counterBSignal.ID);
Simulink.sdi.view(Simulink.sdi.GUITabType.CompareRuns);

Generate Code from Top Model

You can generate code from the top model, referencing the code that is contained in the protected model.

slbuild(model);

See Also

Related Topics