From edd5bb947f7b9d1a811a7bcb4b4b6c1661500867 Mon Sep 17 00:00:00 2001 From: Bruce Luckcuck Date: Sun, 30 Jun 2019 10:13:47 -0400 Subject: [PATCH] Reset out of range filter cutoffs Protection against previous Configurator versions allowing higher cutoff values than the parameter limits in the firmware and causing out-of-range corrupted config errors. --- src/main/fc/config.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/fc/config.c b/src/main/fc/config.c index 0539e12d20..bc609ed74e 100644 --- a/src/main/fc/config.c +++ b/src/main/fc/config.c @@ -162,6 +162,13 @@ static void activateConfig(void) #endif } +static void adjustFilterLimit(uint16_t *parm) +{ + if (*parm > FILTER_FREQUENCY_MAX) { + *parm = FILTER_FREQUENCY_MAX; + } +} + static void validateAndFixConfig(void) { #if !defined(USE_QUAD_MIXER_ONLY) @@ -200,6 +207,13 @@ static void validateAndFixConfig(void) } for (unsigned i = 0; i < PID_PROFILE_COUNT; i++) { + // Fix filter settings to handle cases where an older configurator was used that + // allowed higher cutoff limits from previous firmware versions. + adjustFilterLimit(&pidProfilesMutable(i)->dterm_lowpass_hz); + adjustFilterLimit(&pidProfilesMutable(i)->dterm_lowpass2_hz); + adjustFilterLimit(&pidProfilesMutable(i)->dterm_notch_hz); + adjustFilterLimit(&pidProfilesMutable(i)->dterm_notch_cutoff); + // Prevent invalid notch cutoff if (pidProfilesMutable(i)->dterm_notch_cutoff >= pidProfilesMutable(i)->dterm_notch_hz) { pidProfilesMutable(i)->dterm_notch_hz = 0; @@ -513,6 +527,15 @@ void validateAndFixGyroConfig(void) } #endif + // Fix gyro filter settings to handle cases where an older configurator was used that + // allowed higher cutoff limits from previous firmware versions. + adjustFilterLimit(&gyroConfigMutable()->gyro_lowpass_hz); + adjustFilterLimit(&gyroConfigMutable()->gyro_lowpass2_hz); + adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_hz_1); + adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_cutoff_1); + adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_hz_2); + adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_cutoff_2); + // Prevent invalid notch cutoff if (gyroConfig()->gyro_soft_notch_cutoff_1 >= gyroConfig()->gyro_soft_notch_hz_1) { gyroConfigMutable()->gyro_soft_notch_hz_1 = 0;