Repository navigation
fix(pid): accumulate the integral on fxp and apply dt per term - #7
Merged
Merged
Conversation
The integral now sums ki*dt*e in a wide fxp_acc_t and narrows once per output, so sub-LSB contributions accumulate instead of truncating to zero; anti-windup clamps that accumulator. dt multiplies the I term, divides the D term and leaves P untouched, matching the documented continuous equation, and the controller moves to a caller-owned struct fpc_pid with an all-fxp_t configuration.
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.
Refs #5
Layer 2 of 4. Ports the PID controller onto
fxpand moves it to caller-owned storage.Why
The integral was narrowed to Q16.16 every step, so a contribution below one LSB per step was lost entirely: with
ki = 1LSB,dt = 60ande = 990, each step adds 0.906 LSB and the integral stayed at 0 forever.dtwas also applied inconsistently with the documented equation. The integral now sumski·dt·ein a widefxp_acc_tand narrows once per output; anti-windup clamps that accumulator, so a reversal in error leaves the bound on the next step.Changes
fpc_pid_configis allfxp_t.ki·dtandkd/dtare derived once oninit/set_config; a derived gain that saturates, or rounds to zero from a nonzero gain, is rejected.struct fpc_pid;cfg.dt == 0marks an uninitialised instance, so a zero struct returnsFPC_STATUS_NOT_INITIALIZED.FPC_STATUS_SATURATED.src/pid_controller.c): fxp guaranteesFXP_FRAC_BITS ≤ W − 2, so every sum stays below 2^(2W−1).Acceptance criteria evidence
test_integral_keeps_sub_lsb_contributions: 100 000 steps give exactly 90 637 (100000 × 59400 / 65536 = 90637.2). Per-step narrowing gives 0 (truncate) or 100 000 (round to nearest); a per-step narrowing mutant fails this assert.dtsemantics —test_dt_scales_i_and_d_only: P is unchanged atdt = 10;ki = 1, dt = 2, e = 3gives I = 6 per step;kd = 4, dt = 2, Δe = 3gives D = 6. The header equation documents the same discretisation.test_anti_windup_clamps_accumulator: the integral holds at 5 under repeatede = 10and moves to 4 on the firste = −1.INT32_MIN—test_extreme_inputs_saturate: error, Δe, raw derivative andkp·eall saturate and report.>>or<<insrc/pid_controller.c.Checks
-Werror: pass. clang-Werror: pass.src/(265 lines, 136 branches).-Dwerror=true(the CI step run locally on a clean export): 0 diagnostics.Stack
fxp, caller-owned, saturating; TI C2000 CI gatefxp: wide integral,dtper term, caller-ownedpool-allocator, its options and headers; coverage gate 100/100Review bottom first. Each layer builds and passes the full suite on its own. The whole-change narrative and limitations are in the top pull request (layer 4).