Skip to content

Commit

Permalink
Made improvements as requested
Browse files Browse the repository at this point in the history
  • Loading branch information
pjpei committed Jun 17, 2024
1 parent 895ff5b commit 96217f3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
12 changes: 2 additions & 10 deletions src/main/common/lulu.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@

void luluFilterInit(luluFilter_t *filter, int N)
{
if (N > 15)
{
N = 15;
}
if (N < 1)
{
N = 1;
}
filter->N = N;
filter->N = constrain(N, 1, 15);
filter->windowSize = filter->N * 2 + 1;
filter->windowBufIndex = 0;

Expand Down Expand Up @@ -183,4 +175,4 @@ FAST_CODE float luluFilterApply(luluFilter_t *filter, float input)
float resultA = luluFilterPartialApply(filter, input);
// We use the median interpretation of this filter to remove bias in the output
return resultA;
}
}
14 changes: 7 additions & 7 deletions src/main/fc/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ 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;
// }
// 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;
}
//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

if (accelerometerConfig()->acc_notch_cutoff >= accelerometerConfig()->acc_notch_hz) {
Expand Down
19 changes: 12 additions & 7 deletions src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -216,22 +216,27 @@ 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 samples"
default_value: 1
description: "Gyro processing anti-aliasing filter cutoff frequency. In normal operation this filter setting should never be changed. In Hz"
default_value: 250
field: gyro_anti_aliasing_lpf_hz
max: 1000
- name: gyro_main_lpf_hz
description: "Software based gyro main lowpass filter. Value is samples"
- 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 Hz"
default_value: 60
field: gyro_main_lpf_hz
min: 0
max: 500
- 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
Expand Down
22 changes: 15 additions & 7 deletions src/main/sensors/gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,33 @@ STATIC_UNIT_TESTED gyroSensor_e gyroDetect(gyroDev_t *dev, gyroSensor_e gyroHard
return gyroHardware;
}

static void initGyroFilter(filterApplyFnPtr *applyFn, filter_t state[], uint16_t cutoff, uint32_t looptime)
static void initGyroFilter(filterApplyFnPtr *applyFn, filter_t state[], uint16_t cutoff, uint32_t looptime, filterType_e filterType)
{
*applyFn = nullFilterApply;
if (cutoff > 0) {
*applyFn = (filterApplyFnPtr)luluFilterApply;
for (int axis = 0; axis < 3; axis++) {
// pt1FilterInit(&state[axis].pt1, cutoff, US2S(looptime));
luluFilterInit(&state[axis].lulu, cutoff);
if(filterType == FILTER_LULU) {
luluFilterInit(&state[axis].lulu, cutoff);
*applyFn = (filterApplyFnPtr)luluFilterApply;
} else {
pt1FilterInit(&state[axis].pt1, cutoff, US2S(looptime));
*applyFn = (filterApplyFnPtr)pt1FilterApply;
}
}
}
}

static void gyroInitFilters(void)
{
//First gyro LPF running at full gyro frequency 8kHz
initGyroFilter(&gyroLpfApplyFn, gyroLpfState, gyroConfig()->gyro_anti_aliasing_lpf_hz, getGyroLooptime());
initGyroFilter(&gyroLpfApplyFn, gyroLpfState, gyroConfig()->gyro_anti_aliasing_lpf_hz, getGyroLooptime(), FILTER_PT1);

//Second gyro LPF runnig and PID frequency - this filter is dynamic when gyro_use_dyn_lpf = ON
initGyroFilter(&gyroLpf2ApplyFn, gyroLpf2State, gyroConfig()->gyro_main_lpf_hz, getLooptime());
if(gyroConfig()->gyroFilterMode == GYRO_FILTER_MODE_LULU) {
initGyroFilter(&gyroLpf2ApplyFn, gyroLpf2State, gyroConfig()->gyroLuluSampleCount, getLooptime(), FILTER_LULU);
} else {
//Second gyro LPF runnig and PID frequency - this filter is dynamic when gyro_use_dyn_lpf = ON
initGyroFilter(&gyroLpf2ApplyFn, gyroLpf2State, gyroConfig()->gyro_main_lpf_hz, getLooptime(), FILTER_PT1);
}

#ifdef USE_ADAPTIVE_FILTER
if (gyroConfig()->gyroFilterMode == GYRO_FILTER_MODE_ADAPTIVE) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/sensors/gyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ typedef enum {
typedef enum {
GYRO_FILTER_MODE_STATIC = 0,
GYRO_FILTER_MODE_DYNAMIC = 1,
GYRO_FILTER_MODE_ADAPTIVE = 2
GYRO_FILTER_MODE_ADAPTIVE = 2,
GYRO_FILTER_MODE_LULU = 3
} gyroFilterMode_e;

typedef struct gyro_s {
Expand Down Expand Up @@ -102,6 +103,8 @@ typedef struct gyroConfig_s {
float adaptiveFilterIntegratorThresholdLow;
#endif
uint8_t gyroFilterMode;

uint8_t gyroLuluSampleCount;
} gyroConfig_t;

PG_DECLARE(gyroConfig_t, gyroConfig);
Expand Down

0 comments on commit 96217f3

Please sign in to comment.