Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A different heater check (V3) #2231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Marlin/Conditionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,11 @@
#define WRITE_FAN(v) WRITE(FAN_PIN, v)
#endif

#if HAS_TEMP_BED
#define FIRST_HEATER -1
#else
#define FIRST_HEATER 0
#endif

#endif //CONFIGURATION_LCD
#endif //CONDITIONALS_H
34 changes: 34 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,41 @@
#define THERMAL_PROTECTION_BED_HYSTERESIS 2 // Degrees Celsius
#endif

/**
* An alternate algorithm to test if a heater works as expected.
* If a heater is full on its temperature should not drop.
* If a heater is full off its temperature should not rise.
* In a first step we detect if a temperature is rising or failing.
* This is accomplished by noise on the thermometers. Therefore we oversample the raw measurements an other 16 times.
* Than we test if the difference between the average and the momentary temperature is bigger than TEMP_RAW_NOISE.
* Than the sign of difference tells us the tendency. When we have more than TEMP_CONSEC_COUNT consecutive readings in one
* direction we report rising or falling, else constant.
* In a second step we test if a heater is full on, full of, or somewhere in the PWM area and test if the
* temperature changes as expected. This is accomplished by temperature overshoots. A temperature can rise
* fuhrer when a heater is turned off. The longest time between switching a heater off and the change to failing
* temperatures we call MAX_TEMP_OVERSHOOT_TIME.
* When a heater is off, but the surrounding temperature is higher or equal to the heaters temperature, the temperature
* can not fall. Therefore we do not test for this below MAX_AMBIENT_TEMPERATURE.
* HEATER_STATE_DEBUG produces some output on the serial line to find the right parameters.
*
* Temperatures are inert. If the value of any thermometer jumps, there is something wrong with it.
* Reasons can be: shorted wires, broken wires, leaking water-cooling, ...
* but also: electronic noise, ...
* MAX THERMO_JUMP_AMOUNT is the maximum allowed temperature difference between two measurements of the raw temperatures, (an abstract number).
* The fastest expected change is about (full range of the ADC) / minute / (temp measurements / second).
* This is normally well below the noise. So we have to adjust for the noise.
* If you get 'unreasoned' "error: Thermal jump" messages increase the MAX_THERMO_JUMP_AMOUNT value. 30 is a good value to start. 0 disables.
*
* Set MAX_TEMP_OVERSHOOT_TIME to 0 to deactivate all this tests. 30 is a good value to start.
*/
#define MAX_TEMP_OVERSHOOT_TIME 0
#define TEMP_RAW_NOISE 6
#define TEMP_CONSEC_COUNT 6
#define MAX_AMBIENT_TEMPERATURE 50
#define MAX_THERMO_JUMP_AMOUNT 30
#define HEATER_STATE_DEBUG
#ifdef PIDTEMP

// this adds an experimental additional term to the heating power, proportional to the extrusion speed.
// if Kc is chosen well, the additional required power due to increased melting should be compensated.
#define PID_ADD_EXTRUSION_RATE
Expand Down
4 changes: 2 additions & 2 deletions Marlin/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3414,15 +3414,15 @@ inline void gcode_M105() {
SERIAL_PROTOCOLPGM(" ADC B:");
SERIAL_PROTOCOL_F(degBed(),1);
SERIAL_PROTOCOLPGM("C->");
SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
SERIAL_PROTOCOL_F(rawBedTemp() >> OVESRAMPLESHIFT,0);
#endif
for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
SERIAL_PROTOCOLPGM(" T");
SERIAL_PROTOCOL(cur_extruder);
SERIAL_PROTOCOLCHAR(':');
SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
SERIAL_PROTOCOLPGM("C->");
SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder) >> OVESRAMPLESHIFT,0);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Marlin/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
#define MSG_T_THERMAL_RUNAWAY "Thermal Runaway"
#define MSG_T_MAXTEMP "MAXTEMP triggered"
#define MSG_T_MINTEMP "MINTEMP triggered"

#define MSG_T_JUMP "Thermal jump"

// LCD Menu Messages

Expand Down
3 changes: 3 additions & 0 deletions Marlin/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@
#ifndef MSG_THERMAL_RUNAWAY
#define MSG_THERMAL_RUNAWAY "THERMAL RUNAWAY"
#endif
#ifndef MSG_ERR_THERMAL_JUMP
#define MSG_ERR_THERMAL_JUMP "Err: THER. JUMP"
#endif
#ifndef MSG_ERR_MAXTEMP
#define MSG_ERR_MAXTEMP "Err: MAXTEMP"
#endif
Expand Down
157 changes: 155 additions & 2 deletions Marlin/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include "Marlin.h"
#include "ultralcd.h"
#include "temperature.h"
Expand Down Expand Up @@ -602,6 +604,15 @@ void manage_heater() {
if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
#endif


#if MAX_TEMP_OVERSHOOT_TIME > 0
for (int8_t h = FIRST_HEATER; h < EXTRUDERS; h++) {
//SERIAL_PROTOCOLPGM("Heater: "); SERIAL_PROTOCOL(h+1); SERIAL_PROTOCOLPGM(" H_State: "); SERIAL_PROTOCOLLN(heater_state(h));
if (heater_state(h) && (MAX_AMBIENT_TEMPERATURE < ((h<0) ? current_temperature_bed : current_temperature[h])))
_temp_error(h, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
}
#endif

#if defined(THERMAL_PROTECTION_HOTENDS) || !defined(PIDTEMPBED) || HAS_AUTO_FAN
millis_t ms = millis();
#endif
Expand Down Expand Up @@ -748,7 +759,7 @@ static float analog2temp(int raw, uint8_t e) {

return celsius;
}
return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
return (((raw >> OVESRAMPLESHIFT) * 5.0 * 100.0 / 1024.0) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
}

// Derived from RepRap FiveD extruder::getTemperature()
Expand All @@ -773,7 +784,7 @@ static float analog2tempBed(int raw) {

return celsius;
#elif defined BED_USES_AD595
return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
return (((raw >> OVESRAMPLESHIFT) * 5.0 * 100.0 / 1024.0) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
#else
return 0;
#endif
Expand Down Expand Up @@ -1607,3 +1618,145 @@ ISR(TIMER0_COMPB_vect) {
float scalePID_d(float d) { return d / PID_dT; }
float unscalePID_d(float d) { return d * PID_dT; }
#endif //PIDTEMP

#if MAX_TEMP_OVERSHOOT_TIME > 0
int8_t temperature_state(int8_t hh) {
static int average_temp_raw[EXTRUDERS + 1] = { 0 };
static int last_temp_raw[EXTRUDERS + 1] = { 0 };
static int16_t counter[5] = { INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX }; //init INT16_MAX

int8_t h = hh + 1; // index correctur
// get current_temperature_raw[hh] and divide by OVERSAMPLENR
#ifdef HEATER_0_USES_MAX6675
int t = ((h) ? ((hh) ? current_temperature_raw[hh] >> OVESRAMPLESHIFT: current_temperature_raw[hh]) : current_temperature_bed_raw >> OVESRAMPLESHIFT);
#else
int t = ((h) ? current_temperature_raw[hh]: current_temperature_bed_raw) >> OVESRAMPLESHIFT;
#endif

// floating average over 16 values (MAX6675 is not oversampled until now. Thermistors are still a bit nervous)
int average = (average_temp_raw[h] >> OVESRAMPLESHIFT); // /16
int dt = t - average;
average_temp_raw[h] += dt;

#ifdef HEATER_STATE_DEBUG
SERIAL_PROTOCOLPGM("Temperature: "); SERIAL_PROTOCOL(h); SERIAL_PROTOCOLPGM(" count: "); SERIAL_PROTOCOL((int)counter[h]); SERIAL_PROTOCOLPGM(" Temp.: "); SERIAL_PROTOCOL((int)t); SERIAL_PROTOCOLPGM(" Temp.A.:"); SERIAL_PROTOCOL(average);
#endif

// init
if (counter[h] == INT16_MAX) {
average_temp_raw[h] = (t << OVESRAMPLESHIFT); // *16
last_temp_raw[h] = t;
counter[h] = 0;
return 0;
}

#if MAX_THERMO_JUMP_AMOUNT > 0
if (abs(last_temp_raw[h] - t) > MAX_THERMO_JUMP_AMOUNT) _temp_error(hh, PSTR(MSG_T_JUMP), PSTR(MSG_ERR_THERMAL_JUMP));
else last_temp_raw[h] = t;
#endif

if (abs(dt) < TEMP_RAW_NOISE) { counter[h] = 0; return 0;} // constant
switch (h) {
case 0: {
#if HAS_TEMP_BED
counter[h] += (dt GEBED 0) ? 1 : -1;
#endif
break;
}
case 1: {
#if HAS_TEMP_0 && !defined(HEATER_0_USES_MAX6675)
counter[h] += (dt GE0 0) ? 1 : -1;
#endif
#ifdef HEATER_0_USES_MAX6675
counter[h] += (dt >= 0) ? 1 : -1;
#endif
break;
}
case 2: {
#if HAS_TEMP_1
counter[h] += (dt GE1 0) ? 1 : -1;
#endif
break;
}
case 3: {
#if HAS_TEMP_2
counter[h] += (dt GE2 0) ? 1 : -1;
#endif
break;
}
case 4: {
#if HAS_TEMP_3
counter[h] += (dt GE3 0) ? 1 : -1;
#endif
break;
}
break;
}
// when we have n consecutive rising or falling values we are sure enough to return that
if (abs(counter[h]) >= TEMP_CONSEC_COUNT) return (counter[h] < 0) ? -1 : 1;
// else return constant
return 0;
}

int8_t heater_state(int8_t hh) {
enum hstate_t { ON_S, FULL_ON_S, OFF_S };
static hstate_t hstate[5] = { ON_S };
static int8_t tstate[EXTRUDERS + 1] = { 0 };
static millis_t timerc[EXTRUDERS + 1] = { 0 };
static float initial_temp[EXTRUDERS + 1] = { 0.0 };

uint8_t h = hh + 1;
int8_t current_tstate = temperature_state(hh);

#ifdef HEATER_STATE_DEBUG
SERIAL_PROTOCOLPGM(" Heater: "); SERIAL_PROTOCOL((int)h); SERIAL_PROTOCOLPGM(" T_State: "); SERIAL_PROTOCOL((int)current_tstate); SERIAL_PROTOCOLPGM(" Time:"); SERIAL_PROTOCOL(timerc[h]-millis()); SERIAL_PROTOCOLPGM(" H_State:"); SERIAL_PROTOCOLLN((int)hstate[h]);
#endif

if (((h) ? soft_pwm[hh] : soft_pwm_bed) == ((h) ? PID_MAX >> 1: MAX_BED_POWER >> 1)) {// FULL_ON
if (hstate[h] != FULL_ON_S) {
hstate[h] = FULL_ON_S;
tstate[h] = current_tstate;
timerc[h] = millis() + MAX_TEMP_OVERSHOOT_TIME * 1000UL;
return 0;
}
else {
if (current_tstate != tstate[h]) {
if (current_tstate == 1) return 0;
if (current_tstate == -1) _temp_error(hh, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
}
if (millis() > timerc[h]) {// timeout
hstate[h] = ON_S; // retest
if (current_tstate != -1) return 0;
else _temp_error(hh, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
}
else
return 0;
}
}
else if (((h) ? soft_pwm[hh] : soft_pwm_bed) == 0) // FULL_OFF
if (hstate[h] != OFF_S) {
hstate[h] = OFF_S;
tstate[h] = current_tstate;
timerc[h] = millis() + MAX_TEMP_OVERSHOOT_TIME * 1000UL;
return 0;
}
else {
if (current_tstate != tstate[h]) {
if (current_tstate == 1) 1; // test for MAX_AMBIENT_TEMPERATURE // got false positives when touching the sensor.
else return 0;
}
if (millis() > timerc[h]) {// timeout
hstate[h] = ON_S; // retest
if (current_tstate != 1) return 0;
else return 1; // test for MAX_AMBIENT_TEMPERATURE
}
else
return 0;
}
else {
hstate[h] = ON_S; // on, but not full power so can't say anything about the temperature change
return 0;
}
return 0;
}
#endif // MAX_TEMP_OVERSHOOT_TIME > 0
4 changes: 4 additions & 0 deletions Marlin/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void PID_autotune(float temp, int extruder, int ncycles);
void setExtruderAutoFanState(int pin, bool state);
void checkExtruderAutoFans();

#if MAX_TEMP_OVERSHOOT_TIME > 0
int8_t heater_state(int8_t);
#endif

FORCE_INLINE void autotempShutdown() {
#ifdef AUTOTEMP
if (autotemp_enabled) {
Expand Down
1 change: 1 addition & 0 deletions Marlin/thermistortables.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Marlin.h"

#define OVERSAMPLENR 16
#define OVESRAMPLESHIFT 4 // x >> OVESRAMPLESHIFT == x / OVERSAMPLENR

#if (THERMISTORHEATER_0 == 1) || (THERMISTORHEATER_1 == 1) || (THERMISTORHEATER_2 == 1) || (THERMISTORHEATER_3 == 1) || (THERMISTORBED == 1) //100k bed thermistor

Expand Down