Main Content

Get Started with Simscape Hardware-in-the-Loop Workflow

You can convert a Simscape™ model to HDL code for field-programmable gate array (FPGA) deployment, which requires HDL Coder™. The simulation of physical systems is accelerated, enabling you to rapidly prototype models. The conversion also provides real-time simulation of your HDL implementation model by using hardware-in-the-loop (HIL) simulation.

You can perform HIL simulation with smaller time steps and increased accuracy by deploying the Simscape plant models to the generic FPGAs, system on a chip (SoCs), or Speedgoat® FPGA I/O modules. Using this workflow, you can:

You can model and deploy complex physical systems in Simscape while converting your models into HDL code.

This workflow diagram shows the functionalities at various stages of the Simscape hardware-in-the-loop workflow.

Simscape to HDL workflow diagram

Modeling Physical Systems in Simscape for HDL Compatibility

When you design your Simscape model for compatibility with the Simscape HDL Workflow Advisor, follow these guidelines. Before using this workflow, convert Simscape subsystems into state-space algorithms. For more information, see Modeling Guidelines for Simscape Subsystem Replacement.

You can create a Simscape model by using linear, switched linear, and nonlinear blocks.

  • Linear blocks are blocks that are defined by a linear relationship such as resistors.

  • Switched linear blocks are blocks such as diodes and switches. These blocks are also defined by a linear relationship, such as V = IR, where R can switch between two or more values depending on the state of the diodes or switches.

  • Nonlinear blocks are blocks that are defined by nonlinear equations, such as nonlinear resistor, nonlinear inductor, DC Motor, and PMSM blocks.

You can choose a solver type (Backward Euler or Partitioning) based on the type of blocks in your model. If you have linear and switched linear blocks in your model, choose the Backward Euler solver type. If you have nonlinear blocks in your model, choose the Partitioning solver type. For more information, see Solver Configuration (Simscape).

Configure Solver Settings

To configure the solver settings for HDL code generation, open your model, for example:


Double-click the Solver Configuration (Simscape) block.

Select Solver type.  To choose a solver type for the block, double-click the Solver Configuration (Simscape) block.

  1. In the Block Parameters dialog box, select Use local solver.

  2. From the Solver type drop-down list, select Backward Euler for linear and switched linear blocks. For nonlinear blocks, select Partitioning. For boost converter, select Backward Euler.

Specify Sample Time.  Similarly you can specify a discrete sample time.

  1. In the Block Parameters dialog box, select Use local solver.

  2. In the Sample time text box, specify a discrete sample time Ts.

To verify that the solver settings are specified correctly, run the Check Solver Configuration in the Simscape HDL Workflow Advisor.

Run hdlsetup Function

After creating the model, configure the model for HDL code generation by running the hdlsetup function. The function hdlsetup sets your model configuration parameters to default values recommended for code generation. Open the model before you invoke the hdlsetup function.

To invoke hdlsetup function for your current_model, at the MATLAB® command prompt, enter:


After running hdlsetup function, make sure your model compiles without any errors or warnings.

Simscape Example Models for HDL Code Generation

For HDL code generation, you can design your own Simscape algorithm or choose from a list of example models that are created in Simscape.

Example Models That Use Backward Euler Solver

Example Models That Use Partitioning Solver

All examples are prefixed with sschdlex and postfixed with Example. For example, to open the boost converter model, at the MATLAB command prompt, enter:


You can also find many other application-specific example models. To get a list of all the Simscape models designed for HDL code generation, type sschdlex in the MATLAB Command Window and press the Tab key.

Generate HDL Implementation Model by Using the Simscape HDL Workflow Advisor

You first generate an HDL implementation model from the Simscape model by using the Simscape HDL Workflow Advisor.

Boost converter model

To generate an HDL implementation model, open the Simscape HDL Workflow Advisor.

At the MATLAB command prompt, run the sschdladvisor function:


This function opens the Simscape HDL Workflow Advisor for the current_model.

Run Tasks in the Simscape HDL Workflow Advisor

In the Simscape HDL Workflow Advisor, the left pane lists the folders containing a group or category of related tasks. Expanding the folders shows available tasks in each folder. From the left pane, you can select a folder or an individual task and see the related information in the right pane. For the tasks in the left pane, the right pane contains controls for running the task, a display area for status messages, and other task results. To learn more about each individual task, right-click that task, and select What's This? from the list.

In the Simscape HDL Workflow Advisor window, you can run an individual task or all the tasks in the list. Select the task that you want to run and click Run This Task. To run a task, all tasks before it must have run successfully. To run the workflow to a specific task inside a subfolder, expand that folder, and then right-click the task and select Run to Selected Task.

Simscape HDL Workflow Advisor window

To run the workflow and compare functionality of the HDL implementation model with the original Simscape algorithm:

  1. Select the Generate implementation model task.

  2. Under Verification Settings, select the Generate validation logic for the implementation model check box. This enables the Validation logic tolerance which is set with the default value of 0.001.

  3. Right-click the Generate implementation model task and select Run to Selected Task from the list.

After the task passes, you see a link to the HDL implementation model and a state-space validation model. The implementation model has the same name as the original Simscape model and uses the prefix gmStateSpaceHDL. The state-space validation model has the same name as the implementation model and uses the postfix _vnl.

Simscape HDL Workflow Advisor window with links to generated implementation model and state-space validation model in the display area of right pane

For more information on Simscape HDL Workflow Advisor tasks, see Simscape HDL Workflow Advisor Tasks.

HDL Code Generation and Deployment

For HDL code generation, you can verify the numeric results of the HDL implementation model and your original Simscape algorithm by using the generated state-space validation model.

Boost converter validation model

Before you generate HDL code, validate the generated HDL implementation model.

Validate HDL Algorithm

You can compare the functionality of the HDL implementation model to the original Simscape algorithm. To validate the HDL implementation model, simulate the state-space validation model. If the simulation does not result in any assertion or warning, then it indicates that the output of the HDL implementation model matches the original Simscape algorithm within the specified tolerance. For more information, see Validate HDL Implementation Model to Simscape Algorithm.

Generate Code

Once you validate the HDL algorithm, you can then generate HDL code for the implementation model. At the MATLAB command prompt, enter:

makehdl('gmStateSpaceHDL_current_model/HDL Subsystem')
For more information, see Generate HDL Code for Simscape Models.

After generating the HDL code, you can deploy the real-time model onto your target hardware. For more information on how to deploy the code, see Deploy Simscape Buck Converter Model to Speedgoat IO Module Using HDL Workflow Script.

Restrictions for HDL Code Generation from Simscape Models

HDL Coder does not support code generation from Simscape networks that use:

See Also

| |

Related Topics