ros2_control - jazzy
Loading...
Searching...
No Matches
exponential_filter.hpp
1// Copyright (c) 2024, AIT Austrian Institute of Technology GmbH
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_FILTERS__EXPONENTIAL_FILTER_HPP_
16#define CONTROL_FILTERS__EXPONENTIAL_FILTER_HPP_
17
18#include <limits>
19#include <memory>
20#include <string>
21
22#include "filters/filter_base.hpp"
23
24#include "control_toolbox/exponential_filter_parameters.hpp"
25#include "control_toolbox/filters.hpp"
26
27namespace control_filters
28{
29
30/***************************************************/
45/***************************************************/
46
47template <typename T>
48class ExponentialFilter : public filters::FilterBase<T>
49{
50public:
54 bool configure() override;
55
64 bool update(const T & data_in, T & data_out) override;
65
66private:
67 rclcpp::Clock::SharedPtr clock_;
68 std::shared_ptr<rclcpp::Logger> logger_;
69 std::shared_ptr<exponential_filter::ParamListener> parameter_handler_;
70 exponential_filter::Params parameters_;
71 T last_smoothed_value;
72};
73
74template <typename T>
76{
77 logger_.reset(
78 new rclcpp::Logger(this->logging_interface_->get_logger().get_child(this->filter_name_)));
79
80 // Initialize the parameters once
81 if (!parameter_handler_)
82 {
83 try
84 {
85 parameter_handler_ = std::make_shared<exponential_filter::ParamListener>(
86 this->params_interface_, this->param_prefix_);
87 }
88 catch (const std::exception & ex)
89 {
90 RCLCPP_ERROR(
91 (*logger_), "Exponential filter cannot be configured: %s (type : %s)", ex.what(),
92 typeid(ex).name());
93 parameter_handler_.reset();
94 return false;
95 }
96 catch (...)
97 {
98 RCLCPP_ERROR((*logger_), "Caught unknown exception while configuring Exponential filter");
99 parameter_handler_.reset();
100 return false;
101 }
102 }
103 parameters_ = parameter_handler_->get_params();
104
105 last_smoothed_value = std::numeric_limits<double>::quiet_NaN();
106
107 return true;
108}
109
110template <typename T>
111bool ExponentialFilter<T>::update(const T & data_in, T & data_out)
112{
113 if (!this->configured_)
114 {
115 throw std::runtime_error("Filter is not configured");
116 }
117
118 // Update internal parameters if required
119 if (parameter_handler_->is_old(parameters_))
120 {
121 parameters_ = parameter_handler_->get_params();
122 }
123
124 if (std::isnan(last_smoothed_value))
125 {
126 last_smoothed_value = data_in;
127 }
128
129 data_out = last_smoothed_value =
130 filters::exponentialSmoothing(data_in, last_smoothed_value, parameters_.alpha);
131 return true;
132}
133
134} // namespace control_filters
135
136#endif // CONTROL_FILTERS__EXPONENTIAL_FILTER_HPP_
A exponential filter class.
Definition exponential_filter.hpp:49
bool update(const T &data_in, T &data_out) override
Applies one iteration of the exponential filter.
Definition exponential_filter.hpp:111
bool configure() override
Configure the ExponentialFilter (access and process params).
Definition exponential_filter.hpp:75