-
Notifications
You must be signed in to change notification settings - Fork 115
Exponantial filter refactoring #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
silanus23
wants to merge
21
commits into
ros-controls:master
Choose a base branch
from
silanus23:exponantial-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+342
−14
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5ba7d18
backbone before tests
silanus23 5580c6a
Fixed template errors and tests added.
silanus23 6cfc93d
Styling issues
silanus23 722232f
Merge branch 'ros2-master' into exponantial-filter
silanus23 b684032
Update control_toolbox/include/control_filters/exponential_filter.hpp
silanus23 004c9a7
Update control_toolbox/test/control_filters/test_exponential_filter.cpp
silanus23 c8b1586
Merge branch 'ros2-master' into exponantial-filter
christophfroehlich 3a35890
Linting issues
silanus23 e685aed
adding missing lib
silanus23 d66ab5c
Merge branch 'ros2-master' into exponantial-filter
silanus23 55531b8
Merge branch 'ros2-master' into exponantial-filter
silanus23 2e47eed
Merge branch 'ros2-master' into exponantial-filter
silanus23 61cd168
filter file reviews addressed: deleted unnecessary templates
silanus23 35b7f36
deleting speciliaztions and transforming others accordingly
silanus23 1605c20
styling and test fixture
silanus23 6da8ff8
Merge branch 'master' into exponantial-filter
silanus23 f49fa6f
readding necessary tests
silanus23 51afd55
Merge branch 'master' into exponantial-filter
silanus23 4f72bd9
last delete
silanus23 09b2b40
Fix empty line
christophfroehlich 79f4b3f
delete unnecessary test
silanus23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
control_toolbox/include/control_toolbox/exponential_filter.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt) | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||
| #define CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||
|
|
||
| #include <cmath> | ||
| #include <limits> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| #include "control_toolbox/filter_traits.hpp" | ||
|
|
||
| namespace control_toolbox | ||
| { | ||
| template <typename T> | ||
| class ExponentialFilter | ||
| { | ||
| public: | ||
| // Default constructor | ||
| ExponentialFilter(); | ||
| explicit ExponentialFilter(double alpha) { set_params(alpha); } | ||
|
|
||
| ~ExponentialFilter(); | ||
|
|
||
| bool configure(); | ||
|
|
||
| bool update(const T & data_in, T & data_out); | ||
|
|
||
| bool is_configured() const { return configured_; } | ||
|
|
||
| void set_params(double alpha) { alpha_ = alpha; } | ||
|
|
||
| private: | ||
| double alpha_; | ||
|
|
||
| // Define the storage type based on T | ||
| using Traits = FilterTraits<T>; | ||
| using StorageType = typename Traits::StorageType; | ||
|
|
||
| StorageType old_value_; | ||
|
|
||
| bool configured_ = false; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| ExponentialFilter<T>::ExponentialFilter() : alpha_(0.5) | ||
| { | ||
| } | ||
|
|
||
| template <typename T> | ||
| ExponentialFilter<T>::~ExponentialFilter() | ||
| { | ||
| } | ||
|
|
||
| template <typename T> | ||
| bool ExponentialFilter<T>::configure() | ||
| { | ||
| Traits::initialize(old_value_); | ||
|
|
||
| return configured_ = true; | ||
| } | ||
|
|
||
| template <typename T> | ||
| bool ExponentialFilter<T>::update(const T & data_in, T & data_out) | ||
| { | ||
| if (!configured_) | ||
| { | ||
| throw std::runtime_error("Filter is not configured"); | ||
| } | ||
|
|
||
| // First call: initialize filter state | ||
| if (Traits::is_nan(old_value_) || Traits::is_empty(old_value_)) | ||
| { | ||
| if (!Traits::is_finite(data_in)) | ||
| { | ||
| return false; | ||
| } | ||
| Traits::assign(old_value_, data_in); | ||
| } | ||
| else | ||
| { | ||
| Traits::validate_input(data_in, old_value_, data_out); | ||
| } | ||
|
|
||
| // Convert data_in to StorageType for arithmetic | ||
| StorageType storage_in; | ||
| Traits::assign(storage_in, data_in); | ||
|
|
||
| // Exponential filter update: y[n] = α * x[n] + (1-α) * y[n-1] | ||
| old_value_ = storage_in * alpha_ + old_value_ * (1.0 - alpha_); | ||
|
|
||
| Traits::assign(data_out, old_value_); | ||
| Traits::add_metadata(data_out, data_in); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } // namespace control_toolbox | ||
|
|
||
| #endif // CONTROL_TOOLBOX__EXPONENTIAL_FILTER_HPP_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.