When a switch opens and closes, the switch contacts can bounce off each other before the switch completely transitions to an on or off state. The bouncing action can produce transient signals that do not represent a true change of state. Therefore, when modeling switch logic, it is important to filter out transient signals by using debouncing algorithms.
If you model a controller in a Stateflow® chart, you do not want your switch logic to overwork the controller by turning it on and off in response to every transient signal it receives. To avoid this, design a Stateflow controller that uses temporal logic to debounce your input signals and determine whether a switch is actually on or off.
There are two ways to debounce a signal by using Stateflow:
Filter out transient signals by using the duration
temporal operator.
Filter out transient signals by using an intermediate graphical state. Use intermediate graphical state for advanced filtering techniques, such as fault detection.
The duration
operator is supported only in Stateflow charts in a Simulink® model.
duration
OperatorThe model sf_debouncer_using_duration
illustrates a design pattern that uses the duration
operator to
filter out transient signals.
The Debouncer chart contains this logic.
The debouncer design uses the duration(n)
statement to implement absolute-time temporal logic.
The initial state for this model is Off
. Using the
duration
operator, you can control what state the model is in
based on how long the switch signal, sw
, has been greater or less
than zero. Once sw
has been greater than or equal to zero for
longer than 0.1 seconds, the switch moves from state Off
to state
On
. Then, if sw
has been less than zero
for longer than 0.01 seconds, the switch moves from state On
to
state Off
.
The debouncer has two states, On
and
Off
. The duration
operator controls which
state is active. The logic works as described in this table.
Input Signal | State | Result |
---|---|---|
Retains positive value for 0.1 second | On | Switch turns on |
Retains negative value for 0.01 second | Off | Switch turns off |
Open the sf_debouncer_using_duration
model.
Open the Stateflow chart Debouncer and the Scope block.
Simulate the chart.
The scope shows how the debouncer isolates transient signals from the noisy input signal.
The model sf_debouncer
illustrates a design pattern that uses temporal logic and an intermediate state to
isolate transient signals. With this design pattern, you can also include logic to
detect faults and allow your system time to recover.
The Debouncer chart contains this logic.
The debouncer design uses the after(n, sec)
statement to implement absolute-time temporal logic. The keyword
sec
defines simulation time that has elapsed since activation
of a state.
The debouncer chart contains an intermediate state called Debounce. The Debounce state isolates transient inputs by checking whether the signals retain their positive or negative values, or fluctuate between zero crossings over a prescribed period. The logic works as shown in this table.
Input Signal | State | Transitions | Result |
---|---|---|---|
Retains positive value for 0.1 second | Debounce.On | On | Switch turns on |
Retains negative value for 0.1 second | Debounce.Off | Off | Switch turns off |
Fluctuates between zero crossings for 0.3 second | Debounce | Off.Fault NoteThe Debounce to Off.Fault transition comes from a higher level in the chart hierarchy and overrides the transitions from the Debounce.Off and Debounce.On substates. | Chart isolates the input as a transient signal and gives it time to recover. |
Open the sf_debouncer
.
Open the Stateflow chart Debouncer and the Scope block.
Simulate the chart.
The scope shows how the debouncer isolates transient signals from the noisy input signal.
As an alternative to absolute-time temporal logic, you can apply event-based
temporal logic to determine true state in the Debouncer chart by using the
after(n, tick)
statement. The keyword
tick
specifies and implicitly generates a local event when
the chart awakens.
The Error Generator block in the sf_debouncer
model generates a
pulse signal every 0.001 second. Therefore, to convert the absolute-time temporal
logic specified in the Debouncer chart to event-based logic, multiply the
n
argument by 1000, as follows.
Absolute Time-Based Logic | Event-Based Logic |
---|---|
after ( 0.1, sec ) | after ( 100, tick ) |
after ( 0.3, sec ) | after ( 300, tick ) |
after ( 1, sec ) | after ( 1000, tick ) |