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

LULU filter by C.H. Rohwer -- nonlinear filter which seems to work nicely on the d-term #748

Draft
wants to merge 22 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

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!

src/main/common/filter.h Show resolved Hide resolved
src/main/common/filter.c Outdated Show resolved Hide resolved
src/main/cms/cms_menu_imu.c Outdated Show resolved Hide resolved
@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
@nerdCopter nerdCopter changed the title I made a new type of nonlinear filter which seems to work nicely on the d-term LULU filter by C.H. Rohwer -- nonlinear filter which seems to work nicely on the d-term Oct 7, 2024
src/main/common/filter.c Outdated Show resolved Hide resolved
src/main/common/filter.h Outdated Show resolved Hide resolved
src/main/flight/pid.c Outdated Show resolved Hide resolved
src/main/common/lulu.h Outdated Show resolved Hide resolved
src/main/common/lulu.h Outdated Show resolved Hide resolved
@nerdCopter
Copy link
Member

nerdCopter commented Oct 7, 2024

all tabs in src/main/common/lulu.c should be converted to 4-spaces.

@nerdCopter
Copy link
Member

care to rebase on master?

@Quick-Flash
Copy link
Member

This LULU needs to be made up to date with the LULU in Inav as its more up to date.

pjpei and others added 8 commits October 9, 2024 14:43
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
Fixed IMU menu
Fixed trailing commas
to reduce aliasing and remove all bump sizes up to N
Heavily optimized Roadmaker's algorithm
Added lulu.c to speed-optimized part of compiled code
@pjpei
Copy link
Author

pjpei commented Oct 9, 2024

I've rebased, still to test. Please don't merge yet!

@pjpei pjpei reopened this Oct 9, 2024
src/main/flight/pid.c Outdated Show resolved Hide resolved
Copy link
Member

@nerdCopter nerdCopter left a comment

Choose a reason for hiding this comment

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

i've batched together a few tab-to-spaces recommendations.

@pjpei
Copy link
Author

pjpei commented Oct 9, 2024

Thanks for the review, appreciate it!

Please let me know if I need to change anything else :)

@Quick-Flash
Copy link
Member

Please change the code from Allman indentation to K&R indentation style. https://en.wikipedia.org/wiki/Indentation_style

@pjpei
Copy link
Author

pjpei commented Oct 13, 2024

Tested this morning, pretty happy with it. If you're happy, I'm happy to get it merged. :)

Apologies if I missed any tabs-to-spaces things, please note if I did, will search again.

@pjpei
Copy link
Author

pjpei commented Oct 13, 2024

It actually works best on gyro, and a simple pt1 lowpass on dterm sorts out all the relevant noise on there on all my quads. Might differ for others, but it's quite alright. Note it runs slowly so check CPU use when testing!!

src/main/io/osd.c Outdated Show resolved Hide resolved
@nerdCopter
Copy link
Member

It actually works best on gyro, [...]

you mentioned gyro, but it seems this code is only for dTerm, correct?

@nerdCopter
Copy link
Member

@Quick-Flash , many things are gated #ifdef USE_LULU here, but should "everything" be gated? for example debug, struct, source.mk and partial OSD are not.

Co-authored-by: nerdCopter <[email protected]>
@pjpei
Copy link
Author

pjpei commented Oct 14, 2024

It actually works best on gyro, [...]

you mentioned gyro, but it seems this code is only for dTerm, correct?

Hi!

I did lots of tests, but the bottom line is one LULU on gyro, one dynamic filter one notch and one PT1 on dterm sorts out the filtering on all my crafts tested. 40mm, 2", 3", 5". I did experimental tests on removing the gyrofiltering before calculating the slope, then doing LULU on the "raw" derivative, which worked very well too. This was not implemented, so I think only gyro is best option here.

@nerdCopter
Copy link
Member

  • fair enough, so will you add gyro capabilities here?
  • full-disclosure, there are no plans to release any further EmuFlight . i just thought this was interesting after noticing it was tested on @iNavFlight
  • it would require Configurator support as well, but it is currently impossible to build configurator as the OSX builds are broken.

@pjpei
Copy link
Author

pjpei commented Oct 14, 2024

  • fair enough, so will you add gyro capabilities here?

Yeah, I think it's already available to put on the gyro in the code here if you can select it as an alternative to PT1, right? I'll pull it again and make sure, but I think so.

  • full-disclosure, there are no plans to release any further EmuFlight . i just thought this was interesting after noticing it was tested on @iNavFlight

I understand. This version is improved beyond the current iNav one, and I intend to push the update to iNav as well. Thank you for considering to merge!

  • it would require Configurator support as well, but it is currently impossible to build configurator as the OSX builds are broken.

I understand. Thank you for noting this!!

@pjpei
Copy link
Author

pjpei commented Oct 14, 2024

ah wow, even my comments were incorrect, sorry. Thanks for fixing!

@Quick-Flash
Copy link
Member

Id like to see it as a CLI or OSD thing that can be added independently of the other gyro filters.

@pjpei
Copy link
Author

pjpei commented Oct 16, 2024

I'll put that in my queue, thank you for letting me know!

Id like to see it as a CLI or OSD thing that can be added independently of the other gyro filters.

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