|On this page…|
You can use SimDriveline™ 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 SimDriveline models.
Generated code versions of SimDriveline models typically require fixed-step Simulink solvers, which are discussed previously in Improve Performance. Some SimDriveline features are restricted when you generate code from a model. See Limitations in this chapter.
Note: Code generated from SimDriveline models is intended for rapid prototyping and hardware-in-the-loop applications. It is not intended for generating production code in embedded controller applications.
SimDriveline software shares most of the same code generation features as the Simscape™ product. This section describes code generation features specific to SimDriveline software. Consult the Simscape documentation for general information on code generation and Physical Modeling.
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 Task||Component or Feature|
|Link existing code written in C or other supported languages to Simulink models||Simulink S-functions to generate customized blocks|
|Speed up Simulink simulations||Accelerator mode|
Rapid Accelerator mode
|Generate stand-alone fixed-step code from Simulink models||Simulink Coder software|
|Generate stand-alone variable-step code from Simulink models||Simulink Coder Rapid Simulation Target (RSim)|
|Convert Simulink model to code and compile and run it on a target PC||Simulink Coder and Simulink Real-Time software|
|Generate a block representing a Simulink model||S-function Target*|
|Generate code for designated models or subsystems||Model 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.
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.
The major difference between Simscape and SimDriveline code generation is that some SimDriveline blocks do support a limited set of tunable parameters. Consult Run-Time Parameters in Generated Code and Most SimDriveline Parameters Not Tunable.
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.
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.
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 Simulink Coder rsimgetrtp function. For full information on rsimgetrtp, enter help rsimgetrtp at the command line and consult Run Rapid Simulations.
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.
Change the values of the MATLAB workspace variables upon which your run-time parameter values depend.
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.
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