Skip to content
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

Filter: LowPassFilter: split into two classes for constant and varable dt #27670

Merged
merged 9 commits into from
Aug 19, 2024

Conversation

IamPete1
Copy link
Member

This splits our existing LowPassFilter class into a class which uses variable time step and passes the dt in the apply call and a class which uses constant dt. The variable dt class retains the LowPassFilter name, the constant dt class is LowPassFilter_const_dt.

The key thing is to only provide methods that will work together.

This found two bugs, copters flow hold set_cutoff_frequency call would not work while in the mode, only on init, so a cuttoff frequency param change would not take effect in teal time.

Secondly the SITL battery voltage filter is currently none functional because it was using the constant dt methods without setting the sample rate.

Where needed the class has been changed to the constant dt version.

float LowPassFilter<T>::get_cutoff_freq(void) const {
return _cutoff_freq;
T LowPassFilter_const_dt<T>::apply(const T &sample) {
return this->_apply(sample, alpha);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using this-> is a little odd, its due to class template inheritance. see: https://kernhanda.github.io/templates-inheritance-methods/

@clydemcqueen
Copy link
Contributor

test.Sub.RngfndQuality passes locally for me on this PR. Might be flaky. I'll take a look.

@IamPete1
Copy link
Member Author

test.Sub.RngfndQuality passes locally for me on this PR. Might be flaky. I'll take a look.

I have restarted it for now, Thanks for taking a look!


// call once
filter.set_cutoff_frequency(frequency_hz);

// then on each sample
output = filter.apply(sample, dt);

2) providing a sample freq and cutoff_freq once at start
LowPassFilter_const_dt: providing a sample freq and cutoff_freq once at start
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not enamoured with the name!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to better suggestions, I just wanted to make it clear which should be used in each case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FixedLowPassFilter? StaticLowPassFilter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not fixed in cutoff frequency, its fixed in sample rate. So i'm not sure fixed really works. Or at least it does not line up with what we would call a fixed notch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LowPassFilterConstDt would be my vote

@tridge
Copy link
Contributor

tridge commented Aug 5, 2024

structure good, just needs testing and/or unit tests

@IamPete1
Copy link
Member Author

IamPete1 commented Aug 6, 2024

I have updated the transfer function test to run both the new classes, these are with 1000Hz sample rate and 50Hz cuttoff:
LowPassFilterFloat:
image

LowPassFilterConstDtFloat:
image

Both still exactly match the analytical solution. So there is no change in behavior.

@tridge
Copy link
Contributor

tridge commented Aug 7, 2024

looks good! marked for flight testing on the weekend

@tridge
Copy link
Contributor

tridge commented Aug 19, 2024

tested on a 5" quad and a plane on the weekend with no noticeable issues

@tridge tridge merged commit b811791 into ArduPilot:master Aug 19, 2024
93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants