After you create a model component to initialize, reset, and terminate the state of blocks (see Using Initialize, Reset, and Terminate Functions), you can place the model in a simulation test harness. A test harness is a Simulink® model that you use to develop, test, and debug a model component.
To create the test harness, reference the export-function model containing the model component in a new model, and then add a Stateflow® chart to model a function-call event scheduler.
The export-function model contains the model component for testing. To create the export function model, see Create an Export-Function Model.
Create a Simulink model. Save this model with the name Test_Model
.
Set configuration parameters for solver Type to
Fixed-step
, Solver to auto
, and
Fixed-step size to 1
.
Add a Model block. Open the Block Parameters dialog box. In the
Model name text box, enter the name of your export-function
model. In this example, enter Model02
.
Test the referenced model component by connecting a Function-Call Generator block to
the Run
port. Connect a Constant block to the
Signal In
port and a Scope block to the
Signal Out
port.
Run simulation to verify your model simulates correctly from the parent model. When the model is simulated without function-call event ports, the Initialize Function block executes at the beginning of a simulation and the Terminate Function block executes at the end of the simulation.
Expose function-call input ports on the model block. Right-click the Model block and select Block Parameters. In the Block Parameters dialog box, select the Show model initialize port, Show model reset port, and Show model terminate port check boxes.
Delete the Function-Call Generator block and update the model by pressing Ctr-D.
When you activate the initialize function-call input port on a Model
block, the model has to receive an initialize function call on the
initialize
port before it can execute. The reception of a function
call triggers the execution of the default model initialize routine, and then the
execution of the Initialize Function block contents.
The reception of a function call on the Reset
port triggers the
execution of the Reset Function block contents.
The reception of a function call on the Terminate
port triggers
the execution of the Terminate Function block contents, and then the
execution of the default model terminate routine. The model then stops running. To
execute the model again, you have to reinitialize the model by sending an function-call
event to the initialize
port.
Use a Stateflow chart to model an event schedule and generate the initialize and terminate function call signals.
Add a Stateflow chart. Click the model diagram and start typing Chart
.
From the search list, select .
Open the chart and add two state blocks, one above the other.
Add a default transition and connect it to the top state block. Edit the label:
{step = 0}
Add a transition from the top block to the bottom block. Edit the label:
[step == 2]/{Initialize}
Add a transition from the bottom block and back to the bottom block. Edit the label:
[step == 5]/{Reset}
Add a transition from the bottom block to top block. Edit the label:
[step == 9]/{Terminate}
Edit the content of the top block:
Inactive entry: step = step + 1; during: step = step + 1;
Edit the content of the bottom block:
Running entry: step = step + 1; Run; during: step = step + 1; Run;
Create function-call output ports on the chart to control and run the model component.
Open Model Explorer. On the Modeling tab and from the
Design section, select Model Workspace
..
Create index variable. From the menu, select Add > Data. In the Data dialog box, enter Step
for the
Name.
Create function-call output ports. For each function-call event you create, select Add > Event and in the Event dialog box, enter, and select the following values.
Enter in Event Text Box | Set Scope | Set Trigger |
---|---|---|
Initialize | Output to Simulink | Function call |
Reset | Output to Simulink | Function call |
Terminate | Output to Simulink | Function call |
Run | Output to Simulink | Function call |
Navigate to the top level of the model. Connect the Initialize
,
Reset
, Terminate
, and Run
ports on the chart to the initialize
, reset
,
terminate
, and Run
input ports on the
Model block.
Run simulation.
The model cannot execute until the second time step, when the block state is initialized to 4. At the fifth time step, a reset function call to the reset port triggers the Reset Function block to execute. At the ninth time step, the subsystem stops executing, and the block state remains constant.
If the model receives a function call to run before an initialize function call, a simulation error occurs.