Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ main_sources(COMMON_SRC
common/gps_conversion.h
common/log.c
common/log.h
common/lulu.c
common/lulu.h
common/maths.c
common/maths.h
common/memory.c
Expand Down
8 changes: 6 additions & 2 deletions src/main/common/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#include "platform.h"

#include "common/filter.h"
#include "common/lulu.h"
#include "common/maths.h"
#include "common/utils.h"
#include "common/time.h"

// NULL filter
float nullFilterApply(void *filter, float input)
{
Expand Down Expand Up @@ -326,6 +326,8 @@ void initFilter(const uint8_t filterType, filter_t *filter, const float cutoffFr
pt2FilterInit(&filter->pt2, pt2FilterGain(cutoffFrequency, dT));
} if (filterType == FILTER_PT3) {
pt3FilterInit(&filter->pt3, pt3FilterGain(cutoffFrequency, dT));
} if (filterType == FILTER_LULU) {
luluFilterInit(&filter->lulu, cutoffFrequency);
} else {
biquadFilterInitLPF(&filter->biquad, cutoffFrequency, refreshRate);
}
Expand All @@ -341,8 +343,10 @@ void assignFilterApplyFn(uint8_t filterType, float cutoffFrequency, filterApplyF
*applyFn = (filterApplyFnPtr) pt2FilterApply;
} if (filterType == FILTER_PT3) {
*applyFn = (filterApplyFnPtr) pt3FilterApply;
} if (filterType == FILTER_LULU) {
*applyFn = (filterApplyFnPtr) luluFilterApply;
} else {
*applyFn = (filterApplyFnPtr) biquadFilterApply;
}
}
}
}
8 changes: 6 additions & 2 deletions src/main/common/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include "lulu.h"

typedef struct rateLimitFilter_s {
float state;
} rateLimitFilter_t;
Expand Down Expand Up @@ -50,13 +52,15 @@ typedef union {
pt1Filter_t pt1;
pt2Filter_t pt2;
pt3Filter_t pt3;
luluFilter_t lulu;
} filter_t;

typedef enum {
FILTER_PT1 = 0,
FILTER_BIQUAD,
FILTER_PT2,
FILTER_PT3
FILTER_PT3,
FILTER_LULU
} filterType_e;

typedef enum {
Expand Down Expand Up @@ -134,4 +138,4 @@ void alphaBetaGammaFilterInit(alphaBetaGammaFilter_t *filter, float alpha, float
float alphaBetaGammaFilterApply(alphaBetaGammaFilter_t *filter, float input);

void initFilter(uint8_t filterType, filter_t *filter, float cutoffFrequency, uint32_t refreshRate);
void assignFilterApplyFn(uint8_t filterType, float cutoffFrequency, filterApplyFnPtr *applyFn);
void assignFilterApplyFn(uint8_t filterType, float cutoffFrequency, filterApplyFnPtr *applyFn);
186 changes: 186 additions & 0 deletions src/main/common/lulu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#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)
{
if (N > 15)
{
N = 15;
}
if (N < 1)
{
N = 1;
}
filter->N = N;
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;
}
14 changes: 14 additions & 0 deletions src/main/common/lulu.h
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);
12 changes: 6 additions & 6 deletions src/main/fc/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ void validateAndFixConfig(void)

#ifdef USE_ADAPTIVE_FILTER
// gyroConfig()->adaptiveFilterMinHz has to be at least 5 units lower than gyroConfig()->gyro_main_lpf_hz
if (gyroConfig()->adaptiveFilterMinHz + 5 > gyroConfig()->gyro_main_lpf_hz) {
gyroConfigMutable()->adaptiveFilterMinHz = gyroConfig()->gyro_main_lpf_hz - 5;
}
// if (gyroConfig()->adaptiveFilterMinHz + 5 > gyroConfig()->gyro_main_lpf_hz) {
// gyroConfigMutable()->adaptiveFilterMinHz = gyroConfig()->gyro_main_lpf_hz - 5;
// }
//gyroConfig()->adaptiveFilterMaxHz has to be at least 5 units higher than gyroConfig()->gyro_main_lpf_hz
if (gyroConfig()->adaptiveFilterMaxHz - 5 < gyroConfig()->gyro_main_lpf_hz) {
gyroConfigMutable()->adaptiveFilterMaxHz = gyroConfig()->gyro_main_lpf_hz + 5;
}
// if (gyroConfig()->adaptiveFilterMaxHz - 5 < gyroConfig()->gyro_main_lpf_hz) {
// gyroConfigMutable()->adaptiveFilterMaxHz = gyroConfig()->gyro_main_lpf_hz + 5;
// }
#endif

Copy link
Member

Choose a reason for hiding this comment

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

It might be a good idea to disable LULU when DYNAMIC or ADAPTIVE gyro filter is running

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think if we have specific filter type LULU, as implemented, this should be fixed?

if (accelerometerConfig()->acc_notch_cutoff >= accelerometerConfig()->acc_notch_hz) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -219,13 +219,13 @@ groups:
default_value: 1000
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
description: "Gyro processing anti-aliasing filter cutoff frequency. In normal operation this filter setting should never be changed. In samples"
default_value: 1
Copy link
Member

Choose a reason for hiding this comment

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

Let's not run lulu on antialiasing filter at all. It makes no sense as this filter is only to remove aliasing effect between acquisition and processing

field: gyro_anti_aliasing_lpf_hz
max: 1000
- name: gyro_main_lpf_hz
Copy link
Member

Choose a reason for hiding this comment

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

This setting shold not be changed, but a new one like 'gyro_lulu_sample_count` added

description: "Software based gyro main lowpass filter. Value is cutoff frequency (Hz)"
default_value: 60
description: "Software based gyro main lowpass filter. Value is samples"
default_value: 3
field: gyro_main_lpf_hz
min: 0
max: 500
Expand Down Expand Up @@ -254,7 +254,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
Expand Down Expand Up @@ -293,7 +293,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
Expand Down
5 changes: 3 additions & 2 deletions src/main/sensors/gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ static void initGyroFilter(filterApplyFnPtr *applyFn, filter_t state[], uint16_t
{
*applyFn = nullFilterApply;
if (cutoff > 0) {
*applyFn = (filterApplyFnPtr)pt1FilterApply;
Copy link
Member

Choose a reason for hiding this comment

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

This function should not be modified in this way, because it changes how both gyro LPF work. By doing so you just enabled double LULU on gyro data

The distinction should happen inside static void gyroInitFilters(void) by adding a new setting to run lulu on gyro

*applyFn = (filterApplyFnPtr)luluFilterApply;
for (int axis = 0; axis < 3; axis++) {
pt1FilterInit(&state[axis].pt1, cutoff, US2S(looptime));
// pt1FilterInit(&state[axis].pt1, cutoff, US2S(looptime));
luluFilterInit(&state[axis].lulu, cutoff);
}
}
}
Expand Down