pid/filter: FAST_CODE annotations and RC command caching#11357
Open
sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
Open
pid/filter: FAST_CODE annotations and RC command caching#11357sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
sensei-hacker wants to merge 2 commits intoiNavFlight:maintenance-9.xfrom
Conversation
pidController is FAST_CODE. Its callees that lacked FAST_CODE created
SRAM veneers (trampolines from ITCM to flash) on every call. Annotate
each callee that was audited as genuinely hot:
pid.c:
pTermProcess, dTermProcess, applyItermLimiting,
pidApplySetpointRateLimiting, checkItermLimitingActive,
checkItermFreezingActive, pidRcCommandToAngle, pidRcCommandToRate
smith_predictor.c: applySmithPredictor
filter.c: pt1ComputeRC (static; called from FAST_CODE pt1FilterApply4)
maths.c: constrain, constrainf, scaleRangef
(called from the pid.c functions above; candidates for static inline
in a future refactor, but FAST_CODE resolves the veneer for now)
fc_core.c: getAxisRcCommand
Functions removed from FAST_CODE where no FAST_CODE caller existed
(sin_approx, cos_approx, fast_fsqrtf, failsafeShouldApplyControlInput)
were never added in this tree; no net change for those.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Two independent optimisations for the main PID loop: rcCommand caching (fc_core.c) getAxisRcCommand() and failsafeUpdateRcCommandValues() are now gated on isRXDataNew so they only run when the RX task delivers a fresh frame (~50 Hz) instead of every PID cycle (1 kHz / multirotor rate). rcCommand[] is updated from a small cache on every cycle so the rest of the code is unaffected. pidSumLimit precomputation (pid.c) getPidSumLimit() returns 400 or 500 based on vehicle type and axis — values that are constant for the lifetime of a flight. Add a pidSumLimit field to pidState_t, initialise it once in pidInit(), and replace the two hot-path calls in pidApplyFixedWingRateController and pidApplyMulticopterRateController with a struct field read. getPidSumLimit() is retained for pid_autotune.c (not hot). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two performance improvements to the PID hot path, applied in order:
pid/filter: add FAST_CODE to pidController callees— addsFAST_CODEto functions called frompidController()so they land in ITCM (fast RAM) on STM32 targets, eliminating flash-to-ITCM trampoline jumps at 1 kHzfc/pid: avoid recomputing values that change at lower rates— caches RC command values andpidSumLimitso they are only recalculated when new RX data arrives (~50 Hz) rather than every PID loop iteration (~1000 Hz). Also gatesfailsafeUpdateRcCommandValues()behind the same new-data checkFiles Changed
src/main/common/filter.c— FAST_CODEsrc/main/common/maths.c— FAST_CODEsrc/main/fc/fc_core.c— FAST_CODE, RC command cachingsrc/main/flight/pid.c— FAST_CODE, RC command cachingsrc/main/flight/smith_predictor.c— FAST_CODETesting