Documentation

Create Test Harness to Generate Function Calls

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.

Reference the Export-Function Model

The export-function model contains the model component for testing. To create the export function model, seeCreate an Export-Function Model.

  1. Create a new Simulink model. Save this model with the nameTest_Model.

  2. Set Model Configuration Parameters for solverTypetoFixed-step, Solver toauto, andFixed-step sizeto1.

  3. Add aModelblock. Open the Block Parameters dialog box. In theModel nametext box, enter the name of your export-function model. In this example, enterModel02.

  4. Test the referenced model component by connecting a Function-Call Generator block to theRunport. Connect aConstantblock to theSignal In一个港口nd aScopeblock to theSignal Outport.

  5. 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.

  6. 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.

  7. 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 theinitializeport 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 theResetport triggers the execution of the Reset Function block contents.

    The reception of a function call on theTerminateport 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 theinitializeport.

Model an Event Scheduler

Use a Stateflow chart to model an event schedule and generate the initialize and terminate function call signals.

  1. Add a Stateflow Chart. Click the model diagram and start typingChart. From the search list, select.

  2. Open the chart and add two state blocks, one above the other.

  3. Add a default transition and connect it to the top state block. Edit the label:

    {step = 0}
  4. Add a transition from the top block to the bottom block. Edit the label:

    [步骤= = 2]/{初始化}

  5. Add a transition from the bottom block and back to the bottom block. Edit the label:

    [step == 5]/{Reset}

  6. Add a transition from the bottom block to top block. Edit the label:

    [step == 9]/{Terminate}

  7. Edit the content of the top block:

    Inactive entry: step = step + 1; during: step = step + 1;

  8. Edit the content of the bottom block:

    Running entry: step = step + 1; Run; during: step = step + 1; Run;

Connect Chart to Test Model

Create function-call ports on the chart to control and run the model component.

  1. Open Model Explorer.View>Model Explorer>Model Explorer.

  2. Create index variable. From the menu, selectAdd>Data. In the Data dialog box, enterStepfor theName.

  3. 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

  4. Navigate to the top level of the model. Connect theInitialize,Reset,Terminate, andRunports on the chart to theinitialize,reset,terminate, andRuninput ports on theModelblock.

  5. 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.

See Also

Blocks

Related Topics

Was this topic helpful?