Products & Services Solutions Academia Support User Community Company

Learn more about SimMechanics   

Generating Code

About Code Generation from SimMechanics Models

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.

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 models to code and run them on a target PCReal-Time Workshop and xPC Target software
Generate blocks representing a Simulink models or subsystemsS-function Target*
Generate code for designated models or subsystemsModel 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.

How SimMechanics Code Generation Differs from Simulink

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.

Limited Set of SimMechanics Tunable Parameters

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.

Using Run-Time Parameters in Generated Code

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.

Changing Run-Time Parameters

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,

  1. 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.

  2. Make the necessary modifications to the values in the data structure instance.

  3. Use rt_machine_parameters_to_vector_modelname_uniqueid to reconstruct the vectorized parameters from the data structure instance.

  4. Recompile your generated code.

Example: Changing a Block Parameter

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);
}
  


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