WPILibC++  2019.1.1-beta-4-6-g6593f43
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
LinearDigitalFilter.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 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 <memory>
11 #include <vector>
12 
13 #include <wpi/ArrayRef.h>
14 
15 #include "frc/circular_buffer.h"
16 #include "frc/filters/Filter.h"
17 
18 namespace frc {
19 
70 class LinearDigitalFilter : public Filter {
71  public:
80  wpi::ArrayRef<double> fbGains);
81 
89  LinearDigitalFilter(std::shared_ptr<PIDSource> source,
90  wpi::ArrayRef<double> ffGains,
91  wpi::ArrayRef<double> fbGains);
92 
94  LinearDigitalFilter& operator=(LinearDigitalFilter&&) = default;
95 
96  // Static methods to create commonly used filters
109  double timeConstant, double period);
110 
122  static LinearDigitalFilter HighPass(PIDSource& source, double timeConstant,
123  double period);
124 
135  static LinearDigitalFilter MovingAverage(PIDSource& source, int taps);
136 
148  static LinearDigitalFilter SinglePoleIIR(std::shared_ptr<PIDSource> source,
149  double timeConstant, double period);
150 
162  static LinearDigitalFilter HighPass(std::shared_ptr<PIDSource> source,
163  double timeConstant, double period);
164 
175  static LinearDigitalFilter MovingAverage(std::shared_ptr<PIDSource> source,
176  int taps);
177 
178  // Filter interface
179  double Get() const override;
180  void Reset() override;
181 
182  // PIDSource interface
188  double PIDGet() override;
189 
190  private:
191  circular_buffer<double> m_inputs;
192  circular_buffer<double> m_outputs;
193  std::vector<double> m_inputGains;
194  std::vector<double> m_outputGains;
195 };
196 
197 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
LinearDigitalFilter(PIDSource &source, wpi::ArrayRef< double > ffGains, wpi::ArrayRef< double > fbGains)
Create a linear FIR or IIR filter.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
void Reset() override
Reset the filter state.
double PIDGet() override
Calculates the next value of the filter.
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...
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 ...
double Get() const override
Returns the current filter estimate without also inserting new data as PIDGet() would do...
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]) ...
This class implements a linear, digital filter.
Definition: LinearDigitalFilter.h:70
Interface for filters.
Definition: Filter.h:19