Stateflow User's Guide Previous page   Next Page

Using Junctions in Flow Diagrams

In Simulating Junction Behavior, you see the convenience of a junction in providing alternate paths to complete a transition between states. But you can use junctions without states to provide flow diagrams. Flow diagrams are "visible programming" that let you perform logical operations without using a state. In this section, you see a few examples of flow diagrams and how they can be useful to you.

The simplest flow diagram is a default transition into a junction. You have already seen this flow diagram in Adding a Graphical Function for Convenience. You use it to execute whatever actions you attach to the default transition. The following is an example of the simplest flow diagram:

When this diagram receives an update, the default transition is taken into the junction. Because there is no other flow out of the junction, it is a terminating junction. When Stateflow encounters a terminating junction, the chart is exited altogether. When the next update occurs, execution starts over again with the default transition. This is different from a state that stays active between updates.

The benefit of flow diagrams is their ability to perform complex logic in a visible environment. For example, the following flow diagram behaves like an if statement:

This flow diagram would have the following code representation:

To test the behavior of this flow diagram, do the following:

  1. Open the model SFcontrol2.mdl.
  2. Open the Stateflow block and clear the diagram.
  3. Enter the preceding flow diagram.
  4. Save the model as SFcontrol_if.mdl.
  1. Note that the data speed is already defined for SFcontrol2.mdl (and now SFcontrol_if.mdl) and initialized to zero by default.

  1. Choose Start from the Simulink window Simulation menu to start simulation of SFcontrol_if.mdl.
  2. In the Simulink model, double-click the Manual Switch to move it from the -1 pole to the 1 pole to send an update event:

    The default transition is taken into the first junction.

    The only outgoing transition from the first junction has no condition, so it is taken.

    Remember that transitions with conditions have preference over unconditional transitions. Because speed = 0, the condition [speed==0] is true, and the outgoing transition to the right is taken.

    The only outgoing transition has no condition, so it is taken. Taking it executes the condition action {speed++}, which adds 1 to the value of speed, which is now 1.

    The only outgoing transition has no condition, so it is taken.

    The only outgoing transition has no condition, so it is taken. Its destination is a terminating junction, a junction with no outgoing transitions. This ends execution of the diagram.

  3. In the Simulink model, double-click the Manual Switch to move it from the 1 pole to the -1 pole to send another update event.

    Because speed is no longer equal to 0, when the chart is updated again, the alternate path is taken.

The Display block in Simulink reflects the resulting value for speed.

Besides the flow diagram for an if statement, there are a number of other flow diagram types. For example, you can add an action to the alternate path of the if diagram to create an if-else diagram.

IF-ELSE
  • if (speed == 0){
        speed++;
    }
    else{
        speed--;
    }
    

Here are a few examples of other flow diagrams that you can try on your own. Don't forget to define the counter data i.

FOR loop
  • for (i=0;i<num_loops;i++){
        speed++;
    }
    

WHILE
  • while (i < 3){
        speed++;
        i++;
    }
    


Previous page  Introducing Stateflow Semantics Controlling with Superstates Next page

© 1994-2005 The MathWorks, Inc.