Experimental PID improvement changes

@ReXT3D could you perhaps run a pid tune on your bed and send me the output file?

I’ve had a minor breakthrough, with regards to getting the velocity form to work. Beds such as are simply work better when cohen-coon based parameters are used, and after a little digging i’ve found that cohen coon parameters can be generated via the relay test klipper already uses.

I obviously can’t change the pid parameters outputted during calibration, but i can dump them to the log like Ku & Tu already are.

If you send me a pid calibration output file I manually go through it and send you back some pid parameters to test and tell me what you think.

heattest.csv sent through Discord…

I’ve finally got the last round of development ready for everyone to test. It took far longer than I thought it would to complete, but that’s mainly because I stubbornly went down the path of continuously trying to shove a square peg into a round hole. Add to that distractions of everyday life.

No matter what I tried, I could not get classic Ziegler-Nichols parameters to work well for my bed while using the velocity algorithm, regardless of what temperature I calibrated for or what smoothing algorithm I used. The primary reason for this seems to be the huge lag/dead time associated with thick aluminum beds! The fact that my bed sensor is noisy only exacerbated the issue.

@ReXT3D inspired me to try Cohen-Coon parameters, and to my joy, they worked far better regardless of what control algorithm I used. After reviewing my notes and the various papers I’ve collected, I noticed that Cohen-Coon parameters can be calculated using a relay test.

see equations 5 & 6.

Thus I updated the pid calibration class to also calculate Cohen-Coon calibration constants. If you check the klipper log after a calibration run you will see something like this.

Ziegler-Nichols constants: Ku=0.103092 Tu=41.800000
Cohen-Coon constants: Km=-17.734845 Theta=6.600000 Tau=-10.182680
Autotune: final: Kp=15.773129 Ki=0.754695 Kd=82.414600
PID parameters: pid_Kp=15.773 pid_Ki=0.755 pid_Kd=82.415
The SAVE_CONFIG command will update the printer config file
with these parameters and restart the printer.
save_config: set [extruder] control = pid
save_config: set [extruder] pid_Kp = 15.773
save_config: set [extruder] pid_Ki = 0.755
save_config: set [extruder] pid_Kd = 82.415

You can take the constants from the log and drop them into the attached spreadsheet (can be opened with almost any spreadsheet application, including several free ones), and try out the various pid parameters.
pid_params.zip (5.3 KB)

As a refresher you can run pid calibration with the following command.

PID_CALIBRATE HEATER=heater_bed TARGET=70 TOLERANCE=0.03 WRITE_FILE=1

  1. WRITE_FILE is not needed unless you want to look at the output directly.
  2. TOLERANCE defaults to 0.02 if not passed in. The tighter the tolerance the better, but how tight you can achieve depends on how clean your sensor readings are.

The following graphs show comparisons between Ziegler-Nichols classic & Cohen-Coon parameters generated from the same calibration run.

The plot shorthand is as follows:
positional - the current control algorithm used by klipper
velocity - the new velocity control algorithm
zn - Ziegler-Nichols
cc - Cohen-Coon
calibraded - pid parameters generated directly from a calibration run.
manual - pid parameters generated from a calibration run that have been manually tweaked to yield better performance.

75C bed

55C bed

40C bed
40C is by far the hardest scenario to control on my printer. it’s within 20C of ambient and only requires 4 to 6% of the heaters capacity to maintain temperature.

If you want to try the velocity algorithm, just change the Control algorithm from “pid” to “pid_v” in your config. Be sure to monitor the first run, because if you have bad pid parameters, or a particularly noisy system the velocity algorithm might not work for you without manual tuning.

the code can be found here.
https://github.com/dans98/klipper/tree/PID-Improvements

2 Likes

I’ve heard no negative feedback since the last update so I submitted a PR.

My apologies, beyond the feedback that I passed on to you on Discord I’ve been busy chasing my tail on other subjects, so had no chance to experiment more…

No worries, a lot of people had played with it and given feedback!

1 Like

Hi.
I have tried Your improovements. I am using the bambulab hotend which heats up very quickly, the PID tune finishes within a minute. I finished with takining the Ziegler-Nichols values from the log and using the Some Overshoot values. Additionally changing to pid_v did an additionall improovement. The PWM spread was about 50% with the master branch tune, now it is about 10-15% and holding the temperature more precise. Thank You. My opinion is the pid_v should be merged into main. Also the PID-CALIBRATE routine should return the alternative PID values for Some Overshood and No Overshoot in the terminal window.

1 Like

how do you mean that - edit the python file ( klippy/extras/pid_calibrate.py ) and replace the

Ku = 4. * self.heater_max_power / (math.pi * amplitude)

and

Tu = time_diff

in the code with the values from the log ( i.e Ziegler-Nichols constants: Ku=0.090939 Tu=25.400000 ) ?

Nooooo, taking the values from the log and placing in the excel/calc sheet which is in this topic :grinning:

ahh ok, havent found the excel sheet, but now found it, its in the pid_params.zip (5.3 KB) - thanks

may i ask why you choose the “some overshoot” values and not the “Pessen Integral Rule”?

Im refering to the section “Tuning” and the graph:

@Hundsbuah keep in mind those graphs are based on a perfect simulation/model. what we have are sensor readings with noise, and then random disturbances like your hvac turning on/off, or a door opening or closing etc.

Also each tune acts differently when saturated compared to unsaturated.

Sure, i choose it because it gives me the best effect with less time spend tuning it. Of course i like it to be tuned good, but I also keep in mind how it affects the print.

Longer story:
I am using the bambulab hotend which is heating up very quick. The master (branch) PID CALIBRATE routine takes less than a minute. I think the routine returns the Ziegler-Nichols values way off, this leads to a bad tune. Following the Classic ZN PID values results in permanent oscilation and a PWM signal range of permanently about 50%, never stabilize. The PID CALIBRATE routine from Dans returns almost same PID values. So i took the ZN values from the log, posted into this excel sheet and tried all four PID value sets. The set named “Some Overshoot” gives best results, fairly good stabilization and good reaction during print. Choosing “pid_v” improoves it even more. That was my short trip into this tuning. If someone is interested, i can log whatever is helpfull, just tell me what You need and how to approach.

I think the PID-CALIBRATE mersurement cycle is way off in case of this hotend, may be due to measurement delay or some other effects. Maybe there is some idea to modify it so it could better approach this quick hotend.

During print this hotend stabilize at about 45% PWM.