Main Content

Create Parent and Child Operating Modes

In Stateflow® charts, you can create nested operating modes by placing a state inside the border of an existing state. The existing state is called a parent state, and the states it contains are called child states.

The contents of the parent state act like a smaller chart. Similar to a chart, the parent state contains a default transition. When the parent state becomes active, the child state connected to the default transition also becomes active. Then, the chart transitions between the child states. When the parent state deactivates, the child states also deactivate.

For instance, in this model of the emergency lights on an ambulance, the On state has the child states Red and White. When On is active, the ambulance switches between the colored lights, starting with the Red state. When On deactivates, both lights deactivate.

In this example, you use child states to model the variable charging rates in a rechargeable battery system.

Open Model

The sfGetStartedBattery model represents the control logic for a rechargeable battery system. To build the model, follow the instructions in the previous steps of the tutorial.

Double-click the Chart block to enter the battery chart.

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

Add Child States to Model Hierarchy

Currently, the battery system continues charging when full and discharging when empty. To represent full, empty, and intermediate charging modes, the battery system requires additional operating modes:

Parent Mode Child Mode Actions Exit Condition
Charge FastCharge While active, increase the charge level at a rate of 4% When the charge level is greater than 80%, move to SlowCharge
Charge SlowCharge While active, increase the charge level at a rate of 1%. When the charge level is equal to 100%, move to Full
Charge Full None None
Discharge Powered While active, decrease the charge level at a rate of 3%. When the charge level is less than or equal to 3%, move to Empty
Discharge Empty On entry, set the output wattage to 0 None.

Add a state that represents fast charging.

  1. In the Charge and Discharge states, remove the during actions.

  2. Expand the Charge and Discharge states so that they can contain up to three child states. To resize a state, click any corner and drag outward.

  3. In Charge, add a child state named FastCharge. Child states must be fully enclosed by the parent state. The borders of the child and parent states cannot overlap.

  4. In FastCharge, add a during action that increments the data charge by 4.

Add two states that represent slow and paused charging.

  1. Add a child state named SlowCharge. Add a during action that increments charge by 1.

  2. Add a transition from FastCharge to SlowCharge that executes when charge exceeds 80.

  3. Add a child state named Full.

  4. Add a transition from SlowCharge to Full that executes when charge equals 100.

Add two states that represent normal and paused discharging.

  1. In Discharge, add a child state named Powered. Add a during action that decrements charge by 3.

  2. Add another child state named Empty. Add an entry action that sets sentPower to 0.

  3. Add a transition between Powered and Empty that executes when charge is less than or equal to 3.

Simulate the Model

Observe how the chart transitions between child states.

  1. In the Simulink Editor, ensure the Manual Switch block is set to 1.

  2. In the Simulation tab, click Run to simulate the model. In the first step, the Charge and FastCharge both activate. As charge increases, the chart transitions to child states SlowCharge and Full. In the Symbols pane, charge never exceeds 100.

  3. Return to the top-level Simulink model. Toggle the value of the Manual Switch block by double-clicking the block.

  4. Enter the chart. The chart activates the state Discharge and the child state Powered. When charge decreases below 3, the chart transitions from Powered to Empty. In the Symbols pane, charge never goes below 0.

The battery model is now more realistic. However, the design still has problems. For example, if the connected device requires less than 3.5 watts of power, the battery output does not change.

In the next step in the tutorial, you create a flowchart that varies the power output of the battery system according to the demand of the connected device.

See Also

Topics