Skip to content

Prevent stuck M109/M190 when target is changed #3650

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

Merged
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
54 changes: 36 additions & 18 deletions Marlin/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4463,15 +4463,6 @@ inline void gcode_M109() {
if (code_seen('B')) autotemp_max = code_value();
#endif

bool wants_to_cool = isCoolingHotend(target_extruder);

// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
if (no_wait_for_cooling && wants_to_cool) return;

// Prevents a wait-forever situation if R is misused i.e. M109 R0
// Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP
if (wants_to_cool && degTargetHotend(target_extruder) < (EXTRUDE_MINTEMP)/2) return;

#if TEMP_RESIDENCY_TIME > 0
millis_t residency_start_ms = 0;
// Loop until the temperature has stabilized
Expand All @@ -4481,11 +4472,15 @@ inline void gcode_M109() {
#define TEMP_CONDITIONS (wants_to_cool ? isCoolingHotend(target_extruder) : isHeatingHotend(target_extruder))
#endif //TEMP_RESIDENCY_TIME > 0

KEEPALIVE_STATE(NOT_BUSY);

float theTarget = -1;
bool wants_to_cool;
cancel_heatup = false;
millis_t now, next_temp_ms = 0;

KEEPALIVE_STATE(NOT_BUSY);

do {

now = millis();
if (ELAPSED(now, next_temp_ms)) { //Print temp & remaining time every 1s while waiting
next_temp_ms = now + 1000UL;
Expand All @@ -4506,12 +4501,25 @@ inline void gcode_M109() {
#endif
}

// Target temperature might be changed during the loop
if (theTarget != degTargetHotend(target_extruder)) {
theTarget = degTargetHotend(target_extruder);
wants_to_cool = isCoolingHotend(target_extruder);

// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
if (no_wait_for_cooling && wants_to_cool) break;

// Prevent a wait-forever situation if R is misused i.e. M109 R0
// Try to calculate a ballpark safe margin by halving EXTRUDE_MINTEMP
if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break;
}

idle();
refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out

#if TEMP_RESIDENCY_TIME > 0

float temp_diff = fabs(degTargetHotend(target_extruder) - degHotend(target_extruder));
float temp_diff = fabs(theTarget - degHotend(target_extruder));

if (!residency_start_ms) {
// Start the TEMP_RESIDENCY_TIME timer when we reach target temp for the first time.
Expand Down Expand Up @@ -4543,11 +4551,6 @@ inline void gcode_M109() {
bool no_wait_for_cooling = code_seen('S');
if (no_wait_for_cooling || code_seen('R')) setTargetBed(code_value());

bool wants_to_cool = isCoolingBed();

// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
if (no_wait_for_cooling && wants_to_cool) return;

#if TEMP_BED_RESIDENCY_TIME > 0
millis_t residency_start_ms = 0;
// Loop until the temperature has stabilized
Expand All @@ -4557,11 +4560,13 @@ inline void gcode_M109() {
#define TEMP_BED_CONDITIONS (wants_to_cool ? isCoolingBed() : isHeatingBed())
#endif //TEMP_BED_RESIDENCY_TIME > 0

float theTarget = -1;
bool wants_to_cool;
cancel_heatup = false;
millis_t now, next_temp_ms = 0;

// Wait for temperature to come close enough
KEEPALIVE_STATE(NOT_BUSY);

do {
now = millis();
if (ELAPSED(now, next_temp_ms)) { //Print Temp Reading every 1 second while heating up.
Expand All @@ -4581,6 +4586,19 @@ inline void gcode_M109() {
#endif
}

// Target temperature might be changed during the loop
if (theTarget != degTargetBed()) {
theTarget = degTargetBed();
wants_to_cool = isCoolingBed();

// Exit if S<lower>, continue if S<higher>, R<lower>, or R<higher>
if (no_wait_for_cooling && wants_to_cool) break;

// Prevent a wait-forever situation if R is misused i.e. M190 R0
// Simply don't wait for cooling below 30C
if (wants_to_cool && theTarget < (EXTRUDE_MINTEMP)/2) break;
}

idle();
refresh_cmd_timeout(); // to prevent stepper_inactive_time from running out

Expand Down