Products & Services Solutions Academia Support User Community Company

Learn more about SimMechanics   

Generating and Simulating with Code

About the Stewart Platform Code Generation Examples

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.

  1. Generating an S-Function block for the plant

  2. Model referencing the plant

  3. Generating stand-alone code for the controller and plant together

For More Information About Code Generation

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.

Learning About the Model

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.

FilePurpose
mech_stewart_codegenBasic model
mech_stewart_codegen_plantPlant 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.

Solver and Sample Time Step Sizes

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):

Structure of the Model

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

Simulation Settings for Code Generation

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:

Other settings are optimized for code generation.

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

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

    NodeSettings
    SolverSolver options: Type: Fixed-step
    Solver options: Solver: ode1 (Euler)
    Fixed-step size: dt1 (5e-3 seconds)
    Data Import/ExportTime: tout
    States: xout
    Output: yout
    OptimizationSimulation and code generation: Inline parameters selected (needed for Model Reference)
    Model ReferencingMinimize algebraic loop occurrences selected
    Real-Time WorkshopTarget 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
    SimMechanicsDiagnostics: all cleared
    Visualization: all cleared

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

    TabSettings
    ParametersLinear assembly tolerance: 1e-3 m
    Angular assembly tolerance: 1e-2 rad
    ConstraintsConstraint solver type: Stabilizing
    Use robust singularity handling selected
    VisualizationVisualize machine selected

Generating an S-Function Block for the Plant

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.

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

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

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

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

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

Model Referencing the Plant

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.

Simulation Settings for Model Reference

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.

NodeSettings
Model ReferencingRebuild options for all referenced models: Rebuild options: Never
Rebuild options for all referenced models: Never rebuild targets diagnostic: None
Real-Time WorkshopInterface: Code interface: Single output/update function cleared

Setting Up and Running the Main Model for Model Reference

To reconstruct your model for model referencing,

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

  2. From the Simulink Ports & Subsystems library, drag and drop a Model block into mech_stewart_codegen. Rename the block Model Reference Plant.

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

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

Generating Stand-Alone Code for the Whole Model

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.

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

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

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

  4. 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:

  


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