Skip to content

fix(pid): accumulate the integral on fxp and apply dt per term - #7

Merged
aajll merged 1 commit into
5-fix-filters-fxpfrom
5-fix-pid-fxp
Oct 14, 2026
Merged

aajll merged 1 commit into
5-fix-filters-fxpfrom
5-fix-pid-fxp

Conversation

@aajll

@aajll aajll commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Refs #5

Layer 2 of 4. Ports the PID controller onto fxp and 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 = 1 LSB, dt = 60 and e = 990, each step adds 0.906 LSB and the integral stayed at 0 forever. dt was also applied inconsistently with the documented equation. The integral now sums ki·dt·e in a wide fxp_acc_t and narrows once per output; anti-windup clamps that accumulator, so a reversal in error leaves the bound on the next step.

Changes

  • fpc_pid_config is all fxp_t. ki·dt and kd/dt are derived once on init/set_config; a derived gain that saturates, or rounds to zero from a nonzero gain, is rejected.
  • Public struct fpc_pid; cfg.dt == 0 marks an uninitialised instance, so a zero struct returns FPC_STATUS_NOT_INITIALIZED.
  • Bumpless manual-to-auto re-biases the wide integral; clamping it reports FPC_STATUS_SATURATED.
  • The unchecked wide sums are bounded for every fxp format (comment at the top of src/pid_controller.c): fxp guarantees FXP_FRAC_BITS ≤ W − 2, so every sum stays below 2^(2W−1).

Acceptance criteria evidence

  • Sub-LSB integral — 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.
  • dt semantics — test_dt_scales_i_and_d_only: P is unchanged at dt = 10; ki = 1, dt = 2, e = 3 gives I = 6 per step; kd = 4, dt = 2, Δe = 3 gives D = 6. The header equation documents the same discretisation.
  • Anti-windup — test_anti_windup_clamps_accumulator: the integral holds at 5 under repeated e = 10 and moves to 4 on the first e = −1.
  • INT32_MIN — test_extreme_inputs_saturate: error, Δe, raw derivative and kp·e all saturate and report.
  • No negative shifts — no >> or << in src/pid_controller.c.

Checks

  • gcc ASan+UBSan -Werror: pass. clang -Werror: pass.
  • Coverage: 100% lines and branches across src/ (265 lines, 136 branches).
  • cppcheck with the CI flags: clean. Valgrind memcheck: 0 errors.
  • TI C2000 CGT 22.6.2 and 25.11.1, COFF and EABI, -Dwerror=true (the CI step run locally on a clean export): 0 diagnostics.

Stack

  1. filters — FIR and biquad on fxp, caller-owned, saturating; TI C2000 CI gate
  2. pid — PID on fxp: wide integral, dt per term, caller-owned
  3. build — drop pool-allocator, its options and headers; coverage gate 100/100
  4. docs — README, CONTRIBUTING, AGENTS and CHANGELOG for 0.4.0

Review 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).

@aajll
aajll added this pull request to stack #10 October 14, 2026 16:16
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.
@aajll
aajll merged commit 17cbd7c into master Oct 14, 2026
20 checks passed
@aajll
aajll deleted the 5-fix-pid-fxp branch October 14, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant