-
Notifications
You must be signed in to change notification settings - Fork 4
Added a complementary filter to rangefinder data (for 3.6) #149
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
tridge
wants to merge
7
commits into
mttr-rebase-3.6.7
Choose a base branch
from
pr-lidar-filter-3.6
base: mttr-rebase-3.6.7
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5771f11
Filter: added reset with value to LowPassFilter2p
tridge 7958d2d
Filter: added a ComplementaryFilter library
tridge 3bca716
Copter: use a 0.25Hz complementary filter for rangefinder data
tridge 53caa4e
Copter: apply a slew to the complementary filter
tridge bd0e4f8
Filter: improved reset handling of complementary filter
tridge 900e56f
Copter: use a filter reset when going to high cutoff in RF filter
tridge c09a888
Copter: fixed rangefinder filter when out of range
tridge 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
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
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
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
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,90 @@ | ||
| /* | ||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "ComplementaryFilter.h" | ||
|
|
||
| /* | ||
| complementary filter. Used to combine a reading that is long term | ||
| stable but has high freq noise with another reading that has less | ||
| high frequency noise and poor long term stability. | ||
| */ | ||
|
|
||
| /* | ||
| set crossover frequency between low frequency and high frequency | ||
| components | ||
| */ | ||
| void ComplementaryFilter::set_cutoff_frequency(float freq_hz) | ||
| { | ||
| cutoff_freq = freq_hz; | ||
| } | ||
|
|
||
| /* | ||
| reset the filter to initial values | ||
| */ | ||
| void ComplementaryFilter::reset() | ||
| { | ||
| lp.reset(); | ||
| hp.reset(); | ||
| } | ||
|
|
||
| /* | ||
| apply a low freqency and high freqency input to give a complementary | ||
| filter result where signal below the cutoff comes from the low_freq | ||
| input and above the cutoff from high freqency input. We take a | ||
| timestamp on each input as the input data may vary somewhat in | ||
| frequency | ||
| */ | ||
| float ComplementaryFilter::apply(float low_freq, float high_freq, uint32_t time_us) | ||
| { | ||
| if (!is_positive(cutoff_freq)) { | ||
| reset(); | ||
| return low_freq; | ||
| } | ||
| const uint32_t dt_us = time_us - last_sample_us; | ||
|
|
||
| if (dt_us > 1e6) { | ||
| // if we have not updated for 1s then assume we've had a | ||
| // sensor outage and reset | ||
| reset(); | ||
| } | ||
| last_sample_us = time_us; | ||
|
|
||
| const float dt = MIN(dt_us * 1.0e-6, 1); | ||
|
|
||
| // keep a low pass filter of the sample rate. Rapidly changing | ||
| // sample frequency in bi-quad filters leads to very nasty spikes | ||
| if (!is_positive(sample_freq)) { | ||
| sample_freq = 1.0/dt; | ||
| } else { | ||
| sample_freq = 0.99 * sample_freq + 0.01 * (1.0/dt); | ||
| } | ||
|
|
||
| lp.compute_params(sample_freq, cutoff_freq, params); | ||
|
|
||
| float hp_out = hp.apply(high_freq, params); | ||
| float lp_out = lp.apply(low_freq, params); | ||
|
|
||
| out = (high_freq - hp_out) + lp_out; | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| /* | ||
| return the current value | ||
| */ | ||
| float ComplementaryFilter::get(void) | ||
| { | ||
| return out; | ||
| } | ||
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,45 @@ | ||
| /* | ||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <AP_Math/AP_Math.h> | ||
| #include "LowPassFilter2p.h" | ||
|
|
||
| /* | ||
| complementary filter. Used to combine a reading that is long term | ||
| stable but has high freq noise with another reading that has less | ||
| high frequency noise and poor long term stability. | ||
| */ | ||
|
|
||
| class ComplementaryFilter | ||
| { | ||
| public: | ||
| void set_cutoff_frequency(float freq_hz); | ||
| void reset(); | ||
| float apply(float low_freq, float high_freq, uint32_t time_us); | ||
| float get(void); | ||
|
|
||
| private: | ||
| uint32_t last_sample_us; | ||
| float cutoff_freq; | ||
| float sample_freq; | ||
| // use 2-pole low pass filters to get a reasonably sharp cutoff | ||
| DigitalBiquadFilter<float>::biquad_params params; | ||
| DigitalBiquadFilter<float> lp, hp; | ||
| float out; | ||
| }; | ||
|
|
||
|
|
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tridge is there any reason not to define
get(andset_cutoff_frequency) in the header?