generate hdl code for simscape models -凯发k8网页登录
this example shows how to generate hdl code for a half-wave rectifier model that uses simscape™ blocks. use the simscape hdl workflow advisor to generate an hdl implementation model. you can then generate hdl code for the implementation model. see .
the halfwave rectifier model
to open the half-wave rectifier model, at the matlab® command prompt, enter:
open_system('sschdlexhalfwaverectifierexample')
save this model locally as halfwaverectifier_hdl
to run the workflow.
open_system('halfwaverectifier_hdl') set_param('halfwaverectifier_hdl', 'simulationcommand', 'update')
at the top level of the model, a simscape_system
block models the half-wave rectifier algorithm. the model accepts a sine wave input, uses a rate transition block to discretize the continuous time input, and has a scope block that calculates the output. to see the half-wave rectifier algorithm, double-click the simscape_system
subsystem.
open_system('halfwaverectifier_hdl/simscape_system')
the half-wave rectifier consists of a resistor, which is a linear block, and a diode, which is a switched linear block. the simscape model is preconfigured for hdl compatibility. at the input and output port interfaces, the model has simulink-ps converter and ps-simulink converter blocks. the solver settings are configured for compatibility with simscape hdl workflow advisor. if you open the block parameters dialog box for the solver configuration block, use local solver is selected and backward euler
is specified as the solver type. see .
to see the functionality, simulate the model and then open the scope block.
sim('halfwaverectifier_hdl') open_system('halfwaverectifier_hdl/scope')
run simscape hdl workflow advisor
to generate an hdl implementation model from which you generate code, use the simscape hdl workflow advisor. to open the advisor, run this command:
sschdladvisor('halfwaverectifier_hdl')
this command updates the model advisor cache and opens the simscape hdl workflow advisor. to learn more about the simscape hdl workflow advisor and the various tasks, right-click that folder or task, and select what's this?. see also .
to run the workflow and compare functionality of the hdl implementation model with the original simscape algorithm, select the generate implementation model step, and then select the generate validation logic for the implementation model check box. use a validation logic tolerance of 0.001
. right-click the generate implementation model step and select run to selected task.
the advisor generates an 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
.
open and examine hdl implementation model
in the generate implementation model task, click the link to open the implementation model. the model contains a simscape_system
subsystem that contains a hdl subsystem
block. the hdl subsystem
models the state-space representation that you generated from the simscape model.
open_system('gmstatespacehdl_halfwaverectifier_hdl') open_system('gmstatespacehdl_halfwaverectifier_hdl/simscape_system') set_param('gmstatespacehdl_halfwaverectifier_hdl','simulationcommand','update')
the ports of this subsystem use the same name as the simulink-ps converter and ps-simulink converter blocks in your original simscape model. if you navigate inside this subsystem, you see several delays, adders, and matrix multiply blocks that model the state-space equations.
open_system('gmstatespacehdl_halfwaverectifier_hdl/simscape_system/hdl subsystem/hdl algorithm')
to simulate the hdl implementation model, enter these commands. open the scope block to view results.
sim('gmstatespacehdl_halfwaverectifier_hdl') open_system('gmstatespacehdl_halfwaverectifier_hdl/scope')
hdl code is generated for the hdl subsystem
block inside this model.
validate hdl algorithm
to compare functionality of the hdl implementation model with the original simscape algorithm, open and simulate the state-space validation model.
open_system('gmstatespacehdl_halfwaverectifier_hdl_vnl') sim('gmstatespacehdl_halfwaverectifier_hdl_vnl')
the output of this model matches the original simscape model. the simulation does not generate assertions, which indicates that the outputs match. for a more systemic verification, see .
generate hdl code and validation model
the hdl model and subsystem parameter settings are saved using this command:
hdlsaveparams('gmstatespacehdl_halfwaverectifier_hdl');
%% set model 'gmstatespacehdl_halfwaverectifier_hdl' hdl parameters hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'fptolerancevalue', 1.000000e-03); fpconfig = hdlcoder.createfloatingpointtargetconfig('nativefloatingpoint' ... , 'latencystrategy', 'min' ... ); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'floatingpointtargetconfiguration', fpconfig); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'hdlsubsystem', 'gmstatespacehdl_halfwaverectifier_hdl/simscape_system/hdl subsystem'); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'maskparameterasgeneric', 'on'); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'oversampling', 13); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl', 'usefloatingpoint', 'on'); hdlset_param('gmstatespacehdl_halfwaverectifier_hdl/simscape_system/hdl subsystem/hdl algorithm/mode selection/generate mode vector', 'architecture', 'matlab datapath');
the model uses single data types and generates hdl code in native floating point
mode. floating-point operators can introduce delays. because the design contains feedback loops, to allocate sufficient delays for the operators inside the feedback loops, the model uses clock-rate pipelining.
for more information, see:
before you generate hdl code, enable generation of the validation model. the validation model compares the output of the generated model after code generation and the original model. to learn more, see .
run these commands to save validation model generation settings on your simulink model:
hdlmodelname = 'gmstatespacehdl_halfwaverectifier_hdl'; hdlset_param(hdlmodelname, 'targetdirectory', 'c:/temp/hdlsrc'); hdlset_param(hdlmodelname, 'generatevalidationmodel', 'on');
to generate hdl code, run this command:
makehdl('gmstatespacehdl_halfwaverectifier_hdl/simscape_system/hdl subsystem');
the generated hdl code and validation model are saved in c:/temp/hdlsrc
directory. the generated code is saved as hdl_subsystem.vhd
. to open the validation model, click the link to gm_gmstatespacehdl_halfwaverectifier_hdl_vnl.slx
in the code generation logs in the command window.
open the compare
block at the output of hdl subsystem_vnl
subsystem of the validation model. to see the simulation results after hdl code generation, double-click the double click to turn 'on/off' all scopes
block. for each of the scopes (ir, iout, and vdiode), the first graph represents the output of the generated model, and the middle graph represents the output of the implementation model. the bottom graph calculates the difference between outputs of both models. as the outputs match, the error is zero.
see also
functions
- |
related topics
- get started with simscape electrical (simscape electrical)