| 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 the Stewart Platform Code Generation Examples For More Information About Code Generation |
Note This study requires some experience with the code generation features of Simulink. To complete it, you need to have Real-Time Workshop installed, in addition to the SimMechanics product. |
This case study leads you through a representative set of tasks related to turning a Stewart platform model into generated code. After you read the introductory sections following, proceed with the case study tasks. All code generation-related files and subdirectories are created in your current MATLAB folder.
To learn more about generating code from Simulink models, consult the documentation for Simulink and Real-Time Workshop.
To learn more about SimMechanics code generation, see Generating Code in the Running Mechanical Models chapter.
This study is based on these demo files, in addition to the initialization M-files. Copy them into an empty folder before starting each case study task.
| File | Purpose |
|---|---|
| mech_stewart_codegen | Basic model |
| mech_stewart_codegen_plant | Plant subsystem as separate model |
You use the second model file later for model reference in Model Referencing the Plant.
Open the first model by clicking the first link. Then update the model by pressing Ctrl+D at the keyboard.

The model defines two time steps, dt1 and dt2. The model initializes both to the same value, but you can make them different (see the table, Configuration Parameters for Stewart Platform Code Generation following):
dt1 defines the fixed-step solver time step.
dt2 defines the sample rate for the generation of the trajectory signals. It must be an integer multiple of dt1.
The two major parts of the model are the PID controller and the plant. You can generate code from the entire model or from only part of it. In this study, you convert the plant subsystem to code in two ways, by an S-function block and by model reference. You then convert the whole model to stand-alone code.
Run the model before continuing with code generation. You can view the Stewart platform motion by opening the Scope. You can also enable visualization. (See the SimMechanics Visualization and Import Guide.)
Caution
To convert a subsystem alone to code requires placing all of
the SimMechanics blocks (the blocks with the distinctive Physical
Modeling connector ports
|
Some of the Simulink and SimMechanics settings in mech_stewart_codegen are different from the defaults.
From the model's Format menu, check that these entries are selected:
Port/Signal Displays > Wide Nonscalar Lines
Sample Time Display > Colors
Other settings are optimized for code generation.
View the Plant subsystem parameters by right-clicking the subsystem and selecting Subsystem Parameters, then close the dialog.
Treat as atomic unit is selected.
Minimize algebraic loop occurrences is selected.
Now view the Configuration Parameters dialog by selecting it from the model's Simulation menu. View the different nodes, then close the dialog.
Solver node. The model uses a fixed-step solver. While S-function Target does not require fixed-step solvers, most Real-Time Workshop targets require fixed-step solvers.
Data Import/Export node. Time, states, and output are selected for data export.
Outputs correspond to the ports connected to the outport signals.
Top Plate Position: translation and rotation (Port 1)
Errors: difference of reference and actual top plate positions (Port 2)
Leg Forces: control forces parallel to each Stewart platform leg (Port 3)
The states represent the states of the controller and the plant. The controller has Simulink states, and the plant has SimMechanics mechanical states.
The model states are not identical to the system's independent degrees of freedom (DoFs). See Counting Degrees of Freedom in the Stewart Platform and Identifying the Simulink and Mechanical States of the Stewart Platform preceding.
Configuration Parameters for Stewart Platform Code Generation
| Node | Settings |
|---|---|
| Solver | Solver options: Type: Fixed-step Solver options: Solver: ode1 (Euler) Fixed-step size: dt1 (5e-3 seconds) |
| Data Import/Export | Time: tout States: xout Output: yout |
| Optimization | Simulation and code generation: Inline parameters selected (needed for Model Reference) |
| Model Referencing | Minimize algebraic loop occurrences selected |
| Real-Time Workshop | Target selection: System target file:ert.tlc (no
auto configuration) (Embedded Real-Time Target) Interface: Software environment: continuous time selected Interface: Verification: MAT-file logging selected |
| SimMechanics | Diagnostics: all cleared Visualization: all cleared |
Now open the Plant subsystem and its orange Machine Environment block. Check the following settings, then close the dialog.
The constraint solver is set to stabilizing, a robust choice appropriate for a fixed-step simulation of moderate computational cost. Robust singularity handling is selected.
Machine Environment Settings for Stewart Platform Code Generation
| Tab | Settings |
|---|---|
| Parameters | Linear assembly tolerance: 1e-3
m Angular assembly tolerance: 1e-2 rad |
| Constraints | Constraint solver type: Stabilizing Use robust singularity handling selected |
| Visualization | Visualize machine selected |
The S-function Target feature of Real-Time Workshop lets you generate an S-function block for a subsystem. This block points to a (non-stand-alone) auxiliary binary file that hides the original subsystem. You can then use the S-function block in multiple instances in any Simulink model, including your original one, without SimMechanics software.
Right-click on the Plant subsystem. Select Real-Time Workshop, then click Generate S-Function in the submenu.
A new window opens, Generate S-function for Subsystem: Plant, listing the workspace variables used in the subsystem. At this point, you can make ordinary Simulink parameters tunable, but you cannot tune SimMechanics parameters. See Generating Code in the Running Mechanical Models chapter.
Proceed with generating the code files by clicking Build in the tunable parameter window. Follow the generation in the command window.
Two auxiliary subdirectories are created, as well as C source and header files and a (non-stand-alone) linked binary. Each of these files has a name, Plant_sf, derived from the subsystem name.
A new Simulink model window also appears, containing the new, reusable S-function block named Plant that points to the linked binary. Rename this block to S-Function Plant.
From the original mech_stewart_codegen model window, cut the Plant subsystem. Paste it into the new, untitled window.
Save this new model, containing the S-function and subsystem blocks for future use, as mech_stewart_codegen_plant_sfunc.
Copy the S-Function Plant block from the new untitled window into the original model window. Connect the signal lines to the S-function block.

Now start the model with the new S-function block. This modified model no longer requires SimMechanics software. The performance is about the same as the original model with the subsystem.
Save the modified model for future use as mech_stewart_codegen_sfunc.
Real-Time Workshop gives you another way to generate code for a subsystem. Using the Model Reference feature, you can put the subsystem in a separate model, then replace the subsystem block in the original model with a model reference block that points to the new model holding the subsystem. For mech_stewart_codegen, the plant subsystem is contained in the model file mech_stewart_codegen_plant.
One advantage of model referencing is that it allows you to incrementally compile parts of your model, one at a time. This feature saves significant time when you generate code from large models.
Some settings in mech_stewart_codegen_plant differ from the defaults. Many differ in the same way that mech_stewart_codegen does. Here are additional settings in the Configuration Parameters of this model that differ from the defaults.
| Node | Settings |
|---|---|
| Model Referencing | Rebuild options for all referenced
models: Rebuild options: Never Rebuild options for all referenced models: Never rebuild targets diagnostic: None |
| Real-Time Workshop | Interface: Code interface: Single output/update function cleared |
To reconstruct your model for model referencing,
In mech_stewart_codegen, cut the Plant subsystem.
Keep this subsystem for future use. From the preceding part of the study, Generating an S-Function Block for the Plant, you have a model, mech_stewart_codegen_plant_sfunc, with the subsystem. If you have not already saved the subsystem here, do so now by pasting it in.
From the Simulink Ports & Subsystems library, drag and drop a Model block into mech_stewart_codegen. Rename the block Model Reference Plant.
Open the Model Reference Plant block dialog. In the Model name field, enter mech_stewart_codegen_plant. (Note the default Simulation mode is Accelerator.) Click OK.
Save the new, modified model as mech_stewart_codegen_modelref.
Stewart Platform with Control Reference Trajectory for Model Reference

In the toolbar of mech_stewart_codegen_modelref, click the Start button.
In the command window, watch the code generation for model referencing. When it finishes, the simulation starts. Watch the simulation results by opening the Scope block.
Running or updating the main model generates a code folder and a non-stand-alone (linked) binary file called mech_stewart_codegen_plant_msf from the referenced model. The model reference block in the referencing model points to this binary. You can view the model reference code generation in the command window each time you update the diagram. Once it references the external plant model, double-clicking the Model Reference Plant block opens that model.
Note In the model reference block (Model Reference Plant), this model uses the default simulation mode, Model Reference Accelerator. |
In this section, you generate a stand-alone executable from the original Stewart platform model, mech_stewart_codegen, using Real-Time Workshop and the Embedded Real-Time target. This executable is portable and independent of MATLAB.
From the Tools menu in the model menu bar, select Real-Time Workshop, then Build Model. The build process begins in the command window.
Real-Time Workshop generates two auxiliary subdirectories, as well as a stand-alone executable named mech_stewart_codegen.
Start the executable by entering
!mech_stewart_codegen
A MAT-file called mech_stewart_codegen.mat is created whenever you run the executable. This file contains the output, state, and time data exported from the model.
You can load this MAT-file into your workspace and examine its variables, all distinguished by the rt_ prefix.
From the MATLAB Desktop Current Folder window, right-click mech_stewart_codegen.mat and select Import Data. The Import Wizard appears, listing the variables that were generated, at each time step, by running the executable. These include:
rt_tout: Simulation times
rt_xout: States
rt_yout: Outputs
Click Finish on the Import Wizard dialog. The variables are loaded into your workspace.
Examine the variables in workspace and double-click each of the three. The editor displays the variable values as arrays (in the two cases of outputs and states, arrays as parts of data structures).
These variables include:
The variable rt_tout containing the simulation times.
The variable rt_xout containing the state signals. These states include a six-column array representing the Controller subsystem states and a 52-column array representing the mechanical states of the Plant.
The six controller states are the six leg positions integrated by the Controller/Integrator block for PID control.
The 52 mechanical states are discussed in Identifying the Simulink and Mechanical States of the Stewart Platform earlier in this chapter.
The variable rt_yout containing the output signals. These outputs are the three output signals designated by the model's output signal ports (Top Plate Position, Errors, and Leg Forces).
![]() | Designing and Improving Controllers | Simulating with Hardware in the Loop | ![]() |

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 |