Main Content

Log, Verify, and Debug Charts

In Stateflow®, you can log data and states to understand when and how they change during simulation. You can add breakpoints to pause execution on states or transitions. By combining these techniques, you can verify and debug your chart logic.

In this example, you identify a design problem in a rechargeable battery model by logging the state activity and data values of a chart. Then, you use breakpoints to identify the state and time step in which the design problem occurs.

Open Model

To build the model, follow the instructions in the previous step of the tutorial. Alternatively, open the sfGetStartedBattery model from the MATLAB® command prompt.

  1. In the MATLAB command prompt, enter:

    openExample("sfGetStartedBattery")

    You can charge or discharge the battery by toggling the Manual Switch block.

  2. Double-click the Chart block to enter the Stateflow Editor.

    The states Charge and Discharge represent the operating modes of the battery system. The input isCharging determines the active state. The data sentPower and charge represent the output wattage and charge level of the battery.

Log States and Data

The battery system has these requirements:

  • When isCharging is true, charge the battery. When isCharging is false, discharge the battery.

  • The charge percentage of the battery must remain between 100% and 0%.

Verify the requirements by logging the active state of the chart and the value of the charge input to the Simulation Data Inspector.

  1. To log the active state of the chart, in the Simulation tab, in the Prepare section, click Log Active State. A logging badge appears in the lower-left corner of the canvas.

  2. In the Symbols pane, right-click charge and click Inspect. The Property Inspector opens.

  3. In the Property Inspector, expand the Logging section and select Log signal data.

  4. Return to the Simulink® Editor.

  5. Right-click the signal line from the Manual Switch block to the chart and click Log Selected Signals.

  6. To simulate the model, in the Simulation tab, click Run. The model simulates until you press Stop.

  7. To transition to the Discharge state, double-click the Manual Switch block. Wait several seconds and toggle the Manual Switch block again. Repeat several times. On the final toggle, return the Manual Switch block to 0.

  8. In the Simulation tab, click Stop to end the simulation.

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

  10. In the Simulation Data Inspector, click Visualization and layouts . In the Basic layouts section, click the 2x1 layout .

  11. To display the active state of the chart, click the top graph. Then, in the Inspect tab, expand the Stateflow section and select Battery:ActiveChild.

  12. To display the output of the Manual Switch block, click the bottom graph. Then, in the Inspect tab, expand the Signals section and select Manual Switch:1.

    When you toggle the Manual Switch block, the chart transitions between Charge and Discharge as expected.

  13. To display the value of charge, click the bottom graph. Then, in the Inspect tab, select charge and clear Manual Switch:1.

    The battery contradicts the requirements by charging above 100% and below 0%.

Set Breakpoints

Set breakpoints on states and transitions to identify the time step in which the design problem occurs.

  1. In the chart, right-click the Discharge state and click Set Breakpoint. A red circular breakpoint badge appears on the state. By default, breakpoints on a state trigger when the charts enters or remains in the state.

  2. To disable the breakpoint from triggering when the chart enters the state, click the breakpoint badge. In the Discharge Breakpoints dialog box, clear the On State Entry trigger.

  3. Right-click the transition from Charge to Discharge and click Set Breakpoint. The breakpoint triggers when the chart moves along the transition.

  4. In the Debug tab, click Breakpoints List.

    In the Breakpoints List pane, you can enable or disable breakpoints and change the conditions under which they trigger.

  5. Specify that the Discharge breakpoint triggers only when the battery charge is negative. In the table row for the Discharge state, double-click the Condition column and enter charge<0.

  6. Simulate the model. The simulation pauses on the transition between Charge and Discharge.

  7. To resume the simulation, in the Simulation tab, click Continue. The simulation pauses on the step when charge becomes negative.

  8. Click Continue again. Because charge is still negative, the Discharge breakpoint triggers on the next step.

  9. Stop the simulation.

  10. To remove the breakpoints, right-click each breakpoint badge and select Clear Breakpoint.

In the next step of the tutorial, you fix the design problem by adding nested operating modes that slow or stop charging or discharging when the battery reaches certain charge levels.

See Also

Topics