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

I made a new type of nonlinear filter which seems to work nicely on the d-term #748

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

pjpei
Copy link

@pjpei pjpei commented Jan 30, 2022

Hi!

As in the title :)

It's an implementation of the LULU filter by C.H. Rohwer at a university in our country.
For reference: https://en.wikipedia.org/wiki/Lulu_smoothing

I've optimized it to O(N) complexity and it seems to work OK now. It's still quite slow so check CPU use before flying.

I've enabled it on the d-term only. If anyone wants a gyro filter with this, I checked it and I don't think it's the best use of it, but perhaps my experience was not the most representative so let me know?

In the CLI or on the OSD you can set a new lowpass filter type, LULU. In the advanced PID options on the OSD, right at the end there's now a LULU N value. Set N to an appropriate value, see the CPU usage, test, repeat. It's a global N value as a two LULU filters over each other with different N values just keep the highest N value, it's the nature of the mathematics. Don't do two lulu filters, it's supposed to do nothing except add delay (assuming I implemented correctly).

I've added OSD elements and a debug trace in case someone wants to see what the D-term looks like before and after the d-term. Debug on the pitch and roll only, before and after d-term filter. The debug bit still needs to be properly tested, it was a late night.

Screenshot from 2022-01-30 06-46-36
Above is too little LULU. It makes squarewave type stuff on the d-term. N=6 .

Screenshot from 2022-01-30 06-47-40
This is a better amount of LULU, 8kpid N=12. Still not perfect, but given that this is the only static filter I use on the above in addition to dynamic filter and kalman, think that's quite alright.

If someone wants to test it, here it is. I believe the delay in the filter is approximately N+1 samples, so if you're running 8kpid with lulu_n_val at 12, that's 13/8000 = 1.625ms delay for this filter, which I believe is quite alright!

I've found my MATEKF411 target likes N=6 or N=7 at 4k pid, while my HGLRCF722 is alright with N=12 about at 8kpid. Graphs from HGLRCF722, don't have a logger on the other one.

Kind regards,
pjpei

hellfire added 3 commits January 28, 2022 17:02
Added the LULU filter as an option for D-term and gyro filter
It's garbage for gyro filtering.  Don't do it.
Set the DTerm filter to type LULU in the CLI.  Not accessible via GUI.
This has been fixed to N=10 in filter.h, changeable to anything you like
PROTOTYPE CODE USE AT OWN RISK
Added debug info
Removed LULU on gyro
Copy link

@skylupop skylupop left a comment

Choose a reason for hiding this comment

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

Just a couple of idea/considerations. I'm going to try this filter on a mine feature where I need a good dTerm filtering!

typedef enum {
FILTER_PT1 = 0,
FILTER_BIQUAD,
FILTER_PT2,
FILTER_PT3,
FILTER_PT4,

Choose a reason for hiding this comment

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

the trailing comma is used just for these situations, to make an addition to not cause a change in an already existing line.
You should leave it there (and put one on your entry too)

Copy link
Author

Choose a reason for hiding this comment

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

I'll fix this up, thank you and appreciated!

@@ -273,3 +273,103 @@ FAST_CODE float ptnFilterApply(ptnFilter_t *filter, float input) {

return filter->state[filter->order];
} // ptnFilterApply

void luluFilterInit(luluFilter_t *filter, int N)

Choose a reason for hiding this comment

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

I would create a new file dedicated to this filter.

Copy link
Author

Choose a reason for hiding this comment

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

Will do!

@@ -101,7 +104,10 @@ static const char * const cms_mixerImplTypeLabels[] = {
};

static const char * const cms_FilterType[] = {
"PT1", "BIQUAD", "PT2", "PT3", "PT4",
"PT1", "BIQUAD", "PT2", "PT3", "PT4"

Choose a reason for hiding this comment

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

You should leave the trailing comma, is there for a reason (explained below) :)

Copy link
Author

Choose a reason for hiding this comment

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

Apologies, will fix

Fixed IMU menu
Fixed trailing commas
@pjpei
Copy link
Author

pjpei commented Jan 31, 2022

I added the changes to my repo as requested, and fixed an OSD issue I caused that didn't allow LULU to be selected as a DTERM filter from the OSD. It was only accessible via CLI.

@pjpei
Copy link
Author

pjpei commented Feb 1, 2022

Just a couple of idea/considerations. I'm going to try this filter on a mine feature where I need a good dTerm filtering!

Let me know how it goes! I'm unreasonably excited about this, it took me a month to test and optimize until it worked fast enough for use in a flight controller at reasonable speed.

@nerdCopter nerdCopter added the help wanted Extra attention is needed label Feb 1, 2022
@nerdCopter nerdCopter marked this pull request as draft February 1, 2022 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants