ros2_control - kilted
Loading...
Searching...
No Matches
low_pass_filter.hpp
1// Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt)
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef CONTROL_TOOLBOX__LOW_PASS_FILTER_HPP_
16#define CONTROL_TOOLBOX__LOW_PASS_FILTER_HPP_
17
18#include <cmath>
19#include <limits>
20#include <memory>
21#include <stdexcept>
22#include <string>
23#include <type_traits>
24#include <vector>
25
26#include "control_toolbox/filter_traits.hpp"
27
28#include "geometry_msgs/msg/wrench_stamped.hpp"
29
30namespace control_toolbox
31{
32
33/***************************************************/
75/***************************************************/
76
77template <typename T>
79{
80public:
81 // Default constructor
83
85 {
87 }
88
93
97 bool configure();
98
107 bool update(const T & data_in, T & data_out);
108
109 bool is_configured() const { return configured_; }
110
116 {
117 a1_ = exp(
118 -1.0 / sampling_frequency * (2.0 * M_PI * damping_frequency) /
119 (pow(10.0, damping_intensity / -10.0)));
120 b1_ = 1.0 - a1_;
121 };
122
123private:
124 // Filter parameters
125 double a1_;
126 double b1_;
128 // Define the storage type based on T
129 using Traits = FilterTraits<T>;
130 using StorageType = typename Traits::StorageType;
131
132 StorageType filtered_value_, filtered_old_value_, old_value_;
133
134 bool configured_ = false;
135};
136
137template <typename T>
138LowPassFilter<T>::LowPassFilter() : a1_(1.0), b1_(0.0)
139{
140}
141
142template <typename T>
146
147template <typename T>
149{
150 Traits::initialize(filtered_value_);
151 Traits::initialize(filtered_old_value_);
152 Traits::initialize(old_value_);
153
154 return configured_ = true;
155}
156
157template <typename T>
159{
160 if (!configured_)
161 {
162 throw std::runtime_error("Filter is not configured");
163 }
164 // If this is the first call to update initialize the filter at the current state
165 // so that we dont apply an impulse to the data.
166 if (Traits::is_nan(filtered_value_) || Traits::is_empty(filtered_value_))
167 {
168 if (!Traits::is_finite(data_in))
169 {
170 return false;
171 }
172
173 Traits::assign(filtered_value_, data_in);
174 Traits::assign(filtered_old_value_, data_in);
175 Traits::assign(old_value_, data_in);
176 }
177 else
178 {
179 // Generic validation for all types
180 Traits::validate_input(data_in, filtered_value_, data_out);
181 }
182
183 // Filter
184 filtered_value_ = old_value_ * b1_ + filtered_old_value_ * a1_;
185 filtered_old_value_ = filtered_value_;
186
187 Traits::assign(old_value_, data_in);
188 Traits::assign(data_out, filtered_value_);
189
190 if (Traits::is_finite(data_in))
191 {
192 Traits::assign(old_value_, data_in);
193 }
194
195 Traits::add_metadata(data_out, data_in);
196
197 return true;
198}
199
200} // namespace control_toolbox
201
202#endif // CONTROL_TOOLBOX__LOW_PASS_FILTER_HPP_
A Low-pass filter class.
Definition low_pass_filter.hpp:79
bool update(const T &data_in, T &data_out)
Applies one iteration of the IIR filter.
Definition low_pass_filter.hpp:158
~LowPassFilter()
Destructor of LowPassFilter class.
Definition low_pass_filter.hpp:143
void set_params(double sampling_frequency, double damping_frequency, double damping_intensity)
Internal computation of the feedforward and feedbackward coefficients according to the LowPassFilter ...
Definition low_pass_filter.hpp:115
bool configure()
Configure the LowPassFilter (access and process params).
Definition low_pass_filter.hpp:148
Definition dither.hpp:46