Debugging Common Modeling Errors

In phase 7 of this workflow, you debug the chart.

Debugging State Inconsistencies

In this exercise, you will introduce a state inconsistency error in your chart and troubleshoot the problem. Follow these steps:

  1. Open the model Stage6Simulate — either the one you created in the previous exercises or the supplied model for stage 6.

    To open the supplied model, enter the following command at the MATLAB® prompt:

    addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples'))
    Stage6Simulate

  2. Save the model as Stage7Debug in your local work folder.

  3. Double-click Air Controller to open the chart.

  4. Delete the default transition to FAN2.Off by selecting it and pressing the Delete key.

    Removing the default transition will cause a state inconsistency error. (Recall from Checking That Your Chart Conforms to Best Practices that there must be a default transition at every level of the Stateflow® hierarchy that has exclusive [OR] decomposition.)

    Your chart should look like this:

  5. Save the chart, and start simulation.

    An error appears in the Diagnostic Viewer. The error indicates that the state FAN2 has no default paths to a substate.

    Note

    The state number in your dialog display can differ from the one pictured above.

  6. Locate the offending state in the Air Controller chart, by clicking the link to the state name.

    FAN2 appears highlighted in the chart:

  7. Add back the default transition to FAN2.Off.

    The default transition provides an unconditional default path to one of the substates of FAN2.

  8. Simulate the model again.

    This time, simulation proceeds without any errors.

  9. Save Stage7Debug, and leave the chart open for the next exercise.

Debugging Data Range Violations

In this exercise, you will introduce a data range violation in your chart and troubleshoot the problem. To enable data range violation checking, set Simulation range checking in the Diagnostics: Data Validity pane of the Configuration Parameters dialog box to error.

Follow these steps:

  1. In the Air Controller chart, modify the during action in the SpeedValue state by adding 1 to the computed value, as follows:

    during: airflow = in(FAN1.On) + in(FAN2.On) + 1;

    Recall that in Defining the Inputs and Outputs, you set a limit range of 0 to 2 for airflow. By adding 1 to the computation, the value of airflow will exceed the upper limit of this range when two fans are running.

  2. Start simulation.

    Simulation pauses because of an out-of-range data error:

    As expected, the error occurs in the during action of SpeedValue because the value of airflow is out of range.

  3. To isolate the problem, double-click the last line in the error message:

    Data '#439 (0:0:0)': 'airflow'

    The Model Explorer opens on your desktop, allowing you to view the properties of airflow in the right, read-only pane (read-only because simulation is running).

    Note

    The ID number of the data that appears in the error message can vary from the value shown.

  4. Check the limit range for airflow:

  5. Hover your cursor over airflow to view the value.

    airflow = 3

    This value exceeds the upper limit of 2.

  6. Stop simulation.

  7. Restore the during action to its previous code, and then restart simulation for the model.

    The model should simulate with no errors or warnings.

Related Topics