Reduce Transient Signals by Using Debouncing Logic
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.
How to Debounce a Signal
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.
Debounce Signals with the duration
Operator
This example illustrates a design pattern that uses the duration
operator to filter out transient signals.
The Debouncer chart contains this logic.
State Logic
The initial state for this model is Off
. By using the duration
operator, you can control which state is active based on how long the switch signal, sw
, has been negative or nonnegative.
When
sw
has been nonnegative for longer than 0.01 seconds, the switch moves from stateOff
to stateOn
.When
sw
has been negative for longer than 0.01 seconds, the switch moves from stateOn
to stateOff
.
Run the Debouncer
Open the model.
Open the Scope block.
Open the Stateflow chart Debouncer.
Simulate the model. The scope shows how the debouncer isolates transient signals from the noisy input signal.
Debounce Signals with Fault Detection
This example illustrates a design pattern that uses an intermediate state to isolate transient signals. The debouncer design uses the after
operator to implement absolute-time temporal logic. With this design pattern, you can also detect faults and allow your system time to recover.
The Debouncer chart contains this logic.
State Logic
The Debouncer chart contains an intermediate state called Debounce
. This state isolates transient inputs by checking if the signal sw
remains positive or negative, or if it fluctuates between zero crossings over a prescribed period.
When
sw
has been positive for longer than 0.1 seconds, the switch moves to stateOn
.When
sw
has been negative for longer than 0.1 seconds, the switch moves to stateOff
.When
sw
fluctuates between zero crossings for longer than 0.3 seconds, the switch moves to stateOff.Fault
, isolatingsw
as a transient signal and giving it time to recover.
Run the Debouncer
Open the model.
Open the Scope block.
Open the Stateflow chart Debouncer.
Simulate the model. The scope shows how the debouncer isolates transient signals from the noisy input signal.
Use Event-Based Temporal Logic
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
operator. 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 argument
of the after
operator by 1000, as indicated by this table.
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) |