Main Content

Linearize a Simscape Model to Prepare for HDL Code Generation

Before you run the Simscape™ HDL Workflow Advisor, you must configure your network to exclude delays and enabled runtime parameters. To learn more about the capabilities and limitations of Simscape models in HDL Coder™, visit Get Started with Simscape Hardware-in-the-Loop Workflow (HDL Coder).

To prepare your Simscape model for HDL code generation:

  1. Open a nonlinear model. At the MATLAB® command prompt, enter:

    openExample('simscape/FullWaveBridgeRectifierExample')

  2. To compare the baseline simulation results to subsequent iterations, remove the data point limitation on the Scope block labeled Load Voltage and data logging.

    1. Open the Scope block. Click View > Configuration Properties. On the Logging tab, clear Limit data points to last.

    2. Right-click the input signal of the Scope block and select Log selected signals. The logging badge appears above the signal.

    Baseline model of a full-wave bridge rectifier.

  3. Simulate the model and view the results in the Simulation Data Inspector.

    baselineModel = "FullWaveBridgeRectifier";
    sim(baselineModel)
    runIDs = Simulink.sdi.getAllRunIDs;
    runID = runIDs(end);
    run = Simulink.sdi.getRun(runID);
    signal1 = run.getSignalByIndex(1);
    signal1.checked = true;
    Simulink.sdi.view

    Simulation Data Inspector results from the baseline model.

    The baseline simulation returns the expected results for the full-wave bridge rectifier load voltage.

  4. Before running the advisor, identify and replace blocks that cause your network to be nonlinear. To identify the blocks, use the simscape.findNonlearBlocks function.

    simscape.findNonlinearBlocks(baselineModel)
    Found network that contains nonlinear equations in the following blocks:
        'ssc_bridge_rectifier/AC Voltage Source'
    
    The number of linear or switched linear networks in the model is 0.
    The number of nonlinear networks in the model is 1.
    
    ans =
    
      1×1 cell array
    
        {'ssc_bridge_rectifier/AC Voltage Source'}

    The model contains an AC Voltage Source block, a periodic source that generates nonlinear equations.

  5. You can replace the AC Voltage Source block with a Controlled Voltage Source block in the Simscape network and a Sine Wave block outside the network.

    1. Delete the AC Voltage Source block.

    2. Add a Sine Wave block from the Simulink > Sources library.

    3. Add a Simulink-PS Converter block from the Simscape > Utilities library.

    4. Add a Controlled Voltage Source block from the Simscape > Foundation Library > Electrical > Electrical Sources library.

    5. Connect the Sine Wave block to the Simulink-PS Converter block and the Simulink-PS Converter block to the Controlled Voltage Source block.

    Modified model. The Controlled Voltage Source block and Sine Wave block replaced the AC Voltage Source block in the baseline model.

  6. Configure the Sine Wave block to match the parameters of the AC Voltage Source block that you removed.

    1. Set the Amplitude parameter to sqrt(2)*120.

    2. Set the Frequency (rad/sec) parameter to 60*2*pi.

    3. Set the Sample time parameter to 1e-5. Then click the three-dots icon Icon with three vertical dots next to the Sample time box and select Create variable. Name the variable Ts and click Create. You can now view and edit this variable in your workspace.

  7. Ensure that there are no blocks that cause your network to be nonlinear.

    sim(baselineModel)
    simscape.findNonlinearBlocks(baselineModel)
    The number of linear or switched linear networks in the model is 1.
    
    ans =
    
      0×0 empty cell array

    The model contains only blocks that generate linear or switched linear equations. Now the model is ready for the Backward-Euler solver.

  8. Simulate the model and compare the results to the baseline results in the Simulation Data Inspector.

    runIDs = Simulink.sdi.getAllRunIDs;
    runBaseline = runIDs(end - 1);
    runSwitchedLinear = runIDs(end);
    Simulink.sdi.view
    compBaseline1 = Simulink.sdi.compareRuns(runBaseline,...
        runSwitchedLinear);

    Simulation data inspector output comparing the Run 2 modified model to the baseline. The inspector superimposes the new plot on the old and displays a plot of their difference below.

    The results are similar to the baseline results. To plot a wider tolerance band, specify a value for the Absolute Tolerance property.

  9. To perform future progress checks for the Simscape HDL Workflow Advisor, add and connect a Digital Clock block from the Simulink > Sources library and a Display block from the Simulink > Sinks library, as shown in the figure. For the Digital Clock block, set the Sample time parameter to Ts.

    HDL compatible model including the Digital Clock and Display blocks

  10. The model uses a variable-step solver. For real time-simulation, you must use a fixed-step solver. Use the sample time colors and annotations to help you to determine if your model contains any continuous settings. To turn on sample time colors and annotations, on the Debug tab, click Information Overlays, and in the Sample Time group, select Colors and Text.

    The model diagram updates and the Timing Legend pane opens.

  11. Configure the model for real-time simulation.

    1. Configure the Simulink® model for fixed-step, fixed-cost simulation. In the Configuration Parameters window, click Solver and set:

      • Type to Fixed-step

      • Solver to Discrete (no continuous states)

    2. Configure the Simscape network for fixed-step, fixed-cost simulation. For the Solver Configuration block:

      • Select Use local solver.

      • Set Solver type to Backward Euler.

      • Specify Ts for the Sample time.

  12. Simulate the model and compare the results to the baseline results in the Simulation Data Inspector.

    sim(baselineModel)
    runIDs = Simulink.sdi.getAllRunIDs;
    runBaseline = runIDs(end - 2);
    runRealTime = runIDs(end);
    Simulink.sdi.view
    compBaseline1 = Simulink.sdi.compareRuns(runBaseline,...
        runRealTime);

    Simulation data inspector output comparing the Run 3 modified model to the baseline. The inspector superimposes the new plot on the old and displays a plot of their difference below.

    The results are similar to the baseline results.

See Also

Blocks

Simscape Blocks

Functions

Related Examples

More About