Skip to content

Commit 064aa52

Browse files
authored
Merge pull request #9879 from iNavFlight/dzikuvx-adaptive-filter
Adaptive gyro filter based on signal statistics experiment
2 parents a3a0d72 + 08d46dd commit 064aa52

19 files changed

Lines changed: 454 additions & 48 deletions

File tree

.vscode/c_cpp_properties.json

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@
33
{
44
"name": "Linux",
55
"includePath": [
6-
"${workspaceRoot}/src/main/**",
7-
"${workspaceRoot}/lib/main/**",
8-
"/usr/include/**"
6+
"${workspaceRoot}",
7+
"${workspaceRoot}/src/main/**"
98
],
109
"browse": {
1110
"limitSymbolsToIncludedHeaders": false,
1211
"path": [
13-
"${workspaceRoot}/src/main/**",
14-
"${workspaceRoot}/lib/main/**"
12+
"${workspaceRoot}/**"
1513
]
1614
},
17-
"intelliSenseMode": "linux-gcc-arm",
15+
"intelliSenseMode": "msvc-x64",
1816
"cStandard": "c11",
1917
"cppStandard": "c++17",
2018
"defines": [
21-
"MCU_FLASH_SIZE 512",
22-
"USE_NAV",
23-
"NAV_FIXED_WING_LANDING",
2419
"USE_OSD",
2520
"USE_GYRO_NOTCH_1",
2621
"USE_GYRO_NOTCH_2",
@@ -34,30 +29,13 @@
3429
"USE_RPM_FILTER",
3530
"USE_GLOBAL_FUNCTIONS",
3631
"USE_DYNAMIC_FILTERS",
37-
"USE_IMU_BNO055",
38-
"USE_SECONDARY_IMU",
3932
"USE_DSHOT",
4033
"FLASH_SIZE 480",
4134
"USE_I2C_IO_EXPANDER",
4235
"USE_PCF8574",
4336
"USE_ESC_SENSOR",
44-
"USE_PROGRAMMING_FRAMEWORK",
45-
"USE_SERIALRX_GHST",
46-
"USE_TELEMETRY_GHST",
47-
"USE_CMS",
48-
"USE_DJI_HD_OSD",
49-
"USE_GYRO_KALMAN",
50-
"USE_RANGEFINDER",
51-
"USE_RATE_DYNAMICS",
52-
"USE_SMITH_PREDICTOR",
53-
"USE_ALPHA_BETA_GAMMA_FILTER",
54-
"USE_MAG_VCM5883",
55-
"USE_TELEMETRY_JETIEXBUS",
56-
"USE_NAV",
57-
"USE_SDCARD_SDIO",
58-
"USE_SDCARD",
59-
"USE_Q_TUNE",
60-
"USE_GYRO_FFT_FILTER"
37+
"USE_ADAPTIVE_FILTER",
38+
"MCU_FLASH_SIZE 1024",
6139
],
6240
"configurationProvider": "ms-vscode.cmake-tools"
6341
}

cmake/at32.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(CMSIS_DSP_SRC
1818
BasicMathFunctions/arm_scale_f32.c
1919
BasicMathFunctions/arm_sub_f32.c
2020
BasicMathFunctions/arm_mult_f32.c
21+
BasicMathFunctions/arm_offset_f32.c
2122
TransformFunctions/arm_rfft_fast_f32.c
2223
TransformFunctions/arm_cfft_f32.c
2324
TransformFunctions/arm_rfft_fast_init_f32.c
@@ -26,6 +27,9 @@ set(CMSIS_DSP_SRC
2627
CommonTables/arm_common_tables.c
2728
ComplexMathFunctions/arm_cmplx_mag_f32.c
2829
StatisticsFunctions/arm_max_f32.c
30+
StatisticsFunctions/arm_rms_f32.c
31+
StatisticsFunctions/arm_std_f32.c
32+
StatisticsFunctions/arm_mean_f32.c
2933
)
3034
list(TRANSFORM CMSIS_DSP_SRC PREPEND "${CMSIS_DSP_DIR}/Source/")
3135

cmake/stm32.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ set(CMSIS_DSP_SRC
1919
BasicMathFunctions/arm_scale_f32.c
2020
BasicMathFunctions/arm_sub_f32.c
2121
BasicMathFunctions/arm_mult_f32.c
22+
BasicMathFunctions/arm_offset_f32.c
2223
TransformFunctions/arm_rfft_fast_f32.c
2324
TransformFunctions/arm_cfft_f32.c
2425
TransformFunctions/arm_rfft_fast_init_f32.c
@@ -27,6 +28,9 @@ set(CMSIS_DSP_SRC
2728
CommonTables/arm_common_tables.c
2829
ComplexMathFunctions/arm_cmplx_mag_f32.c
2930
StatisticsFunctions/arm_max_f32.c
31+
StatisticsFunctions/arm_rms_f32.c
32+
StatisticsFunctions/arm_std_f32.c
33+
StatisticsFunctions/arm_mean_f32.c
3034
)
3135
list(TRANSFORM CMSIS_DSP_SRC PREPEND "${CMSIS_DSP_DIR}/Source/")
3236

docs/Settings.md

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,76 @@ For developer ground test use. Disables motors, sets heading status = Trusted on
15821582

15831583
---
15841584

1585+
### gyro_adaptive_filter_hpf_hz
1586+
1587+
High pass filter cutoff frequency
1588+
1589+
| Default | Min | Max |
1590+
| --- | --- | --- |
1591+
| 10 | 1 | 50 |
1592+
1593+
---
1594+
1595+
### gyro_adaptive_filter_integrator_threshold_high
1596+
1597+
High threshold for adaptive filter integrator
1598+
1599+
| Default | Min | Max |
1600+
| --- | --- | --- |
1601+
| 4 | 1 | 10 |
1602+
1603+
---
1604+
1605+
### gyro_adaptive_filter_integrator_threshold_low
1606+
1607+
Low threshold for adaptive filter integrator
1608+
1609+
| Default | Min | Max |
1610+
| --- | --- | --- |
1611+
| -2 | -10 | 0 |
1612+
1613+
---
1614+
1615+
### gyro_adaptive_filter_max_hz
1616+
1617+
Maximum frequency for adaptive filter
1618+
1619+
| Default | Min | Max |
1620+
| --- | --- | --- |
1621+
| 150 | 100 | 500 |
1622+
1623+
---
1624+
1625+
### gyro_adaptive_filter_min_hz
1626+
1627+
Minimum frequency for adaptive filter
1628+
1629+
| Default | Min | Max |
1630+
| --- | --- | --- |
1631+
| 50 | 30 | 250 |
1632+
1633+
---
1634+
1635+
### gyro_adaptive_filter_std_lpf_hz
1636+
1637+
Standard deviation low pass filter cutoff frequency
1638+
1639+
| Default | Min | Max |
1640+
| --- | --- | --- |
1641+
| 2 | 0 | 10 |
1642+
1643+
---
1644+
1645+
### gyro_adaptive_filter_target
1646+
1647+
Target value for adaptive filter
1648+
1649+
| Default | Min | Max |
1650+
| --- | --- | --- |
1651+
| 3.5 | 1 | 6 |
1652+
1653+
---
1654+
15851655
### gyro_anti_aliasing_lpf_hz
15861656

15871657
Gyro processing anti-aliasing filter cutoff frequency. In normal operation this filter setting should never be changed. In Hz
@@ -1622,33 +1692,33 @@ Minimum frequency of the gyro Dynamic LPF
16221692

16231693
---
16241694

1625-
### gyro_main_lpf_hz
1695+
### gyro_filter_mode
16261696

1627-
Software based gyro main lowpass filter. Value is cutoff frequency (Hz)
1697+
Specifies the type of the software LPF of the gyro signals.
16281698

16291699
| Default | Min | Max |
16301700
| --- | --- | --- |
1631-
| 60 | 0 | 500 |
1701+
| STATIC | | |
16321702

16331703
---
16341704

1635-
### gyro_to_use
1705+
### gyro_main_lpf_hz
16361706

1637-
On multi-gyro targets, allows to choose which gyro to use. 0 = first gyro, 1 = second gyro
1707+
Software based gyro main lowpass filter. Value is cutoff frequency (Hz)
16381708

16391709
| Default | Min | Max |
16401710
| --- | --- | --- |
1641-
| 0 | 0 | 2 |
1711+
| 60 | 0 | 500 |
16421712

16431713
---
16441714

1645-
### gyro_use_dyn_lpf
1715+
### gyro_to_use
16461716

1647-
Use Dynamic LPF instead of static gyro stage1 LPF. Dynamic Gyro LPF updates gyro LPF based on the throttle position.
1717+
On multi-gyro targets, allows to choose which gyro to use. 0 = first gyro, 1 = second gyro
16481718

16491719
| Default | Min | Max |
16501720
| --- | --- | --- |
1651-
| OFF | OFF | ON |
1721+
| 0 | 0 | 2 |
16521722

16531723
---
16541724

src/main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ main_sources(COMMON_SRC
346346
flight/dynamic_lpf.h
347347
flight/ez_tune.c
348348
flight/ez_tune.h
349+
flight/adaptive_filter.c
350+
flight/adaptive_filter.h
349351

350352
io/adsb.c
351353
io/beeper.c

src/main/build/debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ typedef enum {
7272
DEBUG_RATE_DYNAMICS,
7373
DEBUG_LANDING,
7474
DEBUG_POS_EST,
75+
DEBUG_ADAPTIVE_FILTER,
7576
DEBUG_COUNT
7677
} debugType_e;

src/main/fc/config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ uint32_t getGyroLooptime(void)
190190

191191
void validateAndFixConfig(void)
192192
{
193+
194+
#ifdef USE_ADAPTIVE_FILTER
195+
// gyroConfig()->adaptiveFilterMinHz has to be at least 5 units lower than gyroConfig()->gyro_main_lpf_hz
196+
if (gyroConfig()->adaptiveFilterMinHz + 5 > gyroConfig()->gyro_main_lpf_hz) {
197+
gyroConfigMutable()->adaptiveFilterMinHz = gyroConfig()->gyro_main_lpf_hz - 5;
198+
}
199+
//gyroConfig()->adaptiveFilterMaxHz has to be at least 5 units higher than gyroConfig()->gyro_main_lpf_hz
200+
if (gyroConfig()->adaptiveFilterMaxHz - 5 < gyroConfig()->gyro_main_lpf_hz) {
201+
gyroConfigMutable()->adaptiveFilterMaxHz = gyroConfig()->gyro_main_lpf_hz + 5;
202+
}
203+
#endif
204+
193205
if (accelerometerConfig()->acc_notch_cutoff >= accelerometerConfig()->acc_notch_hz) {
194206
accelerometerConfigMutable()->acc_notch_hz = 0;
195207
}

src/main/fc/fc_tasks.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "flight/rpm_filter.h"
5252
#include "flight/servos.h"
5353
#include "flight/wind_estimator.h"
54+
#include "flight/adaptive_filter.h"
5455

5556
#include "navigation/navigation.h"
5657

@@ -426,6 +427,10 @@ void fcTasksInit(void)
426427
setTaskEnabled(TASK_SMARTPORT_MASTER, true);
427428
#endif
428429

430+
#ifdef USE_ADAPTIVE_FILTER
431+
setTaskEnabled(TASK_ADAPTIVE_FILTER, (gyroConfig()->gyroFilterMode == GYRO_FILTER_MODE_ADAPTIVE));
432+
#endif
433+
429434
#if defined(SITL_BUILD)
430435
serialProxyStart();
431436
#endif
@@ -680,4 +685,12 @@ cfTask_t cfTasks[TASK_COUNT] = {
680685
.desiredPeriod = TASK_PERIOD_HZ(TASK_AUX_RATE_HZ), // 100Hz @10ms
681686
.staticPriority = TASK_PRIORITY_HIGH,
682687
},
688+
#ifdef USE_ADAPTIVE_FILTER
689+
[TASK_ADAPTIVE_FILTER] = {
690+
.taskName = "ADAPTIVE_FILTER",
691+
.taskFunc = adaptiveFilterTask,
692+
.desiredPeriod = TASK_PERIOD_HZ(ADAPTIVE_FILTER_RATE_HZ), // 100Hz @10ms
693+
.staticPriority = TASK_PRIORITY_LOW,
694+
},
695+
#endif
683696
};

src/main/fc/settings.yaml

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tables:
8383
values: ["NONE", "AGL", "FLOW_RAW", "FLOW", "ALWAYS", "SAG_COMP_VOLTAGE",
8484
"VIBE", "CRUISE", "REM_FLIGHT_TIME", "SMARTAUDIO", "ACC",
8585
"NAV_YAW", "PCF8574", "DYN_GYRO_LPF", "AUTOLEVEL", "ALTITUDE",
86-
"AUTOTRIM", "AUTOTUNE", "RATE_DYNAMICS", "LANDING", "POS_EST"]
86+
"AUTOTRIM", "AUTOTUNE", "RATE_DYNAMICS", "LANDING", "POS_EST", "ADAPTIVE_FILTER"]
8787
- name: aux_operator
8888
values: ["OR", "AND"]
8989
enum: modeActivationOperator_e
@@ -191,6 +191,9 @@ tables:
191191
- name: led_pin_pwm_mode
192192
values: ["SHARED_LOW", "SHARED_HIGH", "LOW", "HIGH"]
193193
enum: led_pin_pwm_mode_e
194+
- name: gyro_filter_mode
195+
values: ["STATIC", "DYNAMIC", "ADAPTIVE"]
196+
enum: gyroFilterType_e
194197

195198
constants:
196199
RPYL_PID_MIN: 0
@@ -226,11 +229,11 @@ groups:
226229
field: gyro_main_lpf_hz
227230
min: 0
228231
max: 500
229-
- name: gyro_use_dyn_lpf
230-
description: "Use Dynamic LPF instead of static gyro stage1 LPF. Dynamic Gyro LPF updates gyro LPF based on the throttle position."
231-
default_value: OFF
232-
field: useDynamicLpf
233-
type: bool
232+
- name: gyro_filter_mode
233+
description: "Specifies the type of the software LPF of the gyro signals."
234+
default_value: "STATIC"
235+
field: gyroFilterMode
236+
table: gyro_filter_mode
234237
- name: gyro_dyn_lpf_min_hz
235238
description: "Minimum frequency of the gyro Dynamic LPF"
236239
default_value: 200
@@ -330,6 +333,55 @@ groups:
330333
field: gravity_cmss_cal
331334
min: 0
332335
max: 2000
336+
- name: gyro_adaptive_filter_target
337+
description: "Target value for adaptive filter"
338+
default_value: 3.5
339+
field: adaptiveFilterTarget
340+
min: 1
341+
max: 6
342+
condition: USE_ADAPTIVE_FILTER
343+
- name: gyro_adaptive_filter_min_hz
344+
description: "Minimum frequency for adaptive filter"
345+
default_value: 50
346+
field: adaptiveFilterMinHz
347+
min: 30
348+
max: 250
349+
condition: USE_ADAPTIVE_FILTER
350+
- name: gyro_adaptive_filter_max_hz
351+
description: "Maximum frequency for adaptive filter"
352+
default_value: 150
353+
field: adaptiveFilterMaxHz
354+
min: 100
355+
max: 500
356+
condition: USE_ADAPTIVE_FILTER
357+
- name: gyro_adaptive_filter_std_lpf_hz
358+
description: "Standard deviation low pass filter cutoff frequency"
359+
default_value: 2
360+
field: adaptiveFilterStdLpfHz
361+
min: 0
362+
max: 10
363+
condition: USE_ADAPTIVE_FILTER
364+
- name: gyro_adaptive_filter_hpf_hz
365+
description: "High pass filter cutoff frequency"
366+
default_value: 10
367+
field: adaptiveFilterHpfHz
368+
min: 1
369+
max: 50
370+
condition: USE_ADAPTIVE_FILTER
371+
- name: gyro_adaptive_filter_integrator_threshold_high
372+
description: "High threshold for adaptive filter integrator"
373+
default_value: 4
374+
field: adaptiveFilterIntegratorThresholdHigh
375+
min: 1
376+
max: 10
377+
condition: USE_ADAPTIVE_FILTER
378+
- name: gyro_adaptive_filter_integrator_threshold_low
379+
description: "Low threshold for adaptive filter integrator"
380+
default_value: -2
381+
field: adaptiveFilterIntegratorThresholdLow
382+
min: -10
383+
max: 0
384+
condition: USE_ADAPTIVE_FILTER
333385

334386
- name: PG_ADC_CHANNEL_CONFIG
335387
type: adcChannelConfig_t

0 commit comments

Comments
 (0)