Advertisement
robathome

PIDs - HERE BE DRAGONS

Jun 3rd, 2019
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. So I want to take a minute to talk about the black-magic/here-be-dragons mystery settings that we call "PID".
  2.  
  3. There are a lot of people on here that post pictures of prints with intermittent under-extrusion, and my suggestion of "try re-tuning your PID" is usually drowned out by "it's a clogged nozzle!!" and "calibrate your e-steps!!". That's not to say that those are invalid answers, but they only really need to be said once, and I feel most people are hesitant to suggest modifying the PID values, simply because they're poorly understood.
  4.  
  5. Simply put, a PID is an "intelligent" controller that is much better-suited for precise control of our hotend's temperature than the traditional "bang-bang" technique...
  6.  
  7. The first step to understanding the PID is understanding what goes in and what comes out: First, the input into a PID controller isn't the actual target temperature (aka the "setpoint"), but rather the error between the setpoint and the current value. Second, in 3D printing applications, the output is almost always converted to a PWM signal that is used to moderate the power delivered to the heater in question. A PWM signal ("Pulse Width Modulated") is how you get a continuously variable output from an on/off input... by turning the solid-state power switches (aka MOSFETs) on and off again REALLY fast, and the ratio of ON time to ON+OFF time (which we call the "duty cycle") blurs together and averages out to the desired output. For example, the PWM of a 24V supply that's repeatedly switched on for 15 milliseconds and then off for 30 milliseconds is essentially the same as a constant 8V signal ( 15/(15+30) = 33%, 33% of 24V is 8V ).
  8.  
  9. In order to determine the appropriate PWM duty cycle, the PID has three components: the Proportional controller, the Integral controller, and the Derivative controller. If you've got ay experience with calculus, you can probably guess where this is going. If not, don't worry, there's no formulas here :) (fun fact: I suck at calculus!)
  10.  
  11. The Proportional controller is extremely simple. It responds to the size of the error. Lots of error = lots of output. It always responds the same, for example, if the difference between the hotend temperature and the setpoint temperature is 50C. It doesn't matter if the target temperature is 200C or 2000C, the Proportional controller only responds to the *current* error between the setpoint and current values.
  12.  
  13. Where the Proportional controller reacts in the moment, the Integral controller responds to the accumulated error over time. If you're only, say five or six degrees from your setpoint, odds are your Proportional controller isn't going to contribute much. However, the longer that small error persists, the stronger the output from the Integral controller becomes.
  14.  
  15. Finally, the Derivative controller responds to the rate of change of error. It's the opposite of the integral controller, instead of looking what the error has been recently, it predicts where the error is going to be, based on the current rate of change. While this component "kick-starts" the response to a sudden change in the setpoint, the Derivative controller spends more time keeping the other two controllers in check than anything else. If you're getting ready to print and you change your hotend temperature setpoint from 25C to 225C, all three controllers are going to freak out and likely try to max out the power to the heater. After a few seconds, the temperature is going to be rising quickly, but the sheer error and its recent history are still going to have the Proportional and Integral components going nuts. This is when the Derivative controller steps in and calms everything down, and hopefully will prevent excessive overshoot of the setpoint. This is why the Derivative component is almost always significantly stronger than the other two.
  16.  
  17. When we "tune" a PID controller, we choose three values, the "Kp", "Ki", and "Kd" multipliers (also called "controller gain" or just "gains"). These gains define how much weight each of the three controllers is given to the PWM calculation. What may not be obvious (because math) is that even a small change in any one of these values can have a massive impact on how it functions.
  18.  
  19. In the past, manual tuning was the only option, and it took hours and hours of trial-and-error, and probably a few machines caught fire in the process (Integral "wind-up" can catch you off guard...). Recently, there have been optimized processes developed for calculating the ideal parameters, the most popular of which is called the Ziegler-Nichols method. If you have a thing for pain, and if you're very, very careful and precise, you can calculate your own PID values. Thankfully, for the rest of us, the magicians that write the code we use as "firmware" (the interface between "software" and "hardware") have generously included a tool to do this painstaking process automatically: You might know it as M303, or the "PID Auto-Tune".
  20.  
  21. Almost all firmwares, Marlin, Klipper, Smoothie, all of them require the same inputs for the auto-tune sequence: The heater to be tuned, the target temperature for testing, the number of test cycles to be performed, and if necessary, a PWM limiter. If you have a significantly over-powered PSU and/or heater, then running the auto-tuning process at 100% is likely going to shoot the temperature through the roof, past the thermal safety limit (aka "max temp"), and the auto-tune fails. If this is the case, you can use the PWM limiter to restrict the PID output to something more sane, allowing the auto-tune process to run properly. Since most people are impatient and want the fastest possible time-to-setpoint for their hotend and bed, you can re-run the PID auto-tune as many times as you want, adjusting the PWM limiter incrementally until you find a value that *almost* throws a fault when heating. Just remember, if the ambient temperature is *colder* than what it is when you tuned the PID that way, the error will be larger when you change the setpoint and the PID will probably throw a fault again.
  22.  
  23. Now, you don't usually need *all* of those values when running an auto-tuning sequence, in fact, most firmwares only need the M303 command and the target temperature, and they will assume you're tuning the hotend and run (usually) five to seven cycles. You can Google "G-code M303" if you'd like to learn more about the individual input parameters. This brings me to the last point, perhaps the most important when aiming for the ideal PID values:
  24.  
  25. Test conditions should be as close to printing conditions as possible.
  26.  
  27. You can run auto-tunes as many times and as frequently as you'd like. If done properly, this will find a set of PID values that best suits the current conditions. And you can save values and use them later. Depending on the printer, different setpoints can have significantly different ideal PID values. If you're planning on printing PETG, don't tune your PIDs with a target temperature of 180C. Personally, I have PID values for four different setpoints in my firmware, but three of them are deactivated (commented out) at any given time. This way, I don't have to re-tune every time I change filaments.
  28.  
  29. This also applies to layer fans: Unless you have your hotend very, very well-insulated, the airflow from strong layer fans will suck some of the heat from the nozzle and/or heater block. If you don't tune your PIDs with the layer fans running, you can expect very different results than what you intended. (note: Creality printers come with some insulation taped around the heater block, but it is exposed on two sides... depending on what angle your layer fans are coming from, this may or may not apply to you. I recommend tuning with the fans on anyways, just to be safe). Finally, unless you're only printing wide, flat prints up close to the bed, the effect of the bed heater and reflected airflow from the layer fans will screw with your auto-tune results. Always auto-tune with the hotend up in the air, at least a few centimeters away from the bed.
  30.  
  31. Now, on the topic of the bed heater... We just established that we tune away from the bed, right? Why does it matter? It matters because of something called "voltage sag". As the load on a PSU increases, the voltage will drop (or "sag") somewhat. Really expensive, high-end PSUs might only sag a few millivolts, but low-budget printers like the Ender-3 don't ship with what I would call "premium" PSUs. Driving the heater cartridge in the hotend puts a lot of load on the PSU... but so does the bed. This can have an effect similar to the PWM limiter, and must be taken into account. That being said, the purpose of the PID is not to raise the temperature as fast as possible, the goal is to maintain as little error as possible throughout the duration of the print. This means that the bed temperature should be constant throughout the auto-tuning process. This can be counter-intuitive, since you have to start the PID auto-tune with the hotend at a stable, near-ambient temperature, those unfamiliar with the inner workings of the PID might start heating the bed at the same time. This is the opposite of what we want, and will result in a twitchy, oversensitive PID controller. Remember: tune as close to printing conditions as possible. If the bed heater will be on while printing, tune the PIDs with the bed heater on.
  32.  
  33. Phew, that's a lot to take in, isn't it?
  34.  
  35. Let's review:
  36.  
  37. 1) PIDs respond to the error between the desired setpoint and the current value.
  38.  
  39. 2) Test conditions should be as close as possible to your printing conditions.
  40.  
  41. 3) Don't be afraid to re-tune your PID controller if you think it needs it.
  42.  
  43. As with any other G-code, if used improperly, the M303 command can damage your hotend, the controller, and anything in between, so do some Googling and read up before you start fiddling with the optional parameters. With that in mind, a proper PID tuning can resolve underextrusion issues that might otherwise leave you scratching your head and re-calibrating your e-steps for the 1000th time...
  44.  
  45. I hope you found this little guide helpful! Feel free to ask any questions about PIDs that you might have, there might be someone else out there with the same question...
  46.  
  47. PS> It is theoretically possible to manually calculate PID gains that are better than the auto-tuned PIDs, however the auto-tuned values should always result in a stable, albeit slower temperature response. If you choose to manually adjust your PIDs (via M304), please do so in very small increments, probably 1% or smaller. That being said, for those of you out there with an Ender-3 and the stock hotend, give my gains a try: (Kp=18.28, Ki=1.28, Kd: 65.48). They're the best values I've found, up to 240-250C. I can't take credit for them, the kudos go to Frank LG!
  48.  
  49. edit #1: Jay Cobb made an excellent point: You should always re-tune your PIDs when *any element* in the heating system changes, including the nozzle, heater block, adding/removing insulation, adding / modifying the layer fan, heater cartridge, thermistor, heat brake, heat sink, and even the controller!
  50.  
  51. edit #2: Michael Albeta caught a math error, fixed it!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement