After you create a model component to initialize, reset, and terminate the state of blocks (seeCustomize 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 an event scheduler.
The export-function model contains the model component for testing. To create the export function model, seeCreate an Export-Function Model.
Create a new Simulink model. Save this model with the nameTest_Model
.
Set Model Configuration Parameters for solverTypetoFixed-step
, Solver toauto
, andFixed-step sizeto1
.
Add aModelblock. Open the Block Parameters dialog box. In theModel nametext box, enter the name of your export-function model. In this example, enterModel02
.
Test the referenced model component by connecting a Function-Call Generator block to theRun
port. Connect aConstantblock to theSignal In
一个港口nd aScopeblock to theSignal Out
port.
Run simulation to verify your model simulates correctly from the parent model. When the model is simulated without event ports, theInitialize Functionblock executes at the beginning of a simulation and theTerminate Functionblock executes at the end of the simulation.
Expose function-call ports on the model block. Right-click theModelblock and selectBlock Parameters. In the Block Parameters dialog box, select theShow model initialize port,Show model reset port, andShow model terminate portcheck boxes.
Delete theFunction-Call Generatorblock and update the model by pressingCtr-D.
When you activate the initialize function-call port on aModelblock, the model has to receive an initialize function call on theinitialize
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 theInitialize Functionblock contents.
The reception of a function call on theReset
port triggers the execution of the Reset Function block contents.
The reception of a function call on theTerminate
port triggers the execution of theTerminate Functionblock 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 event signal to theinitialize
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 typingChart
. 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:
[步骤= = 2]/{初始化}
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 ports on the chart to control and run the model component.
Open Model Explorer.View>Model Explorer>Model Explorer.
Create index variable. From the menu, selectAdd>Data. In the Data dialog box, enterStep
for theName.
Create function-call ports. For each event you create, selectAdd>Eventand 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 theInitialize
,Reset
,Terminate
, andRun
ports on the chart to theinitialize
,reset
,terminate
, andRun
input ports on theModelblock.
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 theReset Functionblock 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.