In phase 4 of this workflow, you define the transitions between states.

### Build It Yourself or Use the Supplied Model

To add the transitions yourself, work through the exercises in this section. Otherwise, open the supplied model to see how the transitions should appear in the chart. Enter this command at the MATLAB® prompt:

```addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage4Transitions```

### Design Considerations for Defining Transitions Between States

The following sections describe the decisions you make for defining state transitions.

#### Deciding How and When to Transition Between Operating Modes

Transitions create paths for the logic flow of a system from one state to another. When a transition is taken from state A to state B, state A becomes inactive and state B becomes active.

Transitions have direction and are represented in a Stateflow® chart by lines with arrowheads. Transitions are unidirectional, not bidirectional. You must add a transition for each direction of flow between two states.

Exclusive (OR) states require transitions. Recall that no two exclusive states can be active at the same time. Therefore, you need to add transitions to specify when and where control flows from one exclusive state to another.

Typically, parallel (AND) states do not require transitions because they execute concurrently.

The Air Controller chart models a system in which power can cycle on and off and, while power is on, fans can cycle on and off. Six exclusive (OR) states represent these operating modes. To model this activity, you need to add the following transitions between exclusive (OR) states:

• `PowerOff` to `PowerOn`

• `PowerOn` to `PowerOff`

• `FAN1.Off` to `FAN1.On`

• `FAN1.On` to `FAN1.Off`

• `FAN2.Off` to `FAN2.On`

• `FAN2.On` to `FAN2.Off`

#### Deciding Where to Place Default Transitions

Good design practice requires that you specify default transitions for exclusive (OR) states at each level of hierarchy. Default transitions indicate which exclusive (OR) state is to be active when there is ambiguity between two or more exclusive (OR) states at the same level in the Stateflow hierarchy. There are three such areas of ambiguity in the Air Controller chart:

• When the chart wakes up, should power be on or off?

• When `FAN1` becomes active, should it be on or off?

• When `FAN2` becomes active, should it be on or off?

In each case, the initial state should be off so you will add default transitions to the states `PowerOff`, `FAN1.Off`, and `FAN2.Off`.

#### Deciding How to Guard the Transitions

Guarding a transition means specifying a condition, action, or event that allows the transition to be taken from one state to another. Based on the design of the Air Controller chart, here are the requirements for guarding the transitions from one exclusive operating mode to another:

TransitionWhen Should It Occur?How to Guard It

`PowerOff` to `PowerOn`

At regular time intervals

Specify an edge-triggered event

`PowerOn` to `PowerOff`

`FAN1.Off` to `FAN1.On`

When the temperature of the physical plant rises above 120 degrees

Specify a condition based on temperature value

`FAN1.On` to `FAN1.Off`

When the temperature of the physical plant falls below 120 degrees

`FAN2.Off` to `FAN2.On`

When the temperature rises above 150 degrees, a threshold indicating that first fan is not providing the required amount of cooling

`FAN2.On` to `FAN2.Off`

When the temperature falls below 150 degrees

### Drawing the Transitions Between States

In Design Considerations for Defining Transitions Between States, you learned that the following transitions occur in the Air Controller chart:

• Power for the control system can cycle on and off.

• Each fan can cycle on and off.

You will model this activity by drawing transitions between the `PowerOn` and `PowerOff` states and between the `On` and `Off` states for each fan. Follow these steps:

1. Open the model `Stage3Actions` — either the one you created in the previous exercises or the supplied model for stage 3.

To open the supplied model, enter the following command at the MATLAB prompt:

```addpath(fullfile(docroot, 'toolbox', 'stateflow', 'gs', 'examples')) Stage3Actions```

2. Save the model as `Stage4Transitions` in your local work folder.

3. In `Stage4Transitions`, double-click the Air Controller block to open the Stateflow chart.

The chart opens on your desktop.

4. Draw transitions between the `PowerOff` to `PowerOn` states:

1. Move your pointer over the top edge of `PowerOff` until the pointer shape changes to crosshairs.

2. Hold down the left mouse button, drag your pointer to the bottom edge of `PowerOn`, and release the mouse.

You should see a transition pointing from `PowerOff` to `PowerOn`:

3. Follow the same procedure to draw a transition from `PowerOn` to `PowerOff`.

Your chart should now look like this:

5. Follow the procedure described in step 3 to draw the following transitions between the `Off` and `On` states for each fan:

• Transition from `Off` to `On` in `FAN1`

• Transition from `On` to `Off` in `FAN1`

• Transition from `Off` to `On` in `FAN2`

• Transition from `On` to `Off` in `FAN2`

Your chart should now look like this:

6. Save `Stage4Transitions`, but leave the chart open for the next exercise.

In Deciding Where to Place Default Transitions, you learned that you need to add default transitions to `PowerOff`, `FAN1.Off`, and `FAN2.Off`. Follow these steps:

1. In the Stateflow Editor, left-click the default transition icon in the object palette:

2. Move your pointer into the drawing area.

The pointer changes to a diagonal arrow.

3. Place your pointer at the left edge of the `PowerOff` state.

4. When the arrow becomes orthogonal to the edge, release the mouse button.

The default transition attaches to the `PowerOff` state. It appears as a directed line with an arrow at its head and a closed tail:

5. Repeat the same procedure to add default transitions at the top edges of `FAN1.Off` and `FAN2.Off`.

Your chart should now look like this:

Tip

The location of the tail of a default transition determines the state it activates. Therefore, make sure that your default transition fits completely inside the parent of the state that it activates. In the Air Controller chart pictured above, notice that the default transition for `FAN1.Off` correctly resides inside the parent state, `FAN1`. Now consider this chart:

In this example, the tail of the default transition resides in `PowerOn`, not in `FAN1`. Therefore, it will activate `FAN1` instead of `FAN1.Off`.

6. Save `Stage4Transitions`, but leave the chart open for the next exercise.

### Adding Conditions to Guard Transitions

Conditions are expressions enclosed in square brackets that evaluate to true or false. When the condition is true, the transition is taken to the destination state; when the condition is false, the transition is not taken and the state of origin remains active.

As you learned in Deciding How to Guard the Transitions, the fans cycle on and off depending on the air temperature. In this exercise, you will add conditions to the transitions in `FAN1` and `FAN2` that model this behavior.

1. Click the transition from `FAN1.Off` to `FAN1.On`.

The transition appears highlighted and displays a question mark (`?`).

2. Click next to the question mark to display a blinking text cursor.

3. Type the following expression:

`[temp >= 120]`

You may need to reposition the condition for readability. Click outside the condition, then left-click and drag the condition expression to a new location.

4. Repeat these steps to add the following conditions to the other transitions in `FAN1` and `FAN2`:

TransitionCondition
`FAN1.On` to `FAN1.Off`[temp < 120]
`FAN2.Off` to `FAN2.On`[temp >= 150]
`FAN2.On` to `FAN2.Off`[temp < 150]

Your chart should look like this:

5. Save `Stage4Transitions`, but leave the chart open for the next exercise.

### Adding Events to Guard Transitions

Events are nongraphical objects that trigger activities during the execution of a Stateflow chart. Depending on where and how you define events, they can trigger a transition to occur, an action to be executed, and state status to be evaluated. In this exercise, you will define an event that triggers transitions.

As you learned in Deciding How to Guard the Transitions, the control system should power on and off at regular intervals. You model this behavior by first defining an event that occurs at the rising or falling edge of an input signal, and then associating that event with the transitions between the `PowerOn` and `PowerOff` states.

Follow these steps to define an edge-triggered event and associate it with the transitions:

1. From the Stateflow Editor, in the Modeling tab, under Design Data, click .

The Event properties dialog box opens on your desktop:

Note that the event is assigned to trigger port 1.

2. Edit the following properties:

PropertyWhat to Specify
NameChange the name to `SWITCH`.
TriggerSelect `Either` from the drop-down menu so the event can be triggered by either the rising edge or falling edge of a signal.
3. Click to record the changes and close the dialog box.

4. Look back at the model and notice that a trigger port appears at the top of the Stateflow block:

When you define one or more input events for a chart, Stateflow software adds a single trigger port to the block. External Simulink® blocks can trigger the input events via a signal or vector of signals connected to the trigger port.

5. Back in the Stateflow Editor, associate the input event `SWITCH` with the transitions:

1. Select the transition from `PowerOff` to `PowerOn` and click the question mark to get a text cursor.

2. Type the name of the event you just defined, `SWITCH`.

You might need to reposition the event text for readability. If so, click outside the text, left-click the text, and drag it to the desired location.

3. Repeat these steps to add the same event, `SWITCH`, to the transition from `PowerOn` to `PowerOff`.

Your chart should now look something like this:

Now that you have associated these transitions with the event `SWITCH`, the control system will alternately power on and off every time `SWITCH` occurs — that is, every time the chart detects a rising or falling signal edge.

Note that the `sf_aircontrol` model has already defined the pulse signal `SWITCH` in the Signal Builder block at the top level of the model hierarchy:

In the next phase of the workflow, you will connect your Stateflow chart to the `SWITCH` signal to trigger the transitions between power on and power off.

6. Save `Stage4Transitions`.

Where to go next.  Now you are ready to implement an edge-triggered event to wake up the chart at regular intervals. See Implementing the Triggers.

Get trial now