WPILibC++ 2026.2.1
Loading...
Searching...
No Matches
EdgeCountFilter.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
9#include "units/time.h"
10
11namespace frc {
12/**
13 * A rising edge counter for boolean streams. Requires that the boolean change
14 * value to true for a specified number of times within a specified time window
15 * after the first rising edge before the filtered value changes.
16 *
17 * The filter activates when the input has risen (transitioned from false to
18 * true) the required number of times within the time window. Once activated,
19 * the output remains true as long as the input is true. The edge count resets
20 * when the time window expires or when the input goes false after activation.
21 *
22 * Input must be stable; consider using a Debouncer before this filter to avoid
23 * counting noise as multiple edges.
24 */
26 public:
27 /**
28 * Creates a new EdgeCounterFilter.
29 *
30 * @param requiredEdges The number of rising edges required before the output
31 * goes true.
32 * @param windowTime The maximum time window in which all required edges must
33 * occur after the first rising edge.
34 */
35 explicit EdgeCounterFilter(int requiredEdges, units::second_t windowTime);
36
37 /**
38 * Applies the edge counter filter to the input stream.
39 *
40 * @param input The current value of the input stream.
41 * @return True if the required number of edges have occurred within the time
42 * window and the input is currently true; false otherwise.
43 */
44 bool Calculate(bool input);
45
46 /**
47 * Sets the time window duration.
48 *
49 * @param windowTime The maximum time window in which all required edges must
50 * occur after the first rising edge.
51 */
52 constexpr void SetWindowTime(units::second_t windowTime) {
53 m_windowTime = windowTime;
54 }
55
56 /**
57 * Gets the time window duration.
58 *
59 * @return The maximum time window in which all required edges must occur
60 * after the first rising edge.
61 */
62 constexpr units::second_t GetWindowTime() const { return m_windowTime; }
63
64 /**
65 * Sets the required number of edges.
66 *
67 * @param requiredEdges The number of rising edges required before the output
68 * goes true.
69 */
70 constexpr void SetRequiredEdges(int requiredEdges) {
71 m_requiredEdges = requiredEdges;
72 }
73
74 /**
75 * Gets the required number of edges.
76 *
77 * @return The number of rising edges required before the output goes true.
78 */
79 constexpr int GetRequiredEdges() const { return m_requiredEdges; }
80
81 private:
82 int m_requiredEdges;
83 units::second_t m_windowTime;
84
85 units::second_t m_firstEdgeTime;
86 int m_currentCount = 0;
87
88 bool m_lastInput = false;
89
90 void ResetTimer();
91
92 bool HasElapsed() const;
93};
94} // namespace frc
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
A rising edge counter for boolean streams.
Definition EdgeCountFilter.h:25
constexpr units::second_t GetWindowTime() const
Gets the time window duration.
Definition EdgeCountFilter.h:62
constexpr int GetRequiredEdges() const
Gets the required number of edges.
Definition EdgeCountFilter.h:79
EdgeCounterFilter(int requiredEdges, units::second_t windowTime)
Creates a new EdgeCounterFilter.
constexpr void SetRequiredEdges(int requiredEdges)
Sets the required number of edges.
Definition EdgeCountFilter.h:70
constexpr void SetWindowTime(units::second_t windowTime)
Sets the time window duration.
Definition EdgeCountFilter.h:52
bool Calculate(bool input)
Applies the edge counter filter to the input stream.
Definition CAN.h:11