Main Content

Connect Transitions to Create Paths and Flowcharts

You can connect multiple transitions into a single path, a branching path, or a flowchart by using junctions.

In Stateflow®, a path is one or more transitions that connect a source to a destination. You can create a path with multiple transitions by connecting each transition to a junction. Junctions are graphical objects that represent a decision point, and appear as circles on the canvas. For instance, in the chart below, several transitions and junctions form a single path from one state to another.

When a chart evaluates a path, it evaluates each connected transition on the same time step, in source-to-destination order, and executes any condition actions it encounters. If the chart encounters a false condition, it stops evaluating the path. If the chart does not encounter any false conditions along the path, it moves from the source to the destination.

For instance, in the chart above, a path with multiple transitions connects a source state to a destination state. If all conditions on the path are true, the chart sets x to 0, 1, and then 2. Then, on the same step, the chart exits the source state and enters the destination state.

However, if a and c are greater than 0, but b is not, the chart instead sets x to 0 and stops evaluating the path.

You can create multiple transitions to or from a state or junction. If a state or junction has multiple outgoing transitions, the chart displays the evaluation order. If the chart moves along one of the paths, it does not evaluate the remaining paths. You can change the order by right-clicking the transition, clicking Execution Order, and selecting a new order.

For instance, the chart below evaluates the path from state A to state B before the path from state A to state C.

In Stateflow, a flowchart refers to a chart or state whose children consist exclusively of junctions and transitions. All paths in a flowchart must terminate at a single, shared junction. You can create a flowchart at any level of a chart hierarchy.

In this example, you add a flowchart to a model of a rechargeable battery system. The flowchart matches the output of the battery system to the demand of the connected device, without exceeding the limits of the battery.

Open Model

To build the model, follow the instructions in the previous step of the tutorial. Alternatively, use the Open Model button above and open the sfGetStartedFlowchart model.

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

The states Charge and Discharge represent the operating modes of the battery system. Each state contains child states that represent the charging and discharging rates as the battery fills or empties. The input isCharging determines the active state. The data sentPower and charge represent the output wattage and charge level of the battery.

Create Flowchart

Currently, when the battery is simultaneously discharging and powered, it always outputs the same wattage. In the Powered state, create a flowchart that matches the battery output to the demand of the connected device, without exceeding a maximum value.

Add a path that represents the device demanding more power than the battery can provide.

  1. In the Discharge state, remove the entry action.

  2. In the Powered state, remove the during action.

  3. In the Powered state, add the first junction. In the palette, click the Junction icon. Place the junction in the Powered state.

  4. Add a second junction to the right of the first junction.

  5. Add a third junction below the second junction.

  6. Draw a transition from the first junction to the second. Add the condition [deviceDemand>maxPower].

  7. Draw a transition from the second junction to the third. Add the action {sentPower=maxPower;}.

Add a path that represents power demand within the battery limits.

  1. Add a fourth junction below the first junction.

  2. Draw a transition from the first junction to the fourth. Add the action {sentPower=deviceDemand;}.

  3. Create a transition from the third junction to the fourth.

The numbers on the first junction indicate the evaluation order. The transition labeled [deviceDemand>maxPower] has the order label 1, which indicates it evaluates first. The transition labeled {sentPower=deviceDemand;} has the order label 2, which indicates it evaluates second.

A terminal junction is a junction without any outgoing transitions. Connect both paths to a shared terminal junction. In the transition to the terminal junction, decrease the battery charge in proportion to the output wattage.

  1. Add a fifth junction below the fourth junction.

  2. Create a transition from the fourth junction to the fifth junction. Add the action {charge=charge-sentPower;}.

Evaluate During Active Steps by Using Inner Transitions

Inner transitions are the graphical equivalent of a during action. When a state contains an inner transition, the inner transition evaluates every step the state is active, but not on steps the state becomes active or inactive. If a state contains both inner transitions and transitions between child states, the inner transitions evaluate first. You can create an inner transition by drawing a transition from the edge of a state to an object within that state.

Draw an inner transition from the edge of the Powered state to the first junction.

Define Chart Data

Define the data you created in the flowchart.

  1. In the Symbols pane, in the maxPower row, under Type, click the icon and select Local Data. Set the Value to 3.5.

  2. In the deviceDemand row, under Type, click the icon and select Input Data.

  3. In the charge row, set the Value to 100.

Add a block that connects to the deviceDemand input port.

  1. Open the top-level model.

  2. Add a Sine Wave block. Connect the output port to the deviceDemand port of the Battery chart.

  3. Specify that the sine wave starts at 0 and reaches a maximum value of 5. Double-click the block to open the block dialog box, then set the Amplitude and Bias parameters to 2.5. Set the Phase parameter to -pi/4.

  4. Right-click the signal line from the Sine Wave block to the Battery chart and click Log selected signals. Repeat this step for the signal line from the Battery chart to the Scope block.

Simulate Model

Simulate the model and observe the results.

  1. To simulate the model, in the Simulation tab, click Run.

  2. To open the Simulation Data Inspector, in the Simulation tab, click Data Inspector.

  3. In the Inspect tab, select Battery:1 and Sine Wave:1.

The output wattage of the battery matches the rise and fall of the power draw, to a maximum of 3.5. Near the end of the simulation, the battery runs out of power, and the output wattage drops to 0.

The battery system outputs power as expected. However, if the battery runs out of power, the system can no longer operate.

In the next step of the tutorial, you add a non-rechargeable emergency battery that maintains essential functions if the main battery becomes empty. To model the simultaneous operation of the batteries, you use parallel states and events.

See Also

Topics