LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
averageFilter.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
9#include <array>
10#include <cstddef>
11
12namespace okapi {
18template <std::size_t n> class AverageFilter : public Filter {
19 public:
23 AverageFilter() = default;
24
31 double filter(const double ireading) override {
32 data[index++] = ireading;
33 if (index >= n) {
34 index = 0;
35 }
36
37 output = 0.0;
38 for (size_t i = 0; i < n; i++)
39 output += data[i];
40 output /= (double)n;
41
42 return output;
43 }
44
50 double getOutput() const override {
51 return output;
52 }
53
54 protected:
55 std::array<double, n> data{0};
56 std::size_t index = 0;
57 double output = 0;
58};
59} // namespace okapi
A filter which returns the average of a list of values.
std::array< double, n > data
AverageFilter()=default
Averaging filter.
double getOutput() const override
Returns the previous output from filter.
double filter(const double ireading) override
Filters a value, like a sensor reading.