-
Notifications
You must be signed in to change notification settings - Fork 1.8k
LULU filter port to iNav as a single replacement for most filters in iNav, simplifying filter configuration drastically #10145
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c1729b5
Implemented basic lulu filtering system
pjpei b6b5165
Merge branch 'iNavFlight:master' into master
pjpei 8eb6fba
Fixed bug
pjpei 895ff5b
Undid target change
pjpei 96217f3
Made improvements as requested
pjpei fc4db8a
Updated CLI settings
pjpei 6e5704f
Reverted unnecessary changes in setting descriptions
pjpei 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,178 @@ | ||
| #include "lulu.h" | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <math.h> | ||
|
|
||
| #include "platform.h" | ||
|
|
||
| #include "common/filter.h" | ||
| #include "common/maths.h" | ||
| #include "common/utils.h" | ||
|
|
||
| #ifdef __ARM_ACLE | ||
| #include <arm_acle.h> | ||
| #endif /* __ARM_ACLE */ | ||
| #include <fenv.h> | ||
|
|
||
| void luluFilterInit(luluFilter_t *filter, int N) | ||
| { | ||
| filter->N = constrain(N, 1, 15); | ||
| filter->windowSize = filter->N * 2 + 1; | ||
| filter->windowBufIndex = 0; | ||
|
|
||
| memset(filter->luluInterim, 0, sizeof(float) * (filter->windowSize)); | ||
| memset(filter->luluInterimB, 0, sizeof(float) * (filter->windowSize)); | ||
| } | ||
|
|
||
| FAST_CODE float fixRoad(float *series, float *seriesB, int index, int filterN, int windowSize) | ||
| { | ||
| register float curVal = 0; | ||
| register float curValB = 0; | ||
| for (int N = 1; N <= filterN; N++) | ||
| { | ||
| int indexNeg = (index + windowSize - 2 * N) % windowSize; | ||
| register int curIndex = (indexNeg + 1) % windowSize; | ||
| register float prevVal = series[indexNeg]; | ||
| register float prevValB = seriesB[indexNeg]; | ||
| register int indexPos = (curIndex + N) % windowSize; | ||
| for (int i = windowSize - 2 * N; i < windowSize - N; i++) | ||
| { | ||
| if (indexPos >= windowSize) | ||
| { | ||
| indexPos = 0; | ||
| } | ||
| if (curIndex >= windowSize) | ||
| { | ||
| curIndex = 0; | ||
| } | ||
| // curIndex = (2 - 1) % 3 = 1 | ||
| curVal = series[curIndex]; | ||
| curValB = seriesB[curIndex]; | ||
| register float nextVal = series[indexPos]; | ||
| register float nextValB = seriesB[indexPos]; | ||
| // onbump (s, 1, 1, 3) | ||
| // if(onBump(series, curIndex, N, windowSize)) | ||
| if (prevVal < curVal && curVal > nextVal) | ||
| { | ||
| float maxValue = MAX(prevVal, nextVal); | ||
|
|
||
| series[curIndex] = maxValue; | ||
| register int k = curIndex; | ||
| for (int j = 1; j < N; j++) | ||
| { | ||
| if (++k >= windowSize) | ||
| { | ||
| k = 0; | ||
| } | ||
| series[k] = maxValue; | ||
| } | ||
| } | ||
|
|
||
| if (prevValB < curValB && curValB > nextValB) | ||
| { | ||
| float maxValue = MAX(prevValB, nextValB); | ||
|
|
||
| curVal = maxValue; | ||
| seriesB[curIndex] = maxValue; | ||
| register int k = curIndex; | ||
| for (int j = 1; j < N; j++) | ||
| { | ||
| if (++k >= windowSize) | ||
| { | ||
| k = 0; | ||
| } | ||
| seriesB[k] = maxValue; | ||
| } | ||
| } | ||
| prevVal = curVal; | ||
| prevValB = curValB; | ||
| curIndex++; | ||
| indexPos++; | ||
| } | ||
|
|
||
| curIndex = (indexNeg + 1) % windowSize; | ||
| prevVal = series[indexNeg]; | ||
| prevValB = seriesB[indexNeg]; | ||
| indexPos = (curIndex + N) % windowSize; | ||
| for (int i = windowSize - 2 * N; i < windowSize - N; i++) | ||
| { | ||
| if (indexPos >= windowSize) | ||
| { | ||
| indexPos = 0; | ||
| } | ||
| if (curIndex >= windowSize) | ||
| { | ||
| curIndex = 0; | ||
| } | ||
| // curIndex = (2 - 1) % 3 = 1 | ||
| curVal = series[curIndex]; | ||
| curValB = seriesB[curIndex]; | ||
| register float nextVal = series[indexPos]; | ||
| register float nextValB = seriesB[indexPos]; | ||
|
|
||
| if (prevVal > curVal && curVal < nextVal) | ||
| { | ||
| float minValue = MIN(prevVal, nextVal); | ||
|
|
||
| curVal = minValue; | ||
| series[curIndex] = minValue; | ||
| register int k = curIndex; | ||
| for (int j = 1; j < N; j++) | ||
| { | ||
| if (++k >= windowSize) | ||
| { | ||
| k = 0; | ||
| } | ||
| series[k] = minValue; | ||
| } | ||
| } | ||
|
|
||
| if (prevValB > curValB && curValB < nextValB) | ||
| { | ||
| float minValue = MIN(prevValB, nextValB); | ||
| curValB = minValue; | ||
| seriesB[curIndex] = minValue; | ||
| register int k = curIndex; | ||
| for (int j = 1; j < N; j++) | ||
| { | ||
| if (++k >= windowSize) | ||
| { | ||
| k = 0; | ||
| } | ||
| seriesB[k] = minValue; | ||
| } | ||
| } | ||
| prevVal = curVal; | ||
| prevValB = curValB; | ||
| curIndex++; | ||
| indexPos++; | ||
| } | ||
| } | ||
| return (curVal - curValB) / 2; | ||
| } | ||
|
|
||
| FAST_CODE float luluFilterPartialApply(luluFilter_t *filter, float input) | ||
| { | ||
| // This is the value N of the LULU filter. | ||
| register int filterN = filter->N; | ||
| // This is the total window size for the rolling buffer | ||
| register int filterWindow = filter->windowSize; | ||
|
|
||
| register int windowIndex = filter->windowBufIndex; | ||
| register float inputVal = input; | ||
| register int newIndex = (windowIndex + 1) % filterWindow; | ||
| filter->windowBufIndex = newIndex; | ||
| filter->luluInterim[windowIndex] = inputVal; | ||
| filter->luluInterimB[windowIndex] = -inputVal; | ||
| return fixRoad(filter->luluInterim, filter->luluInterimB, windowIndex, filterN, filterWindow); | ||
| } | ||
|
|
||
| FAST_CODE float luluFilterApply(luluFilter_t *filter, float input) | ||
| { | ||
| // This is the UL filter | ||
| float resultA = luluFilterPartialApply(filter, input); | ||
| // We use the median interpretation of this filter to remove bias in the output | ||
| return resultA; | ||
| } |
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,14 @@ | ||
| #pragma once | ||
|
|
||
| // Max N = 15 | ||
| typedef struct | ||
| { | ||
| int windowSize; | ||
| int windowBufIndex; | ||
| int N; | ||
| float luluInterim[32] __attribute__((aligned(128))); | ||
| float luluInterimB[32]; | ||
| } luluFilter_t; | ||
|
|
||
| void luluFilterInit(luluFilter_t *filter, int N); | ||
| float luluFilterApply(luluFilter_t *filter, float input); |
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 |
|---|---|---|
|
|
@@ -119,7 +119,7 @@ tables: | |
| - name: filter_type | ||
| values: ["PT1", "BIQUAD"] | ||
| - name: filter_type_full | ||
| values: ["PT1", "BIQUAD", "PT2", "PT3"] | ||
| values: ["PT1", "BIQUAD", "PT2", "PT3", "LULU"] | ||
| - name: log_level | ||
| values: ["ERROR", "WARNING", "INFO", "VERBOSE", "DEBUG"] | ||
| - name: iterm_relax | ||
|
|
@@ -192,7 +192,7 @@ tables: | |
| values: ["SHARED_LOW", "SHARED_HIGH", "LOW", "HIGH"] | ||
| enum: led_pin_pwm_mode_e | ||
| - name: gyro_filter_mode | ||
| values: ["STATIC", "DYNAMIC", "ADAPTIVE"] | ||
| values: ["STATIC", "DYNAMIC", "ADAPTIVE", "LULU"] | ||
| enum: gyroFilterType_e | ||
|
|
||
| constants: | ||
|
|
@@ -216,13 +216,18 @@ groups: | |
| members: | ||
| - name: looptime | ||
| description: "This is the main loop time (in us). Changing this affects PID effect with some PID controllers (see PID section for details). A very conservative value of 3500us/285Hz should work for everyone. Setting it to zero does not limit loop time, so it will go as fast as possible." | ||
| default_value: 1000 | ||
| default_value: 500 | ||
| max: 9000 | ||
| - name: gyro_anti_aliasing_lpf_hz | ||
| description: "Gyro processing anti-aliasing filter cutoff frequency. In normal operation this filter setting should never be changed. In Hz" | ||
| default_value: 250 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this with a bit higher antialiasing filter frequency of 400, the LULU filter seems to be happy with it. We might want to change this to 400 or similar if lulu is used as a default. |
||
| field: gyro_anti_aliasing_lpf_hz | ||
| max: 1000 | ||
| - name: gyro_lulu_sample_count | ||
| description: "Gyro lulu sample count, in number of samples." | ||
| default_value: 3 | ||
| field: gyroLuluSampleCount | ||
| max: 15 | ||
| - name: gyro_main_lpf_hz | ||
| description: "Software based gyro main lowpass filter. Value is cutoff frequency (Hz)" | ||
| default_value: 60 | ||
|
|
@@ -231,7 +236,7 @@ groups: | |
| max: 500 | ||
DzikuVx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: gyro_filter_mode | ||
| description: "Specifies the type of the software LPF of the gyro signals." | ||
| default_value: "STATIC" | ||
| default_value: "LULU" | ||
| field: gyroFilterMode | ||
| table: gyro_filter_mode | ||
| - name: gyro_dyn_lpf_min_hz | ||
|
|
@@ -254,7 +259,7 @@ groups: | |
| max: 10 | ||
| - name: dynamic_gyro_notch_enabled | ||
| description: "Enable/disable dynamic gyro notch also known as Matrix Filter" | ||
| default_value: ON | ||
| default_value: OFF | ||
| field: dynamicGyroNotchEnabled | ||
| condition: USE_DYNAMIC_FILTERS | ||
| type: bool | ||
|
|
@@ -293,7 +298,7 @@ groups: | |
| default_value: 0 | ||
| - name: setpoint_kalman_enabled | ||
| description: "Enable Kalman filter on the gyro data" | ||
| default_value: ON | ||
| default_value: OFF | ||
| condition: USE_GYRO_KALMAN | ||
| field: kalmanEnabled | ||
| type: bool | ||
|
|
||
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.