Feature request : continuous PID autotune

I don’t even know if it can be done, but an interesting experimental feature would be to have continuous pid tuning of heaters. Let me explain : while working (printing), klipper would analyse the heating characteristic of the hot end and bed, and adjust the pid parameters for various temperatures / conditions.

Here you are kind of chasing your own tail:

  • You do a PID tuning where Klipper modifies the target in a controlled way and evaluates the response
  • From the response compared to the wanted target the PID parameters are derived
  • The PID parameters basically are a model of your system, taking into account the heating power, thermal capacity etc, this means a model of your physical characteristics.
  • When printing Klipper uses this model to “autotune” your heating in a feedback loop

To be even more dynamic hardly makes any sense IMO, because this would mean you assume that your physical characteristics are suddenly rapidly changing. For example the mass of your heat block would suddenly grow from 20 grams to 50 grams or the power of your heating cartridge suddenly drop from 50 Watt to 20 Watt, which typically will not happen.

1 Like

Also, if the thermistor would fail, the system would try to compensate that issue instead of alerting a thermal runaway.

All good points. Another one to consider is that PID constant derivations is very largely empirical and different methods exist for different applications and different desired control loop stability and performance goals.

Your “ideal” PID constants will not change unless you change the physical configuration of the printer. For example, if you change your heater cartridge power output the thermal constants (response) of your system will obviously change. This will necessitate potential re-evaluation of the PID constants. Live adjustment of PID constants is not required unless the system exhibits several totally different response modes (multi-modal system). This is not the case for normal heaters of 3D printers.

If you carefully review some of the Klipper documents, you will find references to performing manual PID tuning. This can be extremely effective in some corner cases that are otherwise difficult for the auto-tune method. Just to understand how sensitive or insensitive the PID can be, consider the following set of constants for my CR-10S Pro bed PID controller:

# Klipper PID auto-tune
pid_Kp = 51.75
pid_Ki = 0.823
pid_Kd = 813.2

# Manual PID method using 20% to 25% PWM step test & Cohen-Coon derivation
pid_Kp = 229.5
pid_Ki = 42.36
pid_Kd = 184.2

The bed on my printer seems to be a corner case, but I have seen reports or worse. It heats slowly and it cools extremely slowly with the stock heater plate & WhamBam flex system. It seems to be somewhat challenging for the relay auto-tuning method that Klipper uses. It closes loop well, but there is a large-ish overshoot and the stability is not perfect causing some small drifts. At one point I performed a lengthy step test manually to establish the first order system constants and then use those constants with different FOPD methodologies to derive the PID constants. I achieved the best overall control loop response using Cohen-Coon derivation method. The manually derived PID constants are TOTALLY different, and I mean by a huge amount. But the resultant control lop is extremely tight - I get no more than 0.2 degree C overshoots when heating the bed and the temperature never varies when monitoring with 0.1 degree C resolution. The commanded power (PWM) output is also very well controlled, which is a tell-tale sign of a well working control loop. And this is true for bed temperatures between 50 and 90 degrees C.

The moral of this story is that PID tuning is fundamentally a bit of “black art”. Different systems require different approaches, and there is no single algorithm that will “clean all dirty”. If the built-in method(s) of automatic PID tuning do not result in ideal system response, it takes a lot of time and effort to optimize the PID controller. But it’s worth it IMHO.

A continuous PID auto tune is not really something that’s going to work. PID is a calculus formula using integrals and derivatives. There are different formulas and methods for auto tuning PID loops, but they’re all fundamentally very similar and that they move the set point to at least 10% higher than a currently is then watch the rate of change to get there. After reaching that 10% over point they watch the rate of change for how long it takes to get back to the set point. Rinse and repeat. Add some calculus into the mix and out of the formulas come your P, I and, D parameters.

The different auto tuning theories all give you different results to be used in your PID parameters. Some auto tuning theories are better for maintaining fluid levels in tanks, others are better for maintaining accurate positioning systems and others are better for heating/cooling controllers. There are also other ways to modify your PID process such as slowing down the rate of change, say taking 10 minutes to get to temperature instead of 30 seconds or ramp soak where you ramp up to a set temp, then soak for so many minutes, then ramp up and soak to your next temperature until you reach your final temperature.

If I recall correctly Duet RRF has successfully implemented Feed Forward and ambient dependent PID.

RRF has PID and First-Order Plus Delay Time (FOPDT). According to my basic understanding, FOPDT needs to be tuned just like PID to represent the physical characteristics of the systems but uses a different feedback loop approach.

Klipper has an experimental branch for FOPDT as well. See Experiment with "First Order Plus Delay Time" (FOPDT) temperature tracking

Edit: See Tuning the heater temperature control | Duet3D Documentation

Just for clarification, my earlier post was referring to establishing a FOPDT system model for the given heater (gain, time constant and dead time) using a manual open-loop approach. This model can then be used to derive the PID coefficients using the various standard tuning techniques to drive the existing PID control algorithm in Klipper.