mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[wpimath] Move debouncer to filters (#3838)
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.math.filter;
|
||||
|
||||
import edu.wpi.first.util.WPIUtilJNI;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public class Debouncer {
|
||||
public enum DebounceType {
|
||||
kRising,
|
||||
kFalling,
|
||||
kBoth
|
||||
}
|
||||
|
||||
private final double m_debounceTimeSeconds;
|
||||
private final DebounceType m_debounceType;
|
||||
private boolean m_baseline;
|
||||
|
||||
private double m_prevTimeSeconds;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public Debouncer(double debounceTime, DebounceType type) {
|
||||
m_debounceTimeSeconds = debounceTime;
|
||||
m_debounceType = type;
|
||||
|
||||
resetTimer();
|
||||
|
||||
switch (m_debounceType) {
|
||||
case kBoth: // fall-through
|
||||
case kRising:
|
||||
m_baseline = false;
|
||||
break;
|
||||
case kFalling:
|
||||
m_baseline = true;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid debounce type!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Debouncer. Baseline value defaulted to "false."
|
||||
*
|
||||
* @param debounceTime The number of seconds the value must change from baseline for the filtered
|
||||
* value to change.
|
||||
*/
|
||||
public Debouncer(double debounceTime) {
|
||||
this(debounceTime, DebounceType.kRising);
|
||||
}
|
||||
|
||||
private void resetTimer() {
|
||||
m_prevTimeSeconds = WPIUtilJNI.now() * 1e-6;
|
||||
}
|
||||
|
||||
private boolean hasElapsed() {
|
||||
return (WPIUtilJNI.now() * 1e-6) - m_prevTimeSeconds >= m_debounceTimeSeconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the debouncer to the input stream.
|
||||
*
|
||||
* @param input The current value of the input stream.
|
||||
* @return The debounced value of the input stream.
|
||||
*/
|
||||
public boolean calculate(boolean input) {
|
||||
if (input == m_baseline) {
|
||||
resetTimer();
|
||||
}
|
||||
|
||||
if (hasElapsed()) {
|
||||
if (m_debounceType == DebounceType.kBoth) {
|
||||
m_baseline = input;
|
||||
resetTimer();
|
||||
}
|
||||
return input;
|
||||
} else {
|
||||
return m_baseline;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
wpimath/src/main/native/cpp/filter/Debouncer.cpp
Normal file
45
wpimath/src/main/native/cpp/filter/Debouncer.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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/filter/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;
|
||||
}
|
||||
ResetTimer();
|
||||
}
|
||||
|
||||
void Debouncer::ResetTimer() {
|
||||
m_prevTime = units::microsecond_t(wpi::Now());
|
||||
}
|
||||
|
||||
bool Debouncer::HasElapsed() const {
|
||||
return units::microsecond_t(wpi::Now()) - m_prevTime >= m_debounceTime;
|
||||
}
|
||||
|
||||
bool Debouncer::Calculate(bool input) {
|
||||
if (input == m_baseline) {
|
||||
ResetTimer();
|
||||
}
|
||||
|
||||
if (HasElapsed()) {
|
||||
if (m_debounceType == DebounceType::kBoth) {
|
||||
m_baseline = input;
|
||||
ResetTimer();
|
||||
}
|
||||
return input;
|
||||
} else {
|
||||
return m_baseline;
|
||||
}
|
||||
}
|
||||
51
wpimath/src/main/native/include/frc/filter/Debouncer.h
Normal file
51
wpimath/src/main/native/include/frc/filter/Debouncer.h
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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 <wpi/timestamp.h>
|
||||
|
||||
#include "units/time.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 WPILIB_DLLEXPORT 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:
|
||||
units::second_t m_debounceTime;
|
||||
bool m_baseline;
|
||||
DebounceType m_debounceType;
|
||||
|
||||
units::second_t m_prevTime;
|
||||
|
||||
void ResetTimer();
|
||||
|
||||
bool HasElapsed() const;
|
||||
};
|
||||
} // namespace frc
|
||||
Reference in New Issue
Block a user