WPILibC++  2020.3.2-60-g3011ebe
LinearDigitalFilter.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <initializer_list>
11 #include <memory>
12 #include <vector>
13 
14 #include <wpi/ArrayRef.h>
15 #include <wpi/circular_buffer.h>
16 #include <wpi/deprecated.h>
17 
18 #include "frc/filters/Filter.h"
19 
20 namespace frc {
21 
74 class LinearDigitalFilter : public Filter {
75  public:
83  WPI_DEPRECATED("Use LinearFilter class instead.")
85  wpi::ArrayRef<double> fbGains);
86 
94  WPI_DEPRECATED("Use LinearFilter class instead.")
95  LinearDigitalFilter(PIDSource& source, std::initializer_list<double> ffGains,
96  std::initializer_list<double> fbGains);
97 
105  WPI_DEPRECATED("Use LinearFilter class instead.")
106  LinearDigitalFilter(std::shared_ptr<PIDSource> source,
107  wpi::ArrayRef<double> ffGains,
108  wpi::ArrayRef<double> fbGains);
109 
117  WPI_DEPRECATED("Use LinearFilter class instead.")
118  LinearDigitalFilter(std::shared_ptr<PIDSource> source,
119  std::initializer_list<double> ffGains,
120  std::initializer_list<double> fbGains);
121 
123  LinearDigitalFilter& operator=(LinearDigitalFilter&&) = default;
124 
125  // Static methods to create commonly used filters
138  double timeConstant, double period);
139 
151  static LinearDigitalFilter HighPass(PIDSource& source, double timeConstant,
152  double period);
153 
164  static LinearDigitalFilter MovingAverage(PIDSource& source, int taps);
165 
177  static LinearDigitalFilter SinglePoleIIR(std::shared_ptr<PIDSource> source,
178  double timeConstant, double period);
179 
191  static LinearDigitalFilter HighPass(std::shared_ptr<PIDSource> source,
192  double timeConstant, double period);
193 
204  static LinearDigitalFilter MovingAverage(std::shared_ptr<PIDSource> source,
205  int taps);
206 
207  // Filter interface
208  double Get() const override;
209  void Reset() override;
210 
211  // PIDSource interface
217  double PIDGet() override;
218 
219  private:
222  std::vector<double> m_inputGains;
223  std::vector<double> m_outputGains;
224 };
225 
226 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
frc::LinearDigitalFilter::LinearDigitalFilter
LinearDigitalFilter(PIDSource &source, wpi::ArrayRef< double > ffGains, wpi::ArrayRef< double > fbGains)
Create a linear FIR or IIR filter.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
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.
wpi::circular_buffer< double >
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
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:23
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:74