2023-Robot
Robot code for 2023 FRC season by Argos, FRC team #1756
Loading...
Searching...
No Matches
generic_debouncer.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <units/time.h>
8
9#include <chrono>
10
11namespace argos_lib {
12 template <class T>
14 public:
20 explicit GenericDebouncer(const units::millisecond_t symmetricDebounceTime, T initialValue = {})
21 : m_debounceTime{symmetricDebounceTime}
22 , m_rawStatus{initialValue}
23 , m_debouncedStatus{initialValue}
24 , m_debounceTransitionTime{std::chrono::steady_clock::now()} {}
25
32 [[nodiscard]] T operator()(T newVal) {
33 const auto prevRawValue = m_rawStatus;
34 const auto curTime = std::chrono::steady_clock::now();
35
36 if (newVal != m_debouncedStatus && newVal != prevRawValue) {
38 }
39
40 if (newVal != m_debouncedStatus) {
41 const auto timeSinceTransition = units::millisecond_t{static_cast<double>(
42 std::chrono::duration_cast<std::chrono::milliseconds>(curTime - m_debounceTransitionTime).count())};
43 if (timeSinceTransition >= m_debounceTime) {
44 m_debouncedStatus = newVal;
45 }
46 }
47
48 m_rawStatus = newVal;
49 return m_debouncedStatus;
50 }
51
56 void Reset(T newVal) {
57 m_rawStatus = newVal;
58 m_debouncedStatus = newVal;
59 }
60
61 private:
62 units::millisecond_t m_debounceTime;
65 std::chrono::time_point<std::chrono::steady_clock>
67 };
68
69} // namespace argos_lib
Definition generic_debouncer.h:13
std::chrono::time_point< std::chrono::steady_clock > m_debounceTransitionTime
Time when latest transition detected.
Definition generic_debouncer.h:66
T m_debouncedStatus
Current value after applying debounce.
Definition generic_debouncer.h:64
T operator()(T newVal)
Update debouncer with new input and retreive latest debounced status.
Definition generic_debouncer.h:32
void Reset(T newVal)
Definition generic_debouncer.h:56
T m_rawStatus
Last raw value.
Definition generic_debouncer.h:63
units::millisecond_t m_debounceTime
Definition generic_debouncer.h:62
GenericDebouncer(const units::millisecond_t symmetricDebounceTime, T initialValue={})
Construct a new Debouncer object with symmetric times.
Definition generic_debouncer.h:20
Definition swap_controllers_command.h:12