主要内容

Control When Charts Execute by Using Temporal Logic Operators

You can control when Stateflow charts execute by using temporal logic operators. Temporal logic operators track events, count occurrences, and measure elapsed time. You can apply these operators in state actions and transitions to create timing-dependent behaviors, establish delays between states, detect input values at specific times, and filter transient signals.

Use Temporal Logic Operators

You can use event-based operators to track events, or absolute-time operators to track elapsed time. For a list of temporal operators, see Control Chart Execution by Using Temporal Logic.

Event-Based Temporal Logic Operators

Use these operators to track recurring events:

  • after(n,E): Returns true if event E occurs at least n times after the state becomes active.

  • at(n,E): Returns true if event E occurs exactly n times after the state becomes active.

  • before(n,E): Returns true if event E occurs fewer than n times after the state becomes active.

  • every(n,E): Returns true at every nth occurrence of event E.

  • temporalCount(E): Returns the number of occurrences of event E.

This example shows a motor controller that transitions between the Off, Start, and Run states when event E occurs. The controller starts in the Off state with zero motor speed and advances to the Start state when it detects the start button press. After event E occurs five times, the controller transitions to the Run state, increases the motor speed to full, and returns to the Off state when it detects the stop button press.

Example that shows using events to trigger transitions.

The MATLAB function handles the event broadcasting for the state transitions in the motor controller. The MATLAB function contains this code:

function checkButtons(startBtn, stopBtn, currentState)
if startBtn == true && currentState == 0
    send(E);
elseif stopBtn == true && currentState == 2
    send(E);
end
end

The checkButtons function broadcasts event E based on button inputs and the current state value. It sends event E when the start button is pressed and the chart is in the Off state or when the stop button is pressed and the chart is in the Run state.

Absolute-Time Temporal Logic Operators

Use these operators to track the time that elapses after a state becomes active:

  • after(n,sec): Returns true after n seconds of state activation.

  • after(n,msec): Returns true after n milliseconds.

  • after(n,usec): Returns true after n microseconds.

  • temporalCount(sec): Returns the time elapsed, in seconds.

  • elapsed(sec): Same as temporalCount(sec).

  • duration(C,sec): Returns the time elapsed since condition C became true, in seconds.

Implement Time Delays

You can use time delays in control systems to model real-world process lag, filter noisy signals, and prevent rapid state oscillations. Implement time delays by using temporal logic operators such as after, at, and duration. Use these techniques when you need precise timing for state transitions or want to develop systems that respond only to sustained input signals rather than transient noise.

To add a time delay to a chart:

  1. Create a chart that includes states that represent different operating modes.

  2. Use absolute-time temporal logic on transitions between states.

  3. Specify the time delay by using after(n,sec), after(n,msec), or after(n,usec).

This example shows a TempControl state that manages heating and cooling operations in a building by using absolute-time temporal logic.

A chart that uses the after operator.

The system monitors room temperature and transitions to the Heating state when temperature falls below the minimum threshold or the Cooling state when it exceeds the maximum threshold. The Heating and Cooling states both implement minimum run times by using absolute-time temporal logic operaterations after(180,sec) and after(300,sec). These operations specify timing requirements that are independent of the step size of the model, which creates consistent HVAC operation regardless of other simulation parameters.

Detect Elapsed Time

You can detect when input values meet time-based conditions by using temporal logic operators such as before, after, and duration. You can also configure charts to transition between states based on how long conditions remain true. Use these operators to monitor when inputs cross thresholds inside specific time windows, filter out transient signals, or implement time-based fault detection logic.

This chart shows how to use the duration operator to track elapsed time in a motor temperature control system.

Example that shows elapsed time in a Stateflow chart.

The chart contains the states Normal, Warning, and Shutdown. The system starts in the Normal state with the fan off. If the temperature exceeds 80 degrees Celsius, the system transitions to the Warning state and activates the fan at 50 percent speed. If the temperature stays above 90 degrees Celsius for more than three continuous seconds, the system enters the Shutdown state and runs the fan at 100 percent. When the reset button is pressed and the temperature drops below 70 degrees Celsius, the system returns to the Normal state.

Debounce Signals

Debouncing is a technique that filters out unwanted transient signals that occur when mechanical switches change state. Without debouncing, a single button press can register as multiple inputs, which can cause erratic behavior like duplicate characters when typing. In control systems, debouncing prevents rapid oscillation between states that could damage equipment or waste energy.

Use operators like duration and after to implement debouncing.

This chart shows a two-state debouncer that filters noisy input signals into clean binary outputs. The debouncer uses the duration operator to check that the input signal remains above or below a threshold for a specified time period before changing states.

Stateflow example that shows the duration operator.

Each state sets a different output value. The output is a stable signal that ignores transient noise.

See Also

Topics