Accelerating the pace of engineering and science

# Documentation

## Control Chart Execution Using Temporal Logic

### What Is 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 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:

• 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 or wakeup event to denote the implicit event of a chart waking up.

• Temporal logic operators can appear only in:

• 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: the state in which the action appears or from which the transition originates.

• You must use event notation (see Notations for Event-Based Temporal Logic) to express event-based temporal logic in state actions.

### 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

• 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 the use of conditional notation.

### Operators for Absolute-Time Temporal Logic

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

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 specified seconds (sec), milliseconds (msec), or microseconds (usec) of simulation time have elapsed since activation of the associated state. Otherwise, the operator returns false.

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 n specified seconds (sec), milliseconds (msec), or microseconds (usec) of simulation time have elapsed since activation of the associated state. Otherwise, the operator returns false.

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

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.

### Define Time Delays with Temporal Logic

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.

#### Example of Defining Time Delays

The following continuous-time chart defines two absolute time delays in transitions.

Chart execution occurs as follows:

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

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

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

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

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

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

#### Example of Detecting Elapsed Time

In the following modelmodel, 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...A transition occurs from...
Before t = 2Start to Fast
Between t = 2 and t = 5Start to Good
After t = 5Start to Slow

#### Advantages of Using Absolute-Time Temporal Logic for Delays

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.

• Absolute-time temporal logic supports seconds (sec), milliseconds (msec) and microseconds (usec) syntax for the before and after operators.

### 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)

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.

### Run a Model That Uses Absolute-Time Temporal Logic

The sf_boiler model shows the use of absolute-time temporal logic to implement a bang-bang controller. To run the model:

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

2. Start simulation of the model.

### Absolute-Time Temporal Logic in Conditionally Executed Subsystems

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.

#### Model with Absolute-Time Temporal Logic in an Enabled Subsystem

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

Model with Enabled SubsystemChart in Enabled Subsystem

The Signal Builder block provides the following 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. 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.

 Note:   These semantics also apply to the before operator.

### How Sample Time Affects Chart Execution

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

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.

 Note:   This behavior also applies 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 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 Record State Activity 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.