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:
Open a nonlinear model. At the MATLAB® command prompt, enter:
openExample('simscape/FullWaveBridgeRectifierExample')
To compare the baseline simulation results to subsequent iterations, remove the data point limitation on the Scope block labeled
Load Voltage
and data logging.Open the Scope block. Click View > Configuration Properties. On the Logging tab, clear Limit data points to last.
Right-click the input signal of the Scope block and select Log selected signals. The logging badge appears above the signal.
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
The baseline simulation returns the expected results for the full-wave bridge rectifier load voltage.
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.
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.
Delete the AC Voltage Source block.
Add a Sine Wave block from the Simulink > Sources library.
Add a Simulink-PS Converter block from the Simscape > Utilities library.
Add a Controlled Voltage Source block from the Simscape > Foundation Library > Electrical > Electrical Sources library.
Connect the Sine Wave block to the Simulink-PS Converter block and the Simulink-PS Converter block to the Controlled Voltage Source block.
Configure the Sine Wave block to match the parameters of the AC Voltage Source block that you removed.
Set the Amplitude parameter to
sqrt(2)*120
.Set the Frequency (rad/sec) parameter to
60*2*pi
.Set the Sample time parameter to
1e-5
. Then click the three-dots icon next to the Sample time box and select Create variable. Name the variableTs
and click Create. You can now view and edit this variable in your workspace.
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.
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);
The results are similar to the baseline results. To plot a wider tolerance band, specify a value for the Absolute Tolerance property.
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
.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.
Configure the model for real-time simulation.
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)
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.
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);
The results are similar to the baseline results.
See Also
Blocks
Simscape Blocks
Functions
hdladvisor
(HDL Coder) |hdlsaveparams
(HDL Coder) |hdlset_param
(HDL Coder) |hdlsetup
(HDL Coder) |makehdl
(HDL Coder) |simscape.findNonlinearBlocks
|sschdladvisor
(HDL Coder)