WPILibC++  2021.3.1
LinearDigitalFilter.h
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 <initializer_list>
8 #include <memory>
9 #include <vector>
10 
11 #include <wpi/ArrayRef.h>
12 #include <wpi/circular_buffer.h>
13 #include <wpi/deprecated.h>
14 
15 #include "frc/filters/Filter.h"
16 
17 namespace frc {
18 
71 class LinearDigitalFilter : public Filter {
72  public:
80  WPI_DEPRECATED("Use LinearFilter class instead.")
81  LinearDigitalFilter(PIDSource& source, wpi::ArrayRef<double> ffGains,
82  wpi::ArrayRef<double> fbGains);
83 
91  WPI_DEPRECATED("Use LinearFilter class instead.")
92  LinearDigitalFilter(PIDSource& source, std::initializer_list<double> ffGains,
93  std::initializer_list<double> fbGains);
94 
102  WPI_DEPRECATED("Use LinearFilter class instead.")
103  LinearDigitalFilter(std::shared_ptr<PIDSource> source,
104  wpi::ArrayRef<double> ffGains,
105  wpi::ArrayRef<double> fbGains);
106 
114  WPI_DEPRECATED("Use LinearFilter class instead.")
115  LinearDigitalFilter(std::shared_ptr<PIDSource> source,
116  std::initializer_list<double> ffGains,
117  std::initializer_list<double> fbGains);
118 
120  LinearDigitalFilter& operator=(LinearDigitalFilter&&) = default;
121 
122  // Static methods to create commonly used filters
135  double timeConstant, double period);
136 
148  static LinearDigitalFilter HighPass(PIDSource& source, double timeConstant,
149  double period);
150 
161  static LinearDigitalFilter MovingAverage(PIDSource& source, int taps);
162 
174  static LinearDigitalFilter SinglePoleIIR(std::shared_ptr<PIDSource> source,
175  double timeConstant, double period);
176 
188  static LinearDigitalFilter HighPass(std::shared_ptr<PIDSource> source,
189  double timeConstant, double period);
190 
201  static LinearDigitalFilter MovingAverage(std::shared_ptr<PIDSource> source,
202  int taps);
203 
204  // Filter interface
205  double Get() const override;
206  void Reset() override;
207 
208  // PIDSource interface
214  double PIDGet() override;
215 
216  private:
217  wpi::circular_buffer<double> m_inputs;
218  wpi::circular_buffer<double> m_outputs;
219  std::vector<double> m_inputGains;
220  std::vector<double> m_outputGains;
221 };
222 
223 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:17
frc::LinearDigitalFilter::Reset
void Reset() override
Reset the filter state.
frc::LinearDigitalFilter::SinglePoleIIR
static LinearDigitalFilter SinglePoleIIR(PIDSource &source, double timeConstant, double period)
Creates a one-pole IIR low-pass filter of the form: y[n] = (1 - gain) * x[n] + gain * y[n-1] where ...
frc::LinearDigitalFilter::PIDGet
double PIDGet() override
Calculates the next value of the filter.
frc
WPILib FRC namespace.
Definition: VisionRunner.inc:9
frc::LinearDigitalFilter::HighPass
static LinearDigitalFilter HighPass(PIDSource &source, double timeConstant, double period)
Creates a first-order high-pass filter of the form: y[n] = gain * x[n] + (-gain) * x[n-1] + gain * y...
frc::LinearDigitalFilter::Get
double Get() const override
Returns the current filter estimate without also inserting new data as PIDGet() would do.
frc::Filter
Interface for filters.
Definition: Filter.h:20
frc::LinearFilter
This class implements a linear, digital filter.
Definition: LinearFilter.h:71
frc::LinearDigitalFilter::MovingAverage
static LinearDigitalFilter MovingAverage(PIDSource &source, int taps)
Creates a K-tap FIR moving average filter of the form: y[n] = 1/k * (x[k] + x[k-1] + … + x[0])
frc::LinearDigitalFilter
This class implements a linear, digital filter.
Definition: LinearDigitalFilter.h:71