Control Chart Execution by Using Temporal Logic

Temporal logic controls execution of a 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 tracks recurring event. 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

  • Temporal logic operators can appear in only:

    • State actions

    • Transitions that originate from states

    • Transition segments that originate from junctions when the full transition path connects two states

    Note

    This restriction means that you cannot use temporal logic operators in default transitions or flow chart transitions.

    Every temporal logic operator has an associated state, which is the state in which the action appears or from which the transition originates.

  • Use event notation to express event-based temporal logic in state actions. See Notation for Event-Based Temporal Logic.

  • You can use any explicit or implicit event as a base event for a temporal operator. A base event is a recurring event on which a temporal operator operates.

  • For a chart with no input events, you can use the tick event to denote the implicit event of a chart waking up.

  • Use one of the keywords sec , msec, or usec to define simulation time in seconds, milliseconds, or microseconds that have elapsed since activation of a state. These keywords are valid only in state actions and in transitions that originate from states.

    Use absolute-time temporal logic instead of the implicit tick event for these reasons:

    • Delay expressions that use absolute-time temporal logic are independent of the sample time of the model. However, the tick event is dependent on sample time.

    • Absolute-time temporal logic works in charts that have function-call input events. The tick event does not work in charts with function-call inputs.

Operators for Event-Based Temporal Logic

For event-based temporal logic, use the operators described in this table.

OperatorSyntaxDescription
after

after(n,E)

E is the base event for the after operator and n is either:

  • 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) 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.

at

at(n,E)

E is the base event for the at operator and n is either:

  • 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) 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.

before

before(n,E)

E is the base event for the before operator and n is either:

  • 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) 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.

every

every(n,E)

E is the base event for the every operator and n is either:

  • 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) returns true if the chart has woken up an integer multiple of n times since activation of the associated state.

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

temporalCount

temporalCount(E)

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.

In a chart with no input events, temporalCount(tick) returns the number of times that the chart has woken up since activation of the associated state.

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

You can use quotation marks to enclose the keyword 'tick'. For example, after(5,'tick') is equivalent to after(5,tick).

Note

Temporal logic operators compare the threshold n to an internal counter of integer type. If n is a fixed-point number defined by using either a slope that is not an integer power of two or a nonzero bias, then the comparison can yield unexpected results due to rounding. For more information, see Relational Operations for Fixed-Point Data.

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.

Notation 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

  • tlo is a Boolean temporal logic operator (after, before, at, or every)

  • n is the occurrence count of the operator

  • E is the base event of the operator

  • C is an optional condition expression

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

  • E1 is any nonbase event

  • tlo is a Boolean temporal logic operator (after, before, at, or every)

  • n is the occurrence count of the operator

  • E2 is the base event of the operator

  • C is an optional condition expression

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.

Note

You must use event notation in state actions because the syntax of state actions does not support conditional notation.

Operators for Absolute-Time Temporal Logic

For absolute-time temporal logic, use the operators described in this table.

OperatorSyntaxDescription
after

after(n,sec)

after(n,msec)

after(n,usec)

n is any positive number or expression. sec, msec, and usec are keywords that denote the simulation time elapsed since activation of the associated state.

Returns true if n units of simulation time have elapsed since activation of the associated state. Otherwise, the operator returns false. Specify simulation time in seconds (sec), milliseconds (msec), or microseconds (usec).

Resets the counter for sec, msec, and usec to 0 each time the associated state reactivates.

before

before(n,sec)

before(n,msec)

before(n,usec)

n is any positive number or expression. sec, msec, and usec are keywords that denote the simulation time elapsed since activation of the associated state.

Returns true if fewer than n units of simulation time have elapsed since activation of the associated state. Otherwise, the operator returns false. Specify simulation time in seconds (sec), milliseconds (msec), or microseconds (usec).

Resets the counter for sec, msec, and usec to 0 each time the associated state reactivates.

every

every(n,sec)

every(n,msec)

every(n,usec)

n is any positive number or expression. sec, msec, and usec are keywords that denote the simulation time elapsed since activation of the associated state.

Returns true every n units of simulation time since activation of the associated state. Otherwise, the operator returns false. Specify simulation time in seconds (sec), milliseconds (msec), or microseconds (usec).

Resets the counter for sec, msec, and usec to 0 each time the associated state reactivates.

Use of every as an absolute-time temporal logic operator is supported only in standalone charts for execution as MATLAB® objects.

temporalCount

temporalCount(sec)

temporalCount(msec)

temporalCount(usec)

sec, msec, and usec are keywords that denote the simulation time elapsed since activation of the associated state.

Counts and returns the number of specified seconds (sec), milliseconds (msec), or microseconds (usec) of simulation time that have elapsed since activation of the associated state.

Resets the counter for sec, msec and usec to 0 each time the associated state reactivates.

elapsed

elapsed(sec)

Equivalent to temporalCount(sec). Returns the simulation time in seconds (sec) that has elapsed since the activation of the associated state.

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

count

count(C)

Returns the number of ticks after the conditional expression, C, becomes true. The count operator is reset if the conditional expression becomes false. If the count operator is used within a state, it is reset when the state that contains it is entered. If the count operator is used on a transition, it is reset when the source state for that transition is entered.

In a Simulink® model, the value for count depends on the step size. Changing the solver or step size for your model affects the result for Stateflow® charts that include the count operator.

duration

duration(C)

Returns the number of seconds after the conditional expression, C, becomes true. The duration operator is reset if the conditional expression becomes false. If the duration operator is used within a state, it is reset when the state that contains it is entered. If the duration operator is used on a transition, it is reset when the source state for that transition is entered.

You can use quotation marks to enclose the keywords 'sec', 'msec', and 'usec'. For example, after(5,'sec') is equivalent to after(5,sec).

Note

Temporal logic operators compare the threshold n to an internal counter of integer type. If n is a fixed-point number defined by using either a slope that is not an integer power of two or a nonzero bias, then the comparison can yield unexpected results due to rounding. For more information, see Relational Operations for Fixed-Point Data.

Examples of Absolute-Time Temporal Logic

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

OperatorUsageExampleDescription

after

State action (on after)

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

After 12.3 seconds of simulation time since activation of the state, temp variable becomes LOW .

after

Transition

after(8,msec)

After 8 milliseconds of simulation time have passed since activation of the state, a transition out of the associated state occurs.

after

Transition

after(5,usec)

After 5 microseconds of simulation time have passed since activation of the state, a transition out of the associated state occurs.

before

Transition

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

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

temporalCount

State action (exit)

exit: y = temporalCount(sec);

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

Example of Defining Time Delays

This continuous-time chart defines two absolute time delays in transitions.

Chart execution consists of these steps:

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

  • After 5.33 milliseconds of simulation time, the transition from Input to Output occurs.

  • The state Input deactivates, and then the state Output activates.

  • After 10.5 seconds of simulation time, the transition from Output to Input occurs.

  • The state Output deactivates, and then the state Input activates.

  • Steps 2 through 5 are repeated, until the simulation ends.

If a chart has a discrete sample time, any action in the chart occurs at integer multiples of this sample time. For example, suppose that you change the configuration parameters so that the Simulink® solver uses a fixed step of size 0.1 seconds. Then, the first transition from state Input to state Output occurs at t = 0.1 seconds. This behavior applies because the solver does not wake the chart at exactly t = 5.33 milliseconds. Instead, the solver wakes the chart at integer multiples of 0.1 seconds, such as t = 0.0 and 0.1 seconds.

Example of Detecting Elapsed Time

In this model, the Step block provides a unit step input to the chart.

The chart determines when the input u equals 1:

  • If the input equals 1 before t = 2 seconds, a transition occurs from Start to Fast.

  • If the input equals 1 between t = 2 and t = 5 seconds, a transition occurs from Start to Good.

  • If the input equals 1 after t = 5 seconds, a transition occurs from Start to Slow.

Example of Absolute-Time Temporal Logic in an Enabled Subsystem

You can use absolute-time temporal logic in a chart that resides in a conditionally executed subsystem. 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.

This model has an enabled subsystem with the States when enabling parameter set to held.

The subsystem contains a chart that uses the after operator.

The Signal Builder block provides an input signal with these characteristics:

  • Signal enables subsystem at t = 0.

  • Signal disables subsystem at t = 2.

  • Signal reenables subsystem at t = 6.

This graph shows the total time elapsed in an enabled state (either A or B).

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. 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 chart reinitializes completely when the subsystem is reenabled. In other words, default transitions execute and any temporal logic counters reset to 0.

These semantics also apply to the before operator.

Best Practices for 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 in this chart.

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

In a Stateflow model, 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 in this chart.

Also add a history junction in the state so that the chart remembers the state settings prior to each self-loop transition. See Record State Activity by Using History Junctions.

Use Charts with Discrete Sample Times for More Efficient Code Generation

The code generated for discrete charts that are not inside a triggered or enabled subsystem uses integer counters to track time instead of Simulink provided time. This allows for more efficient code generation in terms of overhead and memory, as well as enabling this code for use in software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulation modes. For more information, see SIL and PIL Simulations (Embedded Coder).

See Also

| | | | | | |

Related Topics