PID tuning

From RigidWiki
Jump to: navigation, search


The Marlin firmware installed on your RigidBot control board includes an implementation of a PID controller. PID controllers use a feedback loop of sensing and sending control commands to home in on a target and track with it. They are used to control everything from oven temperatures to self-driving cars that are trying to stay in their lane. In this case the PID algorithms are trying to keep your hot end at a precise and unchanging temperature while you print. Everything about your thermodynamic system can affect the behavior of your PID controller. If you move your heater cartridge a bit further in, or out, that may change results. The ambient temperature around your machine, the presence or absence of a draft, or an enclosure can all affect the behavior.

PEEK Insulation Meltdown

PEEK meltdown.jpg

PID tuning became a hot topic (ha!) when some users observed the PEEK insulation block above their hot end melting down, something which should not be happening at the target temperature for the print. As it turned out, the default PID parameters flashed into the RigidBot's controller were based on the original, smaller, hot end (which due to poor machining tolerances developed leaks). The new replacement hot end had a larger thermal mass and placed the thermistor further from the heater cartridge. As a result, the PID algorithm was overshooting the target temperature by 10-20 degrees, and in many cases oscillating around without ever settling down to the correct temperature. Thanks to some crowd sourced data collection, new default values were derived that should perform better for the stock RigidBot with the new hot end. These will be built into the "community edition" firmware to help avoid meltdowns, but if you're reading this page, you can do even better! By tuning your PID parameters for your exact machine, you should be able to get your hot end to "lock in" to a temperature within +/- 1 degree Celsius indefinitely.

The PID Auto-tune Function

You can run a test which will generate pretty good parameters for your specific combination of hardware. In Pronterface, Repetier, or Octoprint terminals you can send the command:

M303 E0 S200 C8

This will run a test on extruder 0, with a set target temperature of 200 degrees C, and for 8 test cycles. All extruders share the same PID parameters, but you can also PID tune the heated bed by specifying E-1. Some important things to know about this test:

The PID autotune function from M303 E0 S200 C8
  • The PID test doesn't use a PID control loop to control the temperature! It shouldn't matter what values you have stored when you run the autotune. Instead, the autotune function runs the heater in "bang-bang" mode, leaving the heater on for an extended period of time, and then off for an extended period of time, and simply observing the thermal behavior in response to heating and cooling.
  • Each cycle in the autotune run is a test where the controller cycles the heater, intentionally overshooting the target value for 5 seconds and observing how far up it goes, and how long it takes to coast back down to the target temperature. Once below the target temperature for 5 seconds, it observes how far the temp fell, and then cycles the heater on for another run at the target.
  • The first three cycles calculate a "bias" to dial in the level of maximum power that will be used for the remainder of the cycle/tests. You will get to see those bias numbers, but there's not much you can do with them. You will not get PID parameters from these initial cycles.
  • The fourth and subsequent tests still overshoot by 5 seconds and undershoot by 5 seconds, but this time the observed duration of the heat cycle, and the min and max temperatures reached, are put through the mathematical formulas described in the Ziegler–Nichols PID tuning method. This is a mathematical way to determine PID constants that perform acceptably in many systems. Values for the three parameters Kp, Ki, and Kd will be displayed for the cycle, and a new cycle will begin.
  • After all the test cycles are finished, you can average the values you get from each of these cycles and use those as your baseline parameters.

Using New PID Parameters

Now that you have new values for Kp, Ki, and Kd, you can set them on your controller, test them, and if they perform well save them to preserve between controller reboots.

To see your current PID parameters:

p:22.59 i:1.52 d:83.63 c:1.00

To set new parameters, for example Kp=14.39, Ki=0.66, Kd=80.09:

M301 P14.39 I0.66 D80.09
p:14.39 i:0.66 d:80.09 c:1.00

To set new parameters for your heated bed, use the M304 command with P, I, D values.

When you're satisfied with your hot end's performance, you can save these parameters to the EEPROM so they will be loaded on the next power up:

Settings Stored

Fine Tuning By Hand

A PID system tuned for no overshoot, a 3 minute settle time, and +/- 0.1deg C variance

Control system engineers can tune PID controllers by hand, and so can you!

First you should understand what the PID parameters are.

  • Kp is the "proportional" term. The further off target you are, the more power this term contributes.
  • Ki is the "integral" term. This term contributes more control input as the accumulated offset over time between measured and target temperatures increases. In other words the longer you've been off target, the more input this term will contribute in the direction of the target (could be more or less heater power).
  • Kd is the "derivative" term. This term allows the controller to "predict" or look ahead of the current temperature to slow the rate of change down and let the measured temperature creep up on the target. Mathematically speaking it attempts to minimize the slope of the temperature curve with respect to the target.

From this understanding we can derive some very generic rules of adjusting your values:

  • if the temperature is getting too hot before settling down, increase Kd and decrease Kp (more creeping up on the temperature and less muscling it around)
  • if the temperature tapers off just under the target and never quite reaches it, decrease Kd and increase Ki (less creeping up alongside the target, and more correction for cumulative offset) Increasing Ki means that cruising just a hair under the target for a long time will cause Ki to gradually put more power into the heater until it reaches the target. A higher Kd would fight this process by decreasing power to get the measured temperature curve parallel to the target temperature line.
  • if the temperatures are taking a long time to settle, oscillating or hunting indefinitely, decrease Ki and increase Kd (less agitation from cumulative error, and more gliding into alignment with the target); alternatively try decreasing all three terms… the PID equivalent of taking some deep breaths and relaxing the amount of control you're exerting

Further Reading

What is PID?

RepRap: "PID Tuning" page

Solidoodle PID Tuning page

Youtube: Using the PID autotune feature