This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Generate Code

About Code Generation from Simscape Driveline Models

You can use Simscape™ Driveline™ software with Simulink® Coder™ to generate stand-alone C or C++ code from your driveline models and enhance simulation speed and portability. Certain Simulink features also make use of generated or external code. This section explains code-related tasks you can perform with your Simscape Driveline models.

Generated code versions of Simscape Driveline models typically require fixed-step Simulink solvers, which are discussed previously in Improve Performance. Some Simscape Driveline features are restricted when you generate code from a model. See Limitations in this chapter.

    Note:   Code generated from Simscape Driveline models is intended for rapid prototyping and hardware-in-the-loop applications. It is not intended for generating production code in embedded controller applications.

    Simscape Driveline software shares most of the same code generation features as the Simscape product. This section describes code generation features specific to Simscape Driveline software. Consult the Simscape documentation for general information on code generation and Physical Modeling.

Code-Related Products and Features

With Simulink, Simulink Coder, and Simulink Real-Time™ software, using several code-related technologies, you can link existing code to your models and generate code versions of your models.

Code-Related TaskComponent or Feature
Link existing code written in C or other supported languages to Simulink modelsSimulink S-functions to generate customized blocks
Speed up Simulink simulationsAccelerator mode
Rapid Accelerator mode
Generate stand-alone fixed-step code from Simulink modelsSimulink Coder software
Generate stand-alone variable-step code from Simulink modelsSimulink Coder Rapid Simulation Target (RSim)
Convert Simulink model to code and compile and run it on a target PCSimulink Coder and Simulink Real-Time software
Generate a block representing a Simulink modelS-function Target*
Generate code for designated models or subsystemsModel Reference Accelerator Mode

* S-function Target is supported with Simscape Driveline models or subsystems, but not with Simscape software. Converting a Simscape Driveline subsystem to an S-function block allows you to run a model with Simulink alone.

How Simscape Driveline Code Generation Differs from Simulink

In general, using the code generated from Simscape Driveline models is similar to using code generated from Simscape and normal Simulink models. The Simscape documentation discusses the differences between code generation in the Simulink and in Simscape.

Limited Set of Simscape Driveline Tunable Parameters

The major difference between Simscape and Simscape Driveline code generation is that some Simscape Driveline blocks do support a limited set of tunable parameters. Consult Run-Time Parameters in Generated Code and Most Simscape Driveline Parameters Not Tunable.

Run-Time Parameters in Generated Code

When Simscape Driveline software generates code for a model, it creates a set of code source and header files. This set includes modelname.c and modelname_data.c, containing all the model's run-time parameters. (For C++, these are .cpp files.) In addition, Simscape Driveline software generates two files separately that specify driveline structure and initialization procedures for the Simscape Driveline blocks alone.

The modelname.c file contains all the run-time parameters used in the compiled simulation. To find run-time parameters and change their values, you need to look in modelname_data.c. Once you change them, recompile the code to simulate with the new values.

    Caution:   By default, modelname_data.c contains comments that identify run-time parameters by the names of their parent blocks.

    If you disable comments in your code generation, you can no longer identify run-time parameters in modelname_data.c alone. You also need to examine the associated header file, modelname.h, to identify the data structure elements containing the run-time parameters. Once you identify particular parameters, you can change their values in modelname_data.c.

Changing Run-Time Parameters with the RSim Target

If you generate code from your Simscape Driveline model using the RSim target, you have an alternative way to change run-time parameters in your code by using the Simulink Coder rsimgetrtp function. For full information on rsimgetrtp, enter help rsimgetrtp at the command line.

To use this approach, the block parameters you want to change need to be entered as workspace variable names in the corresponding blocks. You control the block values of these parameters by changing the values of the workspace variables. rsimgetrtp allows you to propagate these changes from your workspace to the code generated from a model without having to regenerate or recompile the code.

  1. Change the values of the MATLAB workspace variables upon which your run-time parameter values depend.

  2. Obtain from rsimgetrtp a data structure appropriate to your model and containing the new variables. Save it as a MAT-file, for example, newrtP.mat.

  3. Enter modelname -p newrtP.mat at your operating system command line to run the generated executable with the new parameter values.

Example.  Suppose an Inertia block references a workspace variable called Iner for the value of its inertia. Originally, its value was 1. To change the value of Iner to 5 in the code generated from its model, enter at the MATLAB command line:

Iner = 5;
rtP = rsimgetrtp('modelname');
save newrtP.mat rtP;

To run the generated code executable with the new value, enter at your operating system command prompt

modelname -p newrtP.mat
Was this topic helpful?