Using Temporal Logic in State Actions and Transitions

What Is Temporal Logic?

Temporal logic controls execution of a Stateflow® chart in terms of time. In state actions and transitions, you can use two types of temporal logic: event-based and absolute-time. Event-based temporal logic keeps track of recurring events, and absolute-time temporal logic defines time periods based on the simulation time of your chart. To operate on these recurring events or simulation time, you use built-in functions called temporal logic operators.

Rules for Using Temporal Logic Operators

These rules apply to the use of temporal logic operators:

Operators for Event-Based Temporal Logic

For event-based temporal logic, use the operators as described below.

OperatorSyntaxDescription

after

after(n, E)

where E is the base event for the after operator and n is one of the following:

  • A positive integer

  • An expression that evaluates to a positive integer value

Returns true if the base event E has occurred at least n times since activation of the associated state. Otherwise, the operator returns false.

In a chart with no input events, after(n, tick) or after(n, wakeup) returns true if the chart has woken up n times or more since activation of the associated state.

Resets the counter for E to 0 each time the associated state reactivates.

before

before(n, E)

where E is the base event for the before operator and n is one of the following:

  • A positive integer

  • An expression that evaluates to a positive integer value

Returns true if the base event E has occurred fewer than n times since activation of the associated state. Otherwise, the operator returns false.

In a chart with no input events, before(n, tick) or before(n, wakeup) returns true if the chart has woken up fewer than n times since activation of the associated state.

Resets the counter for E to 0 each time the associated state reactivates.

at

at(n, E)

where E is the base event for the at operator and n is one of the following:

  • A positive integer

  • An expression that evaluates to a positive integer value

Returns true only at the nth occurrence of the base event E since activation of the associated state. Otherwise, the operator returns false.

In a chart with no input events, at(n, tick) or at(n, wakeup) returns true if the chart has woken up for the nth time since activation of the associated state.

Resets the counter for E to 0 each time the associated state reactivates.

every

every(n, E)

where E is the base event for the every operator and n is one of the following:

  • A positive integer

  • An expression that evaluates to a positive integer value

Returns true at every nth occurrence of the base event E since activation of the associated state. Otherwise, the operator returns false.

In a chart with no input events, every(n, tick) or every(n, wakeup) returns true if the chart has woken up an integer multiple n times since activation of the associated state.

Resets the counter for E to 0 each time the associated state reactivates. Therefore, this operator is useful only in state actions and not in transitions.

temporalCount

temporalCount(E)

where E is the base event for the temporalCount operator.

Increments by 1 and returns a positive integer value for each occurrence of the base event E that takes place after activation of the associated state. Otherwise, the operator returns a value of 0.

Resets the counter for E to 0 each time the associated state reactivates.

Examples of Event-Based Temporal Logic

These examples illustrate usage of event-based temporal logic in state actions and transitions.

OperatorUsageExampleDescription

after

State action

(on after)

on after(5, CLK): status('on');

A status message appears during each CLK cycle, starting 5 clock cycles after activation of the state.

after

Transition

ROTATE[after(10, CLK)]

A transition out of the associated state occurs only on broadcast of a ROTATE event, but no sooner than 10 CLK cycles after activation of the state.

before

State action

(on before)

on before(MAX, CLK): temp++;

The temp variable increments once per CLK cycle until the state reaches the MAX limit.

before

Transition

ROTATE[before(10, CLK)]

A transition out of the associated state occurs only on broadcast of a ROTATE event, but no later than 10 CLK cycles after activation of the state.

at

State action

(on at)

on at(10, CLK): status('on');

A status message appears at exactly 10 CLK cycles after activation of the state.

at

Transition

ROTATE[at(10, CLK)]

A transition out of the associated state occurs only on broadcast of a ROTATE event, at exactly 10 CLK cycles after activation of the state.

every

State action

(on every)

on every(5, CLK): status('on');

A status message appears every 5 CLK cycles after activation of the state.

temporalCount

State action

(during)

du: y = mm[temporalCount(tick)];

This action counts and returns the integer number of ticks that have elapsed since activation of the state. Then, the action assigns to the variable y the value of the mm array whose index is the value that the temporalCount operator returns.

Notations for Event-Based Temporal Logic

You can use one of two notations to express event-based temporal logic.

Event Notation

Use event notation to define a state action or a transition condition that depends only on a base event.

Event notation follows this syntax:

tlo(n, E)[C]

where

Conditional Notation

Use conditional notation to define a transition condition that depends on base and nonbase events.

Conditional notation follows this syntax:

E1[tlo(n, E2) && C]

where

Examples of Event and Conditional Notation

NotationUsageExampleDescription

Event

State action

(on after)

on after(5, CLK): temp = WARM;

The temp variable becomes WARM 5 CLK cycles after activation of the state.

Event

Transition

after(10, CLK)[temp == COLD]

A transition out of the associated state occurs if the temp variable is COLD, but no sooner than 10 CLK cycles after activation of the state.

Conditional

Transition

ON[after(5, CLK) && temp == COLD]

A transition out of the associated state occurs only on broadcast of an ON event, but no sooner than 5 CLK cycles after activation of the state and only if the temp variable is COLD.

Operators for Absolute-Time Temporal Logic

For absolute-time temporal logic, use the operators as described below.

OperatorSyntaxDescription

after

after(n, sec)

where n is any positive number or expression and sec is a keyword that denotes the simulation time elapsed since activation of the associated state.

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

Resets the counter for sec to 0 each time the associated state reactivates.

before

before(n, sec)

where n is any positive number or expression and sec is a keyword that denotes the simulation time elapsed since activation of the associated state.

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

Resets the counter for sec to 0 each time the associated state reactivates.

temporalCount

temporalCount(sec)

where sec is a keyword that denotes the simulation time elapsed since activation of the associated state.

Counts and returns the number of seconds of simulation time that have elapsed since activation of the associated state.

Resets the counter for sec to 0 each time the associated state reactivates.

Defining Time Delays

Use the keyword sec to define simulation time that has elapsed since activation of a state. For example, the continuous-time chart below defines two absolute time delays in transitions. (See Modeling Continuous-Time Systems in Stateflow® Charts for information about modeling continuous-time systems.)

Chart execution occurs as follows:

  1. When the chart awakens, the state Input activates first.

  2. After 5.33 seconds of simulation time pass, the transition from Input to Output occurs.

  3. The state Input deactivates, and the state Output activates.

  4. After another 10.5 seconds of simulation time pass, the transition from Output to Input occurs.

  5. The state Output deactivates, and the state Input activates.

  6. Steps 2–5 repeat, until the simulation ends.

Examples of Absolute-Time Temporal Logic

These examples illustrate usage of absolute-time temporal logic in state actions and transitions.

OperatorUsageExampleDescription

after

State action

(on after)

on after(12.3, sec): temp = LOW;

The temp variable becomes LOW after 12.3 seconds of simulation time have passed, since activation of the state.

after

Transition

after(12.34, sec)

A transition out of the associated state occurs after 12.34 seconds of simulation time have passed, since activation of the state.

before

Transition

[temp > 75 && before(12.34, sec)]

A transition out of the associated state occurs if the variable temp exceeds 75 and fewer than 12.34 seconds have elapsed since activation of the state.

temporalCount

State action

(exit)

ex: y = temporalCount(sec);

This action counts and returns the number of seconds of simulation time that pass between activation and deactivation of the state.

Running a Demo Model

The sf_boiler demo illustrates the use of absolute-time temporal logic to implement a bang-bang controller. To run the model, follow these steps:

  1. Click sf_boiler or type sf_boiler at the MATLAB® command prompt.

  2. Select Simulation > Start in the Simulink® model window.

Using Absolute-Time Temporal Logic in a Conditionally Executed Subsystem

You can use absolute-time temporal logic in a chart that resides in a conditionally executed subsystem. (See Creating Conditional Subsystems in the Simulink documentation for details.) When the subsystem is disabled, the chart becomes inactive and the temporal logic operator pauses while the chart is asleep. The operator does not continue to count simulation time until the subsystem is reenabled and the chart is awake.

Example of Absolute-Time in an Enabled Subsystem

Suppose you have an enabled subsystem that contains a chart with the after operator. In the subsystem, the parameter States when enabling is set to held.

Model with Enabled SubsystemChart in Enabled Subsystem

The Signal Builder block provides this input signal to the subsystem.

The total time elapsed in an enabled state (both A and B) is as follows.

When the input signal enables the subsystem at time t = 0, the state A becomes active, or enabled. While the state is active, the time elapsed increases. However, when the subsystem is disabled at t = 2, the chart goes to sleep and state A becomes inactive.

For 2 < t < 6, the time elapsed in an enabled state stays frozen at 2 seconds because neither state is active. When the chart wakes up at t = 6, state A becomes active again and time elapsed starts to increase. Note that the transition from state A to state B depends on the time elapsed while state A is enabled, not on the simulation time. Therefore, state A stays active until t = 9, so that the time elapsed in that state totals 5 seconds.

When the transition from A to B occurs at t = 9, the output value y changes from 0 to 1.

This model behavior applies only to subsystems where you set the Enable block parameter States when enabling to held. If you set the parameter to reset, the Stateflow chart reinitializes completely when the subsystem is reenabled. In other words, default transitions execute and any temporal logic counters reset to 0.

How Sample Time Affects Chart Execution

If a Stateflow chart has a discrete sample time, any action in the chart occurs at integer multiples of this sample time.

A Simple Example

Suppose you have a chart with a discrete sample time of 0.1 seconds:

State A becomes active at time t = 0, and the transition to state B occurs at t = 2.2 seconds. This behavior applies because the Simulink solver does not wake the chart at exactly t = 2.15 seconds. Instead, the solver wakes the chart at integer multiples of 0.1 seconds, such as t = 2.1 and 2.2 seconds.

Tips for Using Absolute-Time Temporal Logic

Use the after Operator to Replace the at Operator

If you use the at operator with absolute-time temporal logic, an error message appears when you try to simulate your model. Use the after operator instead.

Suppose that you want to define a time delay using the transition at(5.33, sec).

Change the transition to after(5.33, sec), as shown below.

Use an Outer Self-Loop Transition with the after Operator to Replace the every Operator

If you use the every operator with absolute-time temporal logic, an error message appears when you try to simulate your model. Use an outer self-loop transition with the after operator instead.

Suppose that you want to print a status message for an active state every 2.5 seconds during chart execution, as shown in the state action of Check_status.

Replace the state action with an outer self-loop transition, as shown below.

You must also add a history junction in the state so that the chart remembers the state settings prior to each self-loop transition. (See Using History Junctions to Extend Charts and States.)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS