Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
discrete_filter.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2024-2025 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_DISCRETE_FILTER_HPP_
25#define TAPROOT_DISCRETE_FILTER_HPP_
26
27#include <array>
28#include <cstdint>
29
30namespace tap
31{
32namespace algorithms
33{
42template <uint8_t SIZE, typename T = float>
44{
45public:
55 std::array<T, SIZE> &naturalResponseCoefficients,
56 std::array<T, SIZE> &forcedResponseCoefficients)
57 : naturalResponseCoefficients(naturalResponseCoefficients),
58 forcedResponseCoefficients(forcedResponseCoefficients)
59 {
60 reset();
61 }
62
76 T filterData(float dat)
77 {
78 for (int i = SIZE - 1; i > 0; i--)
79 {
80 forcedResponse[i] = forcedResponse[i - 1];
81 }
82 forcedResponse[0] = dat;
83
84 float sum = 0;
85 // Sum of forced response coefficients multiplied by the forced response X(n-k)
86 // (previous input data)
87 for (int i = 0; i < SIZE; i++)
88 {
89 sum += forcedResponseCoefficients[i] * forcedResponse[i];
90 }
91 // Sum of natural response coefficients multiplied by the natural response Y(n-k)
92 // (previous output data)
93 for (int i = 0; i < SIZE - 1; i++)
94 {
95 sum -= naturalResponseCoefficients[i + 1] * naturalResponse[i];
96 }
97 // Apply the 1/a_0 scaling to the output
98 sum /= naturalResponseCoefficients[0];
99
100 // Shift the natural response array to make room for the new output
101 for (int i = SIZE - 1; i > 0; i--)
102 {
103 naturalResponse[i] = naturalResponse[i - 1];
104 }
105
106 naturalResponse[0] = sum;
107
108 return naturalResponse[0];
109 }
110
112 T getLastFiltered() { return naturalResponse[0]; }
113
117 {
118 // Reset the filter state to zero
119 naturalResponse.fill(0.0f);
120 forcedResponse.fill(0.0f);
121 return 0.0f;
122 }
123
124private:
125 std::array<T, SIZE> naturalResponseCoefficients;
126 std::array<T, SIZE> forcedResponseCoefficients;
127 std::array<T, SIZE> naturalResponse;
128 std::array<T, SIZE> forcedResponse;
129};
130
131} // namespace algorithms
132
133} // namespace tap
134
135#endif // TAPROOT_DISCRETE_FILTER_HPP_
DiscreteFilter class implements a discrete-time filter using the finite difference equation.
Definition discrete_filter.hpp:44
DiscreteFilter(std::array< T, SIZE > &naturalResponseCoefficients, std::array< T, SIZE > &forcedResponseCoefficients)
Constructor for the DiscreteFilter class.
Definition discrete_filter.hpp:54
T filterData(float dat)
Filters the input data using the finite difference equation.
Definition discrete_filter.hpp:76
T getLastFiltered()
Returns the last filtered value.
Definition discrete_filter.hpp:112
T reset()
Resets the filter's state to zero, keeps the coefficients
Definition discrete_filter.hpp:116
IUnit i
Definition dimensional_smooth_pid.hpp:2
Definition ballistics.cpp:29