| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → SimMechanics |
| Contents | Index |
| Learn more about SimMechanics |
| On this page… |
|---|
About Code Generation from SimMechanics Models Using Code-Related Products and Features |
You can use SimMechanics software with Real-Time Workshop® to generate stand-alone C or C++ code from your mechanical models and enhance simulation speed and portability. Certain features of Simulink also make use of generated or external code. This section explains code-related tasks you can perform with your SimMechanics models.
Code versions of SimMechanics models typically require fixed-step Simulink solvers, which are discussed previously in Improving Performance. Some SimMechanics features are restricted when you translate a model into code. See Limitations in this chapter.
Note Code generated from SimMechanics models is intended for rapid prototyping and hardware-in-the-loop applications. It is not intended for use as production code in embedded controller applications. SimMechanics software shares most of the same code generation features as Simscape software. This section describes code generation features specific to SimMechanics software. Consult the Simscape documentation for general information on code generation and Physical Modeling. |
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 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 | Real-Time Workshop software |
| Generate stand-alone variable-step code from Simulink models | Real-Time Workshop Rapid Simulation Target (RSIM) |
| Convert Simulink models to code and run them on a target PC | Real-Time Workshop and xPC Target software |
| Generate blocks representing a Simulink models or subsystems | S-function Target* |
| Generate code for designated models or subsystems | Model Reference Accelerator Mode |
* S-function Target is supported with SimMechanics models or subsystems, but not with Simscape software. Converting a SimMechanics subsystem to an S-function block allows you to run a model with Simulink alone.
In general, using the code generated from SimMechanics models is similar to using code generated from Simscape and normal Simulink models. The Simscape documentation discusses the differences between code generation in Simulink and in Simscape.
The major difference between Simscape and SimMechanics code generation is that a few SimMechanics blocks do support a limited set of tunable parameters. Consult Using Run-Time Parameters in Generated Code and Most Tunable Parameters Not Supported by SimMechanics Software following, as well as the SimMechanics block reference.
When SimMechanics 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, SimMechanics software generates two files that contain data structures and function prototypes for the SimMechanics blocks alone.
The modelname.c file contains all the run-time parameters used in the compiled simulation. modelname_data.c and the two special SimMechanics files are auxiliaries to aid in locating and changing the run-time data.
As with code generated from any Simulink model without parameter inlining, you can change any run-time parameters by modifying their values in the block parameters data structure implemented in modelname_data.c. In this data structure, however, SimMechanics block parameters are not associated with their original blocks. Rather, SimMechanics block parameters are grouped together into a single vector associated with the first SimMechanics S-function for each machine in the model.
The data structures and functions found in the special SimMechanics files, rt_mechanism_data.h and rt_mechanism_data.c, allow you to modify SimMechanics block parameters in generated code. The special header file contains a data structure, MachineParameters_modelname_uniqueid, for each machine in the model, that includes a field for each block run-time parameter. To modify mechanical run-time parameters,
Use the function rt_vector_to_machine_parameters_modelname_uniqueid in the special code source file to create an instance of the machine parameters data structure from the vectorized parameters associated with the SimMechanics S-function.
Make the necessary modifications to the values in the data structure instance.
Use rt_machine_parameters_to_vector_modelname_uniqueid to reconstruct the vectorized parameters from the data structure instance.
This code listing is an example of a simple function that updates the mass of the first body in the demo mech_dpen. The argument p should be a pointer to the parameter vector associated with the SimMechanics S-function. The argument mass is the new mass for the first body. You should call this function before model initialization.
void update_mech_dpen_parameters(real_T *p, real_T mass)
{
MachineParameters_mech_dpen_752c07b6 ds;
/*
* convert parameter vector into data structure
*/
rt_vector_to_machine_parameters_mech_dpen_752c07b6(p, &ds);
/*
* change the mass of the first body in the double pendulum
*/
ds.Body.Mass = mass;
/*
* convert the data structure back to the parameter vector
*/
rt_machine_parameters_to_vector_mech_dpen_752c07b6(&ds, p);
}
![]() | Improving Performance | Limitations | ![]() |

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 |