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.

Generating Code

About Code Generation from Simscape Multibody Models

You can use Simscape™ Multibody™ software with Simulink® Coder™ to generate stand-alone 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 Simscape Multibody models.

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

    Note   Code generated from Simscape Multibody 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 Multibody software shares most of the same code generation features as Simscape software. This section describes code generation features specific to Simscape Multibody software. Consult the Simscape documentation for general information on code generation and Physical Modeling.

Using 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 models to code and run them on a target PCSimulink Coder and Simulink Real-Time 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 Simscape Multibody models or subsystems, but not with Simscape software. Converting a Simscape Multibody subsystem to an S-function block allows you to run a model with Simulink alone.

How Simscape Multibody Code Generation Differs from Simulink

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

The major difference between Simscape and Simscape Multibody code generation is that a few Simscape Multibody blocks do support a limited set of tunable parameters. Consult the sections Using Run-Time Parameters in Generated Code and Most Tunable Parameters Not Supported by Simscape Multibody Software.

Using Run-Time Parameters in Generated Code

When Simscape Multibody 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. In addition, Simscape Multibody software generates two files that contain data structures and function prototypes for the Simscape Multibody blocks alone.

The modelname.c file contains all the run-time parameters used in the compiled simulation. modelname_data.c and the two special Simscape Multibody 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, Simscape Multibody block parameters are not associated with their original blocks. Rather, Simscape Multibody block parameters are grouped together into a single vector associated with the first Simscape Multibody S-function for each machine in the model.

The data structures and functions found in the special Simscape Multibody files, rt_mechanism_data.h and rt_mechanism_data.c, allow you to modify Simscape Multibody 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 Simscape Multibody 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 example mech_dpen. The argument p should be a pointer to the parameter vector associated with the Simscape Multibody 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);
Was this topic helpful?