Skip to content

Filter: HarmonicNotch: fix Quintuple Notch - #33974

Merged
tridge merged 5 commits into
ArduPilot:masterfrom
IamPete1:QuintupleNotchFix
Aug 18, 2026
Merged

Filter: HarmonicNotch: fix Quintuple Notch#33974
tridge merged 5 commits into
ArduPilot:masterfrom
IamPete1:QuintupleNotchFix

Conversation

@IamPete1

@IamPete1 IamPete1 commented Aug 6, 2026

Copy link
Copy Markdown
Member

Summary

As far as I can tell Quintuple Notch has never worked, this constrain resulted in them being silently converted to a triple notch. Found when writing ArduPilot/WebTools#332

They were added in #30994 and supposedly were tested and have a autotest.

Classification & Testing (check all that apply and add your own)

  • Checked by a human programmer
  • Non-functional change
  • No-binary change
  • Infrastructure change (e.g. unit tests, helper scripts)
  • Automated test(s) verify changes (e.g. unit test, autotest)
  • Tested manually, description below (e.g. SITL)
  • Tested on hardware
  • Logs attached
  • Logs available on request

Description

@IamPete1
IamPete1 requested a review from andyp1per August 6, 2026 23:02
@IamPete1 IamPete1 added the BUG label Aug 6, 2026
@IamPete1

IamPete1 commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

There might be some argument that we should increment the filter version flag again since updating it to 3 did not really add support for quintuple notch.

@IamPete1

IamPete1 commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

Some more proof that they never worked, they are never hit in the coverage test https://firmware.ardupilot.org/coverage/Filter/HarmonicNotchFilter.cpp.gcov.html.

image

@andyp1per

Copy link
Copy Markdown
Contributor

When I tested it it would have been on a 4.6 derivative rather than master so its conceivable that I did not get bitten by some of the other rework of this area of code.

@IamPete1

IamPete1 commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

When I tested it it would have been on a 4.6 derivative rather than master so its conceivable that I did not get bitten by some of the other rework of this area of code.

The constrain was added in #21026, it is in 4.6

IamPete1 and others added 4 commits August 8, 2026 17:22
DynamicNotches enabled the double, triple and quintuple notch options
and checked only that the FFT peak beat the single-notch peak.  A
quintuple notch which silently ran as a triple notch passed that check,
so the option being entirely non-functional went unnoticed.

Enable notch-per-motor for the composite notch runs so that the number
of allocated filters appears in the NF field of FCN, and check it
against the expected motors * harmonics * composite count.  This also
moves throttle tracking onto per-motor thrust, so those runs fly a notch
per motor; the existing peak checks still hold.

Note this does not cover the case where the filter count is clamped to
HAL_HNF_MAX_FILTERS, which is not reachable from SITL.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
update() checks that a spare filter is available once per harmonic, but
then writes one filter for each notch of the composite.  When the filter
count is clamped to HAL_HNF_MAX_FILTERS the last composite can run off
the end of the array, as neither 78 nor 39 is a multiple of 5.

More frequency sources can arrive than were allocated for at startup,
as AP_ESC_Telem::get_motor_frequencies_hz reports every ESC sending
telemetry rather than only those in the motor mask, so the clamp is
reachable in flight.

Fails under address sanitizer with a heap-buffer-overflow in
set_center_frequency at index 78 of 78.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@IamPete1
IamPete1 force-pushed the QuintupleNotchFix branch from 0cf0889 to 5217e0e Compare August 8, 2026 16:30
@IamPete1
IamPete1 force-pushed the QuintupleNotchFix branch from 5217e0e to caad765 Compare August 8, 2026 17:24
@IamPete1

IamPete1 commented Aug 8, 2026

Copy link
Copy Markdown
Member Author

I have added a fix for the notch count allocation. Because the notch count is not evaluated for the composite filters it could end up off the end of the array. This is a problem if the max number of notches is not a multiple of the number of composite filters. I think this bug exists in 4.6 and 4.7 with F7 as it has a limit of 39 notches. 39 is not divisible by 2, so with a double notch it would be possible to hit the bug. The other limits are all multiples of 1,2, and 3.

@tridge

tridge commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Automated review note - AI-generated (Claude), independently cross-checked by a second model and re-verified against the diff. Please sanity-check before acting.

Reviewed at head caad765164.

Verdict: COMMENT

  • ISSUE - libraries/Filter/Filter.h:20 - The AP_FILTER_VERSION 3→4 bump is warranted, but the consumer is not ready for it. The bump is right — identical QuintupleNotch parameters now produce a materially different transfer function, and firmware needs no special case since FV is just a uint8_t in VER. But WebTools Filter Review recognises only versions 1 and 2, and models only single/double/triple — it ignores the quintuple option bit. So FV=4 logs currently fall back to a default model and cannot be analysed correctly, which is precisely what the version field exists to prevent. Worth a matching WebTools PR alongside this.

  • NOTE - libraries/Filter/tests/test_notchfilter.cpp:366 - The new HarmonicNotchMaxFilters test has no assertion and unit-test CI is not ASan-enabled, so it can run clean despite an overrun. Verified under ASan: at the PR head it passes; with the clamp removed but the old non-rounded MIN(..., 78) restored it is a deterministic heap-buffer-overflow; and against true pre-PR code it passes because the test silently degrades to a triple notch. So it does reproduce the bug in the intermediate state, but it is not a base-vs-PR regression test and will not fail in normal CI.

  • NOTE - libraries/Filter/HarmonicNotchFilter.cpp:207 - notch_count() assumes any value above 3 means exactly 5. Current callers only ever return 1/2/3/5 so this is safe today, but the invariant is unenforced and expand_filter_count() is public and accepts arbitrary sizes — composite_notches=4 would allocate four and write five.

  • NOTE - libraries/Filter/HarmonicNotchFilter.cpp:497 - log_notch_centers() can report wrong harmonic frequencies when the allocation is capped or there are more than six sources: it derives num_sources from a possibly partial allocation, caps it at six, then uses the capped value as the second-harmonic offset, which does not match update's harmonic-major layout. Bounds-safe, but the FCN values can mislead.

Full report, including what was checked and found clean: https://uav.tridgell.net/DevCallReviews/DevCallTopic/devcall_pr_reviews.html

@tridge
tridge merged commit 35afbb8 into ArduPilot:master Aug 18, 2026
100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Pending

Development

Successfully merging this pull request may close these issues.

6 participants