Products & Services Solutions Academia Support User Community Company

Learn more about SimDriveline   

Generating Code

About Code Generation from SimDriveline Models

You can use SimDriveline software with Real-Time Workshop 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 SimDriveline models.

Code versions of SimDriveline models typically require fixed-step Simulink solvers, which are discussed previously in Improving Performance. Some SimDriveline features are restricted when you translate a model into code. See Limitations in this chapter.

Using Code-Related Products and Features

With Simulink, Real-Time Workshop, and xPC Target 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 modelsReal-Time Workshop software
Generate stand-alone variable-step code from Simulink modelsReal-Time Workshop Rapid Simulation Target (RSim)
Convert Simulink model to code and compile and run it on a target PCReal-Time Workshop and xPC Target 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 SimDriveline models or subsystems, but not with Simscape software. Converting a SimDriveline subsystem to an S-function block allows you to run a model with Simulink alone.

How SimDriveline Code Generation Differs from Simulink

In general, using the code generated from SimDriveline 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 SimDriveline Tunable Parameters

The major difference between Simscape and SimDriveline code generation is that some SimDriveline blocks do support a limited set of tunable parameters. Consult Using Run-Time Parameters in Generated Code and Most SimDriveline Parameters Not Tunable following, as well as the SimDriveline block reference.

Using Run-Time Parameters in Generated Code

When SimDriveline 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, SimDriveline software generates two files separately that specify driveline structure and initialization procedures for the SimDriveline 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.

Changing Run-Time Parameters with the RSim Target

If you generate code from your SimDriveline model using the RSim target, you have an alternative way to change run-time parameters in your code by using the Real-Time Workshop rsimgetrtp function. For full information on rsimgetrtp, enter help rsimgetrtp at the command line and consult Running Rapid Simulations in the Real-Time Workshop User's Guide.

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
  


Related Products & Applications

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

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