ErikZalm/Marlin — pull request #519 by tenaciousRas (2013-06-14 22:18:25+00:00)
Title: extruder heater meltdown prevention

The temperature protection in Marlin still allows the hotend to exceed MAXTEMP if the thermal sensor (thermistor or probe) becomes physically dislodged from the hotend. Whereas the WATCH_TEMP_PERIOD check works now in some cases, whereas about 12 months ago it was logically broken and didn't work at all. This patch reduces the response time for a detached thermistor event from 40 seconds to approximately 7 (using bang-bang in my testing).

Background:
Reproducing one edge case of hotend catastrophe in Marlin is rather easy: simply dislodge the thermistor while printing ABS, or after the nozzle is pre-heated to ABS temp. The current algorithm will wait 40 seconds (by default) until it triggers heater shutdown. In this scenario a (non-metal) hotend is likely to melt.

Theory:
A better solution is to measure the 2nd-order differences in analog readings, smooth for noise, and shutdown the heaters if the 2nd-order average is non-positive. In other words, whenever heat is applied the rate-of-change of temperature-change should always be positive. This can be described as "the nozzle is warming". If the heater is on yet the sensor doesn't indicate the "nozzle is warming", then we know failure has occurred.

Implementation:
The theory breaks down in the real world due to thermistor error and system noise, which are significant factors. If the power applied to the hotend is small, then the level of noise can effectively prevent accurate analysis. To handle this the algorithm averages a set of _analog themistor_ readings. Then it stores the latest average in a circular buffer, called the first-order array. As the 1st order array fills with averaged values, the 2nd order array is filled with the difference between the two latest 1st order values. Then, as the 2nd order array is filled, a 3rd order array is also filled with differences in the latest two 2nd order values. When the 3rd order array is full, an average is taken. If the average is increasing, then we trigger a "THERMISTOR DETACHED" message and shut down the heaters.

Testing:
I've tested this on our j-heads with 3% and a 1% thermistor I'm using. Initial testing took some time but I've been able to set target temps from 50C - 200C, not see false-positives, and adjust the temp up or down without false positives. I've found that this works best with bang-bang. In order to work properly with PID the firmware needs _actually calibrated_ PID values, and I didn't have those for this session. This seems to makes sense - if the PID values are too low then not enough power goes to the nozzle, and differences in analog readings are on the same order as noise, thus a false positive is rather likely.