How-to: Quickly hand-tune your PID values

User-Generated tips and tricks for the Rostock Max, Orion, H1.1, or H1 Printers
Post Reply
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

How-to: Quickly hand-tune your PID values

Post by 626Pilot »

Introduction
===========================================
PID auto-tuning algorithms on various controllers can be annoying. I got tired of having to wait for them, only to see results that oscillated rather than being steady (especially during a print), so over time I learned how to do it myself. With the method I describe here, my hot end never wavers from the set temperature by more than 0.1 degrees C during a print. I also never get that thing where it's "warming up forever," but never quite reaches the exact print temperature, causing the print to never start. The heated bed stays on target, rather than oscillating between low and high power, so the cooling fans aren't changing pitch all the time due to large swings in current draw. Finally, I don't have to wait for the auto-tune, only to get temperature control that's wavering by half a degree or more, when I can just do it myself in less time and get a fraction of that variation.


THERMAL RUNAWAY WARNING
===========================================
A "thermal runaway" is where the heating element receives partial or full power well above the set temperature, which may cause it to keep heating until something melts or catches fire, like your whole entire house. DO NOT take your eyes off the temperature reading during the PID tuning process.

The following conditions can cause a thermal runaway:
  • P set to a very low value - I would start at a minimum of 20 for a hot end, and 100 for a heated bed, and adjust up or down from there. (Even 20 might be too small - be cautious about this.) I have seen values less than 10 cause thermal runaway.
  • Possibly other bad PID settings (haven't seen this myself, but can't rule it out).
  • Power MOSFET on the controller, or external solid-state relay (SSR), "fails open," causing partial or full power delivery, even though the controller has commanded it to shut down. This can happen any time the heater is energized, whether or not you are messing with the PID settings. A thermal fuse is one way to compensate for this. If your power supply is capable of accepting a "shutdown signal", your controller and firmware may have a facility for triggering that - look in the documentation. (ATX power supplies may be able to do this. LED-style power supplies can't.) Alternately, it should be possible to fit an external DC SSR that controls voltage to ALL the heaters, assuming your board's microcontroller is fed separately from the heater power. That's pretty common. If your board can let you have 24V heaters, then the power is fed separately. I know Smoothieboards are like this, and I'm pretty sure the Duet and RAMBo are like this as well. The firmware would have to have thermal runaway code that sets the SSR's pin high when the system is in a normal state, to enable the heaters, and it would have to take the pin low if it detected a thermal runaway. I don't know which firmware does and doesn't do this. If you do, post about it!
  • Possibly other things. Feel free to chime in.
I'm providing this information in the hope that it will be useful, but this information comes WITHOUT any express or implied guarantee on my part. I freely acknowledge that some or all of it may be wrong, and potentially dangerous to follow. YOU take full responsibility for messing with this stuff. Again, DO NOT take your eyes off the temperature reading while you are automatically or manually tuning the PID settings! If you see the temperature running away, and you can't figure out how to stop it by changing the settings, TURN THE WHOLE THING OFF BEFORE IT MELTS OR CATCHES FIRE!!!


How PID Variables Work
===========================================
Now that you've made it through the scary disclaimers, we can get on with the show.

You can read up on proportional-integral-derivative (PID) algorithms, but they tend to be very complicated to understand. Instead, let me save you some time on that by showing you this excellent GIF animation from the article on Wikipedia:
PID_Compensation_Animated.gif
PID_Compensation_Animated.gif (401.42 KiB) Viewed 81196 times
See? MUCH easier than reading an enormous wall of text about integrals and derivatives! From that article, there is some explanation of what each factor does:
PID.png
That table is nice, but for me, it's too much theory and not enough application. Let me boil all that jargon down into something directly actionable:
  • "P" controls the reactiveness. It is called "proportion", but I like to think of it as "power" instead. This is because it determines BOTH how aggressively it will try to get the heater up to temperature, AND how aggressively it will back off as the temperature is approached/exceeded. The higher this value, the closer you have to be to the set point to get a big reaction out of the system, so increasing it to the right amount will narrow the range of temperature oscillation. (However, if you set it too high, it will backfire, so you'll have to experiment a little.) Set this first. If the heater doesn't get hot enough, increase it. If the heater overshoots like crazy, and never quite settles down to the requested temperature, decrease it - but not too low. If you set it to a very low value, it will heat constantly, and the temperature will run away! Anyway, don't expect the temperature to be smooth at this point. You just want it to be "sorta close."
  • "I" fine-tunes the result you get from P, but will also make the temperature oscillation more "peaky." As you can see in the GIF animation above, it will get the average temperature to be what you set. If you ask for 210 degrees, and it's oscillating between 209 and 211, you have this one set right.
  • "D" smooths out the final result. Increase this to take the "wobble" out of the temperature. Don't jump the gun and set it to some high number out of the gate. If there is an overshoot, undershoot, or excessive temperature oscillation, doing so may make it harder to figure out whether P or I (or both) need to be changed.
You can see all that in the animated GIF above. When you start, unless you have "known-good" starting values (as below), you will set P=(some reasonable value), I=0, D=0. That lets you see what P is doing by itself. Tune P first, I second, and D third. Do NOT adjust more than one variable at a time! They are all interrelated, so you may wind up chasing your own tail for hours! Once you go through P, I, and D, and get the temperature to hold somewhat, you can go back and adjust the variables individually. (See Troubleshooting, below.)


Maximum Current and PWM
===========================================
In addition to the P, I, and D variables, you can set the maximum current and PWM pulse width. You may want to limit the current to both hot end and heated bed if you're using 12V heater(s) on a 24V system. Otherwise, it's usually left alone. Look at your documentation to be sure. If you are using an external SSR to feed the heated bed, this value is unlikely to do anything, so in that case I'd leave it alone.

The hot end usually doesn't require PWM to be adjusted, but the heated bed might. PWM (pulse-width modulation) controls how frequently the voltage is high (turned on), and how frequently it's low (turned off). The range is usually 0 (always off) to 255 (always on). If it's 127 (halfway point), it will be on half the time, and off half the time. Max PWM can be quite useful, especially if you have a "clean" circuit between the power supply and heated bed (i.e., you're using fat wires and a very-low-voltage-drop SSR to feed the heated bed). I think this is because when you use the controller's on-board FET, there is more of a voltage drop (electricity being lost and turning into waste heat), which has the same effect as reducing the PWM - in other words, less voltage gets to the heated bed per unit of time. When you're using a really good SSR, the voltage drop is negligible, the heater is able to heat more effectively, and the propensity to overshoot the target heat is greater. Reducing the max PWM allows us to "calm down" the heater a little, so it won't be as likely to overshoot.

On Smoothie, current is controlled with X, and max PWM is controlled with Y. On the Duet, it's different, and I'm not sure exactly how it works, but there are some values here on the RepRap Wiki. I don't know whether Repetier supports either current or max PWM.

As for Marlin, I suggest using Repetier instead. Runs smoother in my experience, stores many more config values in EEPROM, direct integration with Repetier Host, etc.


Hot End
===========================================
For the Hot End and Heated Bed sections, I'm using starting values that have worked on me with Smoothie. If you have a RAMBo or Duet, look in the documentation to see what starting values are good.

For an E3D v6, try P=30, I=5, D=5. The Cyclops, with a much bigger heater block, needs a P value more like 60. The hot end has a relatively small thermal mass, so it doesn't require a ton of power. Do the initial tuning with the printer not doing anything. You can refine it when an actual print is running. You may find that your best tune during a print is better than your best tune when the printer is still. I definitely see less oscillation when the printer is actually extruding. (This is another reason why hand-tuning is more accurate than auto-tuning on a 3D printer.)


Heated Bed
===========================================
These are more difficult to tune because they have a large amount of thermal mass, which adds a great deal of inertia to the equation. If you change the PID values on your hot end, you will usually see the result within a few seconds. Change them on your heated bed, and they may take 30 seconds to a minute to settle!

When tuning the heated bed, jog the hot end down to about Z=3 or so. The hot end's cooling fan will wash the heated bed with some air, increasing the heating load, and that will throw off the result. If you have a part cooling fan that you use a lot, it's not a bad idea to turn that on as well. If the printer is homed, you won't have those real-world effects going on, and you may find that the result is not as good as it could be.

For an Onyx, try P=130, I=1, D=15. It has far more thermal mass, so P has to be higher, and you can expect far less oscillation than you'd see with a hot end (assuming you tune it right). On RAMBo, I seem to remember P being set much higher (like 600 or something). Not sure on that, haven't used one for years. It would be a good idea to look up the best values, if they are available. Also, as heat loss increases exponentially with target heat, it's harder to PID tune an Onyx at 75C than it is at 50C. You may want to auto-tune the heated bed, and then fine tune it from there. I will add more information here, assuming I can figure it out myself!


Heated Bed with Solid State Relay (SSR)
===========================================
PID values that work fine for the MOSFET on your controller won't work if you install a solid-state relay (SSR). The resistance of an excellent SSR, like the Auber Instruments 100-amp DC SSR, is extremely low, whereas the resistance of a relatively small MOSFET (like you find on a controller) is - if not exactly "high" - still significant. That's why the FET on your controller will get hot, whereas the Auber SSR will barely be above room temperature at 12V. When I asked Smoothie to auto-tune my SSR-fed Onyx, it came up with the values P=155, I2.5, D2350. P and I are not surprising, but D is huge! Maybe that is how the algorithm compensates for the very long thermal lag time.

EDIT: When I wrote the above paragraph, I was running the SSR on the ground side of the circuit. I have since switched it to the hot (+12V) side of the circuit and installed a Blue Sea Systems 25A circuit breaker. This threw off my PID tune. I'm now using values P=170, I=0.5, D=2250, and the temperature is almost always exactly at set point (75C +- 0.1 degree, sigma appears lower than 0.01C). It may be that the hot side of the circuit is a better location to put the SSR. When it was on the ground side of the circuit, it had more variance, and I had to limit the max PWM. As it is now, max PWM is 255 (highest value) and it has no trouble maintaining temperature.

Fore more info about using SSRs, this appears to be the definitive thread. For reference, this is my (amateur and not certified by an electrical engineer) SSR/breaker setup:
20170116_162656.jpg
Troubleshooting
===========================================
  • Heater keeps getting significant power well above set temperature (thermal runaway): Make sure P is at least 20 (hot end) and 100 (heated bed). If it already is, you may have a "failed open" MOSFET or SSR. SHUT OFF MAIN POWER TO THE PRINTER WITHOUT DELAY.
  • Heater never quite gets up to temperature: Increase P
  • Heater overshoots by a lot/always stays above the requested temperature: Decrease P (but not too low). If not using an SSR, and it does this only on initial warm-up, you can try reducing the current.
  • Heater oscillates in temperature, and the average isn't quite what you set the temperature to: Increase or decrease I
  • Heater oscillates in temperature, and the average is at or very close to what you set the temperature to: Increase D
  • Temperature oscillates by more than a few tenths of a degree: Fine-tune the values during an actual print. What you obtain from a "static" tune is different than an "in-print" tune.
  • I still can't get it to stop oscillating: Try reducing the max PWM. 100-200 is a good starting range. If using an SSR, consider moving it to the hot (positive) side of the circuit if it isn't already.
Last edited by 626Pilot on Mon Jan 16, 2017 8:36 pm, edited 14 times in total.
User avatar
DeltaCon
Printmaster!
Posts: 616
Joined: Sat Nov 14, 2015 5:01 am
Location: Wessem, The Netherlands
Contact:

Re: How-to: Quickly hand-tune your PID values

Post by DeltaCon »

NIce write up! And indeed a GIG that says it almost all.
PITA that I can't print that (at least not with the moving lines! :mrgreen: )
I am DeltaCon, I have a delta, my name is Con, I am definitely PRO delta! ;-)
Rostock V2 / E3D Volcano / FSR kit / Duet 0.6

PS.: Sorry for the avatar, that's my other hobby!
User avatar
rurwin
Printmaster!
Posts: 68
Joined: Sat Sep 24, 2016 10:43 am
Contact:

Re: How-to: Quickly hand-tune your PID values

Post by rurwin »

Nice write-up.

If all this is too complex for you, at least it isn't as bad as one I programmed once.
Imagine a formula-1 clutch. It's six inches in diameter, spins at 20,000RPM and applies a force of around two tonnes between the plates.
I had to program the control for a test-bed that controlled the speed of the output shaft to follow a given profile (the take-off from the grid.)
The speed was controlled by a PID that used the transferred torque as the controlling variable.
The transferred torque was controlled by a PID that used the plate force as a controlling variable.
The plate force was controlled by a PID that used the clutch pedal position as the controlling variable.
The clutch pedal position was implemented by a PID feedback loop with reference to the measured position accurate to a tenth of a millimetre.
All while rotating at 20,000RPM against a varying load that mimicked wheel-spin.
And all of those PID loops were tuned by hand.
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

Added some disclaimers about thermal runaway, and made the formatting nicer.

rurwin, that is pretty cool. I have seen top fuel cars in action. Quite impressive. Must have burned through some expensive hardware getting all that tuned.
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

Added info on adjusting current and max PWM. Added advice to jog the effector down to ~Z=3, so you get the effect of whatever downwash is happening from the cooling fan(s).
User avatar
rurwin
Printmaster!
Posts: 68
Joined: Sat Sep 24, 2016 10:43 am
Contact:

Re: How-to: Quickly hand-tune your PID values

Post by rurwin »

When tuning the PID, you change the setpoint by an amount in order to see the behaviour of the system to that change. You will find that the response to large changes is different from the response to small changes because of something called Integral Wind-up (AKA Reset Wind-up). When starting from cold the temperature may settle a little high before dropping back down to the setpoint. This is because the integral term is constantly gathering strength all the time that the bed or hot-end is warming up. When the temperature reaches the setpoint it takes a while for the integral term to reduce back down and start to do its job. The firmware has an algorithm to limit the effects and other factors with this particular application also limit it*, so it is not likely to be a major issue.

However, you should tune the PID while making small changes of maybe 2-5 degrees in the setpoint, but then check that it still behaves well when starting from cold and also controls well in steady-state conditions.

Another potential issue is that cooling and heating have different characteristics. Heating is dependent on the power being supplied while cooling depends on the radiation and conductive losses to the air and filament. The same PID tuning will behave differently depending on whether the temperature is rising or falling. So always check both directions and don't be surprised if they are different; the correct tuning is a compromise between the two. This can be an issue if the two characteristics are very different. If heating is efficient but cooling is not, or vice-versa, then it can be difficult to find a tuning that is effective in both cases. That might be something you come across when changing to 24V for example. It's another thing that probably makes the heated bed the hardest to tune; it is designed to heat up fast but it takes ten minutes or more to cool down.

---
* If the loop is still running but having no effect for some time then the integral can wind-up for longer and cause problems. Imagine the case that the loop is controlling the temperature but the heater is not powered because of a switch being off. When the switch is turned on the integral term causes a large overshoot. Obviously that situation doesn't apply here, since the firmware is always in control of the heaters.
dc42
Printmaster!
Posts: 454
Joined: Mon Mar 07, 2016 10:17 am

Re: How-to: Quickly hand-tune your PID values

Post by dc42 »

Another issue with standard PID is that the optimum parameters are different (very!) depending on whether your priority is to react quickly to changes in load, or to implement a large change in temperature without massive overshoot. For example, if the heater is at 20C and you command it to 200C, you don't want 10% (i.e. 18C) overshoot and it is better to allow it to take a little longer to reach 200C and avoid serious overshoot. But if the head is already at 200C and changing the fan speed causes it to drop to 198C, you want to bring it back to 200C as quickly as possible, and 10% (i.e. 0.2C) or an even greater overshoot is no problem. That's why in RepRapFirmware, we don't to conventional PID auto tuning. Instead we use auto tuning to develop a first-order-plus-dead=time model of the heater, and from that we calculate two sets of PID parameters. One is used when the setpoint is changed by a large amount. The other (more aggressive) set is used when the temperature is close to target and we want to keep it that way.
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

A dead-time heat manager is implemented in Repetier. It takes only two variables, which effectively describe the thermal inertia of the system. Overshoot is minimized or eliminated, and there's no oscillation around the set temp.
MeasureDeadtime.png
The idea is to figure out the "knee" of the temperature curve. Once the temperature is climbing at a relatively steady rate, you project the slope of that down to the temperature where you started. The time it takes to get from start to that intersection is put into the dead-time variable. The second variable is called "PID drive max" in Repetier, and that tells it how much power it needs to maintain the temp in a steady state. That's all it needs to know!
TempControlComparison.png
If I have time, I would like to try implementing this algorithm in Smoothie. (I asked the devs about this once - they were not interested in doing it themselves.) This algorithm worked very well for me when I was using Repetier. I honestly like it a lot better than PID. It seems to acknowledge that the goal in heating a radiative mass requires a precise, gentle fall-off as the set temperature is approached from below, and then "just enough" to keep it there. PID is more general-purpose, and might be the better algorithm if we were driving a heat pump (like a Peltier) rather than a heating-only circuit. It was originally designed long ago to control the rudder of a large vessel, so it would correct for current-induced drift and keep the vessel on course. The rudder on a ship can turn in both directions, but our heaters only go in one direction.
User avatar
DeltaCon
Printmaster!
Posts: 616
Joined: Sat Nov 14, 2015 5:01 am
Location: Wessem, The Netherlands
Contact:

Re: How-to: Quickly hand-tune your PID values

Post by DeltaCon »

I was very content with dead time on my Rambo for the bed. I am not so sure that it works the same wonder for a hotend because of the very low thermal mass.
Because of the conversion to a duet board, I had to find a different method. Although bang-bang on the duet works much like dead-time on the Rambo, I have found PID to work even more precise. After some manual fiddling with the manual in another thread here I reached an oscillation of not even half a degree in both hotend and bed. Nice about the duet is that the PID switching frequency for the heatbed channel is only 10 Hz so any SSR can handle that. Sorry 626pilot, I know you are a Smoothie guy ;-)
I am DeltaCon, I have a delta, my name is Con, I am definitely PRO delta! ;-)
Rostock V2 / E3D Volcano / FSR kit / Duet 0.6

PS.: Sorry for the avatar, that's my other hobby!
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

When I was still on Repetier, I used the dead-time control for both the hot end and bed, and it handled both of them quite well. With my PID tune on Smoothie, I am seeing a bed oscillation of about 0.2C. I imagine the SSR is a little slower to fully change state than the relatively small MOSFET on the controller. Before I added the SSR, the heated bed was dead flat, less than 0.1 degrees C of variation. I know that Smoothie's PWM is high-resolution even for the heated bed because the bed used to "sing" when I was using GeckoTek build plates. That sound was how I knew it was done heating up! Maybe a per-channel max switching frequency would be in order, to help compensate for switching delays in the SSR.

One thing I am noticing is that setting max PWM applies to the heat-up phase as well as the steady-state phase, so I'm having to set M301 Y255 when it's heating and M301 Y130 when it's done. If I leave Y at 255, it will oscillate by about half a degree when it's hot. It bugs me, but I don't know how much it really matters. Mostly, I don't want to hear the fans accelerating and decelerating because of swings in current consumption.
User avatar
rurwin
Printmaster!
Posts: 68
Joined: Sat Sep 24, 2016 10:43 am
Contact:

Re: How-to: Quickly hand-tune your PID values

Post by rurwin »

dc42 wrote:That's why in RepRapFirmware, we don't to conventional PID auto tuning. Instead we use auto tuning to develop a first-order-plus-dead=time model of the heater, and from that we calculate two sets of PID parameters. One is used when the setpoint is changed by a large amount. The other (more aggressive) set is used when the temperature is close to target and we want to keep it that way.
Repetier only uses PID when the temperature is close to the setpoint. (10% of full-scale IIRC, but I'd have to check the code.) If it is any further off than that it uses full heating or nothing.
dc42
Printmaster!
Posts: 454
Joined: Mon Mar 07, 2016 10:17 am

Re: How-to: Quickly hand-tune your PID values

Post by dc42 »

rurwin wrote:
dc42 wrote:That's why in RepRapFirmware, we don't to conventional PID auto tuning. Instead we use auto tuning to develop a first-order-plus-dead=time model of the heater, and from that we calculate two sets of PID parameters. One is used when the setpoint is changed by a large amount. The other (more aggressive) set is used when the temperature is close to target and we want to keep it that way.
Repetier only uses PID when the temperature is close to the setpoint. (10% of full-scale IIRC, but I'd have to check the code.) If it is any further off than that it uses full heating or nothing.
All PID implementations that I know of do something similar. In RRF the transition point is calculated from the gain of the heater as established during the tuning process.
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

Added some more detail about what P does (higher values narrow the response curve, so you have to be closer to set temp to get a response of a given strength - but you don't want the P value TOO high), and split off SSR info to its own section. I'm also recommending, based on much easier/tighter PID tuning results, to install the SSR on the hot (positive) side of the circuit rather than the negative. Steady-state temperature range and standard deviation are both noticeably lower, and it took a fraction of the time to get those results from tuning.
User avatar
sfuelberth
Prints-a-lot
Posts: 34
Joined: Sun Aug 07, 2016 9:22 pm

Re: How-to: Quickly hand-tune your PID values

Post by sfuelberth »

I was having a hard time wrapping my head around the PID calculations and this post has now made it so that I have a deviation of around 0.2 degrees C which is vastly improved (I admit I was a bit hesitant on changing values not fully understanding the ramifications completely). Unless you understand PID controllers I think this should be mandatory reading as it is concise and to the point, at least for a starting point. Thanks for taking the time to write this up.
User avatar
626Pilot
ULTIMATE 3D JEDI
Posts: 1720
Joined: Tue May 14, 2013 12:52 pm

Re: How-to: Quickly hand-tune your PID values

Post by 626Pilot »

Thanks! I'm always glad to hear when some method I post works for someone who isn't me. :)
User avatar
sfuelberth
Prints-a-lot
Posts: 34
Joined: Sun Aug 07, 2016 9:22 pm

Re: How-to: Quickly hand-tune your PID values

Post by sfuelberth »

I think this should be a sticky, would also fit into the troubleshooting forum.
User avatar
thingismith
Printmaster!
Posts: 68
Joined: Sat Apr 08, 2017 7:45 pm

Re: How-to: Quickly hand-tune your PID values

Post by thingismith »

Just wanted to thank you for another great How-To, 626Pilot! Ever since I installed my accelerometer, the autotune has disappointed me. I'd prefer a more step-by-step guide, but this really does cover everything. I'd like to second the nomination for a sticky!
AllanMac
Noob
Posts: 1
Joined: Sat Nov 18, 2017 5:03 am

Re: How-to: Quickly hand-tune your PID values

Post by AllanMac »

thingisfmith wrote:Just wanted to thank you for another great How-To, 626Pilot! Ever since I installed my accelerometer, the autotune has disappointed me. I'd prefer a more step-by-step guide, but this The Vigrx Plus is a good choice too! really does cover everything. I'd like to second the nomination for a sticky!
A step by step guide for newbs like me would be amazing but either way I'll figure it out in the end.
Post Reply

Return to “General Tips 'N Tricks”