Skip to content

Commit f2da8b7

Browse files
committed
make dynamic dterm filter type configurable
1 parent e7b9b5c commit f2da8b7

File tree

6 files changed

+31
-55
lines changed

6 files changed

+31
-55
lines changed

src/config/config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@
160160
// at eliminating propwash. Motor noise related to rpm is known to have a quadratic relationship with increasing throttle. While a quadratic curve
161161
// could have been selected for this feature, a faster moving parabolic one was selected in its place as the goal is not to follow motor noise, but
162162
// to get the filter out of the way as fast as possible in the interest of better performance and handling through reduced D filter latency when you need it most.
163-
#define DTERM_DYNAMIC_LPF
164-
#define DTERM_DYNAMIC_EXPO 1.0f
163+
#define DTERM_DYNAMIC_TYPE FILTER_LP_PT1
165164
#define DTERM_DYNAMIC_FREQ_MIN 70
166165
#define DTERM_DYNAMIC_FREQ_MAX 260
167166

src/core/profile.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,10 @@ const profile_t default_profile = {
203203
},
204204
},
205205

206-
#ifdef DTERM_DYNAMIC_LPF
207-
.dterm_dynamic_enable = 1,
208-
#else
209-
.dterm_dynamic_enable = 0,
210-
#endif
211-
#ifdef DTERM_DYNAMIC_FREQ_MIN
206+
.dterm_dynamic_type = DTERM_DYNAMIC_TYPE,
212207
.dterm_dynamic_min = DTERM_DYNAMIC_FREQ_MIN,
213-
#endif
214-
#ifdef DTERM_DYNAMIC_FREQ_MAX
215208
.dterm_dynamic_max = DTERM_DYNAMIC_FREQ_MAX,
216-
#endif
209+
217210
#ifdef GYRO_DYNAMIC_NOTCH
218211
.gyro_dynamic_notch_enable = 1,
219212
#else

src/core/profile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#define OSD_NUMBER_ELEMENTS 32
1111

12-
#define PROFILE_VERSION MAKE_SEMVER(0, 2, 5)
12+
#define PROFILE_VERSION MAKE_SEMVER(0, 2, 6)
1313

1414
// Rates
1515
typedef enum {
@@ -327,7 +327,7 @@ typedef struct {
327327
typedef struct {
328328
profile_filter_parameter_t gyro[FILTER_MAX_SLOTS];
329329
profile_filter_parameter_t dterm[FILTER_MAX_SLOTS];
330-
uint8_t dterm_dynamic_enable;
330+
filter_type_t dterm_dynamic_type;
331331
float dterm_dynamic_min;
332332
float dterm_dynamic_max;
333333
uint8_t gyro_dynamic_notch_enable;
@@ -337,7 +337,7 @@ typedef struct {
337337
START_STRUCT(profile_filter_t) \
338338
ARRAY_MEMBER(gyro, FILTER_MAX_SLOTS, profile_filter_parameter_t) \
339339
ARRAY_MEMBER(dterm, FILTER_MAX_SLOTS, profile_filter_parameter_t) \
340-
MEMBER(dterm_dynamic_enable, uint8_t) \
340+
MEMBER(dterm_dynamic_type, uint8_t) \
341341
MEMBER(dterm_dynamic_min, float) \
342342
MEMBER(dterm_dynamic_max, float) \
343343
MEMBER(gyro_dynamic_notch_enable, uint8_t) \

src/flight/filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ typedef enum {
99
FILTER_LP_PT1,
1010
FILTER_LP_PT2,
1111
FILTER_LP_PT3,
12+
13+
FILTER_MAX
1214
} __attribute__((__packed__)) filter_type_t;
1315

1416
typedef struct {

src/flight/pid.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,18 @@ static vec3_t last_error2;
4242
static filter_t filter[FILTER_MAX_SLOTS];
4343
static filter_state_t filter_state[FILTER_MAX_SLOTS][3];
4444

45-
static filter_lp_pt1 dynamic_filter;
45+
static filter_t dynamic_filter;
4646
static filter_state_t dynamic_filter_state[3];
4747

4848
static filter_lp_pt1 rx_filter;
4949
static filter_state_t rx_filter_state[3];
5050

5151
void pid_init() {
5252
filter_lp_pt1_init(&rx_filter, rx_filter_state, 3, state.rx_filter_hz);
53-
5453
for (uint8_t i = 0; i < FILTER_MAX_SLOTS; i++) {
5554
filter_init(profile.filter.dterm[i].type, &filter[i], filter_state[i], 3, profile.filter.dterm[i].cutoff_freq);
5655
}
57-
58-
if (profile.filter.dterm_dynamic_enable) {
59-
// zero out filter, freq will be updated later on
60-
filter_lp_pt1_init(&dynamic_filter, dynamic_filter_state, 3, DTERM_DYNAMIC_FREQ_MAX);
61-
}
56+
filter_init(profile.filter.dterm_dynamic_type, &dynamic_filter, dynamic_filter_state, 3, DTERM_DYNAMIC_FREQ_MAX);
6257
}
6358

6459
// (iwindup = 0 windup is not allowed) (iwindup = 1 windup is allowed)
@@ -92,11 +87,7 @@ static inline float pid_compute_iterm_windup(uint8_t x, float pid_output) {
9287
static inline float pid_filter_dterm(uint8_t x, float dterm) {
9388
dterm = filter_step(profile.filter.dterm[0].type, &filter[0], &filter_state[0][x], dterm);
9489
dterm = filter_step(profile.filter.dterm[1].type, &filter[1], &filter_state[1][x], dterm);
95-
96-
if (profile.filter.dterm_dynamic_enable) {
97-
dterm = filter_lp_pt1_step(&dynamic_filter, &dynamic_filter_state[x], dterm);
98-
}
99-
90+
dterm = filter_step(profile.filter.dterm_dynamic_type, &dynamic_filter, &dynamic_filter_state[x], dterm);
10091
return dterm;
10192
}
10293

@@ -147,9 +138,15 @@ static inline float pid_tda_compensation() {
147138
// output: state.pidoutput.axis[] = change required from motors
148139
void pid_calc() {
149140
filter_lp_pt1_coeff(&rx_filter, state.rx_filter_hz);
141+
150142
filter_coeff(profile.filter.dterm[0].type, &filter[0], profile.filter.dterm[0].cutoff_freq);
151143
filter_coeff(profile.filter.dterm[1].type, &filter[1], profile.filter.dterm[1].cutoff_freq);
152144

145+
const float dynamic_throttle = state.throttle + state.throttle * (1.0f - state.throttle);
146+
const float dterm_dynamic_raw_freq = mapf(dynamic_throttle, 0.0f, 1.0f, profile.filter.dterm_dynamic_min, profile.filter.dterm_dynamic_max);
147+
const float dterm_dynamic_freq = constrain(dterm_dynamic_raw_freq, profile.filter.dterm_dynamic_min, profile.filter.dterm_dynamic_max);
148+
filter_coeff(profile.filter.dterm_dynamic_type, &dynamic_filter, dterm_dynamic_freq);
149+
153150
static vec3_t pid_output = {.roll = 0, .pitch = 0, .yaw = 0};
154151
const float v_compensation = pid_voltage_compensation();
155152
const float tda_compensation = pid_tda_compensation();
@@ -158,14 +155,6 @@ void pid_calc() {
158155
const float *stick_accelerator = profile.pid.stick_rates[stick_boost_profile].accelerator.axis;
159156
const float *stick_transition = profile.pid.stick_rates[stick_boost_profile].transition.axis;
160157

161-
if (profile.filter.dterm_dynamic_enable) {
162-
float dynamic_throttle = state.throttle + state.throttle * (1 - state.throttle) * DTERM_DYNAMIC_EXPO;
163-
float d_term_dynamic_freq = mapf(dynamic_throttle, 0.0f, 1.0f, profile.filter.dterm_dynamic_min, profile.filter.dterm_dynamic_max);
164-
d_term_dynamic_freq = constrain(d_term_dynamic_freq, profile.filter.dterm_dynamic_min, profile.filter.dterm_dynamic_max);
165-
166-
filter_lp_pt1_coeff(&dynamic_filter, d_term_dynamic_freq);
167-
}
168-
169158
// rotates errors
170159
ierror = vec3_rotate(ierror, state.gyro_delta_angle);
171160

src/osd/render.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ static const char *on_off_labels[] = {
9696
" ON",
9797
};
9898

99+
static const char *filter_type_labels[] = {
100+
"NONE",
101+
" PT1",
102+
" PT2",
103+
" PT3",
104+
};
105+
99106
#pragma GCC diagnostic ignored "-Wmissing-braces"
100107

101108
static const vec3_t rate_defaults[RATE_MODE_ACTUAL + 1][3] = {
@@ -815,16 +822,9 @@ void osd_display() {
815822
osd_menu_start();
816823
osd_menu_header("GYRO FILTERS");
817824

818-
const char *filter_type_labels[] = {
819-
"NONE",
820-
" PT1",
821-
" PT2",
822-
" PT3",
823-
};
824-
825825
osd_menu_select(4, 4, "PASS 1 TYPE");
826826
if (osd_menu_select_enum(18, 4, profile.filter.gyro[0].type, filter_type_labels)) {
827-
profile.filter.gyro[0].type = osd_menu_adjust_int(profile.filter.gyro[0].type, 1, 0, FILTER_LP_PT3);
827+
profile.filter.gyro[0].type = osd_menu_adjust_int(profile.filter.gyro[0].type, 1, 0, FILTER_MAX - 1);
828828
osd_state.reboot_fc_requested = 1;
829829
}
830830

@@ -835,7 +835,7 @@ void osd_display() {
835835

836836
osd_menu_select(4, 6, "PASS 2 TYPE");
837837
if (osd_menu_select_enum(18, 6, profile.filter.gyro[1].type, filter_type_labels)) {
838-
profile.filter.gyro[1].type = osd_menu_adjust_int(profile.filter.gyro[1].type, 1, 0, FILTER_LP_PT3);
838+
profile.filter.gyro[1].type = osd_menu_adjust_int(profile.filter.gyro[1].type, 1, 0, FILTER_MAX - 1);
839839
osd_state.reboot_fc_requested = 1;
840840
}
841841

@@ -859,16 +859,9 @@ void osd_display() {
859859
osd_menu_start();
860860
osd_menu_header("D-TERM FILTERS");
861861

862-
const char *filter_type_labels[] = {
863-
"NONE",
864-
" PT1",
865-
" PT2",
866-
" PT3",
867-
};
868-
869862
osd_menu_select(4, 3, "PASS 1 TYPE");
870863
if (osd_menu_select_enum(18, 3, profile.filter.dterm[0].type, filter_type_labels)) {
871-
profile.filter.dterm[0].type = osd_menu_adjust_int(profile.filter.dterm[0].type, 1, 0, FILTER_LP_PT2);
864+
profile.filter.dterm[0].type = osd_menu_adjust_int(profile.filter.dterm[0].type, 1, 0, FILTER_MAX - 1);
872865
osd_state.reboot_fc_requested = 1;
873866
}
874867

@@ -879,7 +872,7 @@ void osd_display() {
879872

880873
osd_menu_select(4, 5, "PASS 2 TYPE");
881874
if (osd_menu_select_enum(18, 5, profile.filter.dterm[1].type, filter_type_labels)) {
882-
profile.filter.dterm[1].type = osd_menu_adjust_int(profile.filter.dterm[1].type, 1, 0, FILTER_LP_PT2);
875+
profile.filter.dterm[1].type = osd_menu_adjust_int(profile.filter.dterm[1].type, 1, 0, FILTER_MAX - 1);
883876
osd_state.reboot_fc_requested = 1;
884877
}
885878

@@ -888,9 +881,9 @@ void osd_display() {
888881
profile.filter.dterm[1].cutoff_freq = osd_menu_adjust_float(profile.filter.dterm[1].cutoff_freq, 10, 50, 500);
889882
}
890883

891-
osd_menu_select(4, 7, "DYNAMIC");
892-
if (osd_menu_select_enum(18, 7, profile.filter.dterm_dynamic_enable, on_off_labels)) {
893-
profile.filter.dterm_dynamic_enable = osd_menu_adjust_int(profile.filter.dterm_dynamic_enable, 1, 0, 1);
884+
osd_menu_select(4, 7, "DYNAMIC TYPE");
885+
if (osd_menu_select_enum(18, 7, profile.filter.dterm_dynamic_type, filter_type_labels)) {
886+
profile.filter.dterm_dynamic_type = osd_menu_adjust_int(profile.filter.dterm_dynamic_type, 1, 0, FILTER_MAX - 1);
894887
osd_state.reboot_fc_requested = 1;
895888
}
896889

0 commit comments

Comments
 (0)