Skip to content

fix: make the PID return to auto mode exact with an integral term - #22

Merged
aajll merged 1 commit into
masterfrom
21-fix-bumpless-integral-step
Oct 15, 2026
Merged

aajll merged 1 commit into
masterfrom
21-fix-bumpless-integral-step

Conversation

@aajll

@aajll aajll commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Layer 1 of 1. The d_filter_alpha fix merged separately as #20.

What and why

fpc_pid_set_mode(..., FPC_PID_MODE_AUTO, ...) set I = u − kp·e − D, but the next fpc_pid_compute adds ki·dt·e before forming its output. So for an unchanged error and a nonzero ki, the first auto output was u + ki·dt·e, while the header promises u.

  • Bias: the new bias is I = u − kp·e − ki·dt·e, clamped to the integral bounds as before.
  • Overflow: the bias holds a widened output and two products, so it can exceed the 2W-bit accumulator. With Q16.16, full-scale kp, ki·dt and error get close to 2^63. The new fpc_pid_msub_sat saturates the second subtraction to FXP_WIDE_MAX/FXP_WIDE_MIN. Either value lies beyond the integral bounds, so the clamp lands on the right bound and returns FPC_STATUS_SATURATED. The overflow-bound comment in pid_controller.c and docs/design.md covers this.
  • Derivative: D was zeroed only by a manual compute step. A manual-then-auto switch with no step in between left D in place, and the next step moved it, so the output still bumped. Entering manual mode now clears D, which drops it from the bias and makes the existing docs/api.md statement, "the derivative term stays 0", true from entry.

Acceptance criteria

  • For an unchanged error, the first auto output after manual mode equals the held output with ki ≠ 0. test_bumpless_return_with_integral checks the draft's case (kp 1, ki 0.5, held 300, error 100 → 300, integral 150) for both error signs.
  • A test with ki ≠ 0 fails on the current code and passes after the fix. On master the test fails at state.integral == 150 (it is 200). test_bumpless_return_without_manual_step fails without the D reset.
  • The overflow-bound comment covers the new bias. test_bumpless_rebias_saturates drives both saturation branches with full-scale gains and errors, and runs clean under UBSan.
  • docs/api.md and docs/design.md describe exact transfer, which is exact unless the bias was clamped.

Checks

  • meson test under ASan and UBSan: 2/2. The coverage gate is 100% of lines (274) and branches (144).
  • misch run --baseline, misch deviations, scripts/format.sh.
  • cl2000 22.6.2 and 25.11.1, COFF and EABI, with -Dwerror=true: all build.

Refs #21

The auto-mode bias omitted the integral step that the next compute adds, so with a nonzero ki the first auto output was one step away from the held output. The bias now subtracts that step, saturating instead of overflowing the accumulator, and entering manual mode clears the derivative term so a return without a manual step is exact too.

Refs #21
@aajll aajll linked an issue Oct 15, 2026 that may be closed by this pull request
4 tasks done
@aajll
aajll merged commit b669296 into master Oct 15, 2026
10 checks passed
@aajll
aajll deleted the 21-fix-bumpless-integral-step branch October 15, 2026 15:28
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.

Returning to auto mode is off by one integral step

1 participant