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.
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
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.
For event-based temporal logic, use the operators described in this table.
Operator | Syntax | Description |
---|---|---|
after |
| Returns In a chart with no input events,
Resets the counter for |
at |
| Returns In a chart with no input
events, Resets the counter for
|
before |
| Returns In a chart with no input events,
Resets the counter for |
every |
| Returns In a chart with no input
events, Resets the counter for
|
temporalCount |
| Increments by 1 and returns a positive integer value for each occurrence
of the base event In a
chart with no input events, Resets the counter for |
You can use quotation marks to enclose the keyword 'tick'
. For
example, after(5,'tick')
is equivalent to
after(5,tick)
.
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.
These examples illustrate usage of event-based temporal logic in state actions and transitions.
Operator | Usage | Example | Description |
---|---|---|---|
| State action ( |
| A status message appears during each |
| Transition |
| A transition out of the associated state occurs only on broadcast of a
|
| State action ( |
| The |
| Transition |
| A transition out of the associated state occurs only on broadcast of a
|
| State action ( |
| A status message appears at exactly 10 |
| Transition |
| A transition out of the associated state occurs only on broadcast of a
|
| State action ( |
| A status message appears every 5 |
| State action ( |
| 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
|
You can use one of two notations to express event-based temporal logic.
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
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
Notation | Usage | Example | Description |
---|---|---|---|
Event | State action ( |
| The |
Event | Transition |
| A transition out of the associated state occurs if the
|
Conditional | Transition |
| A transition out of the associated state occurs only on broadcast of an
|
You must use event notation in state actions because the syntax of state actions does not support conditional notation.
For absolute-time temporal logic, use the operators described in this table.
Operator | Syntax | Description |
---|---|---|
after |
| Returns Resets the counter for
|
before |
| Returns Resets the counter for
|
every |
| Returns Resets the counter for
Use of
|
temporalCount |
| Counts and returns the number of specified seconds
( Resets the counter for
|
elapsed |
| Equivalent to Resets the counter for
|
count |
| Returns the number of ticks after the conditional expression,
In a Simulink® model, the value for |
duration |
| Returns the number of seconds after the conditional expression,
|
You can use quotation marks to enclose the keywords 'sec'
,
'msec'
, and 'usec'
. For example,
after(5,'sec')
is equivalent to after(5,sec)
.
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.
These examples illustrate absolute-time temporal logic in state actions and transitions.
Operator | Usage | Example | Description |
---|---|---|---|
| State action ( |
| After 12.3 seconds of simulation time since activation of the state,
|
| Transition |
| After 8 milliseconds of simulation time have passed since activation of the state, a transition out of the associated state occurs. |
| Transition |
| After 5 microseconds of simulation time have passed since activation of the state, a transition out of the associated state occurs. |
| Transition |
| If the variable |
| State action ( |
| This action counts and returns the number of seconds of simulation time that pass between activation and deactivation of the state. |
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.
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
.
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.
after
Operator to Replace the at
OperatorIf 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.
after
Operator to Replace the every
OperatorIn 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.
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).
after | at | before | count | duration | elapsed | every | temporalCount