PID autotuning in Klipper currently uses the Ziegler-Nichols method to generate PID parameters, with the critical point determined by the Astrom-Hagglund method but using a relay with hysteresis. It’s all in this file.
When implemented, PID uses a Conditional Integration PID loop.
It appears such a setup has numerous shortcomings, and fixing each one just makes the others more problematic… They are:
The hysteresis level in the Astrom-Hagglund method (TUNE_PID_DELTA, a 5C constant) would ideally be zero. By changing that constant, we can see quite how problematic that is:
Yes, the Kp constant really does vary by a factor of 5x depending how its measured!! I think this is the main reason people complain of a slow frequency response and long heatup times. They are using grossly undertuned PID parameters.
The TUNE_PID_DELTA value cannot be set smaller because it is vulnerable to temperature sensor noise. It would be necessary to simulate an ideal relay and use frequency analysis to find the critical frequency, but since the temperature sensors don’t sample at consistent intervals you need some pretty specialized methods to do this (ie. you’d end up needing most of scipy as a dependency). I did this anyway to get the perfect parameters… Which brings me to the next shortcoming:
The PID loop uses Conditional Integration - ie. we don’t update the integral term if the output is max power or zero. With undertuned PID constants, this works fine… But when correctly tuned with the Astrom-Hagglund method, the output value frequently hits maximum or zero because the constants are much higher, while the sampling rate and noise are unchanged. That in turn means that we miss enough integral updates to start to affect performance and stability. I tried removing Conditional Integration and stability is now great… but obviously there are now integral windup issues instead.
Here are some ideas for solutions:
Run the tuning twice, with TUNE_PID_DELTA=5.0 and then =2.5. Use that to linearly interpolate the values that would have been measured with an ideal realy (ie. delta=0). That should get close to the correct PID values, at the expense of double the tuning time.
Modify the PID loop to back-calculate the integrator value as an antiwindup mechanism. Ie. figure 6.7 here.
I’m sure it is possible to improve the PID and PID_CALIBRATE process. However, my own experience is that one can spend dozens of hours attempting to tune/code a control system and end up with only minor improvements. In many cases it even subtly reduces robustness. FWIW, I therefore place high value on keeping things simple.
Some random comments:
TUNE_PID_DELTA: As you indicate, it is there to account for signal measurement noise. I’ve read papers that said it is okay to use this type of delta during the relay test, but I don’t have the citations handy. I always felt it was a bit odd to have it, but it is a common mechanism in relay tests.
Conditional integration: As indicated it is a common mechanism to avoid “integral windup”. It’s very common in PID controllers and this is the first I’ve heard that it is a significant issue. I agree that “back calculating” the impact of a full-on/full-off heat setting would be theoretically better - however it would require an accurate model of the heater - which would add significant complexity and risk a loss of robustness.
I don’t know why you say this. The time between temperature measurements should be very precise (handful of microseconds).
I tried out this method for a ceramic heater that was jumping around wildly and higher delta was just making the problem worse. I actually used ChatGPT to do the interpolation since I suck at math. It completely fixed the tuning on my hotend, and its hitting my set temperature now without a single overshot. Seems to work much better than a higher tune_pid_delta alone.
@hcet It scares me too honestly. If it helps its not exactly easy to get good results, requires some intuition. Also when I say I used ChatGPT I mean I had ChatGPT write python code that would parse klipper log messages and interpolate results. I can read math I just don’t like to write it.