Loading [MathJax]/extensions/tex2jax.js
WPILibC++ 2025.3.2
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
Debouncer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/SymbolExports.h>
8#include <wpi/timestamp.h>
9
10#include "units/time.h"
11
12namespace frc {
13/**
14 * A simple debounce filter for boolean streams. Requires that the boolean
15 * change value from baseline for a specified period of time before the filtered
16 * value changes.
17 */
19 public:
20 /**
21 * Type of debouncing to perform.
22 */
24 /// Rising edge.
26 /// Falling edge.
28 /// Both rising and falling edges.
29 kBoth
30 };
31
32 /**
33 * Creates a new Debouncer.
34 *
35 * @param debounceTime The number of seconds the value must change from
36 * baseline for the filtered value to change.
37 * @param type Which type of state change the debouncing will be
38 * performed on.
39 */
40 explicit Debouncer(units::second_t debounceTime,
41 DebounceType type = DebounceType::kRising);
42
43 /**
44 * Applies the debouncer to the input stream.
45 *
46 * @param input The current value of the input stream.
47 * @return The debounced value of the input stream.
48 */
49 bool Calculate(bool input);
50
51 /**
52 * Sets the time to debounce.
53 *
54 * @param time The number of seconds the value must change from baseline
55 * for the filtered value to change.
56 */
57 constexpr void SetDebounceTime(units::second_t time) {
58 m_debounceTime = time;
59 }
60
61 /**
62 * Gets the time to debounce.
63 *
64 * @return The number of seconds the value must change from baseline
65 * for the filtered value to change.
66 */
67 constexpr units::second_t GetDebounceTime() const { return m_debounceTime; }
68
69 /**
70 * Set the debounce type.
71 *
72 * @param type Which type of state change the debouncing will be performed on.
73 */
74 constexpr void SetDebounceType(DebounceType type) { m_debounceType = type; }
75
76 /**
77 * Get the debounce type.
78 *
79 * @return Which type of state change the debouncing will be performed on.
80 */
81 constexpr DebounceType GetDebounceType() const { return m_debounceType; }
82
83 private:
84 units::second_t m_debounceTime;
85 bool m_baseline;
86 DebounceType m_debounceType;
87
88 units::second_t m_prevTime;
89
90 void ResetTimer();
91
92 bool HasElapsed() const;
93};
94} // namespace frc
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
A simple debounce filter for boolean streams.
Definition Debouncer.h:18
Debouncer(units::second_t debounceTime, DebounceType type=DebounceType::kRising)
Creates a new Debouncer.
constexpr DebounceType GetDebounceType() const
Get the debounce type.
Definition Debouncer.h:81
DebounceType
Type of debouncing to perform.
Definition Debouncer.h:23
@ kFalling
Falling edge.
Definition Debouncer.h:27
@ kRising
Rising edge.
Definition Debouncer.h:25
constexpr units::second_t GetDebounceTime() const
Gets the time to debounce.
Definition Debouncer.h:67
constexpr void SetDebounceType(DebounceType type)
Set the debounce type.
Definition Debouncer.h:74
constexpr void SetDebounceTime(units::second_t time)
Sets the time to debounce.
Definition Debouncer.h:57
bool Calculate(bool input)
Applies the debouncer to the input stream.
Definition CAN.h:11
@ kBoth
Both rising and falling edges configuration.