Schedule Chart Actions by Using Temporal Logic

To define the behavior of a Stateflow® chart in terms of simulation time, include temporal logic operators in the state and transition actions of the chart. Temporal logic operators are built-in functions that can tell you the length of time that a state remains active or that a Boolean condition remains true. With temporal logic, you can control the timing of:

  • Transitions between states

  • Function calls

  • Changes in variable values

For more information, see Define Chart Behavior by Using Actions.

Temporal Logic Operators

The most common operators for absolute-time temporal logic are after, elapsed, and duration.

OperatorSyntaxDescription

after

after(n,sec)

Returns true if n seconds of simulation time have elapsed since the activation of the associated state. Otherwise, the operator returns false.

elapsed

elapsed(sec)

Returns the number of seconds of simulation time that have elapsed since the activation of the associated state.

duration

duration(C)

Returns the number of seconds of simulation time that have elapsed since the Boolean condition C becomes true.

Each operator resets its associated timer to zero every time that:

  • The state containing the operator reactivates.

  • The source state for the transition containing the operator reactivates.

  • The Boolean condition in a duration operator becomes false.

Note

Some operators, such as after, support event-based temporal logic and absolute-time temporal logic in seconds (sec), milliseconds (msec), and microseconds (usec). For more information, see Control Chart Execution by Using Temporal Logic.

Example of Temporal Logic

This example uses temporal logic to model a bang-bang controller that regulates the internal temperature of a boiler.

The example consists of a Stateflow chart and a Simulink® subsystem. The Bang-Bang Controller chart compares the current boiler temperature to a reference set point and determines whether to turn on the boiler. The Boiler Plant Model subsystem models the dynamics inside the boiler, increasing or decreasing its temperature according to the status of the controller. The boiler temperature then goes back into the controller chart for the next step in the simulation.

The Bang-Bang Controller chart uses the temporal logic operator after to:

  • Regulate the timing of the bang-bang cycle as the boiler alternates between on and off.

  • Control a status LED that flashes at different rates depending on the operating mode of the boiler.

The timers defining the behavior of the boiler and LED subsystems operate independently of one another without blocking or disrupting the simulation of the controller.

Timing of Bang-Bang Cycle

The Bang-Bang Controller chart contains a pair of substates that represent the two operating modes of the boiler, On and Off. The chart uses the active state output data boiler to indicate which substate is active.

Chart modeling a bang-bang controller. Subcharts appear as opaque boxes to hide the low-level details of the chart.

The labels on the transitions between the On and Off substates define the behavior of the bang-bang controller.

TransitionLabelDescription
From On to Offafter(20,sec)Transition to the Off state after spending 20 seconds in the On state.
From Off to Onafter(40,sec)[cold()]When the boiler temperature is below the reference set point (when the graphical function cold() returns true), transition to the On state after spending at least 40 seconds in the Off state.
From On to Off[Heater.On.warm()]When the boiler temperature is at or above the reference set point (when the graphical function Heater.On.warm() returns true), transition to the Off state.

As a result of these transition actions, the timing of the bang-bang cycle depends on the current temperature of the boiler. At the start of the simulation, when the boiler is cold, the controller spends 40 seconds in the Off state and 20 seconds in the On state. At time t = 478 seconds, the temperature of the boiler reaches the reference point. From that point on, the boiler has to compensate only for the heat lost while in the Off state. The controller then spends 40 seconds in the Off state and 4 seconds in the On state.

Simulation Data Inspector showing the output of the chart.

Timing of Status LED

The Off state contains a substate Flash with a self-loop transition guarded by the action after(5,sec). Because of this transition, when the Off state is active, the substate executes its entry action and calls the graphical function flash_LED every 5 seconds. The function toggles the value of the output symbol LED between 0 and 1.

The Off substate.

The On state calls the graphical function flash_LED as a state action of type en,du. When the On state is active, it calls the function at every time step of the simulation (in this case, every second), toggling the value of the output symbol LED between 0 and 2.

The On substate.

As a result, the timing of the status LED depends on the operating mode of the boiler. For example:

  • From t = 0 to t = 40 seconds, the boiler is off and the LED signal alternates between 0 and 1 every 5 seconds.

  • From t = 40 to t = 60 seconds, the boiler is on and the LED signal alternates between 0 and 2 every second.

  • From t = 60 to t = 100 seconds, the boiler is once again off and the LED signal alternates between 0 and 1 every 5 seconds.

Simulation Data Inspector showing the output of the chart.

Explore the Example

Use additional temporal logic to investigate how the timing of the bang-bang cycle changes as the temperature of the boiler approaches the reference set point.

  1. Enter new state actions that call the elapsed and duration operators.

    • In the On state, let Timer1 be the length of time that the On state is active:

      en,du,ex: Timer1 = elapsed(sec)

    • In the Off state, let Timer2 be the length of time that the boiler temperature is at or above the reference set point:

      en,du,ex: Timer2 = duration(temp>=reference)

    The label en,du,ex indicates that these actions take place whenever the corresponding state is active.

  2. In the Symbols pane, click Resolve Undefined Symbols . The Stateflow Editor resolves the symbols Timer1 and Timer2 as output data .

  3. Enable logging for each of these symbols. In the Symbols pane, select each symbol. In the Property Inspector, under Logging, select Log signal data.

    • Timer1

    • Timer2

  4. In the Simulation tab, click Run .

  5. In the Simulation tab, under Review Results, click Data Inspector .

  6. In the Simulation Data Inspector, display the signals boiler and Timer1 in the same set of axes. The plot shows that:

    • The On phase of the bang-bang cycle typically lasts 20 seconds when the boiler is cold and 4 seconds when the boiler is warm.

    • The first time that the boiler reaches the reference temperature, the cycle is interrupted prematurely and the controller stays in the On state for only 18 seconds.

    • When the boiler is warm, the first cycle is slightly shorter than the subsequent cycles, as the controller stays in the On state for only 3 seconds.

    Simulation Data Inspector showing the output of the chart.

  7. In the Simulation Data Inspector, display the signals boiler and Timer2 in the same set of axes. The plot shows that:

    • Once the boiler is warm, it typically takes 9 seconds to cool in the Off phase of the bang-bang cycle.

    • The first time that the boiler reaches the reference temperature, it takes more than twice as long to cool (19 seconds).

    Simulation Data Inspector showing the output of the chart.

The shorter cycle and longer cooling time are a consequence of the substate hierarchy inside the On state. When the boiler reaches the reference temperature for the first time, the transition from HIGH to NORM keeps the controller on for an extra time step, resulting in a warmer-than-normal boiler. In later cycles, the history junction causes the On phase to start with an active NORM substate. The controller then turns off immediately after the boiler reaches the reference temperature, resulting in a cooler boiler.

See Also

| |

Related Examples

More About