Main Content

Use Active State Output to Debug Charts

Bugs in Stateflow® can be difficult to identify, diagnose, and fix. By tracking how data and states in your chart change over time, you can understand why bugs occur.

In this example, you debug the control logic for a chemical tank by logging the active state of a subchart and using breakpoints to step through the simulation.

Open the Model

This example models a chemical tank in an assembly line.

The project requirements state the model must execute these steps:

  1. When the power turns on, liquid begins to fill the tank.

  2. When the volume of the liquid exceeds maxVolume, the tank mixes the liquid.

  3. After mixing for mixTime seconds, the liquid drains from the tank.

  4. The tank cools for coolTime seconds.

  5. The tank automatically shuts off.

However, when you simulate the model, the chart never returns to the Power_Off state.

Identify the Problem

Because the chart never returns to the Power_Off state, the error in the chart must occur in the Power_On state. Determine where the error occurs by plotting the active child state in the Simulation Data Inspector.

  1. Open the chart.

  2. Select the Power_On state. In the Simulation tab, click Log Child Activity. The logging badge appears in the lower-left corner of Power_On.

  3. Simulate the model.

  4. In the Simulation tab, in the Review Results section, click Data Inspector .

  5. Select Power_On:ActiveChild. Simulation Data Inspector plots the active child state of Power_On against the simulation time.

    The chart progresses from "None," to "Fill," to "Mix," to "Drain." For the rest of the simulation, the chart remains at "Drain."

The plot shows that the simulation reaches the Drain child state, but never progresses beyond it.

Identify the Cause

Now that you have identified where the bug occurs, use breakpoints to determine the cause.

  1. Right-click the Drain state and click Set Breakpoint. The breakpoint displays as a red circle in the bottom-left corner of Drain.

  2. Click the breakpoint and clear On State Entry.

  3. Simulate the model.

  4. When the simulation pauses at the breakpoint, point to the Drain state. The Data information window displays the values of drainRate and liquid as -1770 and 4536.

    The Data information window shows the values of all data accessed or modified by a state.

  5. Click Continue to resume the simulation. At the next breakpoint, the values are -1770 and 6306.

  6. Click Stop Debugging to end the simulation.

The value of liquid increases rather than decreases. Therefore, liquid never reaches 0, and the chart does not continue to the next state.

Fix the Bug

The value of liquid continually increases because the drainRate state has a negative value. To fix the issue, assign a positive value to drainRate.

  1. In the Modeling tab, click Symbols Pane.

  2. In the Symbols pane, delete the negative sign in the value of drainRate.

  3. Right-click the Drain state and click Clear Breakpoint to remove the breakpoint.

  4. In the Debug tab, under Animation Speed, select "Medium".

  5. Simulate the model. Before the simulation stops, the chart returns to the Power_Off state.

  6. Select the Power_On state. In the Simulation tab, click Log Child Activity to turn off active state logging.

See Also

Functions

Objects

Related Topics