Skip to content

Commit 33c3d00

Browse files
Quick-FlashnerdCopter
authored andcommitted
autonotch on rpm filter
1 parent daaa359 commit 33c3d00

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

src/main/common/auto_notch.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
#include <string.h>
2222
#include "arm_math.h"
2323

24+
#include "platform.h"
25+
2426
#include "auto_notch.h"
2527
#include "build/debug.h"
2628
#include "maths.h"
29+
2730
/*
2831
* The idea is simple, run a passband at the notch frequency to isolate noise
2932
* at the notch frequency. Then look at the averaged squared rate of change over
@@ -34,21 +37,18 @@
3437

3538

3639
void initAutoNotch(autoNotch_t *autoNotch, float initial_frequency, int q, int noiseLimit, float looptimeUs) {
37-
// creates an exponential moving average that will cover the freq we are nothing/bandpassing
38-
float adjustedQ = q / 100.0f;
39-
4040
autoNotch->noiseLimit = noiseLimit;
4141
autoNotch->weight = 1.0;
4242
autoNotch->invWeight = 0.0;
4343

4444
autoNotch->preVariance = 0.0;
4545
pt1FilterInit(&autoNotch->preVarianceFilter, pt1FilterGain(10.0, looptimeUs * 1e-6f));
46-
biquadFilterInit(&autoNotch->preVarianceBandpass, initial_frequency, looptimeUs, adjustedQ, FILTER_BPF, 1.0f);
46+
biquadFilterInit(&autoNotch->preVarianceBandpass, initial_frequency, looptimeUs, q, FILTER_BPF, 1.0f);
4747

48-
biquadFilterInit(&autoNotch->notchFilter, initial_frequency, looptimeUs, adjustedQ, FILTER_NOTCH, 1.0f);
48+
biquadFilterInit(&autoNotch->notchFilter, initial_frequency, looptimeUs, q, FILTER_NOTCH, 1.0f);
4949
}
5050

51-
float applyAutoNotch(autoNotch_t *autoNotch, float input) {
51+
FAST_CODE float applyAutoNotch(autoNotch_t *autoNotch, float input) {
5252
float preNotchNoise = biquadFilterApplyDF1(&autoNotch->preVarianceBandpass, input);
5353
// variance is approximately the noise squared and averaged
5454
autoNotch->preVariance = pt1FilterApply(&autoNotch->preVarianceFilter, preNotchNoise * preNotchNoise);
@@ -58,14 +58,18 @@ float applyAutoNotch(autoNotch_t *autoNotch, float input) {
5858
return autoNotch->weight * notchFilteredNoise + autoNotch->invWeight * input;
5959
}
6060

61-
void updateAutoNotch(autoNotch_t *autoNotch, float frequency, float q, float weightMultiplier, float looptimeUs) {
62-
biquadFilterInit(&autoNotch->preVarianceBandpass, frequency, looptimeUs, q, FILTER_BPF, 1.0f);
63-
biquadFilterUpdate(&autoNotch->notchFilter, frequency, looptimeUs, q, FILTER_NOTCH, 1.0f);
64-
61+
FAST_CODE void updateWeight(autoNotch_t *autoNotch, float weightMultiplier) {
6562
float deviation;
6663
arm_sqrt_f32(autoNotch->preVariance, &deviation);
6764
float weight = deviation / autoNotch->noiseLimit;
6865

6966
autoNotch->weight = MIN(weight * weightMultiplier, 1.0);
7067
autoNotch->invWeight = 1.0 - autoNotch->weight;
7168
}
69+
70+
FAST_CODE void updateAutoNotch(autoNotch_t *autoNotch, float frequency, float q, float weightMultiplier, float looptimeUs) {
71+
biquadFilterInit(&autoNotch->preVarianceBandpass, frequency, looptimeUs, q, FILTER_BPF, 1.0f);
72+
biquadFilterUpdate(&autoNotch->notchFilter, frequency, looptimeUs, q, FILTER_NOTCH, 1.0f);
73+
74+
updateWeight(autoNotch, weightMultiplier);
75+
}

src/main/common/auto_notch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ typedef struct autoNotch_s {
3636

3737
void initAutoNotch(autoNotch_t *autoNotch, float initial_frequency, int q, int noiseLimit, float looptimeUs);
3838
float applyAutoNotch(autoNotch_t *autoNotch, float input);
39+
void updateWeight(autoNotch_t *autoNotch, float weightMultiplier);
3940
void updateAutoNotch(autoNotch_t *autoNotch, float frequency, float q, float weightMultiplier, float looptimeUs);

src/main/common/filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ FAST_CODE void biquadFilterUpdate(biquadFilter_t *filter, float filterFreq, uint
214214
const float a0 = 1.0 / (1.0 + alpha);
215215

216216
// precompute the coefficients
217+
// consider moving inside switch statement as you could save cpu
217218
filter->b0 *= a0;
218219
filter->b1 *= a0;
219220
filter->b2 *= a0;

src/main/flight/rpm_filter.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "common/filter.h"
3232
#include "common/maths.h"
3333
#include "common/time.h"
34+
#include "common/auto_notch.h"
3435

3536
#include "drivers/dshot.h"
3637

@@ -62,7 +63,7 @@ typedef struct rpmNotchFilter_s {
6263
float q;
6364
timeUs_t looptimeUs;
6465

65-
biquadFilter_t notch[XYZ_AXIS_COUNT][MAX_SUPPORTED_MOTORS][RPM_FILTER_MAXHARMONICS];
66+
autoNotch_t notch[XYZ_AXIS_COUNT][MAX_SUPPORTED_MOTORS][RPM_FILTER_MAXHARMONICS];
6667

6768
} rpmNotchFilter_t;
6869

@@ -94,6 +95,8 @@ void pgResetFn_rpmFilterConfig(rpmFilterConfig_t *config)
9495
config->rpm_filter_q = 500;
9596

9697
config->rpm_filter_lpf_hz = 150;
98+
99+
config->noise_limit = 50;
97100
}
98101

99102
static void rpmNotchFilterInit(rpmNotchFilter_t *filter, const rpmFilterConfig_t *config, const timeUs_t looptimeUs)
@@ -108,8 +111,8 @@ static void rpmNotchFilterInit(rpmNotchFilter_t *filter, const rpmFilterConfig_t
108111
for (int axis = 0; axis < XYZ_AXIS_COUNT; axis++) {
109112
for (int motor = 0; motor < getMotorCount(); motor++) {
110113
for (int i = 0; i < filter->harmonics; i++) {
111-
biquadFilterInit(
112-
&filter->notch[axis][motor][i], filter->minHz * i, filter->looptimeUs, filter->q, FILTER_NOTCH, 0.0f);
114+
initAutoNotch(
115+
&filter->notch[axis][motor][i], filter->minHz * i, filter->q, config->noise_limit, filter->looptimeUs);
113116
}
114117
}
115118
}
@@ -153,7 +156,7 @@ static float applyFilter(rpmNotchFilter_t *filter, const int axis, float value)
153156
}
154157
for (int motor = 0; motor < getMotorCount(); motor++) {
155158
for (int i = 0; i < filter->harmonics; i++) {
156-
value = biquadFilterApplyDF1Weighted(&filter->notch[axis][motor][i], value);
159+
value = applyAutoNotch(&filter->notch[axis][motor][i], value);
157160
}
158161
}
159162
return value;
@@ -183,31 +186,37 @@ FAST_CODE_NOINLINE void rpmFilterUpdate(void)
183186

184187
float frequency = constrainf(
185188
(currentHarmonic + 1) * motorFrequency[currentMotor], currentFilter->minHz, currentFilter->maxHz);
186-
biquadFilter_t *template = &currentFilter->notch[0][currentMotor][currentHarmonic];
189+
autoNotch_t *template = &currentFilter->notch[0][currentMotor][currentHarmonic];
187190
// uncomment below to debug filter stepping. Need to also comment out motor rpm DEBUG_SET above
188191
/* DEBUG_SET(DEBUG_RPM_FILTER, 0, harmonic); */
189192
/* DEBUG_SET(DEBUG_RPM_FILTER, 1, motor); */
190193
/* DEBUG_SET(DEBUG_RPM_FILTER, 2, currentFilter == &gyroFilter); */
191194
/* DEBUG_SET(DEBUG_RPM_FILTER, 3, frequency) */
192-
195+
193196
// fade out notch when approaching minHz (turn it off)
194197
float weight = 1.0f;
195198
if (frequency < currentFilter->minHz + currentFilter->fadeRangeHz) {
196199
weight = (frequency - currentFilter->minHz) / currentFilter->fadeRangeHz;
197200
}
198201

199-
biquadFilterUpdate(
200-
template, frequency, currentFilter->looptimeUs, currentFilter->q, FILTER_NOTCH, weight);
202+
updateAutoNotch(
203+
template, frequency, currentFilter->q, weight, currentFilter->looptimeUs);
201204

202205
for (int axis = 1; axis < XYZ_AXIS_COUNT; axis++) {
203-
biquadFilter_t *clone = &currentFilter->notch[axis][currentMotor][currentHarmonic];
204-
clone->b0 = template->b0;
205-
clone->b1 = template->b1;
206-
clone->b2 = template->b2;
207-
clone->a1 = template->a1;
208-
clone->a2 = template->a2;
209-
clone->weight = template->weight;
210-
}
206+
autoNotch_t *clone = &currentFilter->notch[axis][currentMotor][currentHarmonic];
207+
clone->notchFilter.b0 = template->notchFilter.b0;
208+
clone->notchFilter.b1 = template->notchFilter.b1;
209+
clone->notchFilter.b2 = template->notchFilter.b2;
210+
clone->notchFilter.a1 = template->notchFilter.a1;
211+
clone->notchFilter.a2 = template->notchFilter.a2;
212+
213+
clone->preVarianceBandpass.b0 = template->preVarianceBandpass.b0;
214+
//clone->preVarianceBandpass.b1 = template->preVarianceBandpass.b1; // always 0
215+
clone->preVarianceBandpass.b2 = template->preVarianceBandpass.b2;
216+
clone->preVarianceBandpass.b0 = template->preVarianceBandpass.a1;
217+
clone->preVarianceBandpass.b2 = template->preVarianceBandpass.a2;
218+
updateWeight(clone, weight);
219+
}
211220

212221
if (++currentHarmonic == currentFilter->harmonics) {
213222
currentHarmonic = 0;

src/main/flight/rpm_filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ typedef struct rpmFilterConfig_s
3232

3333
uint16_t rpm_filter_lpf_hz; // the cutoff of the lpf on reported motor rpm
3434

35+
uint8_t noise_limit;
36+
3537
} rpmFilterConfig_t;
3638

3739
PG_DECLARE(rpmFilterConfig_t, rpmFilterConfig);

0 commit comments

Comments
 (0)