Code Generation Setup

Before You Begin

Simscape™ Multibody™ software supports code generation for fast simulation in the Simulink® environment or for model deployment onto external targets. If your goal is to obtain standalone C/C++ code for real-time simulation on an external target, you must have an active Simulink Coder™ installation.

Solver Selection

Simscape Multibody models have continuous states and require a continuous or hybrid Simulink solver. You can change solvers from the Solver pane of the Model Configuration Parameters window. Select any solver but that marked discrete (no continuous states). Consider the ODE1 fixed-step solver if you need to approximate the behavior of a discrete solver.

Target Selection

The choice of code generation target depends on the Simulink solver used. If you select a variable-step solver, you must set rsim.tlc as the system target file. You can specify the system target file from the Model Configuration Parameters window. Look for the System target file parameter in the Target selection area of the Code Generation pane.

Run-Time Parameters

You can configure most numerical block parameters as Compile-time (default) or Run-time using a drop-down list that appears beside configurable parameters. The figure shows the run-time drop-down list in a Solid block dialog box. All parameters are by default Compile-time. The drop-down list is disabled when the model is in Fast Restart mode.

Compile-time parameters update in value when you recompile the model. Leave parameters as Compile-time when performing tasks that rely on inlined parameters such as model optimization. Run-time parameters update in value without the need for extra compilations. Set parameters to Run-time when tuning their values in Fast Restart mode or when simulating models that rely at least in part on generated C code.

To set a parameter as Run-time from the block dialog box, you must configure your Simscape preferences. Open the MATLAB® Preferences window, select the Simscape node, and check the Show run-time parameter settings check box. Parameters without a run-time option or with a run-time option that is inactive (i.e., “grayed out”) cannot be configured. The Length parameter in the figure is an example.

For more information about Simscape run-time parameters, see About Simscape Run-Time Parameters (Simscape).

Compiler Optimization

You can set your C/C++ compiler to optimize generated code. Optimized code runs faster but compiles slower. Compilation can be especially slow in large models with many bodies. The choice of compiler can exacerbate the slow compilation times. With certain versions of Microsoft Visual C++, Simulink software may appear to hang as the model is compiled.

If a model takes unusually long to compile, consider switching to a different installed compiler or disabling compiler optimization for your model. The Clang compiler provides a suitable alternative to Microsoft Visual C++. You can perform both tasks from the Code Generation menu of the Model Configuration Parameters window.

To switch compilers, in the Toolchain settings area of the Code Generation menu, set the Toolchain parameter to a different compiler. To disable code optimization, set the Build configuration parameter to Faster Builds.

Was this topic helpful?