Main Content

Test Export-Function Model Simulation Using Stateflow Chart

Use a Stateflow® chart to provide a function-call scheduler where you can fully control the scheduling process for periodic (synchronous) or aperiodic (asynchronous) call sequences.

  1. Create a Simulink® model.

  2. Add a Model block that references the export-function model.

  3. Specify function-call Inputs using a Stateflow chart.

  4. Specify data inputs.

  5. Run a simulation.

Reference an Export-Function Model

Referencing an export-function model from a Model block allows the addition of function-calls events from a Stateflow chart and the logging of data signals for testing without changing the model itself.

  1. Add a Model block to a new Simulink model. In the Model name box, enter the name of an export-function model. For example, use the export-function model created in Create Export-Function Model.

  2. Add and connect Outport blocks to the output_100ms and output_10ms ports for saving simulation data.

  3. Add a Sine Wave block to provide data input. Set Amplitude to 2 and Sample time to 0.01. Connect the block to the input_10ms input port on the Model block.

    Model block that references export-function model with input and output ports connected

Create Periodic Scheduler Using Stateflow Chart

  1. Create a Stateflow chart. This step requires a Stateflow license.

  2. On the Modeling tab and from the Design section, select Model Explorer . In the Model Hierarchy pane, select Chart.

  3. Add function-call events with output ports to the chart. From the menu, select Add > Event. In the Name box, enter out_100ms. From the Scope list, select Output to Simulink. Repeat this step to create a function-call event and output port for out_10ms.

    Stateflow chart with two output ports: out_100ms and out_10ms

  4. Open the chart by double-clicking the block. Add a State block and a Default transition arrow.

    State block and Default transition arrow

  5. Rename the state to function_call_scheduler.

  6. Add periodic function-calls every 10 and 100 milliseconds. In the state block, enter these commands.

    on every(10, tick):send(out_10ms);
    on every(100, tick):send(out_100ms);

    The keyword tick is an implicit event that counts the number of simulation steps while send is an explicit event that outputs a function-call event to the output ports.

    Chart with state named function_call_scheduler

Create Test Model (Harness) for Simulation

Use a Simulink test model only for simulation. After simulation testing, generate code from the export-function model. Then, integrate exported function code with an externally coded scheduler.

  1. Add a Stateflow chart to your test model.

  2. Connect the Stateflow chart outputs to Model block inputs.

    Test harness with Stateflow chart outputs connected to export-function model function-call ports

Scheduling Restrictions for Referenced Export-Function Models

If a test model references an export-function model, there are some restrictions to ensure consistency with simulation results.

For the test model:

  • Function-calls to the input ports on the Model block must follow the execution order of the root-level function-call Inport blocks in the referenced export-function model.

    If the test model calls the referenced model functions out of order at any time step, the software displays an error. For information on sorted execution order, see Control and Display Execution Order. To disable this restriction, clear the configuration parameter Enable strict scheduling checks for referenced models.

For the export-function model:

  • The sample times for the root-level function-call Inport blocks must be set to inherited (-1) or match the sample time of the function-calls from the Stateflow chart that drives them.

Prepare Export-Function Model for Simulation

Before you test the export-function model, configure model settings and specify signals to log.

  1. On the Modeling tab, click Model Settings .

  2. On the Model Referencing pane, clear the configuration parameter Enable strict scheduling check for referenced models.

  3. Verify the settings for these configuration parameters on the Solver pane:

    • Type is set to Fixed-step.

    • Solver is set to discrete (no continuous states).

    • Fixed-step size (fundamental sample time) is set to auto.

  4. Set up logging for signals. Right-click a signal you want to log. Then, select Log selected signal. In this example, the export-function model output signals are logged because they connect to Outport blocks.

Note

When using export-function models in top-model simulations, do not change the enable/disable status of the model during simulation. Enable it at the start of simulation and use function-calls to call it.

Test Export-Function Model Using Stateflow Chart

To test and observe the export-function model behavior before generating code, simulate the completed test model.

Optionally, open and simulate the completed test model named ex_export_function_test_model_with_stateflow_chart.

To view the simulation results, open the Simulation Data Inspector. On the Simulation tab, click Data Inspector.

After you test your model, you can generate code for the functions. See Generate Code for Export-Function Model.

See Also

Related Topics