Create Test Harness to Generate Function Calls

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.

Reference the Export-Function Model

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

  1. Create a new Simulink model. Save this model with the name Test_Model.

  2. Set Model Configuration Parameters for solver Type to Fixed-step, Solver to auto, and Fixed-step size to 1.

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

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

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

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

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

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 typing Chart. 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:

    [step == 2]/{Initialize}

  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 output 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, select Add > Data. In the Data dialog box, enter Step for the Name.

  3. 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 BoxSet ScopeSet Trigger
    InitializeOutput to SimulinkFunction call
    ResetOutput to SimulinkFunction call
    TerminateOutput to SimulinkFunction call
    RunOutput to SimulinkFunction call

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

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

See Also

Blocks

Related Topics