Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
RollingAvgFilter.h
1#pragma once
2
3#include <iterator>
4#include <list>
5#include <numeric>
6
7namespace filters {
8
15template <typename T> class RollingAvgFilter {
16public:
22 explicit RollingAvgFilter(size_t numPoints)
23 : numPoints(numPoints), data() {}
24
34 T get() const {
35 return std::reduce(std::next(data.begin()), data.end(), data.front()) / data.size();
36 }
37
44 T get(const T& val) {
45 data.push_back(val);
46 if (data.size() > numPoints) {
47 data.pop_front();
48 }
49
50 return get();
51 }
52
56 void reset() {
57 data.clear();
58 }
59
65 int getSize() const {
66 return data.size();
67 }
68
74 int getBufferSize() const {
75 return numPoints;
76 }
77
84 return data;
85 }
86
87private:
88 size_t numPoints;
89 std::list<T> data;
90};
91
92} // namespace filters
int getBufferSize() const
Get the maximum number of points that can be stored in the buffer.
Definition RollingAvgFilter.h:74
RollingAvgFilter(size_t numPoints)
Construct a new rolling average filter.
Definition RollingAvgFilter.h:22
T get(const T &val)
Adds data to the filter and gets the new output.
Definition RollingAvgFilter.h:44
void reset()
Clears all data in the buffer.
Definition RollingAvgFilter.h:56
T get() const
Get the output of the filter.
Definition RollingAvgFilter.h:34
int getSize() const
Returns the number of points stored in the buffer.
Definition RollingAvgFilter.h:65
std::list< T > getData() const
Get the underlying buffer of the filter.
Definition RollingAvgFilter.h:83