[wpimath] Move debouncer to filters (#3838)

This commit is contained in:
Oblarg
2021-12-28 12:49:41 -05:00
committed by GitHub
parent 5999a26fba
commit aa9dfabde2
11 changed files with 150 additions and 85 deletions

View File

@@ -1,37 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/Debouncer.h"
using namespace frc;
Debouncer::Debouncer(units::second_t debounceTime, DebounceType type)
: m_debounceTime(debounceTime), m_debounceType(type) {
switch (type) {
case DebounceType::kBoth: // fall-through
case DebounceType::kRising:
m_baseline = false;
break;
case DebounceType::kFalling:
m_baseline = true;
break;
}
m_timer.Start();
}
bool Debouncer::Calculate(bool input) {
if (input == m_baseline) {
m_timer.Reset();
}
if (m_timer.HasElapsed(m_debounceTime)) {
if (m_debounceType == DebounceType::kBoth) {
m_baseline = input;
m_timer.Reset();
}
return input;
} else {
return m_baseline;
}
}

View File

@@ -1,46 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <units/time.h>
#include "frc/Timer.h"
namespace frc {
/**
* A simple debounce filter for boolean streams. Requires that the boolean
* change value from baseline for a specified period of time before the filtered
* value changes.
*/
class Debouncer {
public:
enum DebounceType { kRising, kFalling, kBoth };
/**
* Creates a new Debouncer.
*
* @param debounceTime The number of seconds the value must change from
* baseline for the filtered value to change.
* @param type Which type of state change the debouncing will be
* performed on.
*/
explicit Debouncer(units::second_t debounceTime,
DebounceType type = DebounceType::kRising);
/**
* Applies the debouncer to the input stream.
*
* @param input The current value of the input stream.
* @return The debounced value of the input stream.
*/
bool Calculate(bool input);
private:
frc::Timer m_timer;
units::second_t m_debounceTime;
bool m_baseline;
DebounceType m_debounceType;
};
} // namespace frc